def test_link_hierarchies(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = cubes.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 = cubes.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 = cubes.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 setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = cubes.attribute_list(["amount", "discount"]) self.dimensions = [cubes.create_dimension(desc) for desc in a] self.cube = cubes.Cube("contracts", dimensions=self.dimensions, measures=self.measures)
def setUp(self): engine = create_engine('sqlite://') self.connection = engine.connect() self.metadata = MetaData() self.metadata.bind = engine self.table_name = "test" # Prepare table table = sqlalchemy.Table(self.table_name, self.metadata) table.append_column(Column("id", String)) table.append_column(Column("color", String)) table.append_column(Column("tone", String)) table.append_column(Column("size", String)) table.append_column(Column("temperature", String)) table.create(self.connection) self.table = table # Prepare model self.model = cubes.Model() self.cube = cubes.Cube("test") self.model.add_cube(self.cube) desc = {"name": "color", "levels": ["color", "tone"]} dimension = cubes.create_dimension(desc) self.cube.add_dimension(dimension) dimension = cubes.create_dimension("size") self.cube.add_dimension(dimension)
def setUp(self): a = [DIM_DATE_DESC, DIM_PRODUCT_DESC, DIM_FLAG_DESC] self.measures = cubes.attribute_list(["amount", "discount"]) self.dimensions = [cubes.create_dimension(desc) for desc in a] self.cube = cubes.Cube("contracts", dimensions=self.dimensions, measures=self.measures) self.model = cubes.Model(cubes=[self.cube], dimensions=self.dimensions) self.model_path = os.path.join(DATA_PATH, 'model.json')
def test_links(self): dims = dict((d.name, d) for d in self.dimensions) links = [{"name": "date"}] cube = cubes.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 = cubes.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 setUp(self): date_desc = { "name": "date", "levels": { "year": { "key": "year", "attributes": [{ "name": "year", "order": "ascending" }] }, "month": { "key": "month", "attributes": [{ "name": "month", "order": "ascending" }, { "name": "month_name" }] } }, "default_hierarchy": "ym", "hierarchies": { "ym": { "levels": ["year", "month"] } } } class_desc = { "name": "cls", "levels": { "group": { "key": "group_id", "attributes": ["group_id", "group_desc"] }, "class": { "key": "class_id", "attributes": ["class_id", "class_desc"] } }, "default_hierarchy": "default", "hierarchies": { "default": { "levels": ["group", "class"] } } } cube = { "name": "testcube", } self.date_dim = cubes.create_dimension(date_desc) self.class_dim = cubes.create_dimension(class_desc) dims = [self.date_dim, self.class_dim] self.model = cubes.Model('test', dimensions=dims) self.cube = cubes.Cube("testcube", dimensions=dims) self.model.add_cube(self.cube) self.cube.measures = [cubes.Attribute("amount")] self.cube.mappings = { "amount": "ft_contracts.amount", "date.year": "dm_date.year", "date.month": "dm_date.month", "date.month_name": "dm_date.month_name", "cls.group_id": "dm_cls.group_id", "cls.class_id": "dm_cls.class_id", "cls.group_desc": "dm_cls.group_desc", "cls.class_desc": "dm_cls.class_desc", "cls.id": "dm_cls.id", "date.id": "dm_date.id", "fact.date_id": "ft_contracts.date_id", "fact.cls_id": "ft_contracts.cls_id" } self.cube.fact = "ft_contracts" self.mappings2 = {"amount": "ft_contracts.amount"} self.cube.joins = [{ "master": "fact.date_id", "detail": "date.id" }, { "master": "fact.cls_id", "detail": "cls.id" }] self.prepare_data()
import cubes.tutorial.sql as tutorial engine = sqlalchemy.create_engine('sqlite:///:memory:') tutorial.create_table_from_csv(engine, "IBRD_Balance_Sheet__FY2010.csv", table_name="irbd_balance", fields=[("category", "string"), ("line_item", "string"), ("year", "integer"), ("amount", "integer")], create_id=True) model = cubes.Model model.add_dimension(cubes.Dimension("category")) model.add_dimension(cubes.Dimension("line_item")) model.add_dimension(cubes.Dimension("year")) cube = cubes.Cube(name="irbd_balance", model=model, dimensions=["category", "line_item", "year"], measures=["amount"]) browser = cubes.backends.sql.SQLBrowser(cube, engine.connect(), view_name="irbd_balance") cell = browser.full_cube() result = browser.aggregate(cell) print("Record count: %d" % result.summary["record_count"]) print("Total amount: %d" % result.summary["amount_sum"])