OLD | NEW |
(Empty) | |
| 1 from boto.sdb.db.model import Model |
| 2 from boto.sdb.db.property import StringProperty, IntegerProperty, BooleanPropert
y |
| 3 from boto.sdb.db.property import DateTimeProperty, FloatProperty, ReferencePrope
rty |
| 4 from boto.sdb.db.property import PasswordProperty, ListProperty, MapProperty |
| 5 from datetime import datetime |
| 6 import time |
| 7 from boto.exception import SDBPersistenceError |
| 8 |
| 9 _objects = {} |
| 10 |
| 11 # |
| 12 # This will eventually be moved to the boto.tests module and become a real unit
test |
| 13 # but for now it will live here. It shows examples of each of the Property type
s in |
| 14 # use and tests the basic operations. |
| 15 # |
| 16 class TestBasic(Model): |
| 17 |
| 18 name = StringProperty() |
| 19 size = IntegerProperty() |
| 20 foo = BooleanProperty() |
| 21 date = DateTimeProperty() |
| 22 |
| 23 class TestFloat(Model): |
| 24 |
| 25 name = StringProperty() |
| 26 value = FloatProperty() |
| 27 |
| 28 class TestRequired(Model): |
| 29 |
| 30 req = StringProperty(required=True, default='foo') |
| 31 |
| 32 class TestReference(Model): |
| 33 |
| 34 ref = ReferenceProperty(reference_class=TestBasic, collection_name='refs') |
| 35 |
| 36 class TestSubClass(TestBasic): |
| 37 |
| 38 answer = IntegerProperty() |
| 39 |
| 40 class TestPassword(Model): |
| 41 password = PasswordProperty() |
| 42 |
| 43 class TestList(Model): |
| 44 |
| 45 name = StringProperty() |
| 46 nums = ListProperty(int) |
| 47 |
| 48 class TestMap(Model): |
| 49 |
| 50 name = StringProperty() |
| 51 map = MapProperty() |
| 52 |
| 53 class TestListReference(Model): |
| 54 |
| 55 name = StringProperty() |
| 56 basics = ListProperty(TestBasic) |
| 57 |
| 58 class TestAutoNow(Model): |
| 59 |
| 60 create_date = DateTimeProperty(auto_now_add=True) |
| 61 modified_date = DateTimeProperty(auto_now=True) |
| 62 |
| 63 class TestUnique(Model): |
| 64 name = StringProperty(unique=True) |
| 65 |
| 66 def test_basic(): |
| 67 global _objects |
| 68 t = TestBasic() |
| 69 t.name = 'simple' |
| 70 t.size = -42 |
| 71 t.foo = True |
| 72 t.date = datetime.now() |
| 73 print 'saving object' |
| 74 t.put() |
| 75 _objects['test_basic_t'] = t |
| 76 time.sleep(5) |
| 77 print 'now try retrieving it' |
| 78 tt = TestBasic.get_by_id(t.id) |
| 79 _objects['test_basic_tt'] = tt |
| 80 assert tt.id == t.id |
| 81 l = TestBasic.get_by_id([t.id]) |
| 82 assert len(l) == 1 |
| 83 assert l[0].id == t.id |
| 84 assert t.size == tt.size |
| 85 assert t.foo == tt.foo |
| 86 assert t.name == tt.name |
| 87 #assert t.date == tt.date |
| 88 return t |
| 89 |
| 90 def test_float(): |
| 91 global _objects |
| 92 t = TestFloat() |
| 93 t.name = 'float object' |
| 94 t.value = 98.6 |
| 95 print 'saving object' |
| 96 t.save() |
| 97 _objects['test_float_t'] = t |
| 98 time.sleep(5) |
| 99 print 'now try retrieving it' |
| 100 tt = TestFloat.get_by_id(t.id) |
| 101 _objects['test_float_tt'] = tt |
| 102 assert tt.id == t.id |
| 103 assert tt.name == t.name |
| 104 assert tt.value == t.value |
| 105 return t |
| 106 |
| 107 def test_required(): |
| 108 global _objects |
| 109 t = TestRequired() |
| 110 _objects['test_required_t'] = t |
| 111 t.put() |
| 112 return t |
| 113 |
| 114 def test_reference(t=None): |
| 115 global _objects |
| 116 if not t: |
| 117 t = test_basic() |
| 118 tt = TestReference() |
| 119 tt.ref = t |
| 120 tt.put() |
| 121 time.sleep(10) |
| 122 tt = TestReference.get_by_id(tt.id) |
| 123 _objects['test_reference_tt'] = tt |
| 124 assert tt.ref.id == t.id |
| 125 for o in t.refs: |
| 126 print o |
| 127 |
| 128 def test_subclass(): |
| 129 global _objects |
| 130 t = TestSubClass() |
| 131 _objects['test_subclass_t'] = t |
| 132 t.name = 'a subclass' |
| 133 t.size = -489 |
| 134 t.save() |
| 135 |
| 136 def test_password(): |
| 137 global _objects |
| 138 t = TestPassword() |
| 139 _objects['test_password_t'] = t |
| 140 t.password = "foo" |
| 141 t.save() |
| 142 time.sleep(5) |
| 143 # Make sure it stored ok |
| 144 tt = TestPassword.get_by_id(t.id) |
| 145 _objects['test_password_tt'] = tt |
| 146 #Testing password equality |
| 147 assert tt.password == "foo" |
| 148 #Testing password not stored as string |
| 149 assert str(tt.password) != "foo" |
| 150 |
| 151 def test_list(): |
| 152 global _objects |
| 153 t = TestList() |
| 154 _objects['test_list_t'] = t |
| 155 t.name = 'a list of ints' |
| 156 t.nums = [1, 2, 3, 4, 5] |
| 157 t.put() |
| 158 tt = TestList.get_by_id(t.id) |
| 159 _objects['test_list_tt'] = tt |
| 160 assert tt.name == t.name |
| 161 for n in tt.nums: |
| 162 assert isinstance(n, int) |
| 163 |
| 164 def test_list_reference(): |
| 165 global _objects |
| 166 t = TestBasic() |
| 167 t.put() |
| 168 _objects['test_list_ref_t'] = t |
| 169 tt = TestListReference() |
| 170 tt.name = "foo" |
| 171 tt.basics = [t] |
| 172 tt.put() |
| 173 time.sleep(5) |
| 174 _objects['test_list_ref_tt'] = tt |
| 175 ttt = TestListReference.get_by_id(tt.id) |
| 176 assert ttt.basics[0].id == t.id |
| 177 |
| 178 def test_unique(): |
| 179 global _objects |
| 180 t = TestUnique() |
| 181 name = 'foo' + str(int(time.time())) |
| 182 t.name = name |
| 183 t.put() |
| 184 _objects['test_unique_t'] = t |
| 185 time.sleep(10) |
| 186 tt = TestUnique() |
| 187 _objects['test_unique_tt'] = tt |
| 188 tt.name = name |
| 189 try: |
| 190 tt.put() |
| 191 assert False |
| 192 except(SDBPersistenceError): |
| 193 pass |
| 194 |
| 195 def test_datetime(): |
| 196 global _objects |
| 197 t = TestAutoNow() |
| 198 t.put() |
| 199 _objects['test_datetime_t'] = t |
| 200 time.sleep(5) |
| 201 tt = TestAutoNow.get_by_id(t.id) |
| 202 assert tt.create_date.timetuple() == t.create_date.timetuple() |
| 203 |
| 204 def test(): |
| 205 print 'test_basic' |
| 206 t1 = test_basic() |
| 207 print 'test_required' |
| 208 test_required() |
| 209 print 'test_reference' |
| 210 test_reference(t1) |
| 211 print 'test_subclass' |
| 212 test_subclass() |
| 213 print 'test_password' |
| 214 test_password() |
| 215 print 'test_list' |
| 216 test_list() |
| 217 print 'test_list_reference' |
| 218 test_list_reference() |
| 219 print "test_datetime" |
| 220 test_datetime() |
| 221 print 'test_unique' |
| 222 test_unique() |
| 223 |
| 224 if __name__ == "__main__": |
| 225 test() |
OLD | NEW |