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

Print [Python] seen as a keyword in Pycharm

1  Asked on February 20, 2021 by jleno46

     

Express req.subdomains empty

1  Asked on February 20, 2021

   

How to exclude data in mySQL string?

2  Asked on February 20, 2021 by markus

     

ERROR at line 3: ORA-00911: invalid character” when joining tables

2  Asked on February 19, 2021 by sinafarheidar12

   

How to transport data from csv to Excel

2  Asked on February 19, 2021 by tangercity

         

How to call Ajax datatables with parameter ASP.NET MVC?

3  Asked on February 19, 2021 by truong-hoang

       

Hide several tds inside the table

2  Asked on February 19, 2021 by tree

     

Kubernetes Conditions.Ready and ContainersReady are false

0  Asked on February 19, 2021 by user3877654

 

Highlight multiple cells using mxCellHighlight

2  Asked on February 18, 2021 by boris

   

How to get all dates up to a certain date?

1  Asked on February 18, 2021 by beck

   

How do I write a script that alerts a input value given by a visitor?

3  Asked on February 18, 2021 by mehmet-akif-vardar

 

Why is “==” not acceptable here?

1  Asked on February 18, 2021 by askman

     

Ask a Question

Get help from others!

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