Skip to content

Instantly share code, notes, and snippets.

@joshourisman
Created July 10, 2014 15:13
Show Gist options
  • Save joshourisman/17652191036d110cfa35 to your computer and use it in GitHub Desktop.
Save joshourisman/17652191036d110cfa35 to your computer and use it in GitHub Desktop.
def test_bulk_create_timestamps(self):
provider = Provider.objects.create(**{
'url': 'http://www.google.com/',
'name': 'Google',
'names': json.dumps(['Google', ]),
'favicon_url': 'http://www.google.com/favicon.ico',
})
import_id = uuid4()
pre_create = utc.localize(datetime.datetime.utcnow())
resources = Resource.objects.bulk_create([Resource(**{
'imported_by': self.user,
'provider': provider,
'captured_url': 'http://www.google.com/',
'canonical_url': 'http://www.google.com/',
'title': 'Title',
'author': 'Foo Bar',
'description': 'This is an article.',
'content': 'Yo!',
'import_id': import_id,
})])
post_create = utc.localize(datetime.datetime.utcnow())
resource = resources[0]
original_created = resource.created
original_modified = resource.modified
self.assertTrue(pre_create < original_created < post_create)
self.assertTrue(pre_create < original_modified < post_create)
self.assertTrue(original_created < original_modified)
self.assertTrue(
original_modified - original_created < datetime.timedelta(
seconds=1))
resource = Resource.objects.get(import_id=import_id)
pre_modify = utc.localize(datetime.datetime.utcnow())
resource.save()
post_modify = utc.localize(datetime.datetime.utcnow())
resource = Resource.objects.get(id=resource.id)
self.assertEqual(resource.created, original_created)
self.assertNotEqual(resource.modified, original_modified)
self.assertTrue(resource.created < pre_modify < post_modify)
self.assertTrue(pre_modify < resource.modified < post_modify)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment