test_oa_trip_days.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. """oa_trip_days unit tests"""
  2. from app.services.oa_trip_days import (
  3. apply_trip_kit_days,
  4. calc_trip_leg_days,
  5. sum_trip_leg_days,
  6. validate_trip_kit_form_values,
  7. )
  8. def test_half_day_samples():
  9. assert calc_trip_leg_days("2026-08-01", "上午", "2026-08-01", "上午") == 0.5
  10. assert calc_trip_leg_days("2026-08-01", "上午", "2026-08-01", "下午") == 1.0
  11. assert calc_trip_leg_days("2026-08-01", "下午", "2026-08-01", "上午") is None
  12. assert calc_trip_leg_days("2026-08-01", "下午", "2026-08-02", "上午") == 1.0
  13. def test_sum():
  14. assert sum_trip_leg_days([{"duration": 0.5}, {"duration": 2}]) == 2.5
  15. def test_apply_overwrites_when_not_manual():
  16. schema = [
  17. {
  18. "type": "tripKit",
  19. "props": {"tripListField": "L1", "tripDaysField": "D1"},
  20. "children": [
  21. {"type": "tripItinerary", "field": "L1", "props": {"_fc_kit_field": "tripList"}},
  22. {"type": "inputNumber", "field": "D1", "props": {"_fc_kit_field": "tripDays"}},
  23. ],
  24. }
  25. ]
  26. data = {
  27. "L1": [
  28. {
  29. "transport": "飞机",
  30. "tripType": "单程",
  31. "fromCity": "合肥",
  32. "toCity": "上海",
  33. "startDate": "2026-08-01",
  34. "startHalf": "上午",
  35. "endDate": "2026-08-01",
  36. "endHalf": "下午",
  37. "duration": 9,
  38. "durationManual": False,
  39. }
  40. ],
  41. "D1": 9,
  42. }
  43. out = apply_trip_kit_days(schema, data)
  44. assert out["L1"][0]["duration"] == 1.0
  45. assert out["D1"] == 1.0
  46. def test_respect_manual_flags():
  47. schema = [
  48. {
  49. "type": "tripKit",
  50. "props": {"tripListField": "L1", "tripDaysField": "D1"},
  51. "children": [
  52. {"type": "tripItinerary", "field": "L1", "props": {"_fc_kit_field": "tripList"}},
  53. {"type": "inputNumber", "field": "D1", "props": {"_fc_kit_field": "tripDays"}},
  54. ],
  55. }
  56. ]
  57. data = {
  58. "L1": [
  59. {
  60. "startDate": "2026-08-01",
  61. "startHalf": "上午",
  62. "endDate": "2026-08-01",
  63. "endHalf": "下午",
  64. "duration": 3,
  65. "durationManual": True,
  66. "transport": "火车",
  67. "tripType": "往返",
  68. "fromCity": "a",
  69. "toCity": "b",
  70. }
  71. ],
  72. "D1": 7,
  73. "__manual_D1": True,
  74. }
  75. out = apply_trip_kit_days(schema, data)
  76. assert out["L1"][0]["duration"] == 3
  77. assert out["D1"] == 7
  78. def test_validate_end_before_start():
  79. schema = [
  80. {
  81. "type": "tripKit",
  82. "props": {"tripListField": "L1", "tripDaysField": "D1"},
  83. "children": [
  84. {"type": "tripItinerary", "field": "L1", "props": {"_fc_kit_field": "tripList"}},
  85. {"type": "inputNumber", "field": "D1", "props": {"_fc_kit_field": "tripDays"}},
  86. ],
  87. }
  88. ]
  89. data = {
  90. "L1": [
  91. {
  92. "transport": "飞机",
  93. "tripType": "单程",
  94. "fromCity": "a",
  95. "toCity": "b",
  96. "startDate": "2026-08-01",
  97. "startHalf": "下午",
  98. "endDate": "2026-08-01",
  99. "endHalf": "上午",
  100. "duration": 0.5,
  101. "durationManual": True,
  102. }
  103. ],
  104. "D1": 0.5,
  105. }
  106. errs = validate_trip_kit_form_values(schema, data)
  107. assert errs