-1

I have a model with various fields. One of them is a CharField

field = models.CharField(max_length=100, blank=False, null=False)

Now when I create an object without this field in django tests class function 'setUpTestData', it doesnt give any error. If I add minLengthValidator to it then too it doesn't give any

models.CharField(blank=False, null=False, max_length=100, validators=[MinLengthValidator(1)])

When I print it after the object is created it gives an empty string.

I have another field:

field2 = models.DateTimeField(blank=False, null=False)

Now if I dont provide this data while creating the object, the test fails correctly.

class Bag(models.Model):
    created_on = models.DateTimeField(default=timezone.now)
    order_id = models.CharField(blank=False, null=False, max_length=100)
    order_created_time = models.DateTimeField(blank=False, null=False)


class SalesBagTestCase(TestCase):

    @classmethod
    def setUpTestData(cls):
        Bag.objects.create(order_created_time=datetime.now())

    def test_bag(self):
        bag_response = self.client.get('http://localhost:8000/bag/')
        self.assertEqual(bag_response.status_code, 200)

AssertEqual is True even if I remove the order_id field in the setUpData method

Arjunsingh
  • 703
  • 9
  • 22
  • can you post your test code? – Burhan Khalid Jun 22 '18 at 13:00
  • Without seeing your test code it's hard to say, but note that Django-level validation doesn't happen automatically (see [`full_clean()`](https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.full_clean)). Database-level validation always happens, though, which is probably why `field2` triggers a failure. – Kevin Christopher Henry Jun 22 '18 at 13:06
  • Still not clear what you are expecting to fail here. Where is the `bag` view? – Daniel Roseman Jun 22 '18 at 13:10
  • There ain't a need for the view here I think so. Database level validation should have triggered as I've mentioned null=False in the model. – Arjunsingh Jun 22 '18 at 13:12
  • Where should it have done that? We have no idea, because *you have not posted your view*. – Daniel Roseman Jun 22 '18 at 13:19
  • The view would've been called if I had made a POST call on the url. But here I'm creating the object via ORM directly. Hence while saving the data it should have raised an error as I haven't passed the order_id field. Or do I have misunderstood some concept? – Arjunsingh Jun 22 '18 at 13:24
  • So you're expecting an error to happen in the `setUpTestData` method? You should have said that from the start. In any case, Kevin has already given you the reason for that. – Daniel Roseman Jun 22 '18 at 13:27
  • That's what I didn't understand. Database level validation happens for order_created_time field but not for order_id field. – Arjunsingh Jun 22 '18 at 13:30
  • As Daniel said, the answer is explained in this post - https://stackoverflow.com/questions/40881708/django-model-validator-not-working-on-create – Arjunsingh Jun 22 '18 at 13:33

1 Answers1

0
class Bag(models.Model):
    created_on = models.DateTimeField(default=timezone.now)
    order_id = models.CharField(blank=False, null=False, max_length=100)
    order_created_time = models.DateTimeField(blank=False, null=False)

class SalesBagTestCase(TestCase):
    bag = None
    @classmethod
    def setUpTestData(cls):
        bag = Bag.objects.create(order_created_time=datetime.now())
        cls.bag = bag

    def test_bag(self):
        bag_response = self.client.get('http://localhost:8000/bag/')
        self.bag.full_clean()
        self.assertEqual(bag_response.status_code, 200)

This points out that we need to use the full_clean() method in the pre-save signal.

Arjunsingh
  • 703
  • 9
  • 22