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): 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)
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")
class CubeTestCase(unittest.TestCase): def setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = attribute_list(["amount", "discount"], Measure) self.details = attribute_list(["detail"], Attribute) self.dimensions = [create_dimension(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 = create_cube(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', '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 = create_cube(desc, dims) self.assertEqual(self.cube.dimensions, cube.dimensions) self.assertEqual(self.cube.measures, cube.measures) self.assertEqual(self.cube, cube) def test_links(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) 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) 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 = [create_dimension(md) for md in dims] dims = dict((dim.name, dim) for dim in dims) cube = create_cube(cube) measures = cube.measures self.assertEqual(measures[0].nonadditive, "time")
class CubeTestCase(unittest.TestCase): def setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = attribute_list(["amount", "discount"], Measure) self.details = attribute_list(["detail"], Attribute) self.dimensions = [create_dimension(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 = create_cube(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', '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 = create_cube(desc, dims) self.assertEqual(self.cube.dimensions, cube.dimensions) self.assertEqual(self.cube.measures, cube.measures) self.assertEqual(self.cube, cube) def test_links(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) 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) 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 = [create_dimension(md) for md in dims] dims = dict((dim.name, dim) for dim in dims) cube = create_cube(cube) measures = cube.measures self.assertEqual(measures[0].nonadditive, "time")