|
11 | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
| 14 | +import tempfile |
14 | 15 |
|
15 | 16 | try: |
16 | 17 | import unittest2 as unittest |
|
22 | 23 | import cassandra |
23 | 24 | from cassandra.cqltypes import (BooleanType, lookup_casstype_simple, lookup_casstype, |
24 | 25 | LongType, DecimalType, SetType, cql_typename, |
25 | | - CassandraType, UTF8Type, parse_casstype_args) |
26 | | -from cassandra.decoder import named_tuple_factory |
| 26 | + CassandraType, UTF8Type, parse_casstype_args, |
| 27 | + EmptyValue, _CassandraType, DateType) |
| 28 | +from cassandra.decoder import named_tuple_factory, write_string, read_longstring, write_stringmap, read_stringmap, read_inet, write_inet, cql_quote |
27 | 29 |
|
28 | 30 |
|
29 | 31 | class TypeTests(unittest.TestCase): |
@@ -170,3 +172,75 @@ class BarType(FooType): |
170 | 172 | self.assertEquals(UTF8Type, ctype.subtypes[2]) |
171 | 173 |
|
172 | 174 | self.assertEquals(['city', None, 'zip'], ctype.names) |
| 175 | + |
| 176 | + def test_empty_value(self): |
| 177 | + self.assertEqual(str(EmptyValue()), 'EMPTY') |
| 178 | + |
| 179 | + def test_CassandraType(self): |
| 180 | + cassandra_type = _CassandraType('randomvaluetocheck') |
| 181 | + self.assertEqual(cassandra_type.val, 'randomvaluetocheck') |
| 182 | + self.assertEqual(cassandra_type.validate('randomvaluetocheck2'), 'randomvaluetocheck2') |
| 183 | + self.assertEqual(cassandra_type.val, 'randomvaluetocheck') |
| 184 | + |
| 185 | + def test_DateType(self): |
| 186 | + now = datetime.datetime.now() |
| 187 | + date_type = DateType(now) |
| 188 | + self.assertEqual(date_type.my_timestamp(), now) |
| 189 | + try: |
| 190 | + date_type.interpret_datestring('fakestring') |
| 191 | + self.fail() |
| 192 | + except ValueError: |
| 193 | + pass |
| 194 | + |
| 195 | + def test_write_read_string(self): |
| 196 | + # BUG? |
| 197 | + # Traceback (most recent call last): |
| 198 | + # File "/Users/joaquin/repos/python-driver/tests/unit/test_types.py", line 199, in test_write_read_string |
| 199 | + # self.assertEqual(read_longstring(f), u'test') |
| 200 | + # AssertionError: u'st' != u'test' |
| 201 | + # - st |
| 202 | + # + test |
| 203 | + # ? ++ |
| 204 | + with tempfile.TemporaryFile() as f: |
| 205 | + value = u'test' |
| 206 | + write_string(f, value) |
| 207 | + f.seek(0) |
| 208 | + self.assertEqual(read_longstring(f), value) |
| 209 | + |
| 210 | + def test_write_read_stringmap(self): |
| 211 | + with tempfile.TemporaryFile() as f: |
| 212 | + value = {'key': 'value'} |
| 213 | + write_stringmap(f, value) |
| 214 | + f.seek(0) |
| 215 | + self.assertEqual(read_stringmap(f), value) |
| 216 | + |
| 217 | + def test_write_read_inet(self): |
| 218 | + # BUG? I didn't think it mattered, but just wanted to confirm |
| 219 | + # Traceback (most recent call last): |
| 220 | + # File "/Users/joaquin/repos/python-driver/tests/unit/test_types.py", line 228, in test_write_read_inet |
| 221 | + # self.assertEqual(read_inet(f), value) |
| 222 | + # AssertionError: Tuples differ: ('2001:db8:0:f101::1', 9042) != ('2001:0db8:0:f101::1', 9042) |
| 223 | + # |
| 224 | + # First differing element 0: |
| 225 | + # 2001:db8:0:f101::1 |
| 226 | + # 2001:0db8:0:f101::1 |
| 227 | + # |
| 228 | + # - ('2001:db8:0:f101::1', 9042) |
| 229 | + # + ('2001:0db8:0:f101::1', 9042) |
| 230 | + # ? + |
| 231 | + with tempfile.TemporaryFile() as f: |
| 232 | + value = ('192.168.1.1', 9042) |
| 233 | + write_inet(f, value) |
| 234 | + f.seek(0) |
| 235 | + self.assertEqual(read_inet(f), value) |
| 236 | + |
| 237 | + with tempfile.TemporaryFile() as f: |
| 238 | + value = ('2001:0db8:0:f101::1', 9042) |
| 239 | + write_inet(f, value) |
| 240 | + f.seek(0) |
| 241 | + self.assertEqual(read_inet(f), value) |
| 242 | + |
| 243 | + def test_cql_quote(self): |
| 244 | + self.assertEqual(cql_quote(u'test'), "'test'") |
| 245 | + self.assertEqual(cql_quote('test'), "'test'") |
| 246 | + self.assertEqual(cql_quote(0), '0') |
0 commit comments