Contact: fumanchu@aminus.org

Log in as guest/geniusql to create tickets

Changeset 5

Show
Ignore:
Timestamp:
02/12/07 06:58:30
Author:
fumanchu
Message:

More tests. It looks like I'll have to give Column a "pytype" attribute after all.

Files:

Legend:

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

    r4 r5  
    940940                        (t.qname, fields, values), transconn) 
    941941         
    942         return self._grab_new_ids(t, idkeys, transconn) 
     942        if idkeys: 
     943            return self._grab_new_ids(t, idkeys, transconn) 
     944        else: 
     945            return {} 
    943946     
    944947    def _grab_new_ids(self, table, idkeys): 
  • trunk/geniusql/select.py

    r4 r5  
    492492        if restriction is None: 
    493493            restriction = logic.Expression(lambda *args: True) 
     494        elif not isinstance(restriction, logic.Expression): 
     495            restriction = logic.Expression(restriction) 
    494496        self.restriction = restriction 
    495497         
  • trunk/geniusql/test/zoo_fixture.py

    r4 r5  
    3232 
    3333 
     34Jan_1_2001 = datetime.date(2001, 1, 1) 
     35every13days = [Jan_1_2001 + datetime.timedelta(x * 13) for x in range(20)] 
     36every17days = [Jan_1_2001 + datetime.timedelta(x * 17) for x in range(20)] 
     37del x 
     38 
     39 
    3440class ZooTests(unittest.TestCase): 
    3541     
     
    3743        Animal = db.table('Animal') 
    3844        Animal['ID'] = db.column(int, autoincrement=True, key=True) 
     45        Animal.add_index('ID') 
    3946        Animal['ZooID'] = db.column(int) 
    4047        Animal['Species'] = db.column(hints={'bytes': 100}) 
     
    4754        Animal['PreferredFoodID'] = db.column(int) 
    4855        Animal['AlternateFoodID'] = db.column(int) 
    49         Animal.add_index('ID') 
    5056        Animal.add_index('ZooID') 
    5157        Animal.references['Animal'] = ('ID', 'Animal', 'MotherID') 
     58        Animal.references['Visit'] = ('ID', 'Visit', 'AnimalID') 
    5259        db['Animal'] = Animal 
    5360         
    5461        Zoo = db.table('Zoo') 
    5562        Zoo['ID'] = db.column(int, autoincrement=True, key=True) 
     63        Zoo.add_index('ID') 
    5664        Zoo['Name'] = db.column() 
    5765        Zoo['Founded'] = db.column(datetime.date) 
     
    6775            Zoo['Admission'] = db.column(float) 
    6876         
    69         Zoo.add_index('ID') 
    7077        Zoo.references['Animal'] = ('ID', 'Animal', 'ZooID') 
    7178        db['Zoo'] = Zoo 
     
    7380        Food = db.table('Food') 
    7481        Food['ID'] = db.column(int, autoincrement=True, key=True) 
     82        Food.add_index('ID') 
    7583        Food['Name'] = db.column() 
    7684        Food['NutritionValue'] = db.column(int) 
    77         Food.add_index('ID') 
    7885        Food.references['Animal'] = ('ID', 'Animal', 'PreferredFoodID') 
    7986        Animal.references['Alternate Food'] = ('AlternateFoodID', 'Food', 'ID') 
    8087        db['Food'] = Food 
    8188         
    82 ##        class Vet(Unit): 
    83 ##            """A Veterinarian.""" 
    84 ##            Name = UnitProperty() 
    85 ##            ZooID = UnitProperty(int, index=True) 
    86 ##            City = UnitProperty() 
    87 ##            sequencer = UnitSequencerInteger(initial=200) 
    88 ## 
    89 ##        Vet.many_to_one('ZooID', Zoo, 'ID') 
    90 ## 
    91 ## 
    92 ##        class Visit(Unit): 
    93 ##            """Work done by a Veterinarian on an Animal.""" 
    94 ##            VetID = UnitProperty(int, index=True) 
    95 ##            ZooID = UnitProperty(int, index=True) 
    96 ##            AnimalID = UnitProperty(int, index=True) 
    97 ##            Date = UnitProperty(datetime.date) 
    98 ## 
    99 ##        Vet.one_to_many('ID', Visit, 'VetID') 
    100 ##        Animal.one_to_many('ID', Visit, 'AnimalID') 
    101 ## 
    102 ## 
    103 ##        class Lecture(Visit): 
    104 ##            """A Visit by a Vet to train staff (rather than visit an Animal).""" 
    105 ##            AnimalID = None 
    106 ##            Topic = UnitProperty() 
    107 ## 
    108 ## 
    109 ##        class Exhibit(Unit): 
    110 ##            # Make this a string to help test vs unicode. 
    111 ##            Name = UnitProperty(str) 
    112 ##            ZooID = UnitProperty(int) 
    113 ##            Animals = UnitProperty(list) 
    114 ##            PettingAllowed = UnitProperty(bool) 
    115 ##            Creators = UnitProperty(tuple) 
    116 ##             
    117 ##            if typerefs.decimal: 
    118 ##                Acreage = UnitProperty(typerefs.decimal.Decimal) 
    119 ##            else: 
    120 ##                Acreage = UnitProperty(float) 
    121 ##             
    122 ##            # Remove the ID property (inherited from Unit) from the Exhibit class. 
    123 ##            ID = None 
    124 ##            sequencer = dejavu.UnitSequencer() 
    125 ##            identifiers = ("ZooID", Name) 
    126 ## 
    127 ##        Zoo.one_to_many('ID', Exhibit, 'ZooID') 
    128 ## 
    129 ## 
    130 ##        class NothingToDoWithZoos(Unit): 
    131 ##            ALong = UnitProperty(long, hints={'precision': 1}) 
    132 ##            AFloat = UnitProperty(float, hints={'precision': 1}) 
    133 ##            if typerefs.decimal: 
    134 ##                ADecimal = UnitProperty(typerefs.decimal.Decimal, 
    135 ##                                        hints={'precision': 1, 'scale': 1}) 
    136 ##            if typerefs.fixedpoint: 
    137 ##                AFixed = UnitProperty(typerefs.fixedpoint.FixedPoint, 
    138 ##                                      hints={'precision': 1, 'scale': 1}) 
    139 ## 
    140 ## 
    141 ##        Jan_1_2001 = datetime.date(2001, 1, 1) 
    142 ##        every13days = [Jan_1_2001 + datetime.timedelta(x * 13) for x in range(20)] 
    143 ##        every17days = [Jan_1_2001 + datetime.timedelta(x * 17) for x in range(20)] 
    144 ##        del x 
     89        Vet = db.table('Vet') 
     90        Vet['ID'] = c = db.column(int, autoincrement=True, key=True) 
     91        c.initial = 200 
     92        Vet.add_index('ID') 
     93        Vet['Name'] = db.column() 
     94        Vet['ZooID'] = db.column(int) 
     95        Vet.add_index('ZooID') 
     96        Vet['City'] = db.column() 
     97        Vet.references['Zoo'] = ('ZooID', 'Zoo', 'ID') 
     98        Vet.references['Visit'] = ('ID', 'Visit', 'VetID') 
     99        db['Vet'] = Vet 
     100         
     101        Visit = db.table('Visit') 
     102        Visit['ID'] = db.column(int, autoincrement=True, key=True) 
     103        Visit.add_index('ID') 
     104        Visit['VetID'] = db.column(int) 
     105        Visit.add_index('VetID') 
     106        Visit['ZooID'] = db.column(int) 
     107        Visit.add_index('ZooID') 
     108        Visit['AnimalID'] = db.column(int) 
     109        Visit.add_index('AnimalID') 
     110        Visit['Date'] = db.column(datetime.date) 
     111        db['Visit'] = Visit 
     112         
     113        Exhibit = db.table('Exhibit') 
     114        # Make this a string to help test vs unicode. 
     115        Exhibit['Name'] = db.column(str, key=True) 
     116        Exhibit.add_index('Name') 
     117        Exhibit['ZooID'] = db.column(int, key=True) 
     118        Exhibit.add_index('ZooID') 
     119        Exhibit['Animals'] = db.column(list) 
     120        Exhibit['PettingAllowed'] = db.column(bool) 
     121        Exhibit['Creators'] = db.column(tuple) 
     122         
     123        if typerefs.decimal: 
     124            Exhibit['Acreage'] = db.column(typerefs.decimal.Decimal) 
     125        else: 
     126            Exhibit['Acreage'] = db.column(float) 
     127         
     128        Exhibit.references['Zoo'] = ('ZooID', 'Zoo', 'ID') 
     129        db['Exhibit'] = Exhibit 
     130         
     131        t = db.table('NothingToDoWithZoos') 
     132        t['ALong'] = db.column(long, hints={'precision': 1}) 
     133        t['AFloat'] = db.column(float, hints={'precision': 1}) 
     134        if typerefs.decimal: 
     135            t['ADecimal'] = db.column(typerefs.decimal.Decimal, 
     136                                      hints={'precision': 1, 'scale': 1}) 
     137        if typerefs.fixedpoint: 
     138            t['AFixed'] = db.column(typerefs.fixedpoint.FixedPoint, 
     139                                    hints={'precision': 1, 'scale': 1}) 
     140        db['NothingToDoWithZoos'] = t 
    145141     
    146142    def test_2_populate(self): 
     
    153149                           Admission=4.95, 
    154150                           ) 
    155         wapid=int(newids['ID']) 
    156          
    157         db.insert('Zoo', Name = 'San Diego Zoo', 
    158                   # This early date should play havoc with a number 
    159                   # of implementations. 
    160                   Founded = datetime.date(1835, 9, 13), 
    161                   Opens = datetime.time(9, 0, 0), 
    162                   Admission = 0, 
    163                   ) 
     151        wap = int(newids['ID']) 
     152         
     153        newids = db.insert('Zoo', Name = 'San Diego Zoo', 
     154                           # This early date should play havoc with a number 
     155                           # of implementations. 
     156                           Founded = datetime.date(1835, 9, 13), 
     157                           Opens = datetime.time(9, 0, 0), 
     158                           Admission = 0, 
     159                           ) 
     160        sdz = int(newids['ID']) 
    164161         
    165162        db.insert('Zoo', Name = u'Montr\xe9al Biod\xf4me', 
     
    169166                  ) 
    170167         
    171         db.insert('Zoo', Name = 'Sea_World', Admission = 60) 
     168        newids = db.insert('Zoo', Name = 'Sea_World', Admission = 60) 
     169        seaworld = int(newids['ID']) 
    172170         
    173171        # Animals 
     
    175173        leopardid = int(newids['ID']) 
    176174        self.assertEqual(leopardid, 1) 
    177         db.save('Animal', ID=leopardid, ZooID=wapid
     175        db.save('Animal', ID=leopardid, ZooID=wap
    178176                LastEscape=datetime.datetime(2004, 12, 21, 8, 15, 0, 999907)) 
    179177         
    180         db.insert('Animal', Species='Lion', ZooID=wapid) 
     178        lion = db.insert('Animal', Species='Lion', ZooID=wap)['ID'] 
    181179        db.insert('Animal', Species='Slug', Legs=1, Lifespan=.75, 
    182180                  # Test our 8000-byte limit 
    183181                  PreviousZoos=["f" * (8000 - 14)]) 
    184182         
    185         db.insert('Animal', Species='Tiger', PreviousZoos=['animal\\universe']) 
    186 ##         
    187 ##        # Override Legs.default with itself just to make sure it works. 
    188 ##        box.memorize(Animal(Species='Bear', Legs=4)) 
    189 ##        # Notice that ostrich.PreviousZoos is [], whereas leopard is None. 
    190 ##        box.memorize(Animal(Species='Ostrich', Legs=2, PreviousZoos=[], 
    191 ##                            Lifespan=103.2)) 
    192 ##        box.memorize(Animal(Species='Centipede', Legs=100)) 
    193 ##         
    194 ##        emp = Animal(Species='Emperor Penguin', Legs=2) 
    195 ##        box.memorize(emp) 
    196 ##        adelie = Animal(Species='Adelie Penguin', Legs=2) 
    197 ##        box.memorize(adelie) 
    198 ##         
    199 ##        seaworld.add(emp, adelie) 
    200 ##         
    201 ##        millipede = Animal(Species='Millipede', Legs=1000000) 
    202 ##        millipede.PreviousZoos = [WAP.Name] 
    203 ##        box.memorize(millipede) 
    204 ##         
    205 ##        SDZ.add(tiger, millipede) 
    206 ##         
     183        newids = db.insert('Animal', Species='Tiger', ZooID=sdz, 
     184                           PreviousZoos=['animal\\universe']) 
     185        tiger = int(newids['ID']) 
     186         
     187        # Override Legs.default with itself just to make sure it works. 
     188        db.insert('Animal', Species='Bear', Legs=4) 
     189        # Notice that ostrich.PreviousZoos is [], whereas leopard is None. 
     190        db.insert('Animal', Species='Ostrich', Legs=2, PreviousZoos=[], 
     191                            Lifespan=103.2) 
     192        db.insert('Animal', Species='Centipede', Legs=100) 
     193         
     194        emp = db.insert('Animal', Species='Emperor Penguin', Legs=2, ZooID=seaworld) 
     195        emp = int(emp['ID']) 
     196        adelie = db.insert('Animal', Species='Adelie Penguin', Legs=2, ZooID=seaworld) 
     197        adelie = int(adelie['ID']) 
     198         
     199        db.insert('Animal', Species='Millipede', Legs=1000000, ZooID=sdz, 
     200                  PreviousZoos=['Wild Animal Park']) 
     201         
    207202##        # Add a mother and child to test relationships 
    208203##        bai_yun = Animal(Species='Ape', Legs=2) 
     
    212207##        box.memorize(hua_mei)   # ID = 12 
    213208##        self.assertEqual(hua_mei.ID, 12) 
    214 ##         
    215 ##        # Exhibits 
    216 ##        pe = Exhibit(Name = 'The Penguin Encounter', 
    217 ##                     ZooID = seaworld.ID, 
    218 ##                     Animals = [emp.ID, adelie.ID], 
    219 ##                     PettingAllowed = True, 
    220 ##                     Acreage = "3.1", 
    221 ##                     # See ticket #45 
    222 ##                     Creators = (u'Richard F\xfcrst', u'Sonja Martin'), 
    223 ##                     ) 
    224 ##        box.memorize(pe) 
    225 ##         
    226 ##        tr = Exhibit(Name = 'Tiger River', 
    227 ##                     ZooID = SDZ.ID, 
    228 ##                     Animals = [tiger.ID], 
    229 ##                     PettingAllowed = False, 
    230 ##                     Acreage = "4", 
    231 ##                     ) 
    232 ##        box.memorize(tr) 
    233 ##         
    234 ##        # Vets 
    235 ##        cs = Vet(Name = 'Charles Schroeder', ZooID = SDZ.ID) 
    236 ##        box.memorize(cs) 
    237 ##        self.assertEqual(cs.ID, Vet.sequencer.initial) 
    238 ##         
    239 ##        jm = Vet(Name = 'Jim McBain', ZooID = seaworld.ID) 
    240 ##        box.memorize(jm) 
    241 ##         
    242 ##        # Visits 
    243 ##        for d in every13days: 
    244 ##            box.memorize(Visit(VetID=cs.ID, AnimalID=tiger.ID, Date=d)) 
    245 ##        for d in every17days: 
    246 ##            box.memorize(Visit(VetID=jm.ID, AnimalID=emp.ID, Date=d)) 
    247 ## 
    248 ##        # Foods 
    249 ##        dead_fish = Food(Name="Dead Fish", Nutrition=5) 
    250 ##        live_fish = Food(Name="Live Fish", Nutrition=10) 
    251 ##        bunnies = Food(Name="Live Bunny Wabbit", Nutrition=10) 
    252 ##        steak = Food(Name="T-Bone", Nutrition=7) 
    253 ##        for food in [dead_fish, live_fish, bunnies, steak]: 
    254 ##            box.memorize(food) 
    255 ##         
    256 ##        # Foods --> add preferred foods 
    257 ##        lion.add(steak) 
    258 ##        tiger.add(bunnies) 
    259 ##        emp.add(live_fish) 
    260 ##        adelie.add(live_fish) 
    261 ##         
    262 ##        # Foods --> add alternate foods 
    263 ##        lion.AlternateFoodID = bunnies.ID 
    264 ##        tiger.AlternateFoodID = steak.ID 
    265 ##        emp.AlternateFoodID = dead_fish.ID 
    266 ##        adelie.AlternateFoodID = dead_fish.ID 
    267 ##     
    268 ##    def test_3_Properties(self): 
    269 ##        box = arena.new_sandbox() 
    270 ##        try: 
    271 ##            # Zoos 
    272 ##            WAP = box.unit(Zoo, Name='Wild Animal Park') 
    273 ##            self.assertNotEqual(WAP, None) 
    274 ##            self.assertEqual(WAP.Founded, datetime.date(2000, 1, 1)) 
    275 ##            self.assertEqual(WAP.Opens, datetime.time(8, 15, 59)) 
    276 ##            # This should have been updated when leopard.LastEscape was set. 
    277 ##    ##        self.assertEqual(WAP.LastEscape, 
    278 ##    ##                         datetime.datetime(2004, 12, 21, 8, 15, 0, 999907)) 
    279 ##            self.assertEqual(WAP.Admission, Zoo.Admission.coerce(WAP, "4.95")) 
     209         
     210        # Exhibits 
     211        db.insert('Exhibit', Name = 'The Penguin Encounter', 
     212                  ZooID = seaworld, 
     213                  Animals = [emp, adelie], 
     214                  PettingAllowed = True, 
     215                  Acreage = 3.1, 
     216                  # See ticket #45 
     217                  Creators = (u'Richard F\xfcrst', u'Sonja Martin'), 
     218                  ) 
     219         
     220        db.insert('Exhibit', Name = 'Tiger River', 
     221                  ZooID = sdz, 
     222                  Animals = [tiger], 
     223                  PettingAllowed = False, 
     224                  Acreage = 4, 
     225                  ) 
     226         
     227        # Vets 
     228        newids = db.insert('Vet', Name = 'Charles Schroeder', ZooID = sdz) 
     229        cs = int(newids['ID']) 
     230        self.assertEqual(cs, db['Vet']['ID'].initial) 
     231         
     232        newids = db.insert('Vet', Name = 'Jim McBain', ZooID = seaworld) 
     233        jm = int(newids['ID']) 
     234         
     235        # Visits 
     236        for d in every13days: 
     237            db.insert('Visit', VetID=cs, AnimalID=tiger, Date=d) 
     238        for d in every17days: 
     239            db.insert('Visit', VetID=jm, AnimalID=emp, Date=d) 
     240         
     241        # Foods 
     242        dead_fish = db.insert('Food', Name="Dead Fish", Nutrition=5)['ID'] 
     243        live_fish = db.insert('Food', Name="Live Fish", Nutrition=10)['ID'] 
     244        bunnies = db.insert('Food', Name="Live Bunny Wabbit", Nutrition=10)['ID'] 
     245        steak = db.insert('Food', Name="T-Bone", Nutrition=7)['ID'] 
     246         
     247        # Foods --> add preferred and alternate foods 
     248        db.save('Animal', ID=lion, 
     249                PreferredFoodID=steak, AlternateFoodID=bunnies) 
     250        db.save('Animal', ID=tiger, 
     251                PreferredFoodID=bunnies, AlternateFoodID=steak) 
     252        db.save('Animal', ID=emp, 
     253                PreferredFoodID=live_fish, AlternateFoodID=dead_fish) 
     254        db.save('Animal', ID=adelie, 
     255                PreferredFoodID=live_fish, AlternateFoodID=dead_fish) 
     256     
     257    def test_3_Properties(self): 
     258        # Zoos 
     259        sel = db.select(db['Zoo'], ('Founded', 'Opens', 'Admission'), 
     260                        logic.Expression(lambda z: z.Name == 'Wild Animal Park')) 
     261        data, _ = db.fetch(sel.sql(), db.get_transaction()) 
     262        self.assertEqual(data[0][0], datetime.date(2000, 1, 1)) 
     263        self.assertEqual(data[0][1], datetime.time(8, 15, 59)) 
     264        # This should have been updated when leopard.LastEscape was set. 
     265##        self.assertEqual(WAP.LastEscape, 
     266##                         datetime.datetime(2004, 12, 21, 8, 15, 0, 999907)) 
     267        self.assertEqual(data[0][2], 4.95) 
    280268##             
    281269##            SDZ = box.unit(Zoo, Founded=datetime.date(1835, 9, 13))