-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathtest_fields.py
More file actions
1202 lines (1014 loc) · 41.8 KB
/
test_fields.py
File metadata and controls
1202 lines (1014 loc) · 41.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tests for classes extending Field.
"""
# pylint: disable=protected-access
from contextlib import contextmanager
import datetime as dt
import itertools
import math
import textwrap
import unittest
import warnings
from unittest.mock import Mock
import ddt
from lxml import etree
import pytz
from xblock.core import XBlock, Scope
from xblock.field_data import DictFieldData
from xblock.fields import (
Any, Boolean, Dict, Field, Float, Integer, List, Set, String, XMLString, DateTime, Reference, ReferenceList,
ScopeIds, Sentinel, UNIQUE_ID, scope_key, Date, Timedelta, RelativeTime, ScoreField, ListScoreField
)
from xblock.scorable import Score
from xblock.test.tools import TestRuntime, make_scope_ids_for_testing
class FieldTest(unittest.TestCase):
""" Base test class for Fields. """
FIELD_TO_TEST = Mock()
def get_block(self, enforce_type):
"""
Create a block with a field 'field_x' that is of type FIELD_TO_TEST.
"""
class TestBlock(XBlock):
"""
Block for testing
"""
field_x = self.FIELD_TO_TEST(enforce_type=enforce_type)
runtime = TestRuntime(services={'field-data': DictFieldData({})})
return TestBlock(runtime, scope_ids=make_scope_ids_for_testing())
def set_and_get_field(self, arg, enforce_type):
"""
Set the field to arg in a Block, get it and return the set value.
"""
block = self.get_block(enforce_type)
block.field_x = arg
return block.field_x
@contextmanager
def assertDeprecationWarning(self, count=1):
"""Asserts that the contained code raises `count` deprecation warnings"""
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always", DeprecationWarning)
yield
self.assertEqual(count, sum(
1 for warning in caught
if issubclass(warning.category, DeprecationWarning)
))
def assertJSONOrSetGetEquals(self, expected, arg):
"""
Asserts the result of field.from_json and of setting field.
"""
# from_json(arg) -> expected
self.assertEqual(expected, self.FIELD_TO_TEST().from_json(arg))
# set+get with enforce_type arg -> expected
self.assertEqual(expected, self.set_and_get_field(arg, True))
def assertJSONOrSetEquals(self, expected, arg):
"""
Asserts the result of field.from_json and of setting field.
"""
self.assertJSONOrSetGetEquals(expected, arg)
# set+get without enforce_type arg -> arg
# provoking a warning unless arg == expected
count = 0 if arg == expected else 1
with self.assertDeprecationWarning(count):
self.assertEqual(arg, self.set_and_get_field(arg, False))
def assertToJSONEquals(self, expected, arg):
"""
Assert that serialization of `arg` to JSON equals `expected`.
"""
self.assertEqual(expected, self.FIELD_TO_TEST().to_json(arg))
def assertJSONOrSetValueError(self, arg):
"""
Asserts that field.from_json or setting the field throws a ValueError
for the supplied value.
"""
# from_json and set+get with enforce_type -> ValueError
with self.assertRaises(ValueError):
self.FIELD_TO_TEST().from_json(arg)
with self.assertRaises(ValueError):
self.set_and_get_field(arg, True)
# set+get without enforce_type -> warning
with self.assertDeprecationWarning():
self.set_and_get_field(arg, False)
def assertJSONOrSetTypeError(self, arg):
"""
Asserts that field.from_json or setting the field throws a TypeError
for the supplied value.
"""
# from_json and set+get with enforce_type -> TypeError
with self.assertRaises(TypeError):
self.FIELD_TO_TEST().from_json(arg)
with self.assertRaises(TypeError):
self.set_and_get_field(arg, True)
# set+get without enforce_type -> warning
with self.assertDeprecationWarning():
self.set_and_get_field(arg, False)
class IntegerTest(FieldTest):
"""
Tests the Integer Field.
"""
FIELD_TO_TEST = Integer
def test_integer(self):
self.assertJSONOrSetEquals(5, '5')
self.assertJSONOrSetEquals(0, '0')
self.assertJSONOrSetEquals(-1023, '-1023')
self.assertJSONOrSetEquals(7, 7)
self.assertJSONOrSetEquals(0, False)
self.assertJSONOrSetEquals(1, True)
def test_float_converts(self):
self.assertJSONOrSetEquals(1, 1.023)
self.assertJSONOrSetEquals(-3, -3.8)
def test_none(self):
self.assertJSONOrSetEquals(None, None)
self.assertJSONOrSetEquals(None, '')
def test_error(self):
self.assertJSONOrSetValueError('abc')
self.assertJSONOrSetValueError('[1]')
self.assertJSONOrSetValueError('1.023')
self.assertJSONOrSetTypeError([])
self.assertJSONOrSetTypeError({})
class FloatTest(FieldTest):
"""
Tests the Float Field.
"""
FIELD_TO_TEST = Float
def test_float(self):
self.assertJSONOrSetEquals(.23, '.23')
self.assertJSONOrSetEquals(5, '5')
self.assertJSONOrSetEquals(0, '0.0')
self.assertJSONOrSetEquals(-1023.22, '-1023.22')
self.assertJSONOrSetEquals(0, 0.0)
self.assertJSONOrSetEquals(4, 4)
self.assertJSONOrSetEquals(-0.23, -0.23)
self.assertJSONOrSetEquals(0, False)
self.assertJSONOrSetEquals(1, True)
def test_none(self):
self.assertJSONOrSetEquals(None, None)
self.assertJSONOrSetEquals(None, '')
def test_error(self):
self.assertJSONOrSetValueError('abc')
self.assertJSONOrSetValueError('[1]')
self.assertJSONOrSetTypeError([])
self.assertJSONOrSetTypeError({})
class BooleanTest(FieldTest):
"""
Tests the Boolean Field.
"""
FIELD_TO_TEST = Boolean
def test_false(self):
self.assertJSONOrSetEquals(False, "false")
self.assertJSONOrSetEquals(False, "False")
self.assertJSONOrSetEquals(False, "")
self.assertJSONOrSetEquals(False, "any other string")
self.assertJSONOrSetEquals(False, False)
def test_true(self):
self.assertJSONOrSetEquals(True, "true")
self.assertJSONOrSetEquals(True, "TruE")
self.assertJSONOrSetEquals(True, True)
def test_none(self):
self.assertJSONOrSetEquals(False, None)
def test_everything_converts_to_bool(self):
self.assertJSONOrSetEquals(True, 123)
self.assertJSONOrSetEquals(True, ['a'])
self.assertJSONOrSetEquals(False, [])
class StringTest(FieldTest):
"""
Tests the String Field.
"""
FIELD_TO_TEST = String
def test_json_equals(self):
self.assertJSONOrSetEquals("false", "false")
self.assertJSONOrSetEquals("abba", "abba")
self.assertJSONOrSetEquals('"abba"', '"abba"')
self.assertJSONOrSetEquals('', '')
def test_none(self):
self.assertJSONOrSetEquals(None, None)
def test_error(self):
self.assertJSONOrSetTypeError(['a'])
self.assertJSONOrSetTypeError(1.023)
self.assertJSONOrSetTypeError(3)
self.assertJSONOrSetTypeError([1])
self.assertJSONOrSetTypeError([])
self.assertJSONOrSetTypeError({})
def test_control_characters_filtered(self):
self.assertJSONOrSetGetEquals('', '\v')
self.assertJSONOrSetGetEquals('', b'\v')
with self.assertRaises(AssertionError):
self.assertJSONOrSetGetEquals('\v', b'')
with self.assertRaises(AssertionError):
self.assertJSONOrSetGetEquals('\v', '')
self.assertJSONOrSetGetEquals('\n\r\t', '\n\v\r\b\t')
@ddt.ddt
class XMLStringTest(FieldTest):
"""
Tests the XMLString Field.
"""
FIELD_TO_TEST = XMLString
@ddt.data(
'<abc>Hello</abc>',
'<abc attr="yes">Hello</abc>',
'<xml/>',
b'<bytes/>',
b'<unicode>\xc8\x88</unicode>',
None
)
def test_json_equals(self, input_text):
xml_string = self.FIELD_TO_TEST(enforce_type=True)
self.assertEqual(xml_string.to_json(input_text), input_text)
@ddt.data(
'text',
'<unfinished_tag',
'<xml unquoted_attr=3/>',
'<xml unclosed_attr="3/>',
'<open>with text',
'<xml/>trailing text',
'<open>text</close>',
'<open>',
b'<open>',
b'<invalid_utf8_bytes char="\xe1"/>',
)
def test_bad_xml(self, input_text):
xml_string = self.FIELD_TO_TEST(enforce_type=True)
self.assertRaises(etree.XMLSyntaxError, xml_string.to_json, input_text)
unchecked_xml_string = self.FIELD_TO_TEST(enforce_type=False)
self.assertEqual(unchecked_xml_string.to_json(input_text), input_text)
class DateTest(unittest.TestCase):
"""Tests JSON conversion and type enforcement for Date fields."""
date = Date()
def compare_dates(self, dt1, dt2, expected_delta):
"""Assert that two datetime objects differ by the expected timedelta."""
assert (dt1 - dt2) == expected_delta, (((str(dt1) + "-") + str(dt2)) + "!=") + str(expected_delta)
def test_from_json(self):
"""Test conversion from iso compatible date strings to struct_time"""
self.compare_dates(
DateTest.date.from_json("2013-01-01"), DateTest.date.from_json("2012-12-31"), dt.timedelta(days=1)
)
self.compare_dates(
DateTest.date.from_json("2013-01-01T00"),
DateTest.date.from_json("2012-12-31T23"),
dt.timedelta(hours=1),
)
self.compare_dates(
DateTest.date.from_json("2013-01-01T00:00"),
DateTest.date.from_json("2012-12-31T23:59"),
dt.timedelta(minutes=1),
)
self.compare_dates(
DateTest.date.from_json("2013-01-01T00:00:00"),
DateTest.date.from_json("2012-12-31T23:59:59"),
dt.timedelta(seconds=1),
)
self.compare_dates(
DateTest.date.from_json("2013-01-01T00:00:00Z"),
DateTest.date.from_json("2012-12-31T23:59:59Z"),
dt.timedelta(seconds=1),
)
self.compare_dates(
DateTest.date.from_json("2012-12-31T23:00:01-01:00"),
DateTest.date.from_json("2013-01-01T00:00:00+01:00"),
dt.timedelta(hours=1, seconds=1),
)
def test_enforce_type(self):
"""Test enforcement of input types for Date field."""
assert DateTest.date.enforce_type(None) is None
assert DateTest.date.enforce_type("") is None
assert DateTest.date.enforce_type("2012-12-31T23:00:01") == dt.datetime(2012, 12, 31, 23, 0, 1, tzinfo=pytz.UTC)
assert DateTest.date.enforce_type(1234567890000) == dt.datetime(2009, 2, 13, 23, 31, 30, tzinfo=pytz.UTC)
assert DateTest.date.enforce_type(dt.datetime(2014, 5, 9, 21, 1, 27, tzinfo=pytz.UTC)) == dt.datetime(
2014, 5, 9, 21, 1, 27, tzinfo=pytz.UTC
)
with self.assertRaises(TypeError):
DateTest.date.enforce_type([1])
def test_return_none(self):
"""Test that invalid or empty inputs return None for Date field."""
assert DateTest.date.from_json("") is None
assert DateTest.date.from_json(None) is None
with self.assertRaises(TypeError):
DateTest.date.from_json(["unknown value"])
def test_old_due_date_format(self):
"""Test parsing of non-standard human-readable date formats."""
current = dt.datetime.today()
assert dt.datetime(current.year, 3, 12, 12, tzinfo=pytz.UTC) == DateTest.date.from_json("March 12 12:00")
assert dt.datetime(current.year, 12, 4, 16, 30, tzinfo=pytz.UTC) == DateTest.date.from_json("December 4 16:30")
assert DateTest.date.from_json("12 12:00") is None
def test_non_std_from_json(self):
"""
Test the non-standard args being passed to from_json
"""
now = dt.datetime.now(pytz.UTC)
delta = now - dt.datetime.fromtimestamp(0, pytz.UTC)
assert DateTest.date.from_json(delta.total_seconds() * 1000) == now
yesterday = dt.datetime.now(pytz.UTC) - dt.timedelta(days=-1)
assert DateTest.date.from_json(yesterday) == yesterday
def test_to_json(self):
"""
Test converting time reprs to iso dates
"""
assert (
DateTest.date.to_json(
dt.datetime.strptime("2012-12-31T23:59:59Z", "%Y-%m-%dT%H:%M:%SZ")
) == "2012-12-31T23:59:59Z"
)
assert DateTest.date.to_json(DateTest.date.from_json("2012-12-31T23:59:59Z")) == "2012-12-31T23:59:59Z"
assert (
DateTest.date.to_json(DateTest.date.from_json("2012-12-31T23:00:01-01:00")) == "2012-12-31T23:00:01-01:00"
)
with self.assertRaises(TypeError):
DateTest.date.to_json("2012-12-31T23:00:01-01:00")
@ddt.ddt
class DateTimeTest(FieldTest):
"""
Tests of the DateTime field.
"""
FIELD_TO_TEST = DateTime
def test_json_equals(self):
self.assertJSONOrSetEquals(
dt.datetime(2014, 4, 1, 2, 3, 4, 567890).replace(tzinfo=pytz.utc),
'2014-04-01T02:03:04.567890'
)
self.assertJSONOrSetEquals(
dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc),
'2014-04-01T02:03:04.000000'
)
self.assertJSONOrSetEquals(
dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc),
'2014-04-01T02:03:04Z'
)
self.assertJSONOrSetEquals(
dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc),
dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc)
)
self.assertJSONOrSetEquals(
dt.timedelta(days=1, seconds=1),
dt.timedelta(days=1, seconds=1),
)
self.assertJSONOrSetEquals(
dt.timedelta(days=1, seconds=1),
dt.timedelta(seconds=86401),
)
self.assertJSONOrSetEquals(
dt.timedelta(days=1, seconds=1),
86401,
)
self.assertJSONOrSetEquals(
dt.timedelta(days=-2, seconds=86399),
-86401,
)
self.assertJSONOrSetEquals(
dt.timedelta(days=-1, seconds=86399),
-1,
)
def test_serialize(self):
self.assertToJSONEquals(
'2014-04-01T02:03:04.567890',
dt.datetime(2014, 4, 1, 2, 3, 4, 567890).replace(tzinfo=pytz.utc)
)
self.assertToJSONEquals(
'2014-04-01T02:03:04.000000',
dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc)
)
self.assertToJSONEquals(
86401.0,
dt.timedelta(days=1, seconds=1),
)
self.assertToJSONEquals(
-1.0,
dt.timedelta(days=-1, seconds=86399),
)
self.assertToJSONEquals(
86401,
dt.timedelta(days=1, seconds=1),
)
self.assertToJSONEquals(
-1,
dt.timedelta(days=-1, seconds=86399),
)
@ddt.unpack
@ddt.data(
(
dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc),
dt.datetime(2014, 4, 1, 2, 3, 5)
),
(
dt.datetime(2014, 4, 1, 2, 3, 4),
dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc),
)
)
def test_naive(self, original, replacement):
"""
Make sure field comparison doesn't crash when comparing naive and non-naive datetimes.
"""
for enforce_type in (False, True):
block = self.get_block(enforce_type)
block.field_x = original
block.field_x = replacement
def test_none(self):
self.assertJSONOrSetEquals(None, None)
self.assertJSONOrSetEquals(None, '')
self.assertEqual(DateTime().to_json(None), None)
def test_error(self):
self.assertJSONOrSetTypeError(['a'])
def test_date_format_error(self):
with self.assertRaises(ValueError):
DateTime().from_json('invalid')
def test_serialize_error(self):
with self.assertRaises(TypeError):
DateTime().to_json('not a datetime')
class TimedeltaTest(unittest.TestCase):
"""Tests JSON conversion and type enforcement for Timedelta fields."""
delta = Timedelta()
def test_from_json(self):
"""Test conversion from string representations to timedelta objects."""
assert TimedeltaTest.delta.from_json("1 day 12 hours 59 minutes 59 seconds") == dt.timedelta(
days=1, hours=12, minutes=59, seconds=59
)
assert TimedeltaTest.delta.from_json("1 day 46799 seconds") == dt.timedelta(days=1, seconds=46799)
def test_enforce_type(self):
"""Test enforcement of input types for Timedelta field."""
assert TimedeltaTest.delta.enforce_type(None) is None
assert TimedeltaTest.delta.enforce_type(dt.timedelta(days=1, seconds=46799)) == dt.timedelta(
days=1, seconds=46799
)
assert TimedeltaTest.delta.enforce_type("1 day 46799 seconds") == dt.timedelta(days=1, seconds=46799)
with self.assertRaises(TypeError):
TimedeltaTest.delta.enforce_type([1])
def test_to_json(self):
"""Test converting timedelta objects to string representations."""
assert "1 days 46799 seconds" == TimedeltaTest.delta.to_json(
dt.timedelta(days=1, hours=12, minutes=59, seconds=59)
)
class RelativeTimeTest(unittest.TestCase):
"""Tests JSON conversion and type enforcement for RelativeTime fields."""
delta = RelativeTime()
def test_from_json(self):
"""Test conversion from string or numeric values to timedelta objects."""
assert RelativeTimeTest.delta.from_json("0:05:07") == dt.timedelta(seconds=307)
assert RelativeTimeTest.delta.from_json(100.0) == dt.timedelta(seconds=100)
assert RelativeTimeTest.delta.from_json(None) == dt.timedelta(seconds=0)
with self.assertRaises(TypeError):
RelativeTimeTest.delta.from_json(1234) # int
with self.assertRaises(ValueError):
RelativeTimeTest.delta.from_json("77:77:77")
def test_enforce_type(self):
"""Test enforcement of input types for RelativeTime field."""
assert RelativeTimeTest.delta.enforce_type(None) is None
assert RelativeTimeTest.delta.enforce_type(dt.timedelta(days=1, seconds=46799)) == dt.timedelta(
days=1, seconds=46799
)
assert RelativeTimeTest.delta.enforce_type("0:05:07") == dt.timedelta(seconds=307)
with self.assertRaises(TypeError):
RelativeTimeTest.delta.enforce_type([1])
def test_to_json(self):
"""Test converting timedelta objects to HH:MM:SS string format."""
assert "01:02:03" == RelativeTimeTest.delta.to_json(dt.timedelta(seconds=3723))
assert "00:00:00" == RelativeTimeTest.delta.to_json(None)
assert "00:01:40" == RelativeTimeTest.delta.to_json(100.0)
error_msg = "RelativeTime max value is 23:59:59=86400.0 seconds, but 90000.0 seconds is passed"
with self.assertRaisesRegex(ValueError, error_msg):
RelativeTimeTest.delta.to_json(dt.timedelta(seconds=90000))
with self.assertRaises(TypeError):
RelativeTimeTest.delta.to_json("123")
def test_str(self):
"""Test that RelativeTime outputs correct HH:MM:SS string representations."""
assert "01:02:03" == RelativeTimeTest.delta.to_json(dt.timedelta(seconds=3723))
assert "11:02:03" == RelativeTimeTest.delta.to_json(dt.timedelta(seconds=39723))
class AnyTest(FieldTest):
"""
Tests the Any Field.
"""
FIELD_TO_TEST = Any
def test_json_equals(self):
self.assertJSONOrSetEquals({'bar'}, {'bar'})
self.assertJSONOrSetEquals("abba", "abba")
self.assertJSONOrSetEquals('', '')
self.assertJSONOrSetEquals('3.2', '3.2')
self.assertJSONOrSetEquals(False, False)
self.assertJSONOrSetEquals([3, 4], [3, 4])
def test_none(self):
self.assertJSONOrSetEquals(None, None)
class ListTest(FieldTest):
"""
Tests the List Field.
"""
FIELD_TO_TEST = List
def test_json_equals(self):
self.assertJSONOrSetEquals([], [])
self.assertJSONOrSetEquals(['foo', 'bar'], ['foo', 'bar'])
self.assertJSONOrSetEquals([1, 3.4], [1, 3.4])
def test_none(self):
self.assertJSONOrSetEquals(None, None)
def test_error(self):
self.assertJSONOrSetTypeError('abc')
self.assertJSONOrSetTypeError('')
self.assertJSONOrSetTypeError('1.23')
self.assertJSONOrSetTypeError('true')
self.assertJSONOrSetTypeError(3.7)
self.assertJSONOrSetTypeError(True)
self.assertJSONOrSetTypeError({})
class SetTest(FieldTest):
"""
Tests the Set Field.
"""
FIELD_TO_TEST = Set
def test_json_equals(self):
self.assertJSONOrSetEquals(set(), set())
self.assertJSONOrSetEquals({'foo', 'bar'}, {'foo', 'bar'})
self.assertJSONOrSetEquals({'bar', 'foo'}, {'foo', 'bar'})
self.assertJSONOrSetEquals({1, 3.14}, {1, 3.14})
self.assertJSONOrSetEquals({1, 3.14}, {1, 3.14, 1}) # pylint: disable=duplicate-value
def test_hashable_converts(self):
self.assertJSONOrSetEquals({1, 3.4}, [1, 3.4])
self.assertJSONOrSetEquals({'a', 'b'}, 'ab')
self.assertJSONOrSetEquals({'k1', 'k2'}, {'k1': 1, 'k2': '2'})
def test_none(self):
self.assertJSONOrSetEquals(None, None)
def test_error(self):
self.assertJSONOrSetTypeError(42)
self.assertJSONOrSetTypeError(3.7)
self.assertJSONOrSetTypeError(True)
class ReferenceTest(FieldTest):
"""
Tests the Reference Field.
"""
FIELD_TO_TEST = Reference
def test_json_equals(self):
self.assertJSONOrSetEquals({'id': 'bar', 'usage': 'baz'}, {'id': 'bar', 'usage': 'baz'})
self.assertJSONOrSetEquals("i4x://myu/mycourse/problem/myproblem", "i4x://myu/mycourse/problem/myproblem")
self.assertJSONOrSetEquals('', '')
self.assertJSONOrSetEquals(3.2, 3.2)
self.assertJSONOrSetEquals(False, False)
self.assertJSONOrSetEquals([3, 4], [3, 4])
def test_none(self):
self.assertJSONOrSetEquals(None, None)
class ReferenceListTest(FieldTest):
"""
Tests the ReferenceList Field.
"""
FIELD_TO_TEST = ReferenceList
def test_json_equals(self):
self.assertJSONOrSetEquals([], [])
self.assertJSONOrSetEquals(['foo', 'bar'], ['foo', 'bar'])
self.assertJSONOrSetEquals([1, 3.4], [1, 3.4])
def test_none(self):
self.assertJSONOrSetEquals(None, None)
def test_error(self):
self.assertJSONOrSetTypeError('abc')
self.assertJSONOrSetTypeError('')
self.assertJSONOrSetTypeError('1.23')
self.assertJSONOrSetTypeError('true')
self.assertJSONOrSetTypeError(3.7)
self.assertJSONOrSetTypeError(True)
self.assertJSONOrSetTypeError({})
class DictTest(FieldTest):
"""
Tests the Dict Field.
"""
FIELD_TO_TEST = Dict
def test_json_equals(self):
self.assertJSONOrSetEquals({}, {})
self.assertJSONOrSetEquals({'a': 'b', 'c': 3}, {'a': 'b', 'c': 3})
def test_none(self):
self.assertJSONOrSetEquals(None, None)
def test_error(self):
self.assertJSONOrSetTypeError(['foo', 'bar'])
self.assertJSONOrSetTypeError([])
self.assertJSONOrSetTypeError('abc')
self.assertJSONOrSetTypeError('1.23')
self.assertJSONOrSetTypeError('true')
self.assertJSONOrSetTypeError(3.7)
self.assertJSONOrSetTypeError(True)
def test_field_name_defaults():
# Tests field display name default values
attempts = Integer()
attempts.__name__ = "max_problem_attempts"
assert attempts.display_name == 'max_problem_attempts'
class TestBlock(XBlock):
"""
Block for testing
"""
field_x = List()
assert TestBlock.field_x.display_name == "field_x"
def test_scope_key():
# Tests field display name default values
class TestBlock(XBlock):
"""
Block for testing
"""
field_x = List(scope=Scope.settings, name='')
settings_lst = List(scope=Scope.settings, name='')
uss_lst = List(scope=Scope.user_state_summary, name='')
user_lst = List(scope=Scope.user_state, name='')
pref_lst = List(scope=Scope.preferences, name='')
user_info_lst = List(scope=Scope.user_info, name='')
sids = make_scope_ids_for_testing(
user_id="_bob",
block_type="b.12#ob",
block_id="..",
)
field_data = DictFieldData({})
runtime = TestRuntime(Mock(), services={'field-data': field_data})
block = TestBlock(runtime, None, sids)
# Format: usage or block ID/field_name/user_id
for item, correct_key in [[TestBlock.field_x, "__..../field__x/NONE.NONE"],
[TestBlock.user_info_lst, "NONE.NONE/user__info__lst/____bob"],
[TestBlock.pref_lst, "b..12_35_ob/pref__lst/____bob"],
[TestBlock.user_lst, "__..../user__lst/____bob"],
[TestBlock.uss_lst, "__..../uss__lst/NONE.NONE"],
[TestBlock.settings_lst, "__..../settings__lst/NONE.NONE"]]:
key = scope_key(item, block)
assert key == correct_key
def test_field_display_name():
attempts = Integer(display_name='Maximum Problem Attempts')
attempts._name = "max_problem_attempts"
assert attempts.display_name == "Maximum Problem Attempts"
boolean_field = Boolean(display_name="boolean field")
assert boolean_field.display_name == "boolean field"
class TestBlock(XBlock):
"""
Block for testing
"""
field_x = List(display_name="Field Known as X")
assert TestBlock.field_x.display_name == "Field Known as X"
def test_unique_id_default():
class TestBlock(XBlock):
"""
Block for testing
"""
field_a = String(default=UNIQUE_ID, scope=Scope.settings)
field_b = String(default=UNIQUE_ID, scope=Scope.user_state)
sids = make_scope_ids_for_testing(
user_id="bob",
block_type="bobs-type",
block_id="usage-id",
)
runtime = TestRuntime(services={'field-data': DictFieldData({})})
block = TestBlock(runtime, DictFieldData({}), sids)
unique_a = block.field_a
unique_b = block.field_b
# Create another instance of the same block. Unique ID defaults should not change.
runtime = TestRuntime(services={'field-data': DictFieldData({})})
block = TestBlock(runtime, DictFieldData({}), sids)
assert unique_a == block.field_a
assert unique_b == block.field_b
# Change the user id. Unique ID default should change for field_b with
# user_state scope, but not for field_a with scope=settings.
runtime = TestRuntime(services={'field-data': DictFieldData({})})
block = TestBlock(runtime, DictFieldData({}), sids._replace(user_id='alice'))
assert unique_a == block.field_a
assert unique_b != block.field_b
# Change the usage id. Unique ID default for both fields should change.
runtime = TestRuntime(services={'field-data': DictFieldData({})})
block = TestBlock(runtime, DictFieldData({}), sids._replace(usage_id='usage-2'))
assert unique_a != block.field_a
assert unique_b != block.field_b
def test_values():
# static return value
field_values = ['foo', 'bar']
test_field = String(values=field_values)
assert field_values == test_field.values
# function to generate values
test_field = String(values=lambda: [1, 4])
assert [1, 4] == test_field.values
# default if nothing specified
assert String().values is None
def test_values_boolean():
# Test Boolean, which has values defined
test_field = Boolean()
assert ({'display_name': "True", "value": True}, {'display_name': "False", "value": False}) == \
test_field.values
def test_values_dict():
# Test that the format expected for integers is allowed
test_field = Integer(values={"min": 1, "max": 100})
assert {"min": 1, "max": 100} == test_field.values
def test_set_incomparable_fields():
# if we can't compare a field's value to the value it's going to be reset to
# (i.e. timezone aware and unaware datetimes), just reset the value.
class FieldTester(XBlock):
"""Test block for this test."""
incomparable = Field(scope=Scope.settings)
not_timezone_aware = dt.datetime(2015, 1, 1)
timezone_aware = dt.datetime(2015, 1, 1, tzinfo=pytz.UTC)
runtime = TestRuntime(services={'field-data': DictFieldData({})})
field_tester = FieldTester(runtime, scope_ids=make_scope_ids_for_testing())
field_tester.incomparable = not_timezone_aware
field_tester.incomparable = timezone_aware
assert field_tester.incomparable == timezone_aware
def test_twofaced_field_access():
# Check that a field with different to_json and from_json representations
# persists and saves correctly.
class TwoFacedField(Field):
"""A field that emits different 'json' than it parses."""
def from_json(self, value):
"""Store an int, the length of the string parsed."""
return len(value)
def to_json(self, value):
"""Emit some number of X's."""
return "X" * value
class FieldTester(XBlock):
"""Test block for TwoFacedField."""
how_many = TwoFacedField(scope=Scope.settings)
original_json = "YYY"
runtime = TestRuntime(services={'field-data': DictFieldData({'how_many': original_json})})
field_tester = FieldTester(runtime, scope_ids=make_scope_ids_for_testing())
# Test that the native value isn't equal to the original json we specified.
assert field_tester.how_many != original_json
# Test that the native -> json value isn't equal to the original json we specified.
assert TwoFacedField().to_json(field_tester.how_many) != original_json
# The previous accesses will mark the field as dirty (via __get__)
assert len(field_tester._dirty_fields) == 1
# However, the field should not ACTUALLY be marked as a field that is needing to be saved.
assert 'how_many' not in field_tester._get_fields_to_save() # pylint: disable=W0212
def test_setting_the_same_value_marks_field_as_dirty():
"""
Check that setting field to the same value marks mutable fields as dirty.
However, since the value hasn't changed, these fields won't be saved.
"""
# pylint: disable=unsubscriptable-object
class FieldTester(XBlock):
"""Test block for set - get test."""
non_mutable = String(scope=Scope.settings)
list_field = List(scope=Scope.settings)
dict_field = Dict(scope=Scope.settings)
runtime = TestRuntime(services={'field-data': DictFieldData({})})
field_tester = FieldTester(runtime, scope_ids=make_scope_ids_for_testing())
# precondition checks
assert len(field_tester._dirty_fields) == 0
assert not field_tester.fields['list_field'].is_set_on(field_tester)
assert not field_tester.fields['dict_field'].is_set_on(field_tester)
assert not field_tester.fields['non_mutable'].is_set_on(field_tester)
field_tester.non_mutable = field_tester.non_mutable
field_tester.list_field = field_tester.list_field
field_tester.dict_field = field_tester.dict_field
assert not field_tester.fields['non_mutable'] in field_tester._dirty_fields
assert field_tester.fields['list_field'] in field_tester._dirty_fields
assert field_tester.fields['dict_field'] in field_tester._dirty_fields
assert not field_tester.fields['non_mutable'].is_set_on(field_tester)
assert not field_tester.fields['list_field'].is_set_on(field_tester)
assert not field_tester.fields['dict_field'].is_set_on(field_tester)
class SentinelTest(unittest.TestCase):
"""
Tests of :ref:`xblock.fields.Sentinel`.
"""
def test_equality(self):
base = Sentinel('base')
self.assertEqual(base, base)
self.assertEqual(base, Sentinel('base'))
self.assertNotEqual(base, Sentinel('foo'))
self.assertNotEqual(base, 'base')
def test_hashing(self):
base = Sentinel('base')
a_dict = {base: True}
self.assertEqual(a_dict[Sentinel('base')], True)
self.assertEqual(a_dict[base], True)
self.assertNotIn(Sentinel('foo'), a_dict)
self.assertNotIn('base', a_dict)
@ddt.ddt
class FieldSerializationTest(unittest.TestCase):
"""
Tests field.from_string and field.to_string methods.
"""
def assert_to_string(self, _type, value, string):
"""
Helper method: checks if _type's to_string given instance of _type returns expected string
"""
result = _type().to_string(value)
self.assertEqual(result, string)
def assert_from_string(self, _type, string, value):
"""
Helper method: checks if _type's from_string given string representation of type returns expected value
"""
result = _type().from_string(string)
self.assertEqual(result, value)
# Serialisation test data that is tested both ways, i.e. whether serialisation of the value
# yields the string and deserialisation of the string yields the value.
@ddt.unpack
@ddt.data(
(DateTime, None, None)
)
def test_to_string(self, _type, value, string):
self.assert_to_string(_type, value, string)
@ddt.unpack
@ddt.data(
(Integer, 0, '0'),
(Integer, 5, '5'),
(Integer, -1023, '-1023'),
(Integer, 12345678, "12345678"),
(Float, 5.321, '5.321'),
(Float, -1023.35, '-1023.35'),
(Float, 1e+100, '1e+100'),
(Float, float('inf'), 'Infinity'),
(Float, float('-inf'), '-Infinity'),
(Boolean, True, "true"),
(Boolean, False, "false"),
(Integer, True, 'true'),
(String, "", ""),
(String, "foo", 'foo'),
(String, "bar", 'bar'),
(Dict, {}, '{}'),
(List, [], '[]'),
(Dict, {"foo": 1, "bar": 2}, textwrap.dedent("""\
{
"bar": 2,
"foo": 1
}""")),
(List, [1, 2, 3], textwrap.dedent("""\
[
1,
2,
3
]""")),
(Dict, {"foo": [1, 2, 3], "bar": 2}, textwrap.dedent("""\
{
"bar": 2,
"foo": [
1,
2,
3
]
}""")),
(DateTime, dt.datetime(2014, 4, 1, 2, 3, 4, 567890).replace(tzinfo=pytz.utc), '2014-04-01T02:03:04.567890'),
(DateTime, dt.datetime(2014, 4, 1, 2, 3, 4).replace(tzinfo=pytz.utc), '2014-04-01T02:03:04.000000'),
)
def test_both_directions(self, _type, value, string):
"""Easy cases that work in both directions."""
self.assert_to_string(_type, value, string)
self.assert_from_string(_type, string, value)
@ddt.unpack