Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

Changeset 16

Show
Ignore:
Timestamp:
02/18/07 01:00:47
Author:
fumanchu
Message:

More tests, plus Tables now join themselves with &, >> and <<.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/geniusql/objects.py

    r15 r16  
    429429                    continue 
    430430            yield row 
     431     
     432     
     433    # ------------------------------ Joins ------------------------------ # 
     434     
     435    def __lshift__(self, other): 
     436        return select.Join(self, other, leftbiased=True) 
     437    __rrshift__ = __lshift__ 
     438     
     439    def __rshift__(self, other): 
     440        return select.Join(self, other, leftbiased=False) 
     441    __rlshift__ = __rshift__ 
     442     
     443    def __and__(self, other): 
     444        return select.Join(self, other) 
     445     
     446    def __rand__(self, other): 
     447        return select.Join(other, self) 
    431448 
    432449 
  • trunk/geniusql/test/zoo_fixture.py

    r15 r16  
    4545        Animal.add_index('ID') 
    4646        Animal['ZooID'] = schema.column(int) 
     47        Animal['Name'] = schema.column(hints={'bytes': 100}) 
    4748        Animal['Species'] = schema.column(hints={'bytes': 100}) 
    4849        Animal['Legs'] = schema.column(int, default=4) 
     
    194195         
    195196        # Add a mother and child to test relationships 
    196         bai_yun = schema['Animal'].insert(Species='Ape', Legs=2)['ID'] 
    197         schema['Animal'].insert(Species='Ape', Legs=2, MotherID=bai_yun) 
     197        bai_yun = schema['Animal'].insert(Species='Ape', Name='Bai Yun', Legs=2) 
     198        schema['Animal'].insert(Species='Ape', Name='Hua Mei', Legs=2, 
     199                                MotherID=bai_yun['ID']) 
    198200         
    199201        # Exhibits 
    200202        schema['Exhibit'].insert(Name = 'The Penguin Encounter', 
    201                              ZooID = seaworld, 
    202                              Animals = [emp, adelie], 
    203                              PettingAllowed = True, 
    204                              Acreage = 3.1, 
    205                              # See ticket #45 
    206                              Creators = (u'Richard F\xfcrst', u'Sonja Martin'), 
    207                              ) 
     203                                 ZooID = seaworld, 
     204                                 Animals = [emp, adelie], 
     205                                 PettingAllowed = True, 
     206                                 Acreage = 3.1, 
     207                                 # See ticket #45 
     208                                 Creators = (u'Richard F\xfcrst', 
     209                                             u'Sonja Martin'), 
     210                                 ) 
    208211         
    209212        schema['Exhibit'].insert(Name = 'Tiger River', 
    210                   ZooID = sdz, 
    211                   Animals = [tiger], 
    212                   PettingAllowed = False, 
    213                   Acreage = 4, 
    214                  
     213                                 ZooID = sdz, 
     214                                 Animals = [tiger], 
     215                                 PettingAllowed = False, 
     216                                 Acreage = 4, 
     217                                 
    215218         
    216219        # Vets 
    217         cs = schema['Vet'].insert(Name = 'Charles Schroeder', ZooID = sdz)['ID'] 
    218         self.assertEqual(cs, schema['Vet']['ID'].initial) 
     220        cs = schema['Vet'].insert(Name = 'Charles Schroeder', ZooID = sdz) 
     221        self.assertEqual(cs['ID'], schema['Vet']['ID'].initial) 
    219222         
    220223        jm = schema['Vet'].insert(Name = 'Jim McBain', ZooID = seaworld)['ID'] 
     
    222225        # Visits 
    223226        for d in every13days: 
    224             schema['Visit'].insert(VetID=cs, AnimalID=tiger, Date=d) 
     227            schema['Visit'].insert(VetID=cs['ID'], AnimalID=tiger, Date=d) 
    225228        for d in every17days: 
    226229            schema['Visit'].insert(VetID=jm, AnimalID=emp, Date=d) 
     
    479482            self.assertEqual(SDZ['Admission'], 0.0) 
    480483     
    481 ##    def test_7_Multirecall(self): 
    482 ##        box = arena.new_sandbox() 
    483 ##        try: 
    484 ##            f = (lambda z, a: z.Name == 'San Diego Zoo') 
    485 ##            zooed_animals = box.recall(Zoo & Animal, f) 
    486 ##            self.assertEqual(len(zooed_animals), 2) 
    487 ##             
    488 ##            SDZ = box.unit(Zoo, Name='San Diego Zoo') 
    489 ##            aid = 0 
    490 ##            for z, a in zooed_animals: 
    491 ##                self.assertEqual(id(z), id(SDZ)) 
    492 ##                self.assertNotEqual(id(a), aid) 
    493 ##                aid = id(a) 
    494 ##             
    495 ##            # Assert that multirecalls with no matching related units returns 
    496 ##            # no matches for the initial class, since all joins are INNER. 
    497 ##            # We're also going to test that you can combine a one-arg expr 
    498 ##            # with a two-arg expr. 
    499 ##            sdexpr = logic.filter(Name='San Diego Zoo') 
    500 ##            leo = lambda z, a: a.Species == 'Leopard' 
    501 ##            zooed_animals = box.recall(Zoo & Animal, sdexpr + leo) 
    502 ##            self.assertEqual(len(zooed_animals), 0) 
    503 ##             
    504 ##            # Now try the same expr with INNER, LEFT, and RIGHT JOINs. 
    505 ##            zooed_animals = box.recall(Zoo & Animal) 
    506 ##            self.assertEqual(len(zooed_animals), 6) 
    507 ##            self.assertEqual(set([(z.Name, a.Species) for z, a in zooed_animals]), 
    508 ##                             set([("Wild Animal Park", "Leopard"), 
    509 ##                                  ("Wild Animal Park", "Lion"), 
    510 ##                                  ("San Diego Zoo", "Tiger"), 
    511 ##                                  ("San Diego Zoo", "Millipede"), 
    512 ##                                  ("Sea_World", "Emperor Penguin"), 
    513 ##                                  ("Sea_World", "Adelie Penguin")])) 
    514 ##             
    515 ##            zooed_animals = box.recall(Zoo >> Animal) 
    516 ##            self.assertEqual(len(zooed_animals), 12) 
    517 ##            self.assertEqual(set([(z.Name, a.Species) for z, a in zooed_animals]), 
    518 ##                             set([("Wild Animal Park", "Leopard"), 
    519 ##                                  ("Wild Animal Park", "Lion"), 
    520 ##                                  ("San Diego Zoo", "Tiger"), 
    521 ##                                  ("San Diego Zoo", "Millipede"), 
    522 ##                                  ("Sea_World", "Emperor Penguin"), 
    523 ##                                  ("Sea_World", "Adelie Penguin"), 
    524 ##                                  (None, "Slug"), 
    525 ##                                  (None, "Bear"), 
    526 ##                                  (None, "Ostrich"), 
    527 ##                                  (None, "Centipede"), 
    528 ##                                  (None, "Ape"), 
    529 ##                                  (None, "Ape"), 
    530 ##                                  ])) 
    531 ##             
    532 ##            zooed_animals = box.recall(Zoo << Animal) 
    533 ##            self.assertEqual(len(zooed_animals), 7) 
    534 ##            self.assertEqual(set([(z.Name, a.Species) for z, a in zooed_animals]), 
    535 ##                             set([("Wild Animal Park", "Leopard"), 
    536 ##                                  ("Wild Animal Park", "Lion"), 
    537 ##                                  ("San Diego Zoo", "Tiger"), 
    538 ##                                  ("San Diego Zoo", "Millipede"), 
    539 ##                                  ("Sea_World", "Emperor Penguin"), 
    540 ##                                  ("Sea_World", "Adelie Penguin"), 
    541 ##                                  (u'Montr\xe9al Biod\xf4me', None), 
    542 ##                                  ])) 
    543 ##             
    544 ##            # Try a multiple-arg expression 
    545 ##            f = (lambda a, z: a.Legs >= 4 and z.Admission < 10) 
    546 ##            animal_zoos = box.recall(Animal & Zoo, f) 
    547 ##            self.assertEqual(len(animal_zoos), 4) 
    548 ##            names = [a.Species for a, z in animal_zoos] 
    549 ##            names.sort() 
    550 ##            self.assertEqual(names, ['Leopard', 'Lion', 'Millipede', 'Tiger']) 
    551 ##             
    552 ##            # Let's try three joined classes just for the sadistic fun of it. 
    553 ##            tree = (Animal >> Zoo) >> Vet 
    554 ##            f = (lambda a, z, v: z.Name == 'Sea_World') 
    555 ##            self.assertEqual(len(box.recall(tree, f)), 2) 
    556 ##             
     484    def test_7_Multiview(self): 
     485        try: 
     486            f = (lambda z, a: z.Name == 'San Diego Zoo') 
     487            zooed_animals = list(db.select(schema['Zoo'] & schema['Animal'], 
     488                                           [('ID', ), None], f)) 
     489            self.assertEqual(len(zooed_animals), 2) 
     490             
     491            SDZ = schema['Zoo'].select(Name='San Diego Zoo') 
     492            d = [] 
     493            for row in zooed_animals: 
     494                self.assertEqual(row[0], SDZ['ID']) 
     495                self.assertNotEqual(row, d) 
     496                d = row 
     497             
     498            # Assert that multiviews with no matching related units returns 
     499            # no matches for the initial class (if joins are INNER). 
     500            # We're also going to test that you can combine a one-arg expr 
     501            # with a two-arg expr. 
     502            sdexpr = logic.filter(Name='San Diego Zoo') 
     503            leo = lambda z, a: a.Species == 'Leopard' 
     504            zooed_animals = list(db.select(schema['Zoo'] & schema['Animal'], 
     505                                           None, sdexpr + leo)) 
     506            self.assertEqual(len(zooed_animals), 0) 
     507             
     508            # Now try the same query with INNER, LEFT, and RIGHT JOINs. 
     509            zooed_animals = list(db.select(schema['Zoo'] & schema['Animal'], 
     510                                           [('Name', ), ('Species', )])) 
     511            self.assertEqual(len(zooed_animals), 6) 
     512            self.assertEqual(set([tuple(row) for row in zooed_animals]), 
     513                             set([("Wild Animal Park", "Leopard"), 
     514                                  ("Wild Animal Park", "Lion"), 
     515                                  ("San Diego Zoo", "Tiger"), 
     516                                  ("San Diego Zoo", "Millipede"), 
     517                                  ("Sea_World", "Emperor Penguin"), 
     518                                  ("Sea_World", "Adelie Penguin")])) 
     519             
     520            zooed_animals = list(db.select(schema['Zoo'] >> schema['Animal'], 
     521                                           [('Name', ), ('Species', )])) 
     522            self.assertEqual(len(zooed_animals), 12) 
     523            self.assertEqual(set([tuple(row) for row in zooed_animals]), 
     524                             set([("Wild Animal Park", "Leopard"), 
     525                                  ("Wild Animal Park", "Lion"), 
     526                                  ("San Diego Zoo", "Tiger"), 
     527                                  ("San Diego Zoo", "Millipede"), 
     528                                  ("Sea_World", "Emperor Penguin"), 
     529                                  ("Sea_World", "Adelie Penguin"), 
     530                                  (None, "Slug"), 
     531                                  (None, "Bear"), 
     532                                  (None, "Ostrich"), 
     533                                  (None, "Centipede"), 
     534                                  (None, "Ape"), 
     535                                  (None, "Ape"), 
     536                                  ])) 
     537             
     538            zooed_animals = list(db.select(schema['Zoo'] << schema['Animal'], 
     539                                           [('Name', ), ('Species', )])) 
     540            self.assertEqual(len(zooed_animals), 7) 
     541            self.assertEqual(set([tuple(row) for row in zooed_animals]), 
     542                             set([("Wild Animal Park", "Leopard"), 
     543                                  ("Wild Animal Park", "Lion"), 
     544                                  ("San Diego Zoo", "Tiger"), 
     545                                  ("San Diego Zoo", "Millipede"), 
     546                                  ("Sea_World", "Emperor Penguin"), 
     547                                  ("Sea_World", "Adelie Penguin"), 
     548                                  (u'Montr\xe9al Biod\xf4me', None), 
     549                                  ])) 
     550             
     551            # Try a multiple-arg expression 
     552            f = (lambda a, z: a.Legs >= 4 and z.Admission < 10) 
     553            animal_zoos = list(db.select(schema['Animal'] & schema['Zoo'], 
     554                                         [('Species',), None], 
     555                                         f)) 
     556            self.assertEqual(len(animal_zoos), 4) 
     557            names = [row[0] for row in animal_zoos] 
     558            names.sort() 
     559            self.assertEqual(names, ['Leopard', 'Lion', 'Millipede', 'Tiger']) 
     560             
     561            # Let's try three joined classes just for the sadistic fun of it. 
     562            tree = (schema['Animal'] >> schema['Zoo']) >> schema['Vet'] 
     563            f = (lambda a, z, v: z.Name == 'Sea_World') 
     564            self.assertEqual(len(list(db.select(tree, None, f))), 2) 
     565             
    557566##            # MSAccess can't handle an INNER JOIN nested in an OUTER JOIN. 
    558567##            # Test that this fails for MSAccess, but works for other SM's. 
     
    577586##                set_azv() 
    578587##                self.assertEqual(len(azv[0]), 2) 
    579 ##             
    580 ##            # Try mentioning the same class twice. 
    581 ##            tree = (Animal << Animal
    582 ##            f = (lambda anim, mother: mother.ID != None) 
    583 ##            animals = [mother.ID for anim, mother in box.recall(tree, f)] 
    584 ##            self.assertEqual(animals, [11]) 
    585 ##        finally: 
    586 ##            box.flush_all() 
    587 ##     
     588             
     589            # Try mentioning the same class twice. 
     590            tree = (schema['Animal'] << schema['Animal']
     591            f = (lambda anim, mother: mother.ID != None) 
     592            animals = list(db.select(tree, [(), ('Name', )], f)) 
     593            self.assertEqual(animals, [['Hua Mei']]) 
     594        finally: 
     595            db.connections.commit() 
     596     
    588597##    def test_8_CustomAssociations(self): 
    589598##        box = arena.new_sandbox() 
     
    815824     
    816825    def test_Multithreading(self): 
    817 ##        print "skipped ", 
    818 ##        return 
    819          
    820826        f = lambda x: x.Legs == 4 
    821827        def box_per_thread(): 
     
    895901     
    896902    def test_numbers(self): 
    897 ##        print "skipped ", 
    898 ##        return 
    899          
    900903        float_prec = 53 
    901904        box = arena.new_sandbox() 
     
    10991102            # Run the other cases. 
    11001103##            tools.TestRunner.run(loader(NumericTests)) 
    1101  
    1102             tools.TestRunner.run(loader(ConcurrencyTests)) 
    1103             tools.TestRunner.run(loader(IsolationTests)) 
    1104  
     1104##            tools.TestRunner.run(loader(ConcurrencyTests)) 
     1105##            tools.TestRunner.run(loader(IsolationTests)) 
    11051106##            tools.TestRunner.run(loader(DiscoveryTests)) 
    11061107        except: