| 23 | |
| 24 | class CastsTestCase(unittest.TestCase): |
| 25 | def test_cast_string(self): |
| 26 | cast_func = get_cast("string") |
| 27 | |
| 28 | value = "test1" |
| 29 | result = cast_func(value) |
| 30 | self.assertEqual(result, "test1") |
| 31 | |
| 32 | value = "test2" |
| 33 | result = cast_func(value) |
| 34 | self.assertEqual(result, "test2") |
| 35 | |
| 36 | value = "" |
| 37 | result = cast_func(value) |
| 38 | self.assertEqual(result, "") |
| 39 | |
| 40 | # None should be preserved |
| 41 | value = None |
| 42 | result = cast_func(value) |
| 43 | self.assertEqual(result, None) |
| 44 | |
| 45 | # Non string or non, should throw a friendly exception |
| 46 | value = [] |
| 47 | expected_msg = r'Value "\[\]" must either be a string or None. Got "list"' |
| 48 | self.assertRaisesRegex(ValueError, expected_msg, cast_func, value) |
| 49 | |
| 50 | def test_cast_array(self): |
| 51 | cast_func = get_cast("array") |