| 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 |
|---|
| 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 | |
|---|
| 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) |
|---|