118 lines
3.5 KiB
Python
118 lines
3.5 KiB
Python
"""oa_trip_days unit tests"""
|
|
from app.services.oa_trip_days import (
|
|
apply_trip_kit_days,
|
|
calc_trip_leg_days,
|
|
sum_trip_leg_days,
|
|
validate_trip_kit_form_values,
|
|
)
|
|
|
|
|
|
def test_half_day_samples():
|
|
assert calc_trip_leg_days("2026-08-01", "上午", "2026-08-01", "上午") == 0.5
|
|
assert calc_trip_leg_days("2026-08-01", "上午", "2026-08-01", "下午") == 1.0
|
|
assert calc_trip_leg_days("2026-08-01", "下午", "2026-08-01", "上午") is None
|
|
assert calc_trip_leg_days("2026-08-01", "下午", "2026-08-02", "上午") == 1.0
|
|
|
|
|
|
def test_sum():
|
|
assert sum_trip_leg_days([{"duration": 0.5}, {"duration": 2}]) == 2.5
|
|
|
|
|
|
def test_apply_overwrites_when_not_manual():
|
|
schema = [
|
|
{
|
|
"type": "tripKit",
|
|
"props": {"tripListField": "L1", "tripDaysField": "D1"},
|
|
"children": [
|
|
{"type": "tripItinerary", "field": "L1", "props": {"_fc_kit_field": "tripList"}},
|
|
{"type": "inputNumber", "field": "D1", "props": {"_fc_kit_field": "tripDays"}},
|
|
],
|
|
}
|
|
]
|
|
data = {
|
|
"L1": [
|
|
{
|
|
"transport": "飞机",
|
|
"tripType": "单程",
|
|
"fromCity": "合肥",
|
|
"toCity": "上海",
|
|
"startDate": "2026-08-01",
|
|
"startHalf": "上午",
|
|
"endDate": "2026-08-01",
|
|
"endHalf": "下午",
|
|
"duration": 9,
|
|
"durationManual": False,
|
|
}
|
|
],
|
|
"D1": 9,
|
|
}
|
|
out = apply_trip_kit_days(schema, data)
|
|
assert out["L1"][0]["duration"] == 1.0
|
|
assert out["D1"] == 1.0
|
|
|
|
|
|
def test_respect_manual_flags():
|
|
schema = [
|
|
{
|
|
"type": "tripKit",
|
|
"props": {"tripListField": "L1", "tripDaysField": "D1"},
|
|
"children": [
|
|
{"type": "tripItinerary", "field": "L1", "props": {"_fc_kit_field": "tripList"}},
|
|
{"type": "inputNumber", "field": "D1", "props": {"_fc_kit_field": "tripDays"}},
|
|
],
|
|
}
|
|
]
|
|
data = {
|
|
"L1": [
|
|
{
|
|
"startDate": "2026-08-01",
|
|
"startHalf": "上午",
|
|
"endDate": "2026-08-01",
|
|
"endHalf": "下午",
|
|
"duration": 3,
|
|
"durationManual": True,
|
|
"transport": "火车",
|
|
"tripType": "往返",
|
|
"fromCity": "a",
|
|
"toCity": "b",
|
|
}
|
|
],
|
|
"D1": 7,
|
|
"__manual_D1": True,
|
|
}
|
|
out = apply_trip_kit_days(schema, data)
|
|
assert out["L1"][0]["duration"] == 3
|
|
assert out["D1"] == 7
|
|
|
|
|
|
def test_validate_end_before_start():
|
|
schema = [
|
|
{
|
|
"type": "tripKit",
|
|
"props": {"tripListField": "L1", "tripDaysField": "D1"},
|
|
"children": [
|
|
{"type": "tripItinerary", "field": "L1", "props": {"_fc_kit_field": "tripList"}},
|
|
{"type": "inputNumber", "field": "D1", "props": {"_fc_kit_field": "tripDays"}},
|
|
],
|
|
}
|
|
]
|
|
data = {
|
|
"L1": [
|
|
{
|
|
"transport": "飞机",
|
|
"tripType": "单程",
|
|
"fromCity": "a",
|
|
"toCity": "b",
|
|
"startDate": "2026-08-01",
|
|
"startHalf": "下午",
|
|
"endDate": "2026-08-01",
|
|
"endHalf": "上午",
|
|
"duration": 0.5,
|
|
"durationManual": True,
|
|
}
|
|
],
|
|
"D1": 0.5,
|
|
}
|
|
errs = validate_trip_kit_form_values(schema, data)
|
|
assert errs
|