def test__init_5(self): dogs = Dogs() persons = Persons() with self.assertRaises(TypeError) : junction_.Junction((dogs,persons),()) with self.assertRaises(TypeError) : junction_.Junction((dogs,persons),('owners',)) with self.assertRaisesRegex(TypeError, r'%s is not an instance of %s' % (repr(1), str)): junction_.Junction((dogs,persons), ('owner',1)) with self.assertRaisesRegex(TypeError, r'%s is not an instance of %s' % (repr(None), str)): junction_.Junction((dogs,persons), (None,None))
def test__init_4(self): dogs = Dogs() persons = Persons() with self.assertRaises(TypeError) : junction_.Junction((), ('owners','pets')) with self.assertRaises(TypeError) : junction_.Junction((dogs,), ('owners','pets')) with self.assertRaisesRegex(TypeError, '%s is not an instance of %s' % (repr('foo'), table_.Table)) : junction_.Junction(('foo',persons), ('owners','pets')) with self.assertRaisesRegex(TypeError, '%s is not an instance of %s' % (repr('foo'), table_.Table)) : junction_.Junction((dogs,'foo'), ('owners','pets')) with self.assertRaisesRegex(TypeError, r'%s is not an instance of %s' % (repr(None), table_.Table)): junction_.Junction((None,None), ('owners','pets'))
def test__relation_1(self): dogs = Dogs() persons = Persons() rel1 = junction_.Junction((dogs, persons), ('owners', 'pets')) rel2 = junction_.Junction((dogs, persons), ('sellers', 'sold')) pin = endpoint_.Endpoint(rel1, relation_.LEFT) self.assertIs(pin.relation, rel1) pin.relation = rel2 self.assertIs(pin.relation, rel2) with self.assertRaisesRegex( TypeError, '%s is not an instance of %s' % (repr('foo'), repr(relation_.Relation))): pin.relation = 'foo'
def test__opposite_table_1(self): dogs = Dogs() persons = Persons() rel = junction_.Junction((dogs,persons),('owners','pets')) self.assertIs(rel.opposite_table(relation_.LEFT), persons) self.assertIs(rel.opposite_table(relation_.RIGHT), dogs)
def test__indices_for_3(self): dogs = Dogs() persons = Persons() rel = junction_.Junction((dogs,persons),('owners','pets')) with self.assertRaisesRegex(ValueError, r'%s is neither LEFT nor RIGHT' % repr('foo')): rel.indices_for('foo', 2)
def test__pair_1(self): dogs, persons = (Dogs(), Persons()) rel = junction_.Junction((dogs, persons), ('owners', 'pets')) l_pin = endpoint_.Endpoint(rel, relation_.LEFT) r_pin = endpoint_.Endpoint(rel, relation_.RIGHT) self.assertEqual(l_pin.pair(1, 2), pair_.Pair(1, 2)) self.assertEqual(r_pin.pair(2, 1), pair_.Pair(1, 2))
def test__opposite_table_2(self): dogs = Dogs() persons = Persons() rel = junction_.Junction((dogs,persons),('owners','pets'),) with self.assertRaisesRegex(ValueError, r'%s is neither LEFT nor RIGHT' % repr('foo')): rel.opposite_table('foo')
def test__opposite_entities_1(self): (nil, one) = (Str(('nil', )), Str(('one', ))) (zero, jeden, dwa) = (Str(('zero', )), Str(('jeden', )), Str( ('dwa', ))) l_tab = Strs({'0': nil, '1': one}) r_tab = Strs({0: zero, 1: jeden, 2: dwa}) pairs = (pair_.Pair('0', 1), pair_.Pair('0', 2), pair_.Pair('1', 0)) rel = junction_.Junction((l_tab, r_tab), ('opposite', 'opposite'), pairs) left = endpoint_.Endpoint(rel, relation_.LEFT) right = endpoint_.Endpoint(rel, relation_.RIGHT) self.assertEqual(len(tuple(left.opposite_entities('0'))), 2) self.assertEqual(len(tuple(left.opposite_entities('1'))), 1) self.assertIs(tuple(left.opposite_entities('0'))[0], jeden) self.assertIs(tuple(left.opposite_entities('0'))[1], dwa) self.assertIs(tuple(left.opposite_entities('1'))[0], zero) self.assertEqual(len(tuple(right.opposite_entities(0))), 1) self.assertEqual(len(tuple(right.opposite_entities(1))), 1) self.assertEqual(len(tuple(right.opposite_entities(2))), 1) self.assertIs(tuple(right.opposite_entities(0))[0], one) self.assertIs(tuple(right.opposite_entities(1))[0], nil) self.assertIs(tuple(right.opposite_entities(2))[0], nil)
def test__setitem_by_slice_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1)] rel = junction_.Junction((dogs,persons),('owners','pets')) rel[:] = pairs self.assertIs(rel._list[0], pairs[0]) # (0,0) self.assertIs(rel._list[1], pairs[1]) # (1,1) self.assertIs(rel._list[2], pairs[2]) # (2,0) self.assertIs(rel._list[3], pairs[3]) # (0,1) self.assertEqual(rel._fkmaps[0], {0 : [0, 3], 1 : [1], 2 : [2]}) self.assertEqual(rel._fkmaps[1], {0 : [0, 2], 1 : [1, 3]}) rel[1:3] = [ pairs[0], pairs[3] ] self.assertIs(rel._list[0], pairs[0]) # (0,0) self.assertIs(rel._list[1], pairs[0]) # (0,0) self.assertIs(rel._list[2], pairs[3]) # (0,1) self.assertIs(rel._list[3], pairs[3]) # (0,1) self.assertEqual(rel._fkmaps[0], {0 : [0, 1, 2, 3]}) self.assertEqual(rel._fkmaps[1], {0 : [0, 1], 1 : [2, 3]})
def test__keywrap__1(self): docs = (tableclass(Document))() pages = (tableclass(Page))() dct = join_.JoinTypeDict() rel = junction_.Junction((docs,pages),('pages','doc')) self.assertIsInstance(dct.__keywrap__(rel), ref_.Ref) self.assertIs(dct.__keywrap__(rel).obj, rel)
def test__endnames_1(self): dogs = Dogs() persons = Persons() owners = 'owners' pets = 'pets' rel = junction_.Junction((dogs,persons),(owners, pets)) self.assertIs(rel.endnames[relation_.LEFT], owners) self.assertIs(rel.endnames[relation_.RIGHT], pets)
def test__setitem_by_slice_3(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1) ] rel = junction_.Junction((dogs,persons),('owners','pets'), pairs) with self.assertRaisesRegex(TypeError, r'%s is not an instance of %s' % (repr('foo'), repr(pair_.Pair))): rel[:] = ['foo', 'bar']
def test__indices_for_2(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) self.assertEqual(rel.indices_for(relation_.LEFT, 4), []) self.assertEqual(rel.indices_for(relation_.RIGHT, 4), [])
def test__opposite_table_1(self): left = Dogs() right = Dogs() rel = junction_.Junction((left, right), ('parents', 'children')) self.assertIs( endpoint_.Endpoint(rel, relation_.LEFT).opposite_table, right) self.assertIs( endpoint_.Endpoint(rel, relation_.RIGHT).opposite_table, left)
def test__opposite_records_2(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) self.assertEqual(list(rel.opposite_records(relation_.LEFT, 4)), []) self.assertEqual(list(rel.opposite_records(relation_.RIGHT, 4)), [])
def test__opposite_records_1(self): (nil, one) = (Str(('nil', )), Str(('one', ))) (zero, jeden, dwa) = (Str(('zero', )), Str(('jeden', )), Str( ('dwa', ))) l_tab = Strs({'0': nil, '1': one}) r_tab = Strs({0: zero, 1: jeden, 2: dwa}) pairs = (pair_.Pair('0', 1), pair_.Pair('0', 2), pair_.Pair('1', 0)) rel = junction_.Junction((l_tab, r_tab), ('opposite', 'opposite'), pairs) left = endpoint_.Endpoint(rel, relation_.LEFT) right = endpoint_.Endpoint(rel, relation_.RIGHT) lo = { '0': tuple(left.opposite_records('0')), '1': tuple(left.opposite_records('1')) } ro = { 0: tuple(right.opposite_records(0)), 1: tuple(right.opposite_records(1)), 2: tuple(right.opposite_records(2)) } self.assertEqual(len(lo['0']), 2) self.assertEqual(len(lo['1']), 1) self.assertIsInstance(lo['0'][0], StrRec) self.assertIsInstance(lo['0'][1], StrRec) self.assertIsInstance(lo['1'][0], StrRec) self.assertEqual(lo['0'][0], StrRec(jeden, r_tab, 1)) self.assertEqual(lo['0'][1], StrRec(dwa, r_tab, 2)) self.assertEqual(lo['1'][0], StrRec(zero, r_tab, 0)) self.assertIs(lo['0'][0].table, r_tab) self.assertIs(lo['0'][1].table, r_tab) self.assertIs(lo['1'][0].table, r_tab) self.assertEqual(lo['0'][0].id, 1) self.assertEqual(lo['0'][1].id, 2) self.assertEqual(lo['1'][0].id, 0) self.assertEqual(len(ro[0]), 1) self.assertEqual(len(ro[1]), 1) self.assertEqual(len(ro[2]), 1) self.assertIsInstance(ro[0][0], StrRec) self.assertIsInstance(ro[1][0], StrRec) self.assertIsInstance(ro[2][0], StrRec) self.assertEqual(ro[0][0], StrRec(one, l_tab, 1)) self.assertEqual(ro[1][0], StrRec(nil, l_tab, 0)) self.assertEqual(ro[2][0], StrRec(nil, l_tab, 0)) self.assertIs(ro[0][0].table, l_tab) self.assertIs(ro[1][0].table, l_tab) self.assertIs(ro[2][0].table, l_tab) self.assertIs(ro[0][0].id, '1') self.assertIs(ro[1][0].id, '0') self.assertIs(ro[2][0].id, '0')
def test__repr_1(self): dogs = Dogs() persons = Persons() rel = junction_.Junction((dogs, persons), ('owners', 'pets')) s = functions_.modelrefstr(rel) self.assertEqual(repr(endpoint_.Endpoint(rel, relation_.LEFT)), 'Endpoint(relation=%s,side=LEFT)' % s) self.assertEqual(repr(endpoint_.Endpoint(rel, relation_.RIGHT)), 'Endpoint(relation=%s,side=RIGHT)' % s)
def test__index_2(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(0,0) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) self.assertEqual(rel.index(pairs[0]), 0) self.assertEqual(rel.index(pairs[1]), 1) self.assertEqual(rel.index(pairs[2]), 0)
def test__setitem_by_index_2(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1)] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) with self.assertRaises(IndexError): rel[ 4] = pairs[2] with self.assertRaises(IndexError): rel[-5] = pairs[2]
def test__opposite_side_1(self): dogs = Dogs() persons = Persons() rel = junction_.Junction((dogs,persons),('owners','pets')) self.assertIs(junction_.Junction.opposite_side(relation_.LEFT), relation_.RIGHT) self.assertIs(junction_.Junction.opposite_side(relation_.RIGHT), relation_.LEFT) with self.assertRaisesRegex(ValueError, r'%s is neither LEFT nor RIGHT' % repr('foo')): junction_.Junction.opposite_side('foo')
def test__count_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(0,0) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) self.assertEqual(rel.count(pairs[0]), 2) self.assertEqual(rel.count(pairs[1]), 1) self.assertEqual(rel.count(pairs[2]), 2) self.assertEqual(rel.count(pair_.Pair(2,2)), 0)
def test__repr_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(0,0) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) self.assertEqual(repr(rel), 'Junction((%s,%s),(%s,%s))' % ( functions_.modelrefstr(dogs), functions_.modelrefstr(persons), repr('owners'), repr('pets') ))
def test__contains_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs[:3]) self.assertTrue(pairs[0] in rel) self.assertTrue(pairs[1] in rel) self.assertTrue(pairs[2] in rel) self.assertTrue(pairs[3] not in rel)
def test__opposite_entities_1(self): left = Chars({'a' : Char(('A',)), 'b' : Char(('B',)), 'c' : Char(('C',))}) right = Chars({'a' : Char(('X',)), 'b' : Char(('Y',))}) pairs = [ pair_.Pair('a','a'), pair_.Pair('b','b'), pair_.Pair('c','a'), pair_.Pair('a','b') ] rel = junction_.Junction((left,right),('r','l'),pairs) self.assertEqual(list(rel.opposite_entities(relation_.LEFT, 'a')), [Char(('X',)), Char(('Y',))]) self.assertEqual(list(rel.opposite_entities(relation_.LEFT, 'b')), [Char(('Y',))]) self.assertEqual(list(rel.opposite_entities(relation_.LEFT, 'c')), [Char(('X',))]) self.assertEqual(list(rel.opposite_entities(relation_.RIGHT, 'a')), [Char(('A',)),Char(('C',))]) self.assertEqual(list(rel.opposite_entities(relation_.RIGHT, 'b')), [Char(('B',)),Char(('A',))])
def test__opposite_keys_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair('a','a'), pair_.Pair('b','b'), pair_.Pair('c','a'), pair_.Pair('a','b') ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) self.assertEqual(list(rel.opposite_keys(relation_.LEFT, 'a')), ['a', 'b']) self.assertEqual(list(rel.opposite_keys(relation_.LEFT, 'b')), ['b']) self.assertEqual(list(rel.opposite_keys(relation_.LEFT, 'c')), ['a']) self.assertEqual(list(rel.opposite_keys(relation_.RIGHT, 'a')), ['a','c']) self.assertEqual(list(rel.opposite_keys(relation_.RIGHT, 'b')), ['b','a'])
def test__relations_for_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) self.assertEqual([id(x) for x in rel.relations_for(relation_.LEFT, 0)], [id(pairs[0]),id(pairs[3])]) self.assertEqual([id(x) for x in rel.relations_for(relation_.LEFT, 1)], [id(pairs[1])]) self.assertEqual([id(x) for x in rel.relations_for(relation_.LEFT, 2)], [id(pairs[2])]) self.assertEqual([id(x) for x in rel.relations_for(relation_.RIGHT, 0)], [id(pairs[0]),id(pairs[2])]) self.assertEqual([id(x) for x in rel.relations_for(relation_.RIGHT, 1)], [id(pairs[1]),id(pairs[3])])
def test__init_1(self): dogs = Dogs() persons = Persons() rel = junction_.Junction((dogs, persons), ('owners', 'pets')) l_pin = endpoint_.Endpoint(rel, relation_.LEFT) r_pin = endpoint_.Endpoint(rel, relation_.RIGHT) self.assertIs(l_pin.relation, rel) self.assertIs(r_pin.relation, rel) self.assertIs(l_pin.side, relation_.LEFT) self.assertIs(r_pin.side, relation_.RIGHT)
def test__opposite_side_1(self): dogs = Dogs() persons = Persons() rel = junction_.Junction((dogs, persons), ('owners', 'pets')) pin = endpoint_.Endpoint(rel, relation_.LEFT) self.assertIs(pin.opposite_side, relation_.RIGHT) pin.side = relation_.RIGHT self.assertIs(pin.opposite_side, relation_.LEFT) with self.assertRaisesRegex(AttributeError, r"can't set attribute"): pin.opposite_side = relation_.LEFT
def test__iter_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) it = iter(rel) self.assertIs(next(it), pairs[0]) self.assertIs(next(it), pairs[1]) self.assertIs(next(it), pairs[2]) self.assertIs(next(it), pairs[3])
def test__reversed_1(self): dogs = Dogs() persons = Persons() pairs = [ pair_.Pair(0,0), pair_.Pair(1,1), pair_.Pair(2,0), pair_.Pair(0,1) ] rel = junction_.Junction((dogs,persons),('owners','pets'),pairs) rev = list(reversed(rel)) self.assertIs(rev[0], pairs[3]) self.assertIs(rev[1], pairs[2]) self.assertIs(rev[2], pairs[1]) self.assertIs(rev[3], pairs[0])