| 1 |
import datetime |
|---|
| 2 |
import unittest |
|---|
| 3 |
import warnings |
|---|
| 4 |
|
|---|
| 5 |
import dejavu |
|---|
| 6 |
from dejavu import logic, storage |
|---|
| 7 |
from dejavu.test.zoo_fixture import * |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
arena.add_store("default", "dejavu.storage.CachingProxy") |
|---|
| 11 |
arena.register(Animal) |
|---|
| 12 |
arena.register(Lecture) |
|---|
| 13 |
arena.register(Vet) |
|---|
| 14 |
arena.register(Visit) |
|---|
| 15 |
arena.register(Zoo) |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class UnitTests(unittest.TestCase): |
|---|
| 19 |
|
|---|
| 20 |
def setUp(self): |
|---|
| 21 |
|
|---|
| 22 |
box = arena.new_sandbox() |
|---|
| 23 |
for animal in box.recall(Animal): |
|---|
| 24 |
animal.forget() |
|---|
| 25 |
for zoo_thing in box.recall(Zoo): |
|---|
| 26 |
zoo_thing.forget() |
|---|
| 27 |
|
|---|
| 28 |
def test_Properties(self): |
|---|
| 29 |
|
|---|
| 30 |
f = datetime.date(1916, 10, 2) |
|---|
| 31 |
z = Zoo(Name='San Diego Zoo', Founded=f) |
|---|
| 32 |
self.assertEqual(z.dirty(), True) |
|---|
| 33 |
self.assertEqual(Zoo.ID.type, int) |
|---|
| 34 |
self.assertEqual(z.ID, None) |
|---|
| 35 |
self.assertEqual(z.Name, 'San Diego Zoo') |
|---|
| 36 |
self.assertEqual(type(z.Name), unicode) |
|---|
| 37 |
self.assertEqual(z.Founded, f) |
|---|
| 38 |
self.assertEqual(z.__class__.ID.index, True) |
|---|
| 39 |
|
|---|
| 40 |
a = Animal(Species='Giraffe') |
|---|
| 41 |
self.assertEqual(a.dirty(), True) |
|---|
| 42 |
self.assertEqual(a.ID, None) |
|---|
| 43 |
self.assertEqual(a.Species, 'Giraffe') |
|---|
| 44 |
|
|---|
| 45 |
self.assertEqual(a.Legs, 4) |
|---|
| 46 |
self.assertEqual(a.__class__.ZooID.index, True) |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
s = arena.new_sandbox() |
|---|
| 50 |
s.memorize(z) |
|---|
| 51 |
self.assertEqual(z.ID, 1) |
|---|
| 52 |
s.memorize(a) |
|---|
| 53 |
self.assertEqual(a.ID, 1) |
|---|
| 54 |
z.add(a) |
|---|
| 55 |
self.assertEqual(a.ZooID, 1) |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
z.cleanse() |
|---|
| 59 |
self.assertEqual(z.dirty(), False) |
|---|
| 60 |
a.LastEscape = d = datetime.datetime(2004, 10, 20) |
|---|
| 61 |
self.assertEqual(a.LastEscape, d) |
|---|
| 62 |
self.assertEqual(z.LastEscape, d) |
|---|
| 63 |
self.assertEqual(z.dirty(), True) |
|---|
| 64 |
|
|---|
| 65 |
def test_associations(self): |
|---|
| 66 |
|
|---|
| 67 |
box = arena.new_sandbox() |
|---|
| 68 |
box.memorize(Animal(Species='Liger')) |
|---|
| 69 |
box.memorize(Zoo(Name='Wallingford')) |
|---|
| 70 |
box.flush_all() |
|---|
| 71 |
box = arena.new_sandbox() |
|---|
| 72 |
liger = box.unit(Animal, Species='Liger') |
|---|
| 73 |
wall = box.unit(Zoo, Name='Wallingford') |
|---|
| 74 |
liger.ZooID = wall.ID |
|---|
| 75 |
self.assertEqual(len([a for a in wall.Animal()]), 1) |
|---|
| 76 |
box.flush_all() |
|---|
| 77 |
|
|---|
| 78 |
def test_xrecall(self): |
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
box = arena.new_sandbox() |
|---|
| 83 |
box.memorize(Animal(Species='Wombat')) |
|---|
| 84 |
box.memorize(Animal(Species='Lizard')) |
|---|
| 85 |
|
|---|
| 86 |
animals = [] |
|---|
| 87 |
animals2 = [] |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
box = arena.new_sandbox() |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
animals_is_stopped = False |
|---|
| 94 |
animals2_is_stopped = False |
|---|
| 95 |
animals_iter = box.xrecall(Animal) |
|---|
| 96 |
animals2_iter = box.xrecall(Animal) |
|---|
| 97 |
while not (animals_is_stopped and animals2_is_stopped): |
|---|
| 98 |
try: |
|---|
| 99 |
animals.append(animals_iter.next()) |
|---|
| 100 |
except StopIteration: |
|---|
| 101 |
animals_is_stopped = True |
|---|
| 102 |
|
|---|
| 103 |
try: |
|---|
| 104 |
animals2.append(animals2_iter.next()) |
|---|
| 105 |
except StopIteration: |
|---|
| 106 |
animals2_is_stopped = True |
|---|
| 107 |
|
|---|
| 108 |
for animal in animals: |
|---|
| 109 |
self.failUnless( |
|---|
| 110 |
animal in animals2, |
|---|
| 111 |
"An instance in the first xrecall wasn't in the second" ) |
|---|
| 112 |
|
|---|
| 113 |
for animal in animals2: |
|---|
| 114 |
self.failUnless( |
|---|
| 115 |
animal in animals, |
|---|
| 116 |
"An instance was in the second xrecall but not the first" ) |
|---|
| 117 |
|
|---|
| 118 |
def test_sandbox_cache(self): |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
box = arena.new_sandbox() |
|---|
| 123 |
bat = Animal(Species='Bat') |
|---|
| 124 |
box.memorize(bat) |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
bat.Legs = 2 |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
self.assert_(box.unit(Animal) is bat) |
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
bat3 = arena.new_sandbox().unit(Animal) |
|---|
| 136 |
self.assert_(bat3 is not bat) |
|---|
| 137 |
self.assertEqual(bat3.Legs, 4) |
|---|
| 138 |
|
|---|
| 139 |
def test_Subclassing(self): |
|---|
| 140 |
box = arena.new_sandbox() |
|---|
| 141 |
box.memorize(Visit(VetID=1, ZooID=1, AnimalID=1)) |
|---|
| 142 |
box.memorize(Visit(VetID=1, ZooID=1, AnimalID=2)) |
|---|
| 143 |
box.memorize(Visit(VetID=2, ZooID=1, AnimalID=3)) |
|---|
| 144 |
box.memorize(Lecture(VetID=1, ZooID=1, Topic='Cage Cleaning')) |
|---|
| 145 |
box.memorize(Lecture(VetID=1, ZooID=1, Topic='Ape Mating Habits')) |
|---|
| 146 |
box.memorize(Lecture(VetID=2, ZooID=3, Topic='Your Tiger and Steroids')) |
|---|
| 147 |
|
|---|
| 148 |
visits = box.recall(Visit) |
|---|
| 149 |
self.assertEqual(len(visits), 6) |
|---|
| 150 |
|
|---|
| 151 |
box.flush_all() |
|---|
| 152 |
|
|---|
| 153 |
box = arena.new_sandbox() |
|---|
| 154 |
visits = box.recall(Visit) |
|---|
| 155 |
self.assertEqual(len(visits), 6) |
|---|
| 156 |
cc = [x for x in visits |
|---|
| 157 |
if getattr(x, "Topic", None) == "Cage Cleaning"] |
|---|
| 158 |
self.assertEqual(len(cc), 1) |
|---|
| 159 |
|
|---|
| 160 |
f = logic.filter(AnimalID=2) |
|---|
| 161 |
self.assertEqual(len(box.recall(Visit, f)), 1) |
|---|
| 162 |
self.assertEqual(len(box.recall(Lecture, f)), 0) |
|---|
| 163 |
|
|---|
| 164 |
def test_UnitJoin(self): |
|---|
| 165 |
box = arena.new_sandbox() |
|---|
| 166 |
tree = Animal & Zoo |
|---|
| 167 |
self.assertEqual(str(tree), "(Animal & Zoo)") |
|---|
| 168 |
tree = Animal << Zoo |
|---|
| 169 |
self.assertEqual(str(tree), "(Animal << Zoo)") |
|---|
| 170 |
tree = Animal >> Zoo |
|---|
| 171 |
self.assertEqual(str(tree), "(Animal >> Zoo)") |
|---|
| 172 |
|
|---|
| 173 |
trees = [] |
|---|
| 174 |
def make_tree(): |
|---|
| 175 |
trees.append( (Animal & Zoo) >> Exhibit ) |
|---|
| 176 |
|
|---|
| 177 |
warnings.filterwarnings("error", category=dejavu.StorageWarning) |
|---|
| 178 |
try: |
|---|
| 179 |
self.assertRaises(dejavu.StorageWarning, make_tree) |
|---|
| 180 |
finally: |
|---|
| 181 |
warnings.filters.pop(0) |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
warnings.filterwarnings("ignore", category=dejavu.StorageWarning) |
|---|
| 185 |
try: |
|---|
| 186 |
make_tree() |
|---|
| 187 |
finally: |
|---|
| 188 |
warnings.filters.pop(0) |
|---|
| 189 |
|
|---|
| 190 |
self.assertEqual(str(trees[0]), "((Animal & Zoo) >> Exhibit)") |
|---|
| 191 |
tree = trees[0] & (Visit << Vet) & Exhibit |
|---|
| 192 |
self.assertEqual(str(tree), "((((Animal & Zoo) >> Exhibit) & " |
|---|
| 193 |
"(Visit << Vet)) & Exhibit)") |
|---|
| 194 |
self.assertEqual(list(tree), [Animal, Zoo, Exhibit, Visit, Vet, Exhibit]) |
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
if __name__ == "__main__": |
|---|
| 198 |
unittest.main(__name__) |
|---|
| 199 |
|
|---|