def testViewCreation(self): "Test: creating views" view_def = ''' CREATE VIEW view_name AS SELECT node.a1 = node.a3 - node.a1, node.a2 = node.a1 + node.a2, node.a3 = 2 * node.a5 + node.a3, node.a5, node.a_new = 75 RANKED lowest(2*5, node.a3*(node.a5^2)+1) FROM db ''' where_clause = ''' WHERE node.a3 or (1+2*round(node.a1,2) = .1E5+4.E-4) or node.a1 == 1 and node.a2 == "test" or 1+node.a1 <> 4 ''' create = view_def + ';' create_where = view_def + where_clause + ';' attr_names = [ 'a%s' % i for i in range(1,10) ] db = self.db for i in range(9): db.addAttribute(attr_names[i], int) for a5 in range(-100,100): node = DBNode(db) for i in range(9): setattr(node, attr_names[i], i+1) node.a5 = a5 db.addNode(node) sview0 = NodeView( parse_view_spec(create.replace('view_name', 'sview0')), self.viewreg ) self.assertEqual(2*5, len(sview0.getBucket())) sview1 = NodeView( parse_view_spec(create_where.replace('view_name', 'sview1')), self.viewreg ) self.assertEqual(2*5, len(sview1.getBucket())) sview0_bucket = sview0.getBucket() for node in sview1.getBucket(): self.assert_(node in sview0_bucket) create_subview = create_where.replace('FROM db', 'FROM sview0') sview2 = NodeView( parse_view_spec(create_subview.replace('view_name', 'sview2')), self.viewreg ) self.assertEqual(2*5, len(sview2.getBucket())) create_mergeview = create_where.replace('2*5','2*10').replace('FROM db', 'FROM sview0, sview1') sview3 = NodeView( parse_view_spec(create_mergeview.replace('view_name', 'sview3')), self.viewreg ) # both parent nodes are equal, merging them should filter duplicates self.assertEqual(2*5, len(sview3.getBucket())) ## # merging parents without DISTINCT adds duplicates ## create_mergeview_dup = create_mergeview.replace('DISTINCT','') ## sview3_dup = NodeView( parse_view_spec(create_mergeview_dup.replace('view_name', 'sview3_dup')), self.viewreg ) ## self.assertEqual(2*10, len(sview3_dup.getBucket())) # verify that nodes only appear once nodes = [] for node in sview3.getBucket(): self.assert_(node not in nodes) nodes.append(node) # retry inheriting from all known views for i in range(10): list_of_known_views = ','.join(view_name for view_name in self.viewreg) create_mergeview = create_where.replace('2*5', '2*20').replace('FROM db', 'FROM '+list_of_known_views) sview4 = NodeView( parse_view_spec(create_mergeview.replace('view_name', 'sview%02d'%i)), self.viewreg ) # verify that nodes only appear once nodes = [] for node in sview3.getBucket(): self.assert_(node not in nodes) nodes.append(node) nodes_a5 = set() for node in sview4.getBucket(): self.assert_(node.a5 not in nodes_a5) nodes_a5.add(node.a5)
def testViewVariables(self): "Test: variable usage in views" view_def = ''' CREATE VIEW view_name AS SELECT node.a1 = (node.a3 - node.a1) * var_a, node.a2 = node.a1 + node.a2, node.a3 = 2 * node.a5 + node.a3, node.a5, node.a_new = var_a RANKED lowest(var_a*2, var_a*node.a3*(node.a5^2)+1) FROM db WITH var_a = 5 ''' where_clause = ''' WHERE node.a3 or (1+2*round(node.a1,2) = .1E5+4.E-4) or node.a1 == 1 and node.a2 == "test" or 1+node.a1 <> 4 ''' create = view_def + ';' create_where = view_def + where_clause + ';' attr_names = [ 'a%s' % i for i in range(1,10) ] db = self.db for i in range(9): db.addAttribute(attr_names[i], int) for a5 in range(-100,100): node = DBNode(db) for i in range(9): setattr(node, attr_names[i], i+1) node.a5 = a5 db.addNode(node) sview0 = NodeView( parse_view_spec(create.replace('view_name', 'sview0')), self.viewreg ) self.assertEqual(len(sview0.getBucket()), len(sview0)) some_node = sview0.getBucket()[0] a1_of_some_node = some_node.a1 self.assertEqual(2*5, len(sview0)) sview0.setVariable('var_a', 8) self.assertEqual(2*8, len(sview0)) some_node = sview0.getBucket()[0] self.assertNotEqual(a1_of_some_node, some_node.a1) sview1 = NodeView( parse_view_spec(create_where.replace('view_name', 'sview1')), self.viewreg ) self.assertEqual(2* 5, len(sview1)) sview1.setVariable('var_a', 16) self.assertEqual(2*16, len(sview1)) self.assertEqual(2* 8, len(sview0)) create_subview = create_where.replace('FROM db', 'FROM sview0') sview2 = NodeView( parse_view_spec(create_subview.replace('view_name', 'sview2')), self.viewreg ) sview2.setVariable('var_a', 1000) self.assertEqual(2*8, len(sview0)) self.assertEqual(2*8, len(sview2)) self.assertEqual(2*16, len(sview1))