Skip to content

Commit 29a5b0a

Browse files
yoneyfacebook-github-bot
authored andcommitted
Fix unit test issues
Summary: Fix couple of issues in parameterized tests - Fix `endswith()` string, `thrift_mutable_types` - Check `.__name__.endswith("thrift_mutable_types")` once in `setUp()` - Move `hashability` test(s) to separate test class - Fix few `import as`, `immutable_types` vs `mutable_types` Reviewed By: ahilger Differential Revision: D59618306 fbshipit-source-id: a537a8a0ba1c421a7deb2b6d09ae3c2303ec0faf
1 parent 93ab0ca commit 29a5b0a

File tree

6 files changed

+58
-51
lines changed

6 files changed

+58
-51
lines changed

thrift/lib/python/test/enums.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ def setUp(self) -> None:
6767
self.OptionalColorGroups: Type[OptionalColorGroups] = (
6868
self.test_types.OptionalColorGroups
6969
)
70+
self.is_mutable_run: bool = self.test_types.__name__.endswith(
71+
"thrift_mutable_types"
72+
)
7073
# pyre-ignore[16]: has no attribute `serializer_module`
7174
self.serializer: types.ModuleType = self.serializer_module
7275

@@ -115,8 +118,7 @@ def test_bad_enum_hash_same(self) -> None:
115118
self.serializer.Protocol.JSON,
116119
)
117120
# Mutable types do not support hashing
118-
# pyre-ignore[16]: has no attribute `test_types`
119-
if self.test_types.__name__.endswith("immutable_types"):
121+
if not self.is_mutable_run:
120122
self.assertEqual(hash(x), hash(y))
121123
self.assertEqual(hash(x.type), hash(y.type))
122124
self.assertFalse(x.type is y.type)

thrift/lib/python/test/exceptions.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ def setUp(self) -> None:
146146
self.SimpleError: Type[SimpleErrorType] = self.test_types.SimpleError
147147
self.Color: Type[ColorType] = self.test_types.Color
148148
self.ValueOrError: Type[ValueOrErrorType] = self.test_types.ValueOrError
149+
self.is_mutable_run: bool = self.test_types.__name__.endswith(
150+
"thrift_mutable_types"
151+
)
149152
# pyre-ignore[16]: has no attribute `serializer_module`
150153
self.serializer: types.ModuleType = self.serializer_module
151154

@@ -269,8 +272,7 @@ def test_expr_deserialized(self) -> None:
269272
self.assertEqual(repr(z), "UnfriendlyError(errortext='WAT!', code=22)")
270273

271274
def test_serialize_deserialize(self) -> None:
272-
# pyre-ignore[16]: has no attribute `test_types`
273-
if not self.test_types.__name__.endswith("immutable_types"):
275+
if self.is_mutable_run:
274276
# `ValueOrError` is an union, MutableUnion implementation is not complete yet
275277
return
276278

thrift/lib/python/test/lists.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import python_test.containers.thrift_mutable_types as mutable_containers_types
2626
import python_test.containers.thrift_types as immutable_containers_types
2727

28-
import python_test.lists.thrift_mutable_types as immutable_lists_types
29-
import python_test.lists.thrift_types as mutable_lists_types
28+
import python_test.lists.thrift_mutable_types as mutable_lists_types
29+
import python_test.lists.thrift_types as immutable_lists_types
3030

3131
from folly.iobuf import IOBuf
3232

@@ -43,6 +43,12 @@
4343
from python_test.testing_utils import Untruthy
4444

4545

46+
class ImmutableListTests(unittest.TestCase):
47+
def test_hashability(self) -> None:
48+
hash(easy().val_list)
49+
hash(I32List(range(10)))
50+
51+
4652
# ct = containers type, lt = lists type
4753
@parameterized_class(
4854
("containers_types", "lists_types"),
@@ -64,6 +70,9 @@ def setUp(self) -> None:
6470
self.Foo: Type[Foo] = self.containers_types.Foo
6571
self.Lists: Type[Lists] = self.containers_types.Lists
6672
self.Color: Type[Color] = self.containers_types.Color
73+
self.is_mutable_run: bool = self.containers_types.__name__.endswith(
74+
"thrift_mutable_types"
75+
)
6776

6877
def test_negative_indexes(self) -> None:
6978
length = len(self.int_list)
@@ -117,13 +126,6 @@ def test_create_untruthy_list(self) -> None:
117126
self.assertEqual(I32List(Untruthy(5)), list(range(5)))
118127
self.assertEqual(Lists(i32List=Untruthy(5)).i32List, list(range(5)))
119128

120-
def test_hashability(self) -> None:
121-
# Mutable types do not support hashing
122-
# pyre-ignore[16]: has no attribute `lists_types`
123-
if not self.lists_types.__name__.endswith("thrift_mutable_types"):
124-
hash(self.easy().val_list)
125-
hash(self.I32List(range(10)))
126-
127129
def test_no_dict(self) -> None:
128130
with self.assertRaises(AttributeError):
129131
I32List().__dict__
@@ -252,8 +254,7 @@ def test_struct_with_list_fields(self) -> None:
252254
# test reaccess the list element not recreating the struct
253255
# DO_BEFORE(alperyoney,20240701): Figure out whether mutable containers
254256
# should cache the instance.
255-
# pyre-ignore[16]: has no attribute `containers_types`
256-
if not self.containers_types.__name__.endswith("thrift_mutable_types"):
257+
if not self.is_mutable_run:
257258
self.assertIs(s.structList[0], s.structList[0])
258259
self.assertIs(s.structList[1], s.structList[1])
259260

@@ -262,8 +263,7 @@ def test_count_enum(self) -> None:
262263
self.assertEqual(clist.colorList.count(self.Color.red), 2)
263264
self.assertEqual(clist.colorList.count(self.Color.blue), 1)
264265
self.assertEqual(clist.colorList.count(self.Color.green), 0)
265-
# pyre-ignore[16]: has no attribute `containers_types`
266-
if self.containers_types.__name__.endswith("thrift_mutable_types"):
266+
if self.is_mutable_run:
267267
self.assertEqual(clist.colorList.count(0), 0)
268268
self.assertEqual(clist.colorList.count(1), 0)
269269
self.assertEqual(clist.colorList.count(2), 0)

thrift/lib/python/test/maps.py

+18-17
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import python_test.containers.thrift_mutable_types as mutable_containers_types
2727
import python_test.containers.thrift_types as immutable_containers_types
2828

29-
import python_test.maps.thrift_mutable_types as immutable_maps_types
30-
import python_test.maps.thrift_types as mutable_maps_types
29+
import python_test.maps.thrift_mutable_types as mutable_maps_types
30+
import python_test.maps.thrift_types as immutable_maps_types
3131

3232
from folly.iobuf import IOBuf
3333

@@ -53,6 +53,15 @@ class MyStringEnum(str, Enum):
5353
test = "test"
5454

5555

56+
class ImmutableMapTests(unittest.TestCase):
57+
def test_hashability(self) -> None:
58+
hash(immutable_maps_types.StrI32ListMap())
59+
x = immutable_maps_types.StrStrIntListMapMap(
60+
{"foo": immutable_maps_types.StrI32ListMap()}
61+
)
62+
hash(x["foo"])
63+
64+
5665
@parameterized_class(
5766
("containers_types", "maps_types"),
5867
[
@@ -84,6 +93,9 @@ def setUp(self) -> None:
8493
self.Foo: Type[FooType] = self.containers_types.Foo
8594
self.Maps: Type[MapsType] = self.containers_types.Maps
8695
self.Color: Type[ColorType] = self.containers_types.Color
96+
self.is_mutable_run: bool = self.containers_types.__name__.endswith(
97+
"thrift_mutable_types"
98+
)
8799

88100
def test_recursive_const_map(self) -> None:
89101
self.assertEqual(self.LocationMap[1][1], 1)
@@ -163,14 +175,6 @@ def test_mixed_construction(self) -> None:
163175
self.StrStrIntListMapMap(px)
164176
self.assertNotIn("bar", cx)
165177

166-
def test_hashability(self) -> None:
167-
# Mutable types do not support hashing
168-
# pyre-ignore[16]: has no attribute `lists_types`
169-
if self.maps_types.__name__.endswith("immutable_types"):
170-
hash(self.StrI32ListMap())
171-
x = self.StrStrIntListMapMap({"foo": self.StrI32ListMap()})
172-
hash(x["foo"])
173-
174178
def test_equality(self) -> None:
175179
x = self.StrIntMap({"foo": 5, "bar": 4})
176180
y = self.StrIntMap({"foo": 4, "bar": 5})
@@ -198,9 +202,6 @@ def test_struct_in_map(self) -> None:
198202
self.assertNotEqual(a, c)
199203

200204
def test_struct_with_map_fields(self) -> None:
201-
# pyre-ignore[16]: has no attribute `lists_types`
202-
is_immutable = self.maps_types.__name__.endswith("immutable_types")
203-
204205
s = self.Maps(
205206
boolMap={True: True, False: False},
206207
byteMap={1: 1, 2: 2, 3: 3},
@@ -212,12 +213,12 @@ def test_struct_with_map_fields(self) -> None:
212213
binaryMap={b"foo": b"foo", b"bar": b"bar"},
213214
iobufMap={IOBuf(b"foo"): IOBuf(b"foo"), IOBuf(b"bar"): IOBuf(b"bar")},
214215
structMap=(
215-
{
216+
{}
217+
if self.is_mutable_run
218+
else {
216219
self.Foo(value=1): self.Foo(value=1),
217220
self.Foo(value=2): self.Foo(value=2),
218221
}
219-
if is_immutable
220-
else {}
221222
),
222223
)
223224
self.assertEqual(s.boolMap, {True: True, False: False})
@@ -231,7 +232,7 @@ def test_struct_with_map_fields(self) -> None:
231232
self.assertEqual(
232233
s.iobufMap, {IOBuf(b"foo"): IOBuf(b"foo"), IOBuf(b"bar"): IOBuf(b"bar")}
233234
)
234-
if is_immutable:
235+
if not self.is_mutable_run:
235236
self.assertEqual(
236237
s.structMap,
237238
{

thrift/lib/python/test/refs.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def setUp(self) -> None:
4343
"""
4444
# pyre-ignore[16]: has no attribute `sets_types`
4545
self.ComplexRef: Type[ComplexRefType] = self.test_types.ComplexRef
46+
self.is_mutable_run: bool = self.test_types.__name__.endswith(
47+
"thrift_mutable_types"
48+
)
4649

4750
def test_create_default(self) -> None:
4851
x = self.ComplexRef()
@@ -71,17 +74,16 @@ def test_list(self) -> None:
7174
self.assertEqual(x.list_recursive_ref, (bar, baz))
7275

7376
def test_set(self) -> None:
74-
# pyre-ignore[16]: has no attribute `test_types`
75-
is_immutable = self.test_types.__name__.endswith("immutable_types")
76-
7777
bar, baz = self.ComplexRef(name="bar"), self.ComplexRef(name="baz")
7878
x = self.ComplexRef(
7979
name="foo",
8080
set_basetype_ref={1, 2, 3},
81-
set_recursive_ref={bar, baz} if is_immutable else set(),
81+
set_recursive_ref=set() if self.is_mutable_run else {bar, baz},
8282
)
8383
self.assertEqual(x.set_basetype_ref, {1, 2, 3})
84-
self.assertEqual(x.set_recursive_ref, {bar, baz} if is_immutable else set())
84+
self.assertEqual(
85+
x.set_recursive_ref, set() if self.is_mutable_run else {bar, baz}
86+
)
8587

8688
def test_map(self) -> None:
8789
bar, baz = self.ComplexRef(name="bar"), self.ComplexRef(name="baz")
@@ -99,14 +101,14 @@ def test_shared_ref(self) -> None:
99101
self.assertEqual(x.list_shared_ref, (bar, baz))
100102

101103
def test_const_shared_ref(self) -> None:
102-
# pyre-ignore[16]: has no attribute `test_types`
103-
is_immutable = self.test_types.__name__.endswith("immutable_types")
104-
105104
bar, baz = self.ComplexRef(name="bar"), self.ComplexRef(name="baz")
106105
x = self.ComplexRef(
107-
name="foo", set_const_shared_ref={bar, baz} if is_immutable else set()
106+
name="foo",
107+
set_const_shared_ref=set() if self.is_mutable_run else {bar, baz},
108+
)
109+
self.assertEqual(
110+
x.set_const_shared_ref, set() if self.is_mutable_run else {bar, baz}
108111
)
109-
self.assertEqual(x.set_const_shared_ref, {bar, baz} if is_immutable else set())
110112

111113
def test_recursive(self) -> None:
112114
bar = self.ComplexRef(name="bar")

thrift/lib/python/test/sets.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import python_test.containers.thrift_mutable_types as mutable_containers_types
2626
import python_test.containers.thrift_types as immutable_containers_types
2727

28-
import python_test.sets.thrift_mutable_types as immutable_sets_types
29-
import python_test.sets.thrift_types as mutable_sets_types
28+
import python_test.sets.thrift_mutable_types as mutable_sets_types
29+
import python_test.sets.thrift_types as immutable_sets_types
3030

3131
from folly.iobuf import IOBuf
3232

@@ -61,6 +61,9 @@ def setUp(self) -> None:
6161
self.Foo: Type[Foo] = self.containers_types.Foo
6262
self.Sets: Type[Sets] = self.containers_types.Sets
6363
self.Color: Type[Color] = self.containers_types.Color
64+
self.is_mutable_run: bool = self.containers_types.__name__.endswith(
65+
"thrift_mutable_types"
66+
)
6467

6568
def test_and(self) -> None:
6669
x = self.SetI32({1, 3, 4, 5})
@@ -184,9 +187,6 @@ def test_create_untruthy_set(self) -> None:
184187
self.assertEqual(Sets(i32Set=Untruthy(5)).i32Set, set(range(5)))
185188

186189
def test_struct_with_set_fields(self) -> None:
187-
# pyre-ignore[16]: has no attribute `lists_types`
188-
is_immutable = self.sets_types.__name__.endswith("immutable_types")
189-
190190
s = self.Sets(
191191
boolSet={True, False},
192192
byteSet={1, 2, 3},
@@ -198,7 +198,7 @@ def test_struct_with_set_fields(self) -> None:
198198
binarySet={b"foo", b"bar"},
199199
iobufSet={IOBuf(b"foo"), IOBuf(b"bar")},
200200
structSet=(
201-
{self.Foo(value=1), self.Foo(value=2)} if is_immutable else set()
201+
set() if self.is_mutable_run else {self.Foo(value=1), self.Foo(value=2)}
202202
),
203203
)
204204
self.assertEqual(s.boolSet, {True, False})
@@ -210,7 +210,7 @@ def test_struct_with_set_fields(self) -> None:
210210
self.assertEqual(s.stringSet, {"foo", "bar"})
211211
self.assertEqual(s.binarySet, {b"foo", b"bar"})
212212
self.assertEqual(s.iobufSet, {IOBuf(b"foo"), IOBuf(b"bar")})
213-
if is_immutable:
213+
if not self.is_mutable_run:
214214
self.assertEqual(s.structSet, {Foo(value=1), Foo(value=2)})
215215
# test reaccess the set element won't have to recreating the struct
216216
structs1 = list(s.structSet)

0 commit comments

Comments
 (0)