forked from basho/riak-python-client
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_comparison.py
More file actions
175 lines (153 loc) · 6.94 KB
/
Copy pathtest_comparison.py
File metadata and controls
175 lines (153 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright 2010-present Basho Technologies, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
from riak.riak_object import RiakObject
from riak.bucket import BucketType, RiakBucket
from riak.tests.base import IntegrationTestBase
class BucketTypeRichComparisonTest(unittest.TestCase):
def test_btype_eq(self):
a = BucketType("client", "a")
b = BucketType("client", "a")
c = BucketType(None, "a")
d = BucketType(None, "a")
self.assertEqual(a, b)
self.assertEqual(c, d)
def test_btype_nq(self):
a = BucketType("client", "a")
b = BucketType("client", "b")
c = BucketType(None, "a")
d = BucketType(None, "a")
self.assertNotEqual(a, b, "matched with different name, same client")
self.assertNotEqual(a, c, "matched with different client, same name")
self.assertNotEqual(b, d, "matched with nothing in common")
def test_btype_hash(self):
a = BucketType("client", "a")
b = BucketType("client", "a")
c = BucketType("client", "c")
d = BucketType("client2", "a")
self.assertEqual(hash(a), hash(b), "same bucket type has different hashes")
self.assertNotEqual(hash(a), hash(c), "different bucket has same hash")
self.assertNotEqual(hash(a), hash(d), "same bucket type, different client has same hash")
class RiakBucketRichComparisonTest(unittest.TestCase):
def test_bucket_eq(self):
default_bt = BucketType(None, "default")
foo_bt = BucketType(None, "foo")
a = RiakBucket("client", "a", default_bt)
b = RiakBucket("client", "a", default_bt)
c = RiakBucket("client", "a", foo_bt)
d = RiakBucket("client", "a", foo_bt)
self.assertEqual(a, b)
self.assertEqual(c, d)
def test_bucket_nq(self):
default_bt = BucketType(None, "default")
foo_bt = BucketType(None, "foo")
a = RiakBucket("client", "a", default_bt)
b = RiakBucket("client", "b", default_bt)
c = RiakBucket("client", "a", foo_bt)
self.assertNotEqual(a, b, "matched with a different bucket")
self.assertNotEqual(a, c, "matched with a different bucket type")
def test_bucket_hash(self):
default_bt = BucketType(None, "default")
foo_bt = BucketType(None, "foo")
a = RiakBucket("client", "a", default_bt)
b = RiakBucket("client", "a", default_bt)
c = RiakBucket("client", "c", default_bt)
d = RiakBucket("client", "a", foo_bt)
self.assertEqual(hash(a), hash(b), "same bucket has different hashes")
self.assertNotEqual(hash(a), hash(c), "different bucket has same hash")
self.assertNotEqual(hash(a), hash(d), "same bucket, different bucket type has same hash")
class RiakObjectComparisonTest(unittest.TestCase):
def test_object_eq(self):
a = RiakObject(None, "bucket", "key")
b = RiakObject(None, "bucket", "key")
self.assertEqual(a, b)
default_bt = BucketType(None, "default")
bucket_a = RiakBucket("client", "a", default_bt)
bucket_b = RiakBucket("client", "a", default_bt)
c = RiakObject(None, bucket_a, "key")
d = RiakObject(None, bucket_b, "key")
self.assertEqual(c, d)
def test_object_nq(self):
a = RiakObject(None, "bucket", "key")
b = RiakObject(None, "bucket", "not key")
c = RiakObject(None, "not bucket", "key")
self.assertNotEqual(a, b, "matched with different keys")
self.assertNotEqual(a, c, "matched with different buckets")
default_bt = BucketType(None, "default")
foo_bt = BucketType(None, "foo")
bucket_a = RiakBucket("client", "a", default_bt)
bucket_b = RiakBucket("client", "a", foo_bt)
c = RiakObject(None, bucket_a, "key")
d = RiakObject(None, bucket_b, "key")
self.assertNotEqual(c, d)
def test_object_hash(self):
a = RiakObject(None, "bucket", "key")
b = RiakObject(None, "bucket", "key")
c = RiakObject(None, "bucket", "not key")
self.assertEqual(hash(a), hash(b), "same object has different hashes")
self.assertNotEqual(hash(a), hash(c), "different object has same hash")
default_bt = BucketType(None, "default")
foo_bt = BucketType(None, "foo")
bucket_a = RiakBucket("client", "a", default_bt)
bucket_b = RiakBucket("client", "a", foo_bt)
d = RiakObject(None, bucket_a, "key")
e = RiakObject(None, bucket_a, "key")
f = RiakObject(None, bucket_b, "key")
g = RiakObject(None, bucket_b, "not key")
self.assertEqual(hash(d), hash(e),
"same object, same bucket_type has different hashes")
self.assertNotEqual(
hash(e), hash(f), "same object, different bucket type has the same hash",
)
self.assertNotEqual(
hash(d), hash(g), "different object, different bucket type has same hash",
)
def test_object_valid_key(self):
a = RiakObject(None, "bucket", "key")
self.assertIsInstance(a, RiakObject, "valid key name is rejected")
try:
b = RiakObject(None, "bucket", "")
except ValueError:
b = None
self.assertIsNone(b, "empty object key not allowed")
class RiakClientComparisonTest(IntegrationTestBase, unittest.TestCase):
def test_client_eq(self):
self.protocol = "http"
a = self.create_client(host="host1", http_port=11)
b = self.create_client(host="host1", http_port=11)
self.assertEqual(a, b)
a.close()
b.close()
def test_client_nq(self):
self.protocol = "http"
a = self.create_client(host="host1", http_port=11)
b = self.create_client(host="host2", http_port=11)
c = self.create_client(host="host1", http_port=12)
self.assertNotEqual(a, b, "matched with different hosts")
self.assertNotEqual(a, c, "matched with different ports")
a.close()
b.close()
c.close()
def test_client_hash(self):
self.protocol = "http"
a = self.create_client(host="host1", http_port=11)
b = self.create_client(host="host1", http_port=11)
c = self.create_client(host="host2", http_port=11)
self.assertEqual(hash(a), hash(b), "same object has different hashes")
self.assertNotEqual(hash(a), hash(c), "different object has same hash")
a.close()
b.close()
c.close()
if __name__ == "__main__":
unittest.main()