|
| 1 | +# Copyright: See the LICENSE file. |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | +import factory |
| 6 | +from factory import MISSING |
| 7 | + |
| 8 | + |
| 9 | +class MissingSentinelTestCase(unittest.TestCase): |
| 10 | + """Tests for the MISSING sentinel object.""" |
| 11 | + |
| 12 | + def test_missing_basic(self): |
| 13 | + """Test basic MISSING exclusion from DictFactory.""" |
| 14 | + |
| 15 | + class MyDictFactory(factory.DictFactory): |
| 16 | + name = "John" |
| 17 | + email = MISSING |
| 18 | + phone = "123-456" |
| 19 | + |
| 20 | + result = MyDictFactory() |
| 21 | + self.assertEqual(result, {"name": "John", "phone": "123-456"}) |
| 22 | + self.assertNotIn("email", result) |
| 23 | + |
| 24 | + def test_missing_override(self): |
| 25 | + """Test MISSING can be overridden per-instance.""" |
| 26 | + |
| 27 | + class MyDictFactory(factory.DictFactory): |
| 28 | + name = "John" |
| 29 | + email = "default@example.com" |
| 30 | + |
| 31 | + result = MyDictFactory(email=MISSING) |
| 32 | + self.assertEqual(result, {"name": "John"}) |
| 33 | + self.assertNotIn("email", result) |
| 34 | + |
| 35 | + def test_missing_all(self): |
| 36 | + """Test all fields can be MISSING.""" |
| 37 | + |
| 38 | + class MyDictFactory(factory.DictFactory): |
| 39 | + field1 = MISSING |
| 40 | + field2 = MISSING |
| 41 | + |
| 42 | + result = MyDictFactory() |
| 43 | + self.assertEqual(result, {}) |
| 44 | + |
| 45 | + def test_missing_with_lazy_function(self): |
| 46 | + """Test MISSING works with declarations.""" |
| 47 | + |
| 48 | + class MyDictFactory(factory.DictFactory): |
| 49 | + name = factory.LazyFunction(lambda: "Generated") |
| 50 | + skipped = MISSING |
| 51 | + value = 42 |
| 52 | + |
| 53 | + result = MyDictFactory() |
| 54 | + self.assertEqual(result["name"], "Generated") |
| 55 | + self.assertEqual(result["value"], 42) |
| 56 | + self.assertNotIn("skipped", result) |
| 57 | + |
| 58 | + def test_missing_singleton(self): |
| 59 | + """Test MISSING is a singleton.""" |
| 60 | + another_missing = factory.MISSING |
| 61 | + self.assertIs(MISSING, another_missing) |
| 62 | + |
| 63 | + def test_missing_bool(self): |
| 64 | + """Test MISSING is falsy.""" |
| 65 | + self.assertFalse(MISSING) |
| 66 | + self.assertFalse(bool(MISSING)) |
| 67 | + |
| 68 | + def test_missing_repr(self): |
| 69 | + """Test MISSING has readable repr.""" |
| 70 | + self.assertEqual(repr(MISSING), "<MISSING>") |
| 71 | + |
| 72 | + def test_missing_with_transformer(self): |
| 73 | + """Test MISSING works with Transformer declarations.""" |
| 74 | + |
| 75 | + class MyDictFactory(factory.DictFactory): |
| 76 | + timeout = factory.Transformer(30.0, transform=int) |
| 77 | + |
| 78 | + result = MyDictFactory() |
| 79 | + self.assertEqual(result, {"timeout": 30}) |
| 80 | + |
| 81 | + result = MyDictFactory(timeout=MISSING) |
| 82 | + self.assertEqual(result, {}) |
| 83 | + self.assertNotIn("timeout", result) |
| 84 | + |
| 85 | + def test_missing_transformer_default(self): |
| 86 | + """Test MISSING as Transformer default excludes without error.""" |
| 87 | + |
| 88 | + class MyDictFactory(factory.DictFactory): |
| 89 | + timeout = factory.Transformer(MISSING, transform=int) |
| 90 | + |
| 91 | + result = MyDictFactory() |
| 92 | + self.assertEqual(result, {}) |
| 93 | + self.assertNotIn("timeout", result) |
0 commit comments