def test_has_details(self): """Level "has_details" flag""" attrs = [Attribute("year")] level = Level("year", attrs) self.assertFalse(level.has_details) attrs = [Attribute("month"), Attribute("month_name")] level = Level("month", attrs) self.assertTrue(level.has_details)
def test_locale(self): """References to localizable attributes""" attr = Attribute("foo") self.assertRaises(ArgumentError, attr.localized_ref, locale="xx") attr = Attribute("foo", locales=["en", "sk"]) self.assertEqual("foo", attr.name) self.assertEqual("foo", str(attr)) self.assertEqual("foo.en", attr.localized_ref("en")) self.assertEqual("foo.sk", attr.localized_ref("sk")) self.assertRaises(ArgumentError, attr.localized_ref, locale="xx")
def test_comparison(self): """Comparison of level instances""" attrs = [Attribute("info"), Attribute("code"), Attribute("name")] level1 = Level("product", attrs, key="code", label_attribute="name") level2 = Level("product", attrs, key="code", label_attribute="name") level3 = Level("product", attrs) attrs = [Attribute("month"), Attribute("month_name")] level4 = Level("product", attrs) self.assertEqual(level1, level2) self.assertNotEqual(level2, level3) self.assertNotEqual(level2, level4)
def test_logical_reference(self): dim = self.provider.dimension("date") attr = Attribute("month", dimension=dim) self.assertEqual("date.month", attr.ref) dim = self.provider.dimension("product") attr = Attribute("category", dimension=dim) self.assertEqual("product.category", attr.ref) dim = self.provider.dimension("flag") attr = Attribute("flag", dimension=dim) self.assertEqual("flag", attr.ref) attr = Attribute("measure", dimension=None) self.assertEqual("measure", attr.ref)
def test_simplify(self): """Simplification of attribute reference (with and without details)""" level = Level("name", attributes=[Attribute("name")]) dim = Dimension("group", levels=[level]) attr = dim.attribute("name") self.assertEqual("name", attr.name) # Simplified -> dimension name self.assertEqual("group", str(attr)) self.assertEqual("group", attr.ref) level = Level("name", attributes=[Attribute("key"), Attribute("name")]) dim = Dimension("group", levels=[level]) attr = dim.attribute("name") self.assertEqual("name", attr.name) self.assertEqual("group.name", str(attr)) self.assertEqual("group.name", attr.ref)
def test_create_attribute(self): """Coalesce attribute object (string or Attribute instance)""" level = Level("name", attributes=[Attribute("key"), Attribute("name")]) dim = Dimension("group", levels=[level]) obj = Attribute.from_metadata("name") self.assertIsInstance(obj, Attribute) self.assertEqual("name", obj.name) obj = Attribute.from_metadata({"name": "key"}) obj.dimension = dim self.assertIsInstance(obj, Attribute) self.assertEqual("key", obj.name) self.assertEqual(dim, obj.dimension) attr = dim.attribute("key") obj = Attribute.from_metadata(attr) obj.dimension = dim self.assertIsInstance(obj, Attribute) self.assertEqual("key", obj.name) self.assertEqual(obj, attr)
def setUp(self): self.levels = [ Level("year", attributes=[Attribute("year")]), Level("month", attributes=[ Attribute("month"), Attribute("month_name"), Attribute("month_sname") ]), Level("day", attributes=[Attribute("day")]), Level("week", attributes=[Attribute("week")]) ] self.level_names = [level.name for level in self.levels] self.dimension = Dimension("date", levels=self.levels) levels = [self.levels[0], self.levels[1], self.levels[2]] self.hierarchy = Hierarchy("default", levels)
def test_key_label_attributes(self): """Test key and label attributes - explicit and implicit""" attrs = [Attribute("code")] level = Level("product", attrs) self.assertIsInstance(level.key, Attribute) self.assertEqual("code", str(level.key)) self.assertIsInstance(level.label_attribute, Attribute) self.assertEqual("code", str(level.label_attribute)) attrs = [Attribute("code"), Attribute("name")] level = Level("product", attrs) self.assertIsInstance(level.key, Attribute) self.assertEqual("code", str(level.key)) self.assertIsInstance(level.label_attribute, Attribute) self.assertEqual("name", str(level.label_attribute)) attrs = [Attribute("info"), Attribute("code"), Attribute("name")] level = Level("product", attrs, key="code", label_attribute="name") self.assertIsInstance(level.key, Attribute) self.assertEqual("code", str(level.key)) self.assertIsInstance(level.label_attribute, Attribute) self.assertEqual("name", str(level.label_attribute)) # Test key/label in full desc desc = { "name": "product", "attributes": ["info", "code", "name"], "label_attribute": "name", "key": "code" } level = Level.from_metadata(desc) self.assertIsInstance(level.key, Attribute) self.assertEqual("code", str(level.key)) self.assertIsInstance(level.label_attribute, Attribute) self.assertEqual("name", str(level.label_attribute))
def test_basics(self): """Attribute creation and attribute references""" attr = Attribute("foo") self.assertEqual("foo", attr.name) self.assertEqual("foo", str(attr)) self.assertEqual("foo", attr.ref)
def test_operators(self): """Level to string conversion""" attrs = [Attribute("foo")] self.assertEqual("date", str(Level("date", attrs)))