Skip to content

Commit e43d94a

Browse files
committed
Improve readability by extracting repeated type instantiations
- test_marshalling.py: Extract DecimalType(proto_ver) to avoid duplicate calls - test_orderedmap.py: Extract key_type_class(protocol_version) to reuse instance - test_geometry.py: Extract cql_type(proto_ver) and PointType(0) to avoid duplicates This makes the code more readable and efficient by avoiding repeated instantiation of the same type objects.
1 parent 76a1c8c commit e43d94a

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

tests/unit/advanced/test_geometry.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ class GeoTypes(unittest.TestCase):
3535
def test_marshal_platform(self):
3636
for proto_ver in protocol_versions:
3737
for geo in self.samples:
38-
cql_type = lookup_casstype(geo.__class__.__name__ + 'Type')
39-
assert cql_type(proto_ver).from_binary(cql_type(proto_ver).to_binary(geo)) == geo
38+
cql_type_class = lookup_casstype(geo.__class__.__name__ + 'Type')
39+
cql_type = cql_type_class(proto_ver)
40+
assert cql_type.from_binary(cql_type.to_binary(geo)) == geo
4041

4142
def _verify_both_endian(self, typ, body_fmt, params, expected):
4243
for proto_ver in protocol_versions:
@@ -51,9 +52,11 @@ def test_both_endian(self):
5152
def test_empty_wkb(self):
5253
for cls in (LineString, Polygon):
5354
class_name = cls.__name__
54-
cql_type = lookup_casstype(class_name + 'Type')
55-
assert str(cql_type(0).from_binary(cql_type(0).to_binary(cls()))) == class_name.upper() + " EMPTY"
56-
assert str(PointType(0).from_binary(PointType(0).to_binary(Point()))) == "POINT (nan nan)"
55+
cql_type_class = lookup_casstype(class_name + 'Type')
56+
cql_type = cql_type_class(0)
57+
assert str(cql_type.from_binary(cql_type.to_binary(cls()))) == class_name.upper() + " EMPTY"
58+
point_type = PointType(0)
59+
assert str(point_type.from_binary(point_type.to_binary(Point()))) == "POINT (nan nan)"
5760

5861
def test_str_wkt(self):
5962
assert str(Point(1., 2.)) == 'POINT (1.0 2.0)'

tests/unit/test_marshalling.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def test_decimal(self):
132132
converted_types = (10001, (0, (1, 0, 0, 0, 0, 1), -3), 100.1, -87.629798)
133133

134134
for proto_ver in range(3, ProtocolVersion.MAX_SUPPORTED + 1):
135+
decimal_type = DecimalType(proto_ver)
135136
for n in converted_types:
136137
expected = Decimal(n)
137-
assert DecimalType(proto_ver).from_binary(DecimalType(proto_ver).to_binary(n)) == expected
138+
assert decimal_type.from_binary(decimal_type.to_binary(n)) == expected

tests/unit/test_orderedmap.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,14 @@ def test_init(self):
171171
assert om == {}
172172

173173
def test_normalized_lookup(self):
174-
key_type = lookup_casstype('MapType(UTF8Type, Int32Type)')
174+
key_type_class = lookup_casstype('MapType(UTF8Type, Int32Type)')
175175
protocol_version = 3
176-
om = OrderedMapSerializedKey(key_type(protocol_version))
176+
key_type = key_type_class(protocol_version)
177+
om = OrderedMapSerializedKey(key_type)
177178
key_ascii = {'one': 1}
178179
key_unicode = {u'two': 2}
179-
om._insert_unchecked(key_ascii, key_type(protocol_version).serialize(key_ascii), object())
180-
om._insert_unchecked(key_unicode, key_type(protocol_version).serialize(key_unicode), object())
180+
om._insert_unchecked(key_ascii, key_type.serialize(key_ascii), object())
181+
om._insert_unchecked(key_unicode, key_type.serialize(key_unicode), object())
181182

182183
# type lookup is normalized by key_type
183184
# PYTHON-231

0 commit comments

Comments
 (0)