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