def test_link_hierarchies(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "nonadditive":None}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "hierarchies": ["ym"]}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 1) self.assertEqual(dim.hierarchy().name, "ym")
def test_links(self): # TODO: test link alias! dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 1) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) links = [{"name": "date"}, "product", "flag"] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 3) self.assertIsInstance(cube.dimension("flag"), Dimension)
class CubeTestCase(unittest.TestCase): def setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = create_list_of(Measure, ["amount", "discount"]) self.details = create_list_of(Attribute, ["detail"]) self.dimensions = [Dimension.from_metadata(desc) for desc in a] self.cube = Cube("contracts", dimensions=self.dimensions, measures=self.measures, details=self.details) def test_create_cube(self): cube = { "name": "cube", "dimensions": ["date"], "aggregates": ["record_count"], "details": ["some_detail", "another_detail"] } cube = Cube.from_metadata(cube) self.assertEqual(cube.name, "cube") self.assertEqual(len(cube.aggregates), 1) self.assertEqual(len(cube.details), 2) def test_get_dimension(self): self.assertListEqual(self.dimensions, self.cube.dimensions) self.assertEqual("date", self.cube.dimension("date").name) self.assertEqual("product", self.cube.dimension("product").name) self.assertEqual("flag", self.cube.dimension("flag").name) self.assertRaises(NoSuchDimensionError, self.cube.dimension, "xxx") def test_get_measure(self): self.assertListEqual(self.measures, self.cube.measures) self.assertEqual("amount", self.cube.measure("amount").name) self.assertEqual("discount", self.cube.measure("discount").name) self.assertRaises(NoSuchAttributeError, self.cube.measure, "xxx") def test_attributes(self): all_attributes = self.cube.all_attributes refs = [a.ref for a in all_attributes] expected = [ 'date.year', 'date.month', 'date.month_name', 'date.day', 'product.key', 'product.name', 'product.description', 'flag.flag', 'detail', 'amount', 'discount'] self.assertSequenceEqual(expected, refs) attributes = self.cube.get_attributes(["date.year", "product.name"]) refs = [a.ref for a in attributes] expected = ['date.year', 'product.name'] self.assertSequenceEqual(expected, refs) attributes = self.cube.get_attributes(["amount"]) refs = [a.ref for a in attributes] self.assertSequenceEqual(["amount"], refs) with self.assertRaises(NoSuchAttributeError): self.cube.get_attributes(["UNKNOWN"]) @unittest.skip("deferred (needs workspace)") def test_to_dict(self): desc = self.cube.to_dict() dims = dict((dim.name, dim) for dim in self.dimensions) cube = Cube.from_metadata(desc, dims) self.assertEqual(self.cube.dimensions, cube.dimensions) self.assertEqual(self.cube.measures, cube.measures) self.assertEqual(self.cube, cube) @unittest.skip("requires revision") def test_links(self): # TODO: test link alias! dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 1) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) links = [{"name": "date"}, "product", "flag"] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) self.assertEqual(len(cube.dimensions), 3) self.assertIsInstance(cube.dimension("flag"), Dimension) @unittest.skip("requires revision") def test_link_hierarchies(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "nonadditive":None}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 2) self.assertEqual(dim.hierarchy().name, "ymd") links = [{"name": "date", "hierarchies": ["ym"]}] cube = Cube("contracts", dimension_links=links, measures=self.measures) cube.link_dimensions(dims) dim = cube.dimension("date") self.assertEqual(len(dim.hierarchies), 1) self.assertEqual(dim.hierarchy().name, "ym") def test_inherit_nonadditive(self): dims = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] cube = { "name": "contracts", "dimensions": ["date", "product"], "nonadditive": "time", "measures": ["amount", "discount"] } dims = [Dimension.from_metadata(md) for md in dims] dims = dict((dim.name, dim) for dim in dims) cube = Cube.from_metadata(cube) measures = cube.measures self.assertEqual(measures[0].nonadditive, "time")