OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2010 Chris Moyer http://coredumped.org/ |
| 2 # |
| 3 # Permission is hereby granted, free of charge, to any person obtaining a |
| 4 # copy of this software and associated documentation files (the |
| 5 # "Software"), to deal in the Software without restriction, including |
| 6 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 7 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 # persons to whom the Software is furnished to do so, subject to the fol- |
| 9 # lowing conditions: |
| 10 # |
| 11 # The above copyright notice and this permission notice shall be included |
| 12 # in all copies or substantial portions of the Software. |
| 13 # |
| 14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 16 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 17 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 18 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 # IN THE SOFTWARE. |
| 21 # |
| 22 from boto.sdb.db.property import ListProperty, StringProperty, ReferenceProperty
, IntegerProperty |
| 23 from boto.sdb.db.model import Model |
| 24 import time |
| 25 |
| 26 class SimpleModel(Model): |
| 27 """Simple Test Model""" |
| 28 name = StringProperty() |
| 29 strs = ListProperty(str) |
| 30 num = IntegerProperty() |
| 31 |
| 32 class SubModel(SimpleModel): |
| 33 """Simple Subclassed Model""" |
| 34 ref = ReferenceProperty(SimpleModel, collection_name="reverse_ref") |
| 35 |
| 36 |
| 37 class TestQuerying(object): |
| 38 """Test different querying capabilities""" |
| 39 |
| 40 def setup_class(cls): |
| 41 """Setup this class""" |
| 42 cls.objs = [] |
| 43 |
| 44 o = SimpleModel() |
| 45 o.name = "Simple Object" |
| 46 o.strs = ["B", "A", "C", "Foo"] |
| 47 o.num = 1 |
| 48 o.put() |
| 49 cls.objs.append(o) |
| 50 |
| 51 o2 = SimpleModel() |
| 52 o2.name = "Referenced Object" |
| 53 o2.num = 2 |
| 54 o2.put() |
| 55 cls.objs.append(o2) |
| 56 |
| 57 o3 = SubModel() |
| 58 o3.name = "Sub Object" |
| 59 o3.num = 3 |
| 60 o3.ref = o2 |
| 61 o3.put() |
| 62 cls.objs.append(o3) |
| 63 |
| 64 time.sleep(3) |
| 65 |
| 66 |
| 67 |
| 68 def teardown_class(cls): |
| 69 """Remove our objects""" |
| 70 for o in cls.objs: |
| 71 try: |
| 72 o.delete() |
| 73 except: |
| 74 pass |
| 75 |
| 76 def test_find(self): |
| 77 """Test using the "Find" method""" |
| 78 assert(SimpleModel.find(name="Simple Object").next().id == self.objs[0].
id) |
| 79 assert(SimpleModel.find(name="Referenced Object").next().id == self.objs
[1].id) |
| 80 assert(SimpleModel.find(name="Sub Object").next().id == self.objs[2].id) |
| 81 |
| 82 def test_like_filter(self): |
| 83 """Test a "like" filter""" |
| 84 query = SimpleModel.all() |
| 85 query.filter("name like", "% Object") |
| 86 assert(query.count() == 3) |
| 87 |
| 88 query = SimpleModel.all() |
| 89 query.filter("name not like", "% Object") |
| 90 assert(query.count() == 0) |
| 91 |
| 92 def test_equals_filter(self): |
| 93 """Test an "=" and "!=" filter""" |
| 94 query = SimpleModel.all() |
| 95 query.filter("name =", "Simple Object") |
| 96 assert(query.count() == 1) |
| 97 |
| 98 query = SimpleModel.all() |
| 99 query.filter("name !=", "Simple Object") |
| 100 assert(query.count() == 2) |
| 101 |
| 102 def test_or_filter(self): |
| 103 """Test a filter function as an "or" """ |
| 104 query = SimpleModel.all() |
| 105 query.filter("name =", ["Simple Object", "Sub Object"]) |
| 106 assert(query.count() == 2) |
| 107 |
| 108 def test_and_filter(self): |
| 109 """Test Multiple filters which are an "and" """ |
| 110 query = SimpleModel.all() |
| 111 query.filter("name like", "% Object") |
| 112 query.filter("name like", "Simple %") |
| 113 assert(query.count() == 1) |
| 114 |
| 115 def test_none_filter(self): |
| 116 """Test filtering for a value that's not set""" |
| 117 query = SimpleModel.all() |
| 118 query.filter("ref =", None) |
| 119 assert(query.count() == 2) |
| 120 |
| 121 def test_greater_filter(self): |
| 122 """Test filtering Using >, >=""" |
| 123 query = SimpleModel.all() |
| 124 query.filter("num >", 1) |
| 125 assert(query.count() == 2) |
| 126 |
| 127 query = SimpleModel.all() |
| 128 query.filter("num >=", 1) |
| 129 assert(query.count() == 3) |
| 130 |
| 131 def test_less_filter(self): |
| 132 """Test filtering Using <, <=""" |
| 133 query = SimpleModel.all() |
| 134 query.filter("num <", 3) |
| 135 assert(query.count() == 2) |
| 136 |
| 137 query = SimpleModel.all() |
| 138 query.filter("num <=", 3) |
| 139 assert(query.count() == 3) |
| 140 |
| 141 def test_query_on_list(self): |
| 142 """Test querying on a list""" |
| 143 assert(SimpleModel.find(strs="A").next().id == self.objs[0].id) |
| 144 assert(SimpleModel.find(strs="B").next().id == self.objs[0].id) |
| 145 assert(SimpleModel.find(strs="C").next().id == self.objs[0].id) |
| 146 |
| 147 def test_like(self): |
| 148 """Test with a "like" expression""" |
| 149 query = SimpleModel.all() |
| 150 query.filter("strs like", "%oo%") |
| 151 print query.get_query() |
| 152 assert(query.count() == 1) |
OLD | NEW |