AnswerBun.com

Why is my django unittest failing a constraint?

Stack Overflow Asked on January 5, 2022

I have this model:

class TestopiaEvent(Model):
    event_id = AutoField(primary_key=True)
    name = CharField(max_length=255)
    start_date = DateField()
    end_date = DateField()
    testers_required = IntegerField()

    class Meta:
        constraints = [
            CheckConstraint(
                check=Q(start_date__lte=F('end_date'), start_date__gte=datetime.now().date()),
                name='correct_datetime'
            )
        ]

And this test:

class TestopiaEventTestCase(TestCase):
    def setUp(self):
        self.default_values = {
            'name': 'Testopia 1',
            'start_date': datetime.now().date(),
            'end_date': datetime.now().date() + timedelta(days=1),
            'testers_required': 1
        }
        self.testopia_event = TestopiaEvent(**self.default_values)

    def test_save_with_valid_model_check_database(self):
        self.assertIsNone(self.testopia_event.save())

And it fails with this error:

django.db.utils.IntegrityError: new row for relation "webserver_testopiaevent" violates check constraint "correct_datetime"
DETAIL:  Failing row contains (1, Testopia 1, 2020-07-24 00:00:00+00, 2020-07-25 00:00:00+00, 1).

I don’t understand why it is failing as it should only fail if today’s date is less than the start date and the start date or/and the start date is greater than the end date, which it isn’t?

What have I done wrong? Thanks

Edit: Here are the postgresdb constraints:

testopia=# d+ webserver_testopiaevent
Table

"public.webserver_testopiaevent"
      Column      |          Type          | Collation | Nullable |                          Default                          | Storage  | Stats target | Description 
------------------+------------------------+-----------+----------+-----------------------------------------------------------+----------+--------------+-------------
 event_id         | integer                |           | not null | nextval('webserver_testopiaevent_event_id_seq'::regclass) | plain    |              | 
 name             | character varying(255) |           | not null |                                                           | extended |              | 
 start_date       | date                   |           | not null |                                                           | plain    |              | 
 end_date         | date                   |           | not null |                                                           | plain    |              | 
 testers_required | integer                |           | not null |                                                           | plain    |              | 
Indexes:
    "webserver_testopiaevent_pkey" PRIMARY KEY, btree (event_id)
Check constraints:
    "correct_datetime" CHECK (start_date >= statement_timestamp() AND start_date <= end_date)
Access method: heap

One Answer

Now() returns a DateTimeField() so with the timestamp addition it will be more than the current date if my DateField is set to the same date.

Answered by OultimoCoder on January 5, 2022

Add your own answers!

Related Questions

Removing duplicates while sorting numbers inside a String in java

5  Asked on November 30, 2020 by swetha-haridoss

       

pointers cant read the correct elements in array

2  Asked on November 29, 2020 by jabou

   

pandas does not load the sub packages properly

0  Asked on November 29, 2020 by realbro

   

Webpack: how to copy html files into build folder?

2  Asked on November 29, 2020 by wai-yan-hein

   

Save string to file without converting newlines using Python

1  Asked on November 29, 2020 by kleiton-kurti

       

ngx-mask Do not allow negative value for the currency input

2  Asked on November 28, 2020 by ashot-aleqsanyan

   

Unity: Add extra time to slider

1  Asked on November 28, 2020 by christopher-madsen

     

Python cannot access list

3  Asked on November 28, 2020 by forge-mods

   

Get property name and value only if there is a value

1  Asked on November 28, 2020 by jimenemex

   

Count percentage of upper case words

1  Asked on November 28, 2020 by user13623188

   

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP