|
1 |
| -from collections import Counter |
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
2 | 3 | import unittest
|
3 | 4 |
|
4 | 5 | from luhn import Luhn
|
5 | 6 |
|
6 | 7 |
|
7 | 8 | class LuhnTests(unittest.TestCase):
|
8 |
| - def test_addends(self): |
9 |
| - # uses a Counter to avoid specifying order of return value |
10 |
| - self.assertEqual(Counter([1, 4, 1, 4, 1]), |
11 |
| - Counter(Luhn(12121).addends())) |
| 9 | + def test_single_digit_strings_can_not_be_valid(self): |
| 10 | + self.assertFalse(Luhn("1").is_valid()) |
| 11 | + |
| 12 | + def test_a_single_zero_is_invalid(self): |
| 13 | + self.assertFalse(Luhn("0").is_valid()) |
| 14 | + |
| 15 | + def test_a_simple_valid_SIN_that_remains_valid_if_reversed(self): |
| 16 | + self.assertTrue(Luhn("059").is_valid()) |
| 17 | + |
| 18 | + def test_a_simple_valid_SIN_that_becomes_invalid_if_reversed(self): |
| 19 | + self.assertTrue(Luhn("59").is_valid()) |
| 20 | + |
| 21 | + def test_a_valid_Canadian_SIN(self): |
| 22 | + self.assertTrue(Luhn("055 444 285").is_valid()) |
12 | 23 |
|
13 |
| - def test_addends_large(self): |
14 |
| - # uses a Counter to avoid specifying order of return value |
15 |
| - self.assertEqual(Counter([7, 6, 6, 1]), |
16 |
| - Counter(Luhn(8631).addends())) |
| 24 | + def test_invalid_Canadian_SIN(self): |
| 25 | + self.assertFalse(Luhn("055 444 286").is_valid()) |
17 | 26 |
|
18 |
| - def test_checksum1(self): |
19 |
| - self.assertEqual(22, Luhn(4913).checksum()) |
| 27 | + def test_invalid_credit_card(self): |
| 28 | + self.assertFalse(Luhn("8273 1232 7352 0569").is_valid()) |
20 | 29 |
|
21 |
| - def test_ckecksum2(self): |
22 |
| - self.assertEqual(21, Luhn(201773).checksum()) |
| 30 | + def test_valid_strings_with_a_non_digit_included_become_invalid(self): |
| 31 | + self.assertFalse(Luhn("055a 444 285").is_valid()) |
23 | 32 |
|
24 |
| - def test_invalid_number(self): |
25 |
| - self.assertFalse(Luhn(738).is_valid()) |
| 33 | + def test_valid_strings_with_punctuation_included_become_invalid(self): |
| 34 | + self.assertFalse(Luhn("055-444-285").is_valid()) |
26 | 35 |
|
27 |
| - def test_valid_number(self): |
28 |
| - self.assertTrue(Luhn(8739567).is_valid()) |
| 36 | + def test_valid_strings_with_symbols_included_become_invalid(self): |
| 37 | + self.assertFalse(Luhn("055£ 444$ 285").is_valid()) |
29 | 38 |
|
30 |
| - def test_create_valid_number1(self): |
31 |
| - self.assertEqual(1230, Luhn.create(123)) |
| 39 | + def test_single_zero_with_space_is_invalid(self): |
| 40 | + self.assertFalse(Luhn("0").is_valid()) |
32 | 41 |
|
33 |
| - def test_create_valid_number2(self): |
34 |
| - self.assertEqual(8739567, Luhn.create(873956)) |
| 42 | + def test_more_than_a_single_zero_is_valid(self): |
| 43 | + self.assertTrue(Luhn("0000 0").is_valid()) |
35 | 44 |
|
36 |
| - def test_create_valid_number3(self): |
37 |
| - self.assertEqual(8372637564, Luhn.create(837263756)) |
| 45 | + def test_input_digit_9_is_correctly_converted_to_output_digit_9(self): |
| 46 | + self.assertTrue(Luhn("091").is_valid()) |
38 | 47 |
|
39 | 48 | def test_is_valid_can_be_called_repeatedly(self):
|
| 49 | + # Additional track specific test case |
40 | 50 | # This test was added, because we saw many implementations
|
41 | 51 | # in which the first call to is_valid() worked, but the
|
42 | 52 | # second call failed().
|
43 |
| - number = Luhn(8739567) |
| 53 | + number = Luhn("055 444 285") |
44 | 54 | self.assertTrue(number.is_valid())
|
45 | 55 | self.assertTrue(number.is_valid())
|
46 | 56 |
|
|
0 commit comments