31
31
from simplefix import FixMessage
32
32
33
33
34
+ # NOTE: RHEL6 ships with Python 2.6, which is increasingly difficult to
35
+ # support as both 2.7 and 3.x have added numerous features that seem like
36
+ # they've been around forever, but actually aren't in 2.6.
37
+ #
38
+ # While in a few cases, I try to monkey-patch stuff up to 2.7-equivalent,
39
+ # in a lot of the test code, I just skip the tests if they're run on
40
+ # a 2.6 interpreter -- or I would but unittest in 2.6 doesn't understand
41
+ # skipping, so they just pass without testing anything.
42
+ #
43
+ # This constant is used to check the version and alter behaviour to suit.
34
44
VERSION = (sys .version_info [0 ] * 10 ) + sys .version_info [1 ]
35
45
36
46
@@ -58,7 +68,8 @@ def test_string_with_equals(self):
58
68
59
69
def test_string_without_equals (self ):
60
70
"""Test field set with string not containing equals sign"""
61
- if VERSION == 26 : return
71
+ if VERSION == 26 :
72
+ return
62
73
63
74
msg = FixMessage ()
64
75
with self .assertRaises (ValueError ):
@@ -67,7 +78,8 @@ def test_string_without_equals(self):
67
78
68
79
def test_string_with_bad_tag (self ):
69
80
"""Test field set with bad tag in tag=value string"""
70
- if VERSION == 26 : return
81
+ if VERSION == 26 :
82
+ return
71
83
72
84
msg = FixMessage ()
73
85
with self .assertRaises (ValueError ):
@@ -101,7 +113,8 @@ def test_set_session_version(self):
101
113
102
114
def test_get_repeating (self ):
103
115
"""Test retrieval of repeating field's value"""
104
- if VERSION == 26 : return
116
+ if VERSION == 26 :
117
+ return
105
118
106
119
pkt = FixMessage ()
107
120
pkt .append_pair (42 , "a" )
@@ -138,7 +151,8 @@ def test_raw_msg_type(self):
138
151
139
152
def test_empty_message (self ):
140
153
"""Test encoding of empty message"""
141
- if VERSION == 26 : return
154
+ if VERSION == 26 :
155
+ return
142
156
143
157
msg = FixMessage ()
144
158
with self .assertRaises (ValueError ):
@@ -147,7 +161,8 @@ def test_empty_message(self):
147
161
148
162
def test_encode_no_35 (self ):
149
163
"""Test encoding without MessageType(35) field"""
150
- if VERSION == 26 : return
164
+ if VERSION == 26 :
165
+ return
151
166
152
167
msg = FixMessage ()
153
168
msg .append_pair (8 , "FIX.4.2" )
@@ -157,7 +172,8 @@ def test_encode_no_35(self):
157
172
158
173
def test_encode_no_8 (self ):
159
174
"""Test encoding without BeginString(8) field"""
160
- if VERSION == 26 : return
175
+ if VERSION == 26 :
176
+ return
161
177
162
178
msg = FixMessage ()
163
179
msg .append_pair (35 , "D" )
@@ -318,7 +334,8 @@ def test_time_seconds_only(self):
318
334
319
335
def test_time_bad_precision (self ):
320
336
"""Test bad time precision values"""
321
- if VERSION == 26 : return
337
+ if VERSION == 26 :
338
+ return
322
339
323
340
msg = FixMessage ()
324
341
t = 1484581872.933458
@@ -392,7 +409,8 @@ def test_utcts_seconds_only(self):
392
409
393
410
def test_utcts_bad_precision (self ):
394
411
"""Test UTCTimestamp bad time precision values"""
395
- if VERSION == 26 : return
412
+ if VERSION == 26 :
413
+ return
396
414
397
415
msg = FixMessage ()
398
416
t = 1484581872.933458
@@ -448,7 +466,8 @@ def test_utcto_seconds_only(self):
448
466
449
467
def test_utcto_bad_precision (self ):
450
468
"""Test UTCTimeOnly bad time precision values"""
451
- if VERSION == 26 : return
469
+ if VERSION == 26 :
470
+ return
452
471
453
472
msg = FixMessage ()
454
473
t = 1484581872.933458
@@ -475,7 +494,8 @@ def test_utcto_parts_15_51_12_933_458(self):
475
494
return
476
495
477
496
def test_utcto_parts_bad_hour (self ):
478
- if VERSION == 26 : return
497
+ if VERSION == 26 :
498
+ return
479
499
480
500
msg = FixMessage ()
481
501
with self .assertRaises (ValueError ):
@@ -487,7 +507,8 @@ def test_utcto_parts_bad_hour(self):
487
507
return
488
508
489
509
def test_utcto_parts_bad_minute (self ):
490
- if VERSION == 26 : return
510
+ if VERSION == 26 :
511
+ return
491
512
492
513
msg = FixMessage ()
493
514
with self .assertRaises (ValueError ):
@@ -499,7 +520,8 @@ def test_utcto_parts_bad_minute(self):
499
520
return
500
521
501
522
def test_utcto_parts_bad_seconds (self ):
502
- if VERSION == 26 : return
523
+ if VERSION == 26 :
524
+ return
503
525
504
526
msg = FixMessage ()
505
527
with self .assertRaises (ValueError ):
@@ -511,7 +533,8 @@ def test_utcto_parts_bad_seconds(self):
511
533
return
512
534
513
535
def test_utcto_parts_bad_ms (self ):
514
- if VERSION == 26 : return
536
+ if VERSION == 26 :
537
+ return
515
538
516
539
msg = FixMessage ()
517
540
with self .assertRaises (ValueError ):
@@ -523,7 +546,8 @@ def test_utcto_parts_bad_ms(self):
523
546
return
524
547
525
548
def test_utcto_parts_bad_us (self ):
526
- if VERSION == 26 : return
549
+ if VERSION == 26 :
550
+ return
527
551
528
552
msg = FixMessage ()
529
553
with self .assertRaises (ValueError ):
@@ -535,7 +559,8 @@ def test_utcto_parts_bad_us(self):
535
559
return
536
560
537
561
def test_offset_range (self ):
538
- if VERSION == 26 : return
562
+ if VERSION == 26 :
563
+ return
539
564
540
565
msg = FixMessage ()
541
566
with self .assertRaises (ValueError ):
@@ -582,6 +607,14 @@ def test_append_tzts_none(self):
582
607
msg .append_tz_timestamp (1253 , None )
583
608
return
584
609
610
+ @staticmethod
611
+ def calculate_tz_offset (t ):
612
+ local = datetime .datetime .fromtimestamp (t )
613
+ utc = datetime .datetime .utcfromtimestamp (t )
614
+ td = local - utc
615
+ offset = int (((td .days * 86400 ) + td .seconds ) / 60 )
616
+ return offset
617
+
585
618
def test_append_tzts_float (self ):
586
619
msg = FixMessage ()
587
620
t = 1484581872.933458
@@ -592,9 +625,7 @@ def test_append_tzts_float(self):
592
625
(test .tm_year , test .tm_mon , test .tm_mday ,
593
626
test .tm_hour , test .tm_min , test .tm_sec ,
594
627
int ((t - int (t )) * 1000 ))
595
- offset = int ((datetime .datetime .fromtimestamp (t ) -
596
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
597
- / 60 )
628
+ offset = self .calculate_tz_offset (t )
598
629
if offset == 0 :
599
630
s += "Z"
600
631
else :
@@ -619,9 +650,7 @@ def test_append_tzts_datetime(self):
619
650
(test .tm_year , test .tm_mon , test .tm_mday ,
620
651
test .tm_hour , test .tm_min , test .tm_sec ,
621
652
int ((t - int (t )) * 1000 ))
622
- offset = int ((datetime .datetime .fromtimestamp (t ) -
623
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
624
- / 60 )
653
+ offset = self .calculate_tz_offset (t )
625
654
if offset == 0 :
626
655
s += "Z"
627
656
else :
@@ -646,9 +675,7 @@ def test_tzts_microseconds(self):
646
675
(test .tm_year , test .tm_mon , test .tm_mday ,
647
676
test .tm_hour , test .tm_min , test .tm_sec ,
648
677
int ((t % 1 ) * 1e6 ))
649
- offset = int ((datetime .datetime .fromtimestamp (t ) -
650
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
651
- / 60 )
678
+ offset = self .calculate_tz_offset (t )
652
679
if offset == 0 :
653
680
s += "Z"
654
681
else :
@@ -672,9 +699,7 @@ def test_tzts_seconds_only(self):
672
699
s = "%04u%02u%02u-%02u:%02u:%02u" % \
673
700
(test .tm_year , test .tm_mon , test .tm_mday ,
674
701
test .tm_hour , test .tm_min , test .tm_sec )
675
- offset = int ((datetime .datetime .fromtimestamp (t ) -
676
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
677
- / 60 )
702
+ offset = self .calculate_tz_offset (t )
678
703
if offset == 0 :
679
704
s += "Z"
680
705
else :
@@ -690,7 +715,8 @@ def test_tzts_seconds_only(self):
690
715
691
716
def test_tzts_bad_precision (self ):
692
717
"""Test bad TZTimestamp precision value"""
693
- if VERSION == 26 : return
718
+ if VERSION == 26 :
719
+ return
694
720
695
721
msg = FixMessage ()
696
722
t = 1484581872.933458
@@ -706,9 +732,7 @@ def test_tzto_datetime(self):
706
732
test = time .localtime (t )
707
733
s = "%02u:%02u:%02u.%03u" % \
708
734
(test .tm_hour , test .tm_min , test .tm_sec , int ((t % 1 ) * 1e3 ))
709
- offset = int ((datetime .datetime .fromtimestamp (t ) -
710
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
711
- / 60 )
735
+ offset = self .calculate_tz_offset (t )
712
736
if offset == 0 :
713
737
s += "Z"
714
738
else :
@@ -730,9 +754,7 @@ def test_tzto_minutes(self):
730
754
731
755
test = time .localtime (t )
732
756
s = "%02u:%02u" % (test .tm_hour , test .tm_min )
733
- offset = int ((datetime .datetime .fromtimestamp (t ) -
734
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
735
- / 60 )
757
+ offset = self .calculate_tz_offset (t )
736
758
if offset == 0 :
737
759
s += "Z"
738
760
else :
@@ -755,9 +777,7 @@ def test_tzto_microseconds(self):
755
777
test = time .localtime (t )
756
778
s = "%02u:%02u:%02u.%06u" % \
757
779
(test .tm_hour , test .tm_min , test .tm_sec , int ((t % 1 ) * 1e6 ))
758
- offset = int ((datetime .datetime .fromtimestamp (t ) -
759
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
760
- / 60 )
780
+ offset = self .calculate_tz_offset (t )
761
781
if offset == 0 :
762
782
s += "Z"
763
783
else :
@@ -780,9 +800,7 @@ def test_tzto_seconds_only(self):
780
800
test = time .localtime (t )
781
801
s = "%02u:%02u:%02u" % \
782
802
(test .tm_hour , test .tm_min , test .tm_sec )
783
- offset = int ((datetime .datetime .fromtimestamp (t ) -
784
- datetime .datetime .utcfromtimestamp (t )).total_seconds ()
785
- / 60 )
803
+ offset = self .calculate_tz_offset (t )
786
804
if offset == 0 :
787
805
s += "Z"
788
806
else :
@@ -798,7 +816,8 @@ def test_tzto_seconds_only(self):
798
816
799
817
def test_tzto_bad_precision (self ):
800
818
"""Test bad TZTimeOnly precision value"""
801
- if VERSION == 26 : return
819
+ if VERSION == 26 :
820
+ return
802
821
803
822
msg = FixMessage ()
804
823
t = 1484581872.933458
@@ -855,7 +874,8 @@ def test_tzto_parts_15_51_12_933_458_150(self):
855
874
856
875
def test_tzto_parts_bad_hour (self ):
857
876
"""Test TZTimeOnly with out-of-range hour components"""
858
- if VERSION == 26 : return
877
+ if VERSION == 26 :
878
+ return
859
879
860
880
msg = FixMessage ()
861
881
with self .assertRaises (ValueError ):
@@ -868,7 +888,8 @@ def test_tzto_parts_bad_hour(self):
868
888
869
889
def test_tzto_parts_bad_minute (self ):
870
890
"""Test TZTimeOnly with out-of-range minute components"""
871
- if VERSION == 26 : return
891
+ if VERSION == 26 :
892
+ return
872
893
873
894
msg = FixMessage ()
874
895
with self .assertRaises (ValueError ):
@@ -881,7 +902,8 @@ def test_tzto_parts_bad_minute(self):
881
902
882
903
def test_tzto_parts_bad_seconds (self ):
883
904
"""Test TZTimeOnly with out-of-range seconds components"""
884
- if VERSION == 26 : return
905
+ if VERSION == 26 :
906
+ return
885
907
886
908
msg = FixMessage ()
887
909
with self .assertRaises (ValueError ):
@@ -894,7 +916,8 @@ def test_tzto_parts_bad_seconds(self):
894
916
895
917
def test_tzto_parts_bad_ms (self ):
896
918
"""Test TZTimeOnly with out-of-range milliseconds components"""
897
- if VERSION == 26 : return
919
+ if VERSION == 26 :
920
+ return
898
921
899
922
msg = FixMessage ()
900
923
with self .assertRaises (ValueError ):
@@ -907,7 +930,8 @@ def test_tzto_parts_bad_ms(self):
907
930
908
931
def test_tzto_parts_bad_us (self ):
909
932
"""Test TZTimeOnly with out-of-range microseconds components"""
910
- if VERSION == 26 : return
933
+ if VERSION == 26 :
934
+ return
911
935
912
936
msg = FixMessage ()
913
937
with self .assertRaises (ValueError ):
@@ -943,7 +967,8 @@ def test_strings(self):
943
967
944
968
def test_contains (self ):
945
969
"""Test use of 'in' and 'not in' operators"""
946
- if VERSION == 26 : return
970
+ if VERSION == 26 :
971
+ return
947
972
948
973
msg = FixMessage ()
949
974
msg .append_strings (["8=FIX.4.4" , "35=0" ])
@@ -955,7 +980,8 @@ def test_contains(self):
955
980
956
981
def test_none_value (self ):
957
982
"""Test encoding of None value"""
958
- if VERSION == 26 : return
983
+ if VERSION == 26 :
984
+ return
959
985
960
986
msg = FixMessage ()
961
987
msg .append_pair (99999 , None )
0 commit comments