2021-04-24 23:46:16 +00:00
|
|
|
from unittest import mock
|
|
|
|
|
|
|
|
from django.test import SimpleTestCase
|
|
|
|
from django.test.runner import Shuffler
|
|
|
|
|
|
|
|
|
|
|
|
class ShufflerTests(SimpleTestCase):
|
|
|
|
def test_hash_text(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
actual = Shuffler._hash_text("abcd")
|
|
|
|
self.assertEqual(actual, "e2fc714c4727ee9395f324cd2e7f331f")
|
2021-04-24 23:46:16 +00:00
|
|
|
|
|
|
|
def test_hash_text_hash_algorithm(self):
|
|
|
|
class MyShuffler(Shuffler):
|
2022-02-03 19:24:19 +00:00
|
|
|
hash_algorithm = "sha1"
|
2021-04-24 23:46:16 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
actual = MyShuffler._hash_text("abcd")
|
|
|
|
self.assertEqual(actual, "81fe8bfe87576c3ecb22426f8e57847382917acf")
|
2021-04-24 23:46:16 +00:00
|
|
|
|
|
|
|
def test_init(self):
|
|
|
|
shuffler = Shuffler(100)
|
|
|
|
self.assertEqual(shuffler.seed, 100)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(shuffler.seed_source, "given")
|
2021-04-24 23:46:16 +00:00
|
|
|
|
|
|
|
def test_init_none_seed(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
with mock.patch("random.randint", return_value=200):
|
2021-04-24 23:46:16 +00:00
|
|
|
shuffler = Shuffler(None)
|
|
|
|
self.assertEqual(shuffler.seed, 200)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(shuffler.seed_source, "generated")
|
2021-04-24 23:46:16 +00:00
|
|
|
|
|
|
|
def test_init_no_seed_argument(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
with mock.patch("random.randint", return_value=300):
|
2021-04-24 23:46:16 +00:00
|
|
|
shuffler = Shuffler()
|
|
|
|
self.assertEqual(shuffler.seed, 300)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(shuffler.seed_source, "generated")
|
2021-04-24 23:46:16 +00:00
|
|
|
|
|
|
|
def test_seed_display(self):
|
|
|
|
shuffler = Shuffler(100)
|
2022-02-03 19:24:19 +00:00
|
|
|
shuffler.seed_source = "test"
|
|
|
|
self.assertEqual(shuffler.seed_display, "100 (test)")
|
2021-04-24 23:46:16 +00:00
|
|
|
|
|
|
|
def test_hash_item_seed(self):
|
|
|
|
cases = [
|
2022-02-03 19:24:19 +00:00
|
|
|
(1234, "64ad3fb166ddb41a2ca24f1803b8b722"),
|
2021-04-24 23:46:16 +00:00
|
|
|
# Passing a string gives the same value.
|
2022-02-03 19:24:19 +00:00
|
|
|
("1234", "64ad3fb166ddb41a2ca24f1803b8b722"),
|
|
|
|
(5678, "4dde450ad339b6ce45a0a2666e35b975"),
|
2021-04-24 23:46:16 +00:00
|
|
|
]
|
|
|
|
for seed, expected in cases:
|
|
|
|
with self.subTest(seed=seed):
|
|
|
|
shuffler = Shuffler(seed=seed)
|
2022-02-03 19:24:19 +00:00
|
|
|
actual = shuffler._hash_item("abc", lambda x: x)
|
2021-04-24 23:46:16 +00:00
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_hash_item_key(self):
|
|
|
|
cases = [
|
2022-02-03 19:24:19 +00:00
|
|
|
(lambda x: x, "64ad3fb166ddb41a2ca24f1803b8b722"),
|
|
|
|
(lambda x: x.upper(), "ee22e8597bff91742affe4befbf4649a"),
|
2021-04-24 23:46:16 +00:00
|
|
|
]
|
|
|
|
for key, expected in cases:
|
|
|
|
with self.subTest(key=key):
|
|
|
|
shuffler = Shuffler(seed=1234)
|
2022-02-03 19:24:19 +00:00
|
|
|
actual = shuffler._hash_item("abc", key)
|
2021-04-24 23:46:16 +00:00
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_shuffle_key(self):
|
|
|
|
cases = [
|
2022-02-03 19:24:19 +00:00
|
|
|
(lambda x: x, ["a", "d", "b", "c"]),
|
|
|
|
(lambda x: x.upper(), ["d", "c", "a", "b"]),
|
2021-04-24 23:46:16 +00:00
|
|
|
]
|
|
|
|
for num, (key, expected) in enumerate(cases, start=1):
|
|
|
|
with self.subTest(num=num):
|
|
|
|
shuffler = Shuffler(seed=1234)
|
2022-02-03 19:24:19 +00:00
|
|
|
actual = shuffler.shuffle(["a", "b", "c", "d"], key)
|
2021-04-24 23:46:16 +00:00
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_shuffle_consistency(self):
|
|
|
|
seq = [str(n) for n in range(5)]
|
|
|
|
cases = [
|
2022-02-03 19:24:19 +00:00
|
|
|
(None, ["3", "0", "2", "4", "1"]),
|
|
|
|
(0, ["3", "2", "4", "1"]),
|
|
|
|
(1, ["3", "0", "2", "4"]),
|
|
|
|
(2, ["3", "0", "4", "1"]),
|
|
|
|
(3, ["0", "2", "4", "1"]),
|
|
|
|
(4, ["3", "0", "2", "1"]),
|
2021-04-24 23:46:16 +00:00
|
|
|
]
|
|
|
|
shuffler = Shuffler(seed=1234)
|
|
|
|
for index, expected in cases:
|
|
|
|
with self.subTest(index=index):
|
|
|
|
if index is None:
|
|
|
|
new_seq = seq
|
|
|
|
else:
|
|
|
|
new_seq = seq.copy()
|
|
|
|
del new_seq[index]
|
|
|
|
actual = shuffler.shuffle(new_seq, lambda x: x)
|
|
|
|
self.assertEqual(actual, expected)
|
|
|
|
|
|
|
|
def test_shuffle_same_hash(self):
|
|
|
|
shuffler = Shuffler(seed=1234)
|
|
|
|
msg = "item 'A' has same hash 'a56ce89262959e151ee2266552f1819c' as item 'a'"
|
|
|
|
with self.assertRaisesMessage(RuntimeError, msg):
|
2022-02-03 19:24:19 +00:00
|
|
|
shuffler.shuffle(["a", "b", "A"], lambda x: x.upper())
|