Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed timezone tests when dict randomization is on

Refs #17758.

Backport of 1d6b7f3 from master.
  • Loading branch information...
commit 6fd321652aba307c86ac0df3c25c920e0e948d1e 1 parent 9edec9e
Aymeric Augustin authored October 25, 2012

Showing 1 changed file with 38 additions and 24 deletions. Show diff stats Hide diff stats

  1. 62  tests/modeltests/timezones/tests.py
62  tests/modeltests/timezones/tests.py
@@ -5,6 +5,7 @@
5 5
 import sys
6 6
 import time
7 7
 import warnings
  8
+from xml.dom.minidom import parseString
8 9
 
9 10
 try:
10 11
     import pytz
@@ -492,27 +493,40 @@ class SerializationTests(TestCase):
492 493
     #   returns a naive datetime object in UTC (http://pyyaml.org/ticket/202).
493 494
     # Tests are adapted to take these quirks into account.
494 495
 
  496
+    def assert_python_contains_datetime(self, objects, dt):
  497
+        self.assertEqual(objects[0]['fields']['dt'], dt)
  498
+
  499
+    def assert_json_contains_datetime(self, json, dt):
  500
+        self.assertIn('"fields": {"dt": "%s"}' % dt, json)
  501
+
  502
+    def assert_xml_contains_datetime(self, xml, dt):
  503
+        field = parseString(xml).getElementsByTagName('field')[0]
  504
+        self.assertXMLEqual(field.childNodes[0].wholeText, dt)
  505
+
  506
+    def assert_yaml_contains_datetime(self, yaml, dt):
  507
+        self.assertIn("- fields: {dt: !!timestamp '%s'}" % dt, yaml)
  508
+
495 509
     def test_naive_datetime(self):
496 510
         dt = datetime.datetime(2011, 9, 1, 13, 20, 30)
497 511
 
498 512
         data = serializers.serialize('python', [Event(dt=dt)])
499  
-        self.assertEqual(data[0]['fields']['dt'], dt)
  513
+        self.assert_python_contains_datetime(data, dt)
500 514
         obj = next(serializers.deserialize('python', data)).object
501 515
         self.assertEqual(obj.dt, dt)
502 516
 
503 517
         data = serializers.serialize('json', [Event(dt=dt)])
504  
-        self.assertIn('"fields": {"dt": "2011-09-01T13:20:30"}', data)
  518
+        self.assert_json_contains_datetime(data, "2011-09-01T13:20:30")
505 519
         obj = next(serializers.deserialize('json', data)).object
506 520
         self.assertEqual(obj.dt, dt)
507 521
 
508 522
         data = serializers.serialize('xml', [Event(dt=dt)])
509  
-        self.assertIn('<field type="DateTimeField" name="dt">2011-09-01T13:20:30</field>', data)
  523
+        self.assert_xml_contains_datetime(data, "2011-09-01T13:20:30")
510 524
         obj = next(serializers.deserialize('xml', data)).object
511 525
         self.assertEqual(obj.dt, dt)
512 526
 
513 527
         if 'yaml' in serializers.get_serializer_formats():
514 528
             data = serializers.serialize('yaml', [Event(dt=dt)])
515  
-            self.assertIn("- fields: {dt: !!timestamp '2011-09-01 13:20:30'}", data)
  529
+            self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30")
516 530
             obj = next(serializers.deserialize('yaml', data)).object
517 531
             self.assertEqual(obj.dt, dt)
518 532
 
@@ -520,23 +534,23 @@ def test_naive_datetime_with_microsecond(self):
520 534
         dt = datetime.datetime(2011, 9, 1, 13, 20, 30, 405060)
521 535
 
522 536
         data = serializers.serialize('python', [Event(dt=dt)])
523  
-        self.assertEqual(data[0]['fields']['dt'], dt)
  537
+        self.assert_python_contains_datetime(data, dt)
524 538
         obj = next(serializers.deserialize('python', data)).object
525 539
         self.assertEqual(obj.dt, dt)
526 540
 
527 541
         data = serializers.serialize('json', [Event(dt=dt)])
528  
-        self.assertIn('"fields": {"dt": "2011-09-01T13:20:30.405"}', data)
  542
+        self.assert_json_contains_datetime(data, "2011-09-01T13:20:30.405")
529 543
         obj = next(serializers.deserialize('json', data)).object
530 544
         self.assertEqual(obj.dt, dt.replace(microsecond=405000))
531 545
 
532 546
         data = serializers.serialize('xml', [Event(dt=dt)])
533  
-        self.assertIn('<field type="DateTimeField" name="dt">2011-09-01T13:20:30.405060</field>', data)
  547
+        self.assert_xml_contains_datetime(data, "2011-09-01T13:20:30.405060")
534 548
         obj = next(serializers.deserialize('xml', data)).object
535 549
         self.assertEqual(obj.dt, dt)
536 550
 
537 551
         if 'yaml' in serializers.get_serializer_formats():
538 552
             data = serializers.serialize('yaml', [Event(dt=dt)])
539  
-            self.assertIn("- fields: {dt: !!timestamp '2011-09-01 13:20:30.405060'}", data)
  553
+            self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30.405060")
540 554
             obj = next(serializers.deserialize('yaml', data)).object
541 555
             self.assertEqual(obj.dt, dt)
542 556
 
@@ -544,23 +558,23 @@ def test_aware_datetime_with_microsecond(self):
544 558
         dt = datetime.datetime(2011, 9, 1, 17, 20, 30, 405060, tzinfo=ICT)
545 559
 
546 560
         data = serializers.serialize('python', [Event(dt=dt)])
547  
-        self.assertEqual(data[0]['fields']['dt'], dt)
  561
+        self.assert_python_contains_datetime(data, dt)
548 562
         obj = next(serializers.deserialize('python', data)).object
549 563
         self.assertEqual(obj.dt, dt)
550 564
 
551 565
         data = serializers.serialize('json', [Event(dt=dt)])
552  
-        self.assertIn('"fields": {"dt": "2011-09-01T17:20:30.405+07:00"}', data)
  566
+        self.assert_json_contains_datetime(data, "2011-09-01T17:20:30.405+07:00")
553 567
         obj = next(serializers.deserialize('json', data)).object
554 568
         self.assertEqual(obj.dt, dt.replace(microsecond=405000))
555 569
 
556 570
         data = serializers.serialize('xml', [Event(dt=dt)])
557  
-        self.assertIn('<field type="DateTimeField" name="dt">2011-09-01T17:20:30.405060+07:00</field>', data)
  571
+        self.assert_xml_contains_datetime(data, "2011-09-01T17:20:30.405060+07:00")
558 572
         obj = next(serializers.deserialize('xml', data)).object
559 573
         self.assertEqual(obj.dt, dt)
560 574
 
561 575
         if 'yaml' in serializers.get_serializer_formats():
562 576
             data = serializers.serialize('yaml', [Event(dt=dt)])
563  
-            self.assertIn("- fields: {dt: !!timestamp '2011-09-01 17:20:30.405060+07:00'}", data)
  577
+            self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30.405060+07:00")
564 578
             obj = next(serializers.deserialize('yaml', data)).object
565 579
             self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
566 580
 
@@ -568,23 +582,23 @@ def test_aware_datetime_in_utc(self):
568 582
         dt = datetime.datetime(2011, 9, 1, 10, 20, 30, tzinfo=UTC)
569 583
 
570 584
         data = serializers.serialize('python', [Event(dt=dt)])
571  
-        self.assertEqual(data[0]['fields']['dt'], dt)
  585
+        self.assert_python_contains_datetime(data, dt)
572 586
         obj = next(serializers.deserialize('python', data)).object
573 587
         self.assertEqual(obj.dt, dt)
574 588
 
575 589
         data = serializers.serialize('json', [Event(dt=dt)])
576  
-        self.assertIn('"fields": {"dt": "2011-09-01T10:20:30Z"}', data)
  590
+        self.assert_json_contains_datetime(data, "2011-09-01T10:20:30Z")
577 591
         obj = next(serializers.deserialize('json', data)).object
578 592
         self.assertEqual(obj.dt, dt)
579 593
 
580 594
         data = serializers.serialize('xml', [Event(dt=dt)])
581  
-        self.assertIn('<field type="DateTimeField" name="dt">2011-09-01T10:20:30+00:00</field>', data)
  595
+        self.assert_xml_contains_datetime(data, "2011-09-01T10:20:30+00:00")
582 596
         obj = next(serializers.deserialize('xml', data)).object
583 597
         self.assertEqual(obj.dt, dt)
584 598
 
585 599
         if 'yaml' in serializers.get_serializer_formats():
586 600
             data = serializers.serialize('yaml', [Event(dt=dt)])
587  
-            self.assertIn("- fields: {dt: !!timestamp '2011-09-01 10:20:30+00:00'}", data)
  601
+            self.assert_yaml_contains_datetime(data, "2011-09-01 10:20:30+00:00")
588 602
             obj = next(serializers.deserialize('yaml', data)).object
589 603
             self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
590 604
 
@@ -592,23 +606,23 @@ def test_aware_datetime_in_local_timezone(self):
592 606
         dt = datetime.datetime(2011, 9, 1, 13, 20, 30, tzinfo=EAT)
593 607
 
594 608
         data = serializers.serialize('python', [Event(dt=dt)])
595  
-        self.assertEqual(data[0]['fields']['dt'], dt)
  609
+        self.assert_python_contains_datetime(data, dt)
596 610
         obj = next(serializers.deserialize('python', data)).object
597 611
         self.assertEqual(obj.dt, dt)
598 612
 
599 613
         data = serializers.serialize('json', [Event(dt=dt)])
600  
-        self.assertIn('"fields": {"dt": "2011-09-01T13:20:30+03:00"}', data)
  614
+        self.assert_json_contains_datetime(data, "2011-09-01T13:20:30+03:00")
601 615
         obj = next(serializers.deserialize('json', data)).object
602 616
         self.assertEqual(obj.dt, dt)
603 617
 
604 618
         data = serializers.serialize('xml', [Event(dt=dt)])
605  
-        self.assertIn('<field type="DateTimeField" name="dt">2011-09-01T13:20:30+03:00</field>', data)
  619
+        self.assert_xml_contains_datetime(data, "2011-09-01T13:20:30+03:00")
606 620
         obj = next(serializers.deserialize('xml', data)).object
607 621
         self.assertEqual(obj.dt, dt)
608 622
 
609 623
         if 'yaml' in serializers.get_serializer_formats():
610 624
             data = serializers.serialize('yaml', [Event(dt=dt)])
611  
-            self.assertIn("- fields: {dt: !!timestamp '2011-09-01 13:20:30+03:00'}", data)
  625
+            self.assert_yaml_contains_datetime(data, "2011-09-01 13:20:30+03:00")
612 626
             obj = next(serializers.deserialize('yaml', data)).object
613 627
             self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
614 628
 
@@ -616,23 +630,23 @@ def test_aware_datetime_in_other_timezone(self):
616 630
         dt = datetime.datetime(2011, 9, 1, 17, 20, 30, tzinfo=ICT)
617 631
 
618 632
         data = serializers.serialize('python', [Event(dt=dt)])
619  
-        self.assertEqual(data[0]['fields']['dt'], dt)
  633
+        self.assert_python_contains_datetime(data, dt)
620 634
         obj = next(serializers.deserialize('python', data)).object
621 635
         self.assertEqual(obj.dt, dt)
622 636
 
623 637
         data = serializers.serialize('json', [Event(dt=dt)])
624  
-        self.assertIn('"fields": {"dt": "2011-09-01T17:20:30+07:00"}', data)
  638
+        self.assert_json_contains_datetime(data, "2011-09-01T17:20:30+07:00")
625 639
         obj = next(serializers.deserialize('json', data)).object
626 640
         self.assertEqual(obj.dt, dt)
627 641
 
628 642
         data = serializers.serialize('xml', [Event(dt=dt)])
629  
-        self.assertIn('<field type="DateTimeField" name="dt">2011-09-01T17:20:30+07:00</field>', data)
  643
+        self.assert_xml_contains_datetime(data, "2011-09-01T17:20:30+07:00")
630 644
         obj = next(serializers.deserialize('xml', data)).object
631 645
         self.assertEqual(obj.dt, dt)
632 646
 
633 647
         if 'yaml' in serializers.get_serializer_formats():
634 648
             data = serializers.serialize('yaml', [Event(dt=dt)])
635  
-            self.assertIn("- fields: {dt: !!timestamp '2011-09-01 17:20:30+07:00'}", data)
  649
+            self.assert_yaml_contains_datetime(data, "2011-09-01 17:20:30+07:00")
636 650
             obj = next(serializers.deserialize('yaml', data)).object
637 651
             self.assertEqual(obj.dt.replace(tzinfo=UTC), dt)
638 652
 

0 notes on commit 6fd3216

Please sign in to comment.
Something went wrong with that request. Please try again.