Exemple #1
0
    def test_initialization(self):
        """No dimension on initialization should raise an exception."""
        with self.assertRaisesRegex(ModelInconsistencyError, "not be empty"):
            Hierarchy("default", [])

        with self.assertRaisesRegex(ModelInconsistencyError, "as strings"):
            Hierarchy("default", ["iamastring"])
Exemple #2
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)
Exemple #3
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)
Exemple #4
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)
Exemple #5
0
class HierarchyTestCase(unittest.TestCase):
    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_initialization(self):
        """No dimension on initialization should raise an exception."""
        with self.assertRaisesRegex(ModelInconsistencyError, "not be empty"):
            Hierarchy("default", [])

        with self.assertRaisesRegex(ModelInconsistencyError, "as strings"):
            Hierarchy("default", ["iamastring"])

    def test_operators(self):
        """Hierarchy operators len(), hier[] and level in hier"""
        # __len__
        self.assertEqual(3, len(self.hierarchy))

        # __getitem__ by name
        self.assertEqual(self.levels[1], self.hierarchy[1])

        # __contains__ by name or level
        self.assertTrue(self.levels[1] in self.hierarchy)
        self.assertTrue("year" in self.hierarchy)
        self.assertFalse("flower" in self.hierarchy)

    def test_levels_for(self):
        """Levels for depth"""
        levels = self.hierarchy.levels_for_depth(0)
        self.assertEqual([], levels)

        levels = self.hierarchy.levels_for_depth(1)
        self.assertEqual([self.levels[0]], levels)

        self.assertRaises(HierarchyError, self.hierarchy.levels_for_depth, 4)

    def test_level_ordering(self):
        """Ordering of levels (next, previous)"""
        self.assertEqual(self.levels[0], self.hierarchy.next_level(None))
        self.assertEqual(self.levels[1],
                         self.hierarchy.next_level(self.levels[0]))
        self.assertEqual(self.levels[2],
                         self.hierarchy.next_level(self.levels[1]))
        self.assertEqual(None, self.hierarchy.next_level(self.levels[2]))

        self.assertEqual(None, self.hierarchy.previous_level(None))
        self.assertEqual(None, self.hierarchy.previous_level(self.levels[0]))
        self.assertEqual(self.levels[0],
                         self.hierarchy.previous_level(self.levels[1]))
        self.assertEqual(self.levels[1],
                         self.hierarchy.previous_level(self.levels[2]))

        self.assertEqual(0, self.hierarchy.level_index(self.levels[0]))
        self.assertEqual(1, self.hierarchy.level_index(self.levels[1]))
        self.assertEqual(2, self.hierarchy.level_index(self.levels[2]))

        self.assertRaises(HierarchyError, self.hierarchy.level_index,
                          self.levels[3])

    def test_rollup(self):
        """Path roll-up for hierarchy"""
        path = [2010, 1, 5]

        self.assertEqual([2010, 1], self.hierarchy.rollup(path))
        self.assertEqual([2010, 1], self.hierarchy.rollup(path, "month"))
        self.assertEqual([2010], self.hierarchy.rollup(path, "year"))
        self.assertRaises(HierarchyError, self.hierarchy.rollup,
                          [2010], "month")
        self.assertRaises(HierarchyError, self.hierarchy.rollup,
                          [2010], "unknown")

    def test_base_path(self):
        """Test base paths"""
        self.assertTrue(self.hierarchy.path_is_base([2012, 1, 5]))
        self.assertFalse(self.hierarchy.path_is_base([2012, 1]))
        self.assertFalse(self.hierarchy.path_is_base([2012]))
        self.assertFalse(self.hierarchy.path_is_base([]))

    def test_attributes(self):
        """Collecting attributes and keys"""
        keys = [a.name for a in self.hierarchy.key_attributes()]
        self.assertEqual(["year", "month", "day"], keys)

        attrs = [a.name for a in self.hierarchy.all_attributes]
        self.assertEqual(["year", "month", "month_name", "month_sname", "day"],
                         attrs)

    def test_copy(self):
        class DummyDimension(object):
            def __init__(self):
                self.name = "dummy"
                self.is_flat = False

        left = self.hierarchy.levels[0].attributes[0]
        left.dimension = DummyDimension()

        clone = copy.deepcopy(self.hierarchy)

        left = self.hierarchy.levels[0].attributes[0]
        right = clone.levels[0].attributes[0]
        # Make sure that the dimension is not copied
        self.assertIsNone(right.dimension)

        self.assertEqual(self.hierarchy.levels, clone.levels)
        self.assertEqual(self.hierarchy, clone)