Overwrite auto_now for unittest

后端 未结 3 350
北荒
北荒 2021-01-12 15:50

I\'ve defined some timestamps for events in the database as auto_now_add, as the information should be stored with it\'s timestamp the same time the event is st

3条回答
  •  北荒
    北荒 (楼主)
    2021-01-12 16:27

    I've managed to create data overriding the default values using a fixture.

    I've created a test_data.json file with the data in the following format:

    [
    {
        "model": "stats_agg.newevent",
        "pk": 1,
        "fields": {
            "name": "event1",
            "quantity":0.0,
            "timestamp": "2010-02-15 00:27:40"
         }
    },
    {
        "model": "stats_agg.newevent",
        "pk": 2,
        "fields": {
            "name": "event1",
            "quantity":1.0,
            "timestamp": "2010-02-15 00:27:40"
         }
    },
    ...
    

    and then add to the test unit

    class SimpleTest(TestCase):
       fixtures = ['test_data.json']
    

提交回复
热议问题