def test_min_max_values(self): v = IntegerField(min_value=-100, max_value=0) self.assertTrue(-100 <= v.generate() <= 0) v = IntegerField(min_value=0, max_value=0) self.assertEqual(v.generate(), 0) v = IntegerField(min_value=100, max_value=0) with self.assertRaises(ValueError): v.generate()
def test_blankable(self): v = IntegerField(blank=True, blank_prob=0.5) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 1 mocked_random.randint = random.randint self.assertTrue(0 <= v.generate() <= 100) v = IntegerField(blank=True, blank_prob=0.5) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 0 mocked_random.randint = random.randint self.assertEqual(v.generate(), 0)
def test_nullable(self): v = IntegerField(null=True, null_prob=0.5) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 1 mocked_random.randint = random.randint self.assertTrue(0 <= v.generate() <= 100) v = IntegerField(null=True, null_prob=0.5) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 0 mocked_random.randint = random.randint self.assertIsNone(v.generate())
def test_nullable_blankable_choices(self): choices = [1, 5, 10] v = IntegerField(null=True, null_prob=0.3, blank=True, blank_prob=0.3, choices=choices) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 0 mocked_random.choice = random.choice self.assertIsNone(v.generate()) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 0.5 mocked_random.choice = random.choice self.assertEqual(v.generate(), 0) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 1 mocked_random.choice = random.choice self.assertIn(v.generate(), choices)
def test_not_required(self): v = IntegerField(required=False, not_present_prob=0) self.assertTrue(0 <= v.generate() <= 100) v = IntegerField(required=False, not_present_prob=1) with self.assertRaises(NotGeneratedException): v.generate() v = IntegerField(required=False, not_present_prob=0.5) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 0 with self.assertRaises(NotGeneratedException): v.generate() v = IntegerField(required=False, not_present_prob=0.5) with mock.patch(FIELDS_RANDOM_MODULE) as mocked_random: mocked_random.random.return_value = 1 mocked_random.randint = random.randint self.assertTrue(0 <= v.generate() <= 100)
def test_choices(self): choices = [1, 5, 10] v = IntegerField(choices=choices) self.assertIn(v.generate(), choices)
def test_default(self): v = IntegerField() self.assertTrue(0 <= v.generate() <= 100)