I defined some timestamps for events in the database as auto_now_add , since the information should be stored along with it timestamp simultaneously with saving the event.
Description of 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 generate some information in the database in the test.py file, as follows:
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 have to generate events with my label yesterday (the module then summarizes them). The problem is that you cannot overwrite the timestamp. This timestamp, when she created the event, the documentation , speaks very clearly about this.
So how to generate data with suitable timestamps for testing? I had a few ideas:
- It is possible to generate 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 the test, something like
_
if testing: timestamp = models.DateTimeField(auto_now_add=True) else: timestamp = models.DateTimeField(auto_now_add=False)
Or maybe there is an even easier way to do this ... Any ideas?
python django unit-testing
Khelben
source share