test_update.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. from collections import deque
  2. from typing import Type
  3. from multidict import MultiMapping
  4. def test_update_replace(any_multidict_class: Type[MultiMapping[str]]) -> None:
  5. obj1 = any_multidict_class([("a", 1), ("b", 2), ("a", 3), ("c", 10)])
  6. obj2 = any_multidict_class([("a", 4), ("b", 5), ("a", 6)])
  7. obj1.update(obj2)
  8. expected = [("a", 4), ("b", 5), ("a", 6), ("c", 10)]
  9. assert list(obj1.items()) == expected
  10. def test_update_append(any_multidict_class: Type[MultiMapping[str]]) -> None:
  11. obj1 = any_multidict_class([("a", 1), ("b", 2), ("a", 3), ("c", 10)])
  12. obj2 = any_multidict_class([("a", 4), ("a", 5), ("a", 6)])
  13. obj1.update(obj2)
  14. expected = [("a", 4), ("b", 2), ("a", 5), ("c", 10), ("a", 6)]
  15. assert list(obj1.items()) == expected
  16. def test_update_remove(any_multidict_class: Type[MultiMapping[str]]) -> None:
  17. obj1 = any_multidict_class([("a", 1), ("b", 2), ("a", 3), ("c", 10)])
  18. obj2 = any_multidict_class([("a", 4)])
  19. obj1.update(obj2)
  20. expected = [("a", 4), ("b", 2), ("c", 10)]
  21. assert list(obj1.items()) == expected
  22. def test_update_replace_seq(any_multidict_class: Type[MultiMapping[str]]) -> None:
  23. obj1 = any_multidict_class([("a", 1), ("b", 2), ("a", 3), ("c", 10)])
  24. obj2 = [("a", 4), ("b", 5), ("a", 6)]
  25. obj1.update(obj2)
  26. expected = [("a", 4), ("b", 5), ("a", 6), ("c", 10)]
  27. assert list(obj1.items()) == expected
  28. def test_update_replace_seq2(any_multidict_class: Type[MultiMapping[str]]) -> None:
  29. obj1 = any_multidict_class([("a", 1), ("b", 2), ("a", 3), ("c", 10)])
  30. obj1.update([("a", 4)], b=5, a=6)
  31. expected = [("a", 4), ("b", 5), ("a", 6), ("c", 10)]
  32. assert list(obj1.items()) == expected
  33. def test_update_append_seq(any_multidict_class: Type[MultiMapping[str]]) -> None:
  34. obj1 = any_multidict_class([("a", 1), ("b", 2), ("a", 3), ("c", 10)])
  35. obj2 = [("a", 4), ("a", 5), ("a", 6)]
  36. obj1.update(obj2)
  37. expected = [("a", 4), ("b", 2), ("a", 5), ("c", 10), ("a", 6)]
  38. assert list(obj1.items()) == expected
  39. def test_update_remove_seq(any_multidict_class: Type[MultiMapping[str]]) -> None:
  40. obj1 = any_multidict_class([("a", 1), ("b", 2), ("a", 3), ("c", 10)])
  41. obj2 = [("a", 4)]
  42. obj1.update(obj2)
  43. expected = [("a", 4), ("b", 2), ("c", 10)]
  44. assert list(obj1.items()) == expected
  45. def test_update_md(
  46. case_sensitive_multidict_class: Type[MultiMapping[str]],
  47. ) -> None:
  48. d = case_sensitive_multidict_class()
  49. d.add("key", "val1")
  50. d.add("key", "val2")
  51. d.add("key2", "val3")
  52. d.update(key="val")
  53. assert [("key", "val"), ("key2", "val3")] == list(d.items())
  54. def test_update_istr_ci_md(
  55. case_insensitive_multidict_class: Type[MultiMapping[str]],
  56. case_insensitive_str_class: str,
  57. ) -> None:
  58. d = case_insensitive_multidict_class()
  59. d.add(case_insensitive_str_class("KEY"), "val1")
  60. d.add("key", "val2")
  61. d.add("key2", "val3")
  62. d.update({case_insensitive_str_class("key"): "val"})
  63. assert [("key", "val"), ("key2", "val3")] == list(d.items())
  64. def test_update_ci_md(
  65. case_insensitive_multidict_class: Type[MultiMapping[str]],
  66. ) -> None:
  67. d = case_insensitive_multidict_class()
  68. d.add("KEY", "val1")
  69. d.add("key", "val2")
  70. d.add("key2", "val3")
  71. d.update(Key="val")
  72. assert [("Key", "val"), ("key2", "val3")] == list(d.items())
  73. def test_update_list_arg_and_kwds(
  74. any_multidict_class: Type[MultiMapping[str]],
  75. ) -> None:
  76. obj = any_multidict_class()
  77. arg = [("a", 1)]
  78. obj.update(arg, b=2)
  79. assert list(obj.items()) == [("a", 1), ("b", 2)]
  80. assert arg == [("a", 1)]
  81. def test_update_tuple_arg_and_kwds(
  82. any_multidict_class: Type[MultiMapping[str]],
  83. ) -> None:
  84. obj = any_multidict_class()
  85. arg = (("a", 1),)
  86. obj.update(arg, b=2)
  87. assert list(obj.items()) == [("a", 1), ("b", 2)]
  88. assert arg == (("a", 1),)
  89. def test_update_deque_arg_and_kwds(
  90. any_multidict_class: Type[MultiMapping[str]],
  91. ) -> None:
  92. obj = any_multidict_class()
  93. arg = deque([("a", 1)])
  94. obj.update(arg, b=2)
  95. assert list(obj.items()) == [("a", 1), ("b", 2)]
  96. assert arg == deque([("a", 1)])