freeze_test.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """Tests for freeze and thaw."""
  2. import collections
  3. from pyrsistent import v, m, s, freeze, thaw, PRecord, field, mutant
  4. ## Freeze (standard)
  5. def test_freeze_basic():
  6. assert freeze(1) == 1
  7. assert freeze('foo') == 'foo'
  8. def test_freeze_list():
  9. assert freeze([1, 2]) == v(1, 2)
  10. def test_freeze_dict():
  11. result = freeze({'a': 'b'})
  12. assert result == m(a='b')
  13. assert type(freeze({'a': 'b'})) is type(m())
  14. def test_freeze_defaultdict():
  15. test_dict = collections.defaultdict(dict)
  16. test_dict['a'] = 'b'
  17. result = freeze(test_dict)
  18. assert result == m(a='b')
  19. assert type(freeze({'a': 'b'})) is type(m())
  20. def test_freeze_set():
  21. result = freeze(set([1, 2, 3]))
  22. assert result == s(1, 2, 3)
  23. assert type(result) is type(s())
  24. def test_freeze_recurse_in_dictionary_values():
  25. result = freeze({'a': [1]})
  26. assert result == m(a=v(1))
  27. assert type(result['a']) is type(v())
  28. def test_freeze_recurse_in_defaultdict_values():
  29. test_dict = collections.defaultdict(dict)
  30. test_dict['a'] = [1]
  31. result = freeze(test_dict)
  32. assert result == m(a=v(1))
  33. assert type(result['a']) is type(v())
  34. def test_freeze_recurse_in_pmap_values():
  35. input = {'a': m(b={'c': 1})}
  36. result = freeze(input)
  37. # PMap and PVector are == to their mutable equivalents
  38. assert result == input
  39. assert type(result) is type(m())
  40. assert type(result['a']['b']) is type(m())
  41. def test_freeze_recurse_in_lists():
  42. result = freeze(['a', {'b': 3}])
  43. assert result == v('a', m(b=3))
  44. assert type(result[1]) is type(m())
  45. def test_freeze_recurse_in_pvectors():
  46. input = [1, v(2, [3])]
  47. result = freeze(input)
  48. # PMap and PVector are == to their mutable equivalents
  49. assert result == input
  50. assert type(result) is type(v())
  51. assert type(result[1][1]) is type(v())
  52. def test_freeze_recurse_in_tuples():
  53. """Values in tuples are recursively frozen."""
  54. result = freeze(('a', {}))
  55. assert result == ('a', m())
  56. assert type(result[1]) is type(m())
  57. ## Freeze (weak)
  58. def test_freeze_nonstrict_no_recurse_in_pmap_values():
  59. input = {'a': m(b={'c': 1})}
  60. result = freeze(input, strict=False)
  61. # PMap and PVector are == to their mutable equivalents
  62. assert result == input
  63. assert type(result) is type(m())
  64. assert type(result['a']['b']) is dict
  65. def test_freeze_nonstrict_no_recurse_in_pvectors():
  66. input = [1, v(2, [3])]
  67. result = freeze(input, strict=False)
  68. # PMap and PVector are == to their mutable equivalents
  69. assert result == input
  70. assert type(result) is type(v())
  71. assert type(result[1][1]) is list
  72. ## Thaw
  73. def test_thaw_basic():
  74. assert thaw(1) == 1
  75. assert thaw('foo') == 'foo'
  76. def test_thaw_list():
  77. result = thaw(v(1, 2))
  78. assert result == [1, 2]
  79. assert type(result) is list
  80. def test_thaw_dict():
  81. result = thaw(m(a='b'))
  82. assert result == {'a': 'b'}
  83. assert type(result) is dict
  84. def test_thaw_set():
  85. result = thaw(s(1, 2))
  86. assert result == set([1, 2])
  87. assert type(result) is set
  88. def test_thaw_recurse_in_mapping_values():
  89. result = thaw(m(a=v(1)))
  90. assert result == {'a': [1]}
  91. assert type(result['a']) is list
  92. def test_thaw_recurse_in_dict_values():
  93. result = thaw({'a': v(1, m(b=2))})
  94. assert result == {'a': [1, {'b': 2}]}
  95. assert type(result['a']) is list
  96. assert type(result['a'][1]) is dict
  97. def test_thaw_recurse_in_vectors():
  98. result = thaw(v('a', m(b=3)))
  99. assert result == ['a', {'b': 3}]
  100. assert type(result[1]) is dict
  101. def test_thaw_recurse_in_lists():
  102. result = thaw(v(['a', m(b=1), v(2)]))
  103. assert result == [['a', {'b': 1}, [2]]]
  104. assert type(result[0]) is list
  105. assert type(result[0][1]) is dict
  106. def test_thaw_recurse_in_tuples():
  107. result = thaw(('a', m()))
  108. assert result == ('a', {})
  109. assert type(result[1]) is dict
  110. def test_thaw_can_handle_subclasses_of_persistent_base_types():
  111. class R(PRecord):
  112. x = field()
  113. result = thaw(R(x=1))
  114. assert result == {'x': 1}
  115. assert type(result) is dict
  116. ## Thaw (weak)
  117. def test_thaw_non_strict_no_recurse_in_dict_values():
  118. result = thaw({'a': v(1, m(b=2))}, strict=False)
  119. assert result == {'a': [1, {'b': 2}]}
  120. assert type(result['a']) is type(v())
  121. assert type(result['a'][1]) is type(m())
  122. def test_thaw_non_strict_no_recurse_in_lists():
  123. result = thaw(v(['a', m(b=1), v(2)]), strict=False)
  124. assert result == [['a', {'b': 1}, [2]]]
  125. assert type(result[0][1]) is type(m())
  126. def test_mutant_decorator():
  127. @mutant
  128. def fn(a_list, a_dict):
  129. assert a_list == v(1, 2, 3)
  130. assert isinstance(a_dict, type(m()))
  131. assert a_dict == {'a': 5}
  132. return [1, 2, 3], {'a': 3}
  133. pv, pm = fn([1, 2, 3], a_dict={'a': 5})
  134. assert pv == v(1, 2, 3)
  135. assert pm == m(a=3)
  136. assert isinstance(pm, type(m()))