2016-04-09 25 views
1

DRF yanıtını ve giriş değerini karşılaştırmayı denedim.Birim testi Django zaman dilimi farkında tarih aralığı

AssertionError: {'date': '2016-04-09T07:35:28.039393Z', 'co[37 chars]': 1} != {'tag': 1, 'content': 'test content', 'id':[69 chars]TC>)} 
    {'content': 'test content', 
- 'date': '2016-04-09T07:35:28.039393Z', 
+ 'date': datetime.datetime(2016, 4, 9, 7, 35, 28, 39393, tzinfo=<UTC>), 
    'id': 1, 
    'tag': 1} 

Django datetime karşılaştırmak doğru yolu nedir:

class ViewTest(TransactionTestCase): 
    reset_sequences = True 
    current_date_time = timezone.now() 

    def setUp(self): 
     self.client = APIClient() 
     self.user = User.objects.create_user('hiren', '[email protected]', 'password') 
     self.client.force_authenticate(user=self.user) 
     self.tag = Tag.objects.create(name="Test tag") 
     Notes.objects.create(tag=self.tag, content="test content", date=self.current_date_time) 

    def test_return_correct_note(self): 
     response = self.client.get('/api/notes/1/') 
     self.assertEqual(response.json(), {'content': 'test content', 'id': 1, 
              'tag': 1, 'date': self.current_date_time}) 

Sonra bu hata var?

cevap

3

Python datetime nesnesini bir ISO zaman dizgesine dönüştürebilir veya ISO zaman dizesini datetime nesnesine python olarak ayrıştırabilirsiniz. Örneğin

... 
'tag': 1, 'date': self.current_date_time.strftime('%Y-%m-%dT%H:%M:%S.%fZ')})