Skip to content

Commit 90f6bab

Browse files
committed
[SPARK-57368][PYTHON][ML][TEST] Fix assertTrue misuse in PySpark tests
### What changes were proposed in this pull request? Replace `self.assertTrue(a, b)` with `self.assertEqual(a, b)` in cases where the second argument is an expected value, not a failure message. `unittest.TestCase.assertTrue(expr, msg=None)` treats the second argument as an optional failure message, so `assertTrue(a, b)` only checks that `a` is truthy — it never compares `a` against `b`. These tests were silently passing regardless of the actual value. Fixed in three files: - `sql/tests/test_functions.py`: `assertTrue(row[1], 1)` / `assertTrue(row[2], 1)` — crosstab result checks - `ml/tests/test_linalg.py`: repr/str checks for dense and sparse matrices, norm value checks, UDT schema and matrix equality checks - `mllib/tests/test_linalg.py`: same patterns (mirrors the `ml` file) ### Why are the changes needed? These are silent test bugs: the assertions always pass as long as the first argument is truthy, which it always is (non-empty strings, non-zero numbers, matrix objects). The expected values in the second argument were never verified. `assertEqual` makes the tests actually check what they intended. ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? Test-only change. The assertions being fixed already passed (because they only checked truthiness), and after the fix they continue to pass because the actual values match the expected values. ### Was this patch authored or co-authored using generative AI tooling? Generated-by: Claude Code (claude-sonnet-4-6) Closes #56428 from zhengruifeng/fix-assertTrue-dev2. Authored-by: Ruifeng Zheng <ruifengz@apache.org> Signed-off-by: Ruifeng Zheng <ruifengz@foxmail.com>
1 parent 60acc8f commit 90f6bab

3 files changed

Lines changed: 56 additions & 56 deletions

File tree

python/pyspark/ml/tests/test_linalg.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,51 +193,51 @@ def test_matrix_indexing(self):
193193

194194
def test_repr_dense_matrix(self):
195195
mat = DenseMatrix(3, 2, [0, 1, 4, 6, 8, 10])
196-
self.assertTrue(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], False)")
196+
self.assertEqual(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], False)")
197197

198198
mat = DenseMatrix(3, 2, [0, 1, 4, 6, 8, 10], True)
199-
self.assertTrue(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], False)")
199+
self.assertEqual(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], True)")
200200

201201
mat = DenseMatrix(6, 3, zeros(18))
202-
self.assertTrue(
202+
self.assertEqual(
203203
repr(mat),
204-
"DenseMatrix(6, 3, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ..., \
205-
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], False)",
204+
"DenseMatrix(6, 3, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ..., "
205+
"0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], False)",
206206
)
207207

208208
def test_repr_sparse_matrix(self):
209209
sm1t = SparseMatrix(
210210
3, 4, [0, 2, 3, 5], [0, 1, 2, 0, 2], [3.0, 2.0, 4.0, 9.0, 8.0], isTransposed=True
211211
)
212-
self.assertTrue(
212+
self.assertEqual(
213213
repr(sm1t),
214214
"SparseMatrix(3, 4, [0, 2, 3, 5], [0, 1, 2, 0, 2], [3.0, 2.0, 4.0, 9.0, 8.0], True)",
215215
)
216216

217217
indices = tile(arange(6), 3)
218218
values = ones(18)
219219
sm = SparseMatrix(6, 3, [0, 6, 12, 18], indices, values)
220-
self.assertTrue(
220+
self.assertEqual(
221221
repr(sm),
222-
"SparseMatrix(6, 3, [0, 6, 12, 18], \
223-
[0, 1, 2, 3, 4, 5, 0, 1, ..., 4, 5, 0, 1, 2, 3, 4, 5], \
224-
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, ..., \
225-
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], False)",
222+
"SparseMatrix(6, 3, [0, 6, 12, 18], "
223+
"[0, 1, 2, 3, 4, 5, 0, 1, ..., 4, 5, 0, 1, 2, 3, 4, 5], "
224+
"[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, ..., "
225+
"1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], False)",
226226
)
227227

228-
self.assertTrue(
228+
self.assertEqual(
229229
str(sm),
230-
"6 X 3 CSCMatrix\n\
231-
(0,0) 1.0\n(1,0) 1.0\n(2,0) 1.0\n(3,0) 1.0\n(4,0) 1.0\n(5,0) 1.0\n\
232-
(0,1) 1.0\n(1,1) 1.0\n(2,1) 1.0\n(3,1) 1.0\n(4,1) 1.0\n(5,1) 1.0\n\
233-
(0,2) 1.0\n(1,2) 1.0\n(2,2) 1.0\n(3,2) 1.0\n..\n..",
230+
"6 X 3 CSCMatrix\n"
231+
"(0,0) 1.0\n(1,0) 1.0\n(2,0) 1.0\n(3,0) 1.0\n(4,0) 1.0\n(5,0) 1.0\n"
232+
"(0,1) 1.0\n(1,1) 1.0\n(2,1) 1.0\n(3,1) 1.0\n(4,1) 1.0\n(5,1) 1.0\n"
233+
"(0,2) 1.0\n(1,2) 1.0\n(2,2) 1.0\n(3,2) 1.0\n..\n..",
234234
)
235235

236236
sm = SparseMatrix(1, 18, zeros(19), [], [])
237-
self.assertTrue(
237+
self.assertEqual(
238238
repr(sm),
239-
"SparseMatrix(1, 18, \
240-
[0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0], [], [], False)",
239+
"SparseMatrix(1, 18, "
240+
"[0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0], [], [], False)",
241241
)
242242

243243
def test_sparse_matrix(self):
@@ -248,7 +248,7 @@ def test_sparse_matrix(self):
248248
self.assertEqual(sm1.colPtrs.tolist(), [0, 2, 2, 4, 4])
249249
self.assertEqual(sm1.rowIndices.tolist(), [1, 2, 1, 2])
250250
self.assertEqual(sm1.values.tolist(), [1.0, 2.0, 4.0, 5.0])
251-
self.assertTrue(
251+
self.assertEqual(
252252
repr(sm1),
253253
"SparseMatrix(3, 4, [0, 2, 2, 4, 4], [1, 2, 1, 2], [1.0, 2.0, 4.0, 5.0], False)",
254254
)
@@ -307,12 +307,12 @@ def test_dense_matrix_is_transposed(self):
307307
def test_norms(self):
308308
a = DenseVector([0, 2, 3, -1])
309309
self.assertAlmostEqual(a.norm(2), 3.742, 3)
310-
self.assertTrue(a.norm(1), 6)
311-
self.assertTrue(a.norm(inf), 3)
310+
self.assertEqual(a.norm(1), 6)
311+
self.assertEqual(a.norm(inf), 3)
312312
a = SparseVector(4, [0, 2], [3, -4])
313313
self.assertAlmostEqual(a.norm(2), 5)
314-
self.assertTrue(a.norm(1), 7)
315-
self.assertTrue(a.norm(inf), 4)
314+
self.assertEqual(a.norm(1), 7)
315+
self.assertEqual(a.norm(inf), 4)
316316

317317
tmp = SparseVector(4, [0, 2], [3, 0])
318318
self.assertEqual(tmp.numNonzeros(), 1)
@@ -385,14 +385,14 @@ def test_infer_schema(self):
385385
rdd = self.sc.parallelize([("dense", self.dm1), ("sparse", self.sm1)])
386386
df = rdd.toDF()
387387
schema = df.schema
388-
self.assertTrue(schema.fields[1].dataType, self.udt)
388+
self.assertEqual(schema.fields[1].dataType, self.udt)
389389
matrices = df.rdd.map(lambda x: x._2).collect()
390390
self.assertEqual(len(matrices), 2)
391391
for m in matrices:
392392
if isinstance(m, DenseMatrix):
393-
self.assertTrue(m, self.dm1)
393+
self.assertEqual(m, self.dm1)
394394
elif isinstance(m, SparseMatrix):
395-
self.assertTrue(m, self.sm1)
395+
self.assertEqual(m, self.sm1)
396396
else:
397397
raise ValueError("Expected a matrix but got type %r" % type(m))
398398

python/pyspark/mllib/tests/test_linalg.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -199,51 +199,51 @@ def test_matrix_indexing(self):
199199

200200
def test_repr_dense_matrix(self):
201201
mat = DenseMatrix(3, 2, [0, 1, 4, 6, 8, 10])
202-
self.assertTrue(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], False)")
202+
self.assertEqual(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], False)")
203203

204204
mat = DenseMatrix(3, 2, [0, 1, 4, 6, 8, 10], True)
205-
self.assertTrue(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], False)")
205+
self.assertEqual(repr(mat), "DenseMatrix(3, 2, [0.0, 1.0, 4.0, 6.0, 8.0, 10.0], True)")
206206

207207
mat = DenseMatrix(6, 3, zeros(18))
208-
self.assertTrue(
208+
self.assertEqual(
209209
repr(mat),
210-
"DenseMatrix(6, 3, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ..., \
211-
0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], False)",
210+
"DenseMatrix(6, 3, [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ..., "
211+
"0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], False)",
212212
)
213213

214214
def test_repr_sparse_matrix(self):
215215
sm1t = SparseMatrix(
216216
3, 4, [0, 2, 3, 5], [0, 1, 2, 0, 2], [3.0, 2.0, 4.0, 9.0, 8.0], isTransposed=True
217217
)
218-
self.assertTrue(
218+
self.assertEqual(
219219
repr(sm1t),
220220
"SparseMatrix(3, 4, [0, 2, 3, 5], [0, 1, 2, 0, 2], [3.0, 2.0, 4.0, 9.0, 8.0], True)",
221221
)
222222

223223
indices = tile(arange(6), 3)
224224
values = ones(18)
225225
sm = SparseMatrix(6, 3, [0, 6, 12, 18], indices, values)
226-
self.assertTrue(
226+
self.assertEqual(
227227
repr(sm),
228-
"SparseMatrix(6, 3, [0, 6, 12, 18], \
229-
[0, 1, 2, 3, 4, 5, 0, 1, ..., 4, 5, 0, 1, 2, 3, 4, 5], \
230-
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, ..., \
231-
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], False)",
228+
"SparseMatrix(6, 3, [0, 6, 12, 18], "
229+
"[0, 1, 2, 3, 4, 5, 0, 1, ..., 4, 5, 0, 1, 2, 3, 4, 5], "
230+
"[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, ..., "
231+
"1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0], False)",
232232
)
233233

234-
self.assertTrue(
234+
self.assertEqual(
235235
str(sm),
236-
"6 X 3 CSCMatrix\n\
237-
(0,0) 1.0\n(1,0) 1.0\n(2,0) 1.0\n(3,0) 1.0\n(4,0) 1.0\n(5,0) 1.0\n\
238-
(0,1) 1.0\n(1,1) 1.0\n(2,1) 1.0\n(3,1) 1.0\n(4,1) 1.0\n(5,1) 1.0\n\
239-
(0,2) 1.0\n(1,2) 1.0\n(2,2) 1.0\n(3,2) 1.0\n..\n..",
236+
"6 X 3 CSCMatrix\n"
237+
"(0,0) 1.0\n(1,0) 1.0\n(2,0) 1.0\n(3,0) 1.0\n(4,0) 1.0\n(5,0) 1.0\n"
238+
"(0,1) 1.0\n(1,1) 1.0\n(2,1) 1.0\n(3,1) 1.0\n(4,1) 1.0\n(5,1) 1.0\n"
239+
"(0,2) 1.0\n(1,2) 1.0\n(2,2) 1.0\n(3,2) 1.0\n..\n..",
240240
)
241241

242242
sm = SparseMatrix(1, 18, zeros(19), [], [])
243-
self.assertTrue(
243+
self.assertEqual(
244244
repr(sm),
245-
"SparseMatrix(1, 18, \
246-
[0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0], [], [], False)",
245+
"SparseMatrix(1, 18, "
246+
"[0, 0, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0, 0, 0], [], [], False)",
247247
)
248248

249249
def test_sparse_matrix(self):
@@ -254,7 +254,7 @@ def test_sparse_matrix(self):
254254
self.assertEqual(sm1.colPtrs.tolist(), [0, 2, 2, 4, 4])
255255
self.assertEqual(sm1.rowIndices.tolist(), [1, 2, 1, 2])
256256
self.assertEqual(sm1.values.tolist(), [1.0, 2.0, 4.0, 5.0])
257-
self.assertTrue(
257+
self.assertEqual(
258258
repr(sm1),
259259
"SparseMatrix(3, 4, [0, 2, 2, 4, 4], [1, 2, 1, 2], [1.0, 2.0, 4.0, 5.0], False)",
260260
)
@@ -329,12 +329,12 @@ def test_parse_vector(self):
329329
def test_norms(self):
330330
a = DenseVector([0, 2, 3, -1])
331331
self.assertAlmostEqual(a.norm(2), 3.742, 3)
332-
self.assertTrue(a.norm(1), 6)
333-
self.assertTrue(a.norm(inf), 3)
332+
self.assertEqual(a.norm(1), 6)
333+
self.assertEqual(a.norm(inf), 3)
334334
a = SparseVector(4, [0, 2], [3, -4])
335335
self.assertAlmostEqual(a.norm(2), 5)
336-
self.assertTrue(a.norm(1), 7)
337-
self.assertTrue(a.norm(inf), 4)
336+
self.assertEqual(a.norm(1), 7)
337+
self.assertEqual(a.norm(inf), 4)
338338

339339
tmp = SparseVector(4, [0, 2], [3, 0])
340340
self.assertEqual(tmp.numNonzeros(), 1)
@@ -487,14 +487,14 @@ def test_infer_schema(self):
487487
rdd = self.sc.parallelize([("dense", self.dm1), ("sparse", self.sm1)])
488488
df = rdd.toDF()
489489
schema = df.schema
490-
self.assertTrue(schema.fields[1].dataType, self.udt)
490+
self.assertEqual(schema.fields[1].dataType, self.udt)
491491
matrices = df.rdd.map(lambda x: x._2).collect()
492492
self.assertEqual(len(matrices), 2)
493493
for m in matrices:
494494
if isinstance(m, DenseMatrix):
495-
self.assertTrue(m, self.dm1)
495+
self.assertEqual(m, self.dm1)
496496
elif isinstance(m, SparseMatrix):
497-
self.assertTrue(m, self.sm1)
497+
self.assertEqual(m, self.sm1)
498498
else:
499499
raise ValueError("Expected a matrix but got type %r" % type(m))
500500

python/pyspark/sql/tests/test_functions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ def test_crosstab(self):
301301
ct = sorted(ct, key=lambda x: x[0])
302302
for i, row in enumerate(ct):
303303
self.assertEqual(row[0], str(i))
304-
self.assertTrue(row[1], 1)
305-
self.assertTrue(row[2], 1)
304+
self.assertEqual(row[1], 1)
305+
self.assertEqual(row[2], 1)
306306

307307
def test_math_functions(self):
308308
df = self.spark.createDataFrame([Row(a=i, b=2 * i) for i in range(10)])

0 commit comments

Comments
 (0)