def setUp(self): self.levels = [ Level("year", attributes=["year"]), Level("month", attributes=["month", "month_name", "month_sname"]), Level("day", attributes=["day"]), Level("week", attributes=["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_simplify(self): """Simplification of attribute reference (with and without details)""" level = Level("name", attributes=["name"]) dim = Dimension("group", levels=[level]) attr = dim.attribute("name") self.assertEqual("name", attr.name) self.assertEqual("name", str(attr)) self.assertEqual("group", attr.ref()) self.assertEqual("group.name", attr.ref(simplify=False)) self.assertEqual("group", attr.ref(simplify=True)) level = Level("name", attributes=["key", "name"]) dim = Dimension("group", levels=[level]) attr = dim.attribute("name") self.assertEqual("name", attr.name) self.assertEqual("name", str(attr)) self.assertEqual("group.name", attr.ref()) self.assertEqual("group.name", attr.ref(simplify=False)) self.assertEqual("group.name", attr.ref(simplify=True))
def test_create_attribute(self): """Coalesce attribute object (string or Attribute instance)""" level = Level("name", attributes=["key", "name"]) dim = Dimension("group", levels=[level]) obj = create_attribute("name") self.assertIsInstance(obj, Attribute) self.assertEqual("name", obj.name) obj = create_attribute({"name": "key"}) obj.dimension = dim self.assertIsInstance(obj, Attribute) self.assertEqual("key", obj.name) self.assertEqual(dim, obj.dimension) attr = dim.attribute("key") obj = create_attribute(attr) obj.dimension = dim self.assertIsInstance(obj, Attribute) self.assertEqual("key", obj.name) self.assertEqual(obj, attr)
class DimensionTestCase(unittest.TestCase): def setUp(self): self.levels = [ Level("year", attributes=["year"]), Level("month", attributes=["month", "month_name", "month_sname"]), Level("day", attributes=["day"]), Level("week", attributes=["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_create(self): """Dimension from a dictionary""" dim = create_dimension("year") self.assertIsInstance(dim, Dimension) self.assertEqual("year", dim.name) self.assertEqual(["year"], [str(a) for a in dim.attributes]) # Test default: explicit level attributes desc = {"name": "date", "levels": ["year"]} dim = create_dimension(desc) self.assertTrue(dim.is_flat) self.assertFalse(dim.has_details) self.assertIsInstance(dim, Dimension) self.assertEqual("date", dim.name) self.assertEqual(["year"], [str(a) for a in dim.attributes]) desc = {"name": "date", "levels": ["year", "month", "day"]} dim = create_dimension(desc) self.assertIsInstance(dim, Dimension) self.assertEqual("date", dim.name) names = [str(a) for a in dim.attributes] self.assertEqual(["year", "month", "day"], names) self.assertFalse(dim.is_flat) self.assertFalse(dim.has_details) self.assertEqual(3, len(dim.levels)) for level in dim.levels: self.assertIsInstance(level, Level) self.assertEqual(1, len(dim.hierarchies)) self.assertEqual(3, len(dim.hierarchy())) # Test default: implicit single level attributes desc = {"name": "product", "attributes": ["code", "name"]} dim = create_dimension(desc) names = [str(a) for a in dim.attributes] self.assertEqual(["code", "name"], names) self.assertEqual(1, len(dim.levels)) self.assertEqual(1, len(dim.hierarchies)) def test_flat_dimension(self): """Flat dimension and 'has details' flags""" dim = create_dimension("foo") self.assertTrue(dim.is_flat) self.assertFalse(dim.has_details) self.assertEqual(1, len(dim.levels)) level = dim.level("foo") self.assertIsInstance(level, Level) self.assertEqual("foo", level.name) self.assertEqual(1, len(level.attributes)) self.assertEqual("foo", str(level.key)) attr = level.attributes[0] self.assertIsInstance(attr, Attribute) self.assertEqual("foo", attr.name) def test_comparisons(self): """Comparison of dimension instances""" dim1 = create_dimension(DIM_DATE_DESC) dim2 = create_dimension(DIM_DATE_DESC) self.assertListEqual(dim1.levels, dim2.levels) self.assertListEqual(dim1.hierarchies, dim2.hierarchies) self.assertEqual(dim1, dim2) def test_to_dict(self): desc = self.dimension.to_dict() dim = create_dimension(desc) self.assertEqual(self.dimension.hierarchies, dim.hierarchies) self.assertEqual(self.dimension.levels, dim.levels) self.assertEqual(self.dimension, dim) def test_template(self): dims = {"date": self.dimension} desc = {"template": "date", "name": "date"} dim = create_dimension(desc, dims) self.assertEqual(self.dimension, dim) hier = dim.hierarchy() self.assertEqual(4, len(hier.levels)) desc["hierarchy"] = ["year", "month"] dim = create_dimension(desc, dims) self.assertEqual(1, len(dim.hierarchies)) hier = dim.hierarchy() self.assertEqual(2, len(hier.levels)) template = self.dimension.to_dict() template["hierarchies"] = [{ "name": "ym", "levels": ["year", "month"] }, { "name": "ymd", "levels": ["year", "month", "day"] }] template["default_hierarchy_name"] = "ym" template = create_dimension(template) dims = {"date": template} desc = {"template": "date", "name": "another_date"} dim = create_dimension(desc, dims) self.assertEqual(2, len(dim.hierarchies)) self.assertEqual(["ym", "ymd"], [hier.name for hier in dim.hierarchies]) def test_template_hierarchies(self): md = { "name": "time", "levels": ["year", "month", "day", "hour"], "hierarchies": [ { "name": "full", "levels": ["year", "month", "day", "hour"] }, { "name": "ymd", "levels": ["year", "month", "day"] }, { "name": "ym", "levels": ["year", "month"] }, { "name": "y", "levels": ["year"] }, ] } dim_time = create_dimension(md) templates = {"time": dim_time} md = { "name": "date", "template": "time", "hierarchies": ["ymd", "ym", "y"] } dim_date = create_dimension(md, templates) self.assertEqual(dim_date.name, "date") self.assertEqual(len(dim_date.hierarchies), 3) names = [h.name for h in dim_date.hierarchies] self.assertEqual(["ymd", "ym", "y"], names) def test_template_info(self): md = { "name": "template", "levels": [{ "name": "one", "info": { "units": "$", "format": "foo" } }] } tempdim = create_dimension(md) md = { "name": "dim", "levels": [{ "name": "one", "info": { "units": "USD" } }], "template": "template" } templates = {"template": tempdim} dim = create_dimension(md, templates) level = dim.level("one") self.assertIn("units", level.info) self.assertIn("format", level.info) self.assertEqual(level.info["units"], "USD") self.assertEqual(level.info["format"], "foo")
class DimensionTestCase(unittest.TestCase): def setUp(self): self.levels = [ Level("year", attributes=["year"]), Level("month", attributes=["month", "month_name", "month_sname"]), Level("day", attributes=["day"]), Level("week", attributes=["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_create(self): """Dimension from a dictionary""" dim = create_dimension("year") self.assertIsInstance(dim, Dimension) self.assertEqual("year", dim.name) self.assertEqual(["year"], [str(a) for a in dim.attributes]) # Test default: explicit level attributes desc = {"name": "date", "levels": ["year"]} dim = create_dimension(desc) self.assertTrue(dim.is_flat) self.assertFalse(dim.has_details) self.assertIsInstance(dim, Dimension) self.assertEqual("date", dim.name) self.assertEqual(["year"], [str(a) for a in dim.attributes]) desc = {"name": "date", "levels": ["year", "month", "day"]} dim = create_dimension(desc) self.assertIsInstance(dim, Dimension) self.assertEqual("date", dim.name) names = [str(a) for a in dim.attributes] self.assertEqual(["year", "month", "day"], names) self.assertFalse(dim.is_flat) self.assertFalse(dim.has_details) self.assertEqual(3, len(dim.levels)) for level in dim.levels: self.assertIsInstance(level, Level) self.assertEqual(1, len(dim.hierarchies)) self.assertEqual(3, len(dim.hierarchy())) # Test default: implicit single level attributes desc = {"name": "product", "attributes": ["code", "name"]} dim = create_dimension(desc) names = [str(a) for a in dim.attributes] self.assertEqual(["code", "name"], names) self.assertEqual(1, len(dim.levels)) self.assertEqual(1, len(dim.hierarchies)) def test_flat_dimension(self): """Flat dimension and 'has details' flags""" dim = create_dimension("foo") self.assertTrue(dim.is_flat) self.assertFalse(dim.has_details) self.assertEqual(1, len(dim.levels)) level = dim.level("foo") self.assertIsInstance(level, Level) self.assertEqual("foo", level.name) self.assertEqual(1, len(level.attributes)) self.assertEqual("foo", str(level.key)) attr = level.attributes[0] self.assertIsInstance(attr, Attribute) self.assertEqual("foo", attr.name) def test_comparisons(self): """Comparison of dimension instances""" dim1 = create_dimension(DIM_DATE_DESC) dim2 = create_dimension(DIM_DATE_DESC) self.assertListEqual(dim1.levels, dim2.levels) self.assertListEqual(dim1.hierarchies, dim2.hierarchies) self.assertEqual(dim1, dim2) def test_to_dict(self): desc = self.dimension.to_dict() dim = create_dimension(desc) self.assertEqual(self.dimension.hierarchies, dim.hierarchies) self.assertEqual(self.dimension.levels, dim.levels) self.assertEqual(self.dimension, dim) def test_template(self): dims = {"date": self.dimension} desc = {"template": "date", "name": "date"} dim = create_dimension(desc, dims) self.assertEqual(self.dimension, dim) hier = dim.hierarchy() self.assertEqual(4, len(hier.levels)) desc["hierarchy"] = ["year", "month"] dim = create_dimension(desc, dims) self.assertEqual(1, len(dim.hierarchies)) hier = dim.hierarchy() self.assertEqual(2, len(hier.levels)) template = self.dimension.to_dict() template["hierarchies"] = [ {"name": "ym", "levels": ["year", "month"]}, {"name": "ymd", "levels": ["year", "month", "day"]} ] template["default_hierarchy_name"] = "ym" template = create_dimension(template) dims = {"date": template} desc = {"template": "date", "name":"another_date"} dim = create_dimension(desc, dims) self.assertEqual(2, len(dim.hierarchies)) self.assertEqual(["ym", "ymd"], [hier.name for hier in dim.hierarchies]) def test_template_hierarchies(self): md = { "name": "time", "levels": ["year", "month", "day", "hour"], "hierarchies": [ {"name": "full", "levels": ["year", "month", "day", "hour"]}, {"name": "ymd", "levels": ["year", "month", "day"]}, {"name": "ym", "levels": ["year", "month"]}, {"name": "y", "levels": ["year"]}, ] } dim_time = create_dimension(md) templates = {"time": dim_time} md = { "name": "date", "template": "time", "hierarchies": [ "ymd", "ym", "y" ] } dim_date = create_dimension(md, templates) self.assertEqual(dim_date.name, "date") self.assertEqual(len(dim_date.hierarchies), 3) names = [h.name for h in dim_date.hierarchies] self.assertEqual(["ymd", "ym", "y"], names) def test_template_info(self): md = { "name": "template", "levels": [ { "name": "one", "info": {"units":"$", "format": "foo"}} ] } tempdim = create_dimension(md) md = { "name": "dim", "levels": [ { "name": "one", "info": {"units":"USD"}} ], "template": "template" } templates = {"template": tempdim} dim = create_dimension(md, templates) level = dim.level("one") self.assertIn("units", level.info) self.assertIn("format", level.info) self.assertEqual(level.info["units"], "USD") self.assertEqual(level.info["format"], "foo")