Esempio n. 1
0
    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)
Esempio n. 2
0
    def setUp(self):
        self.levels = [
            Level("year", attributes=create_list_of(Attribute, ["year"])),
            Level("month", attributes=create_list_of(Attribute, ["month", "month_name",
                                             "month_sname"])),
            Level("day", attributes=create_list_of(Attribute, ["day"])),
            Level("week", attributes=create_list_of(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)
Esempio n. 3
0
    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)
Esempio n. 4
0
    def test_create(self):
        """Create level from a dictionary"""
        desc = "year"
        level = Level.from_metadata(desc)
        self.assertIsInstance(level, Level)
        self.assertEqual("year", level.name)
        self.assertEqual(["year"], [str(a) for a in level.attributes])

        # Test default: Attributes
        desc = {"name": "year"}
        level = Level.from_metadata(desc)
        self.assertIsInstance(level, Level)
        self.assertEqual("year", level.name)
        self.assertEqual(["year"], [str(a) for a in level.attributes])

        # Test default: Attributes
        desc = {"name": "year", "attributes": ["key"]}
        level = Level.from_metadata(desc)
        self.assertIsInstance(level, Level)
        self.assertEqual("year", level.name)
        self.assertEqual(["key"], [str(a) for a in level.attributes])
        self.assertFalse(level.has_details)

        desc = {"name": "year", "attributes": ["key", "label"]}
        level = Level.from_metadata(desc)
        self.assertTrue(level.has_details)
        self.assertEqual(["key", "label"], [str(a) for a in level.attributes])

        # Level from description with full details
        desc = {
            "name":
            "month",
            "attributes": [{
                "name": "month"
            }, {
                "name": "month_name",
                "locales": ["en", "sk"]
            }, {
                "name": "month_sname",
                "locales": ["en", "sk"]
            }]
        }

        level = Level.from_metadata(desc)
        self.assertTrue(level.has_details)
        self.assertEqual(3, len(level.attributes))
        names = [str(a) for a in level.attributes]
        self.assertEqual(["month", "month_name", "month_sname"], names)
Esempio n. 5
0
 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)
Esempio n. 6
0
    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.name", str(attr))
        self.assertEqual("group.name", 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)
Esempio n. 7
0
    def test_level_inherit(self):
        desc = {"name": "product_type", "label": "Product Type"}

        level = Level.from_metadata(desc)
        self.assertEqual(1, len(level.attributes))

        attr = level.attributes[0]
        self.assertEqual("product_type", attr.name)
        self.assertEqual("Product Type", attr.label)
Esempio n. 8
0
    def test_create(self):
        """Create level from a dictionary"""
        desc = "year"
        level = Level.from_metadata(desc)
        self.assertIsInstance(level, Level)
        self.assertEqual("year", level.name)
        self.assertEqual(["year"], [str(a) for a in level.attributes])

        # Test default: Attributes
        desc = {"name": "year"}
        level = Level.from_metadata(desc)
        self.assertIsInstance(level, Level)
        self.assertEqual("year", level.name)
        self.assertEqual(["year"], [str(a) for a in level.attributes])

        # Test default: Attributes
        desc = {"name": "year", "attributes": ["key"]}
        level = Level.from_metadata(desc)
        self.assertIsInstance(level, Level)
        self.assertEqual("year", level.name)
        self.assertEqual(["key"], [str(a) for a in level.attributes])
        self.assertFalse(level.has_details)

        desc = {"name": "year", "attributes": ["key", "label"]}
        level = Level.from_metadata(desc)
        self.assertTrue(level.has_details)
        self.assertEqual(["key", "label"], [str(a) for a in level.attributes])

        # Level from description with full details
        desc = {
            "name": "month",
            "attributes": [
                {"name": "month"},
                {"name": "month_name", "locales": ["en", "sk"]},
                {"name": "month_sname", "locales": ["en", "sk"]}
            ]
        }

        level = Level.from_metadata(desc)
        self.assertTrue(level.has_details)
        self.assertEqual(3, len(level.attributes))
        names = [str(a) for a in level.attributes]
        self.assertEqual(["month", "month_name", "month_sname"], names)
Esempio n. 9
0
    def test_level_inherit(self):
        desc = {
            "name": "product_type",
            "label": "Product Type"
        }

        level = Level.from_metadata(desc)
        self.assertEqual(1, len(level.attributes))

        attr = level.attributes[0]
        self.assertEqual("product_type", attr.name)
        self.assertEqual("Product Type", attr.label)
Esempio n. 10
0
    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))
Esempio n. 11
0
    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)
Esempio n. 12
0
 def test_operators(self):
     """Level to string conversion"""
     attrs = [Attribute("foo")]
     self.assertEqual("date", str(Level("date", attrs)))