覆盖auto_now进行单元测试 [英] Overwrite auto_now for unittest

查看:101
本文介绍了覆盖auto_now进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将数据库中的事件定义为 auto_now_add 的事件,因为在存储事件的同时,信息应与其时间戳一起存储。 >

事件的描述类似于

  class NewEvent(models.Model )
'''
个别事件
'''
name = models.CharField(max_length = 100)
quantity = models.FloatField(null = True)
timestamp = models.DateTimeField(auto_now_add = True)

要测试模块,在 test.py 文件中的数据库中生成一些信息,方式如下:

  for EVENT_TYPES中的事件:
time = datetime.datetime.now() - datetime.timedelta(days = 1)
for i in range(48):
time = time .replace(hour = i / 2)
NewEvent(name = event,
timestamp = time,
quan tity = i).save()

我必须生成其时间戳为昨天的事件(模块将然后总结一下)。问题是你不能覆盖时间戳。时间戳是事件生成的时间戳,文档非常清楚地表示。



那么,如何使用适当的时间戳生成数据来进行测试?我有几个想法:




  • 可能在Model类之外以不同的方式生成数据库数据。在哪里和如何?

  • 不知何故定义一个不同的类,或者在测试过程中改变类的行为,例如



_

 如果测试:
timestamp = models.DateTimeField(auto_now_add = True)
else:
timestamp = models.DateTimeField(auto_now_add = False)

或者也许有一个更简单的方法来做...任何想法?

解决方案

我设法创建了使用



我已经使用以下格式的数据创建了一个 test_data.json 文件:

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

然后添加到测试单元

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


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 stored.

The description of the events is something like

class NewEvent(models.Model):
    '''
    Individual event
    '''
    name = models.CharField(max_length=100)
    quantity = models.FloatField(null=True)
    timestamp = models.DateTimeField(auto_now_add=True)

To test the module, I'm generating some information in the database in the test.py file, this way:

    for event in EVENT_TYPES:
        time = datetime.datetime.now() - datetime.timedelta(days=1)
        for i in range(48):
            time = time.replace(hour=i / 2)
            NewEvent(name=event,
                     timestamp=time,
                     quantity=i).save()

I must generate events with its timestamp of yesterday (the module will then summarize them). The problem is that you can't overwrite the timestamp. The timestamp it's the one when the event it's produced, the documentation states that very clearly.

So, how to generate data with appropiate timestamps for the testing? I've had several ideas:

  • Maybe generate the database data in a different way, outside the Model classes. Where and how?
  • Somehow define a different class or change the class to behave differently during test, something like

_

 if testing:
     timestamp = models.DateTimeField(auto_now_add=True)
 else:
     timestamp = models.DateTimeField(auto_now_add=False)

Or maybe there's an even easier way to do this... Any ideas?

解决方案

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']

这篇关于覆盖auto_now进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆