test_format.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. from __future__ import annotations
  2. import datetime
  3. from typing import Any, List
  4. import plotly.graph_objects as go
  5. import pytest
  6. from reflex.components.tags.tag import Tag
  7. from reflex.event import EventChain, EventHandler, EventSpec, FrontendEvent
  8. from reflex.ivars.base import ImmutableVar, LiteralVar
  9. from reflex.ivars.object import ObjectVar
  10. from reflex.style import Style
  11. from reflex.utils import format
  12. from reflex.utils.serializers import serialize_figure
  13. from reflex.vars import Var
  14. from tests.test_state import (
  15. ChildState,
  16. ChildState2,
  17. ChildState3,
  18. DateTimeState,
  19. GrandchildState,
  20. GrandchildState2,
  21. GrandchildState3,
  22. TestState,
  23. )
  24. def mock_event(arg):
  25. pass
  26. @pytest.mark.parametrize(
  27. "input,output",
  28. [
  29. ("{", "}"),
  30. ("(", ")"),
  31. ("[", "]"),
  32. ("<", ">"),
  33. ('"', '"'),
  34. ("'", "'"),
  35. ],
  36. )
  37. def test_get_close_char(input: str, output: str):
  38. """Test getting the close character for a given open character.
  39. Args:
  40. input: The open character.
  41. output: The expected close character.
  42. """
  43. assert format.get_close_char(input) == output
  44. @pytest.mark.parametrize(
  45. "text,open,expected",
  46. [
  47. ("", "{", False),
  48. ("{wrap}", "{", True),
  49. ("{wrap", "{", False),
  50. ("{wrap}", "(", False),
  51. ("(wrap)", "(", True),
  52. ],
  53. )
  54. def test_is_wrapped(text: str, open: str, expected: bool):
  55. """Test checking if a string is wrapped in the given open and close characters.
  56. Args:
  57. text: The text to check.
  58. open: The open character.
  59. expected: Whether the text is wrapped.
  60. """
  61. assert format.is_wrapped(text, open) == expected
  62. @pytest.mark.parametrize(
  63. "text,open,check_first,num,expected",
  64. [
  65. ("", "{", True, 1, "{}"),
  66. ("wrap", "{", True, 1, "{wrap}"),
  67. ("wrap", "(", True, 1, "(wrap)"),
  68. ("wrap", "(", True, 2, "((wrap))"),
  69. ("(wrap)", "(", True, 1, "(wrap)"),
  70. ("{wrap}", "{", True, 2, "{wrap}"),
  71. ("(wrap)", "{", True, 1, "{(wrap)}"),
  72. ("(wrap)", "(", False, 1, "((wrap))"),
  73. ],
  74. )
  75. def test_wrap(text: str, open: str, expected: str, check_first: bool, num: int):
  76. """Test wrapping a string.
  77. Args:
  78. text: The text to wrap.
  79. open: The open character.
  80. expected: The expected output string.
  81. check_first: Whether to check if the text is already wrapped.
  82. num: The number of times to wrap the text.
  83. """
  84. assert format.wrap(text, open, check_first=check_first, num=num) == expected
  85. @pytest.mark.parametrize(
  86. "string,expected_output",
  87. [
  88. ("This is a random string", "This is a random string"),
  89. (
  90. "This is a random string with `backticks`",
  91. "This is a random string with \\`backticks\\`",
  92. ),
  93. (
  94. "This is a random string with `backticks`",
  95. "This is a random string with \\`backticks\\`",
  96. ),
  97. (
  98. "This is a string with ${someValue[`string interpolation`]} unescaped",
  99. "This is a string with ${someValue[`string interpolation`]} unescaped",
  100. ),
  101. (
  102. "This is a string with `backticks` and ${someValue[`string interpolation`]} unescaped",
  103. "This is a string with \\`backticks\\` and ${someValue[`string interpolation`]} unescaped",
  104. ),
  105. (
  106. "This is a string with `backticks`, ${someValue[`the first string interpolation`]} and ${someValue[`the second`]}",
  107. "This is a string with \\`backticks\\`, ${someValue[`the first string interpolation`]} and ${someValue[`the second`]}",
  108. ),
  109. ],
  110. )
  111. def test_escape_js_string(string, expected_output):
  112. assert format._escape_js_string(string) == expected_output
  113. @pytest.mark.parametrize(
  114. "text,indent_level,expected",
  115. [
  116. ("", 2, ""),
  117. ("hello", 2, "hello"),
  118. ("hello\nworld", 2, " hello\n world\n"),
  119. ("hello\nworld", 4, " hello\n world\n"),
  120. (" hello\n world", 2, " hello\n world\n"),
  121. ],
  122. )
  123. def test_indent(text: str, indent_level: int, expected: str, windows_platform: bool):
  124. """Test indenting a string.
  125. Args:
  126. text: The text to indent.
  127. indent_level: The number of spaces to indent by.
  128. expected: The expected output string.
  129. windows_platform: Whether the system is windows.
  130. """
  131. assert format.indent(text, indent_level) == (
  132. expected.replace("\n", "\r\n") if windows_platform else expected
  133. )
  134. @pytest.mark.parametrize(
  135. "input,output",
  136. [
  137. ("", ""),
  138. ("hello", "hello"),
  139. ("Hello", "hello"),
  140. ("camelCase", "camel_case"),
  141. ("camelTwoHumps", "camel_two_humps"),
  142. ("_start_with_underscore", "_start_with_underscore"),
  143. ("__start_with_double_underscore", "__start_with_double_underscore"),
  144. ("kebab-case", "kebab_case"),
  145. ("double-kebab-case", "double_kebab_case"),
  146. (":start-with-colon", ":start_with_colon"),
  147. (":-start-with-colon-dash", ":_start_with_colon_dash"),
  148. ],
  149. )
  150. def test_to_snake_case(input: str, output: str):
  151. """Test converting strings to snake case.
  152. Args:
  153. input: The input string.
  154. output: The expected output string.
  155. """
  156. assert format.to_snake_case(input) == output
  157. @pytest.mark.parametrize(
  158. "input,output",
  159. [
  160. ("", ""),
  161. ("hello", "hello"),
  162. ("Hello", "Hello"),
  163. ("snake_case", "snakeCase"),
  164. ("snake_case_two", "snakeCaseTwo"),
  165. ("kebab-case", "kebabCase"),
  166. ("kebab-case-two", "kebabCaseTwo"),
  167. ("snake_kebab-case", "snakeKebabCase"),
  168. ("_hover", "_hover"),
  169. ("-starts-with-hyphen", "-startsWithHyphen"),
  170. ("--starts-with-double-hyphen", "--startsWithDoubleHyphen"),
  171. ("_starts_with_underscore", "_startsWithUnderscore"),
  172. ("__starts_with_double_underscore", "__startsWithDoubleUnderscore"),
  173. (":start-with-colon", ":startWithColon"),
  174. (":-start-with-colon-dash", ":StartWithColonDash"),
  175. ],
  176. )
  177. def test_to_camel_case(input: str, output: str):
  178. """Test converting strings to camel case.
  179. Args:
  180. input: The input string.
  181. output: The expected output string.
  182. """
  183. assert format.to_camel_case(input) == output
  184. @pytest.mark.parametrize(
  185. "input,output",
  186. [
  187. ("", ""),
  188. ("hello", "Hello"),
  189. ("Hello", "Hello"),
  190. ("snake_case", "SnakeCase"),
  191. ("snake_case_two", "SnakeCaseTwo"),
  192. ],
  193. )
  194. def test_to_title_case(input: str, output: str):
  195. """Test converting strings to title case.
  196. Args:
  197. input: The input string.
  198. output: The expected output string.
  199. """
  200. assert format.to_title_case(input) == output
  201. @pytest.mark.parametrize(
  202. "input,output",
  203. [
  204. ("", ""),
  205. ("hello", "hello"),
  206. ("Hello", "hello"),
  207. ("snake_case", "snake-case"),
  208. ("snake_case_two", "snake-case-two"),
  209. (":startWithColon", ":start-with-colon"),
  210. (":StartWithColonDash", ":-start-with-colon-dash"),
  211. (":start_with_colon", ":start-with-colon"),
  212. (":_start_with_colon_dash", ":-start-with-colon-dash"),
  213. ],
  214. )
  215. def test_to_kebab_case(input: str, output: str):
  216. """Test converting strings to kebab case.
  217. Args:
  218. input: the input string.
  219. output: the output string.
  220. """
  221. assert format.to_kebab_case(input) == output
  222. @pytest.mark.parametrize(
  223. "input,output",
  224. [
  225. ("", "{``}"),
  226. ("hello", "{`hello`}"),
  227. ("hello world", "{`hello world`}"),
  228. ("hello=`world`", "{`hello=\\`world\\``}"),
  229. ],
  230. )
  231. def test_format_string(input: str, output: str):
  232. """Test formating the input as JS string literal.
  233. Args:
  234. input: the input string.
  235. output: the output string.
  236. """
  237. assert format.format_string(input) == output
  238. @pytest.mark.parametrize(
  239. "input,output",
  240. [
  241. (Var.create(value="test"), '"test"'),
  242. (Var.create(value="test", _var_is_local=True), '"test"'),
  243. (Var.create(value="test", _var_is_local=False), "test"),
  244. (Var.create(value="test", _var_is_string=True), '"test"'),
  245. (Var.create(value='"test"', _var_is_string=False), '"test"'),
  246. (Var.create(value="test", _var_is_local=False, _var_is_string=False), "test"),
  247. ],
  248. )
  249. def test_format_var(input: Var, output: str):
  250. assert str(input) == output
  251. @pytest.mark.parametrize(
  252. "route,format_case,expected",
  253. [
  254. ("", True, "index"),
  255. ("/", True, "index"),
  256. ("custom-route", True, "custom-route"),
  257. ("custom-route", False, "custom-route"),
  258. ("custom-route/", True, "custom-route"),
  259. ("custom-route/", False, "custom-route"),
  260. ("/custom-route", True, "custom-route"),
  261. ("/custom-route", False, "custom-route"),
  262. ("/custom_route", True, "custom-route"),
  263. ("/custom_route", False, "custom_route"),
  264. ("/CUSTOM_route", True, "custom-route"),
  265. ("/CUSTOM_route", False, "CUSTOM_route"),
  266. ],
  267. )
  268. def test_format_route(route: str, format_case: bool, expected: bool):
  269. """Test formatting a route.
  270. Args:
  271. route: The route to format.
  272. format_case: Whether to change casing to snake_case.
  273. expected: The expected formatted route.
  274. """
  275. assert format.format_route(route, format_case=format_case) == expected
  276. @pytest.mark.parametrize(
  277. "condition, match_cases, default,expected",
  278. [
  279. (
  280. "state__state.value",
  281. [
  282. [LiteralVar.create(1), LiteralVar.create("red")],
  283. [LiteralVar.create(2), LiteralVar.create(3), LiteralVar.create("blue")],
  284. [TestState.mapping, TestState.num1],
  285. [
  286. LiteralVar.create(f"{TestState.map_key}-key"),
  287. LiteralVar.create("return-key"),
  288. ],
  289. ],
  290. LiteralVar.create("yellow"),
  291. '(() => { switch (JSON.stringify(state__state.value)) {case JSON.stringify(1): return ("red"); break;case JSON.stringify(2): case JSON.stringify(3): '
  292. f'return ("blue"); break;case JSON.stringify({TestState.get_full_name()}.mapping): return '
  293. f'({TestState.get_full_name()}.num1); break;case JSON.stringify(({TestState.get_full_name()}.map_key+"-key")): return ("return-key");'
  294. ' break;default: return ("yellow"); break;};})()',
  295. )
  296. ],
  297. )
  298. def test_format_match(
  299. condition: str,
  300. match_cases: List[List[ImmutableVar]],
  301. default: ImmutableVar,
  302. expected: str,
  303. ):
  304. """Test formatting a match statement.
  305. Args:
  306. condition: The condition to match.
  307. match_cases: List of match cases to be matched.
  308. default: Catchall case for the match statement.
  309. expected: The expected string output.
  310. """
  311. assert format.format_match(condition, match_cases, default) == expected
  312. @pytest.mark.parametrize(
  313. "prop,formatted",
  314. [
  315. ("string", '"string"'),
  316. ("{wrapped_string}", "{wrapped_string}"),
  317. (True, "{true}"),
  318. (False, "{false}"),
  319. (123, "{123}"),
  320. (3.14, "{3.14}"),
  321. ([1, 2, 3], "{[1, 2, 3]}"),
  322. (["a", "b", "c"], '{["a", "b", "c"]}'),
  323. ({"a": 1, "b": 2, "c": 3}, '{({ ["a"] : 1, ["b"] : 2, ["c"] : 3 })}'),
  324. ({"a": 'foo "bar" baz'}, r'{({ ["a"] : "foo \"bar\" baz" })}'),
  325. (
  326. {
  327. "a": 'foo "{ "bar" }" baz',
  328. "b": ImmutableVar(_var_name="val", _var_type=str).guess_type(),
  329. },
  330. r'{({ ["a"] : "foo \"{ \"bar\" }\" baz", ["b"] : val })}',
  331. ),
  332. (
  333. EventChain(
  334. events=[EventSpec(handler=EventHandler(fn=mock_event))],
  335. args_spec=lambda: [],
  336. ),
  337. '{(...args) => addEvents([Event("mock_event", {})], args, {})}',
  338. ),
  339. (
  340. EventChain(
  341. events=[
  342. EventSpec(
  343. handler=EventHandler(fn=mock_event),
  344. args=(
  345. (
  346. LiteralVar.create("arg"),
  347. ImmutableVar(
  348. _var_name="_e",
  349. )
  350. .to(ObjectVar, FrontendEvent)
  351. .target.value,
  352. ),
  353. ),
  354. )
  355. ],
  356. args_spec=lambda e: [e.target.value],
  357. ),
  358. '{(_e) => addEvents([Event("mock_event", {"arg":_e["target"]["value"]})], [_e], {})}',
  359. ),
  360. (
  361. EventChain(
  362. events=[EventSpec(handler=EventHandler(fn=mock_event))],
  363. args_spec=lambda: [],
  364. event_actions={"stopPropagation": True},
  365. ),
  366. '{(...args) => addEvents([Event("mock_event", {})], args, {"stopPropagation": true})}',
  367. ),
  368. (
  369. EventChain(
  370. events=[EventSpec(handler=EventHandler(fn=mock_event))],
  371. args_spec=lambda: [],
  372. event_actions={"preventDefault": True},
  373. ),
  374. '{(...args) => addEvents([Event("mock_event", {})], args, {"preventDefault": true})}',
  375. ),
  376. ({"a": "red", "b": "blue"}, '{({ ["a"] : "red", ["b"] : "blue" })}'),
  377. (ImmutableVar(_var_name="var", _var_type=int).guess_type(), "var"),
  378. (
  379. ImmutableVar(
  380. _var_name="_",
  381. _var_type=Any,
  382. ),
  383. "_",
  384. ),
  385. (
  386. ImmutableVar(_var_name='state.colors["a"]', _var_type=str).guess_type(),
  387. 'state.colors["a"]',
  388. ),
  389. (
  390. {"a": ImmutableVar(_var_name="val", _var_type=str).guess_type()},
  391. '{({ ["a"] : val })}',
  392. ),
  393. (
  394. {"a": ImmutableVar(_var_name='"val"', _var_type=str).guess_type()},
  395. '{({ ["a"] : "val" })}',
  396. ),
  397. (
  398. {
  399. "a": ImmutableVar(
  400. _var_name='state.colors["val"]', _var_type=str
  401. ).guess_type()
  402. },
  403. '{({ ["a"] : state.colors["val"] })}',
  404. ),
  405. # tricky real-world case from markdown component
  406. (
  407. {
  408. "h1": ImmutableVar.create_safe(
  409. f"(({{node, ...props}}) => <Heading {{...props}} {''.join(Tag(name='', props=Style({'as_': 'h1'})).format_props())} />)"
  410. )
  411. },
  412. '{({ ["h1"] : (({node, ...props}) => <Heading {...props} as={"h1"} />) })}',
  413. ),
  414. ],
  415. )
  416. def test_format_prop(prop: ImmutableVar, formatted: str):
  417. """Test that the formatted value of an prop is correct.
  418. Args:
  419. prop: The prop to test.
  420. formatted: The expected formatted value.
  421. """
  422. assert format.format_prop(prop) == formatted
  423. @pytest.mark.parametrize(
  424. "single_props,key_value_props,output",
  425. [
  426. (
  427. [ImmutableVar.create_safe("{...props}")],
  428. {"key": 42},
  429. ["key={42}", "{...props}"],
  430. ),
  431. ],
  432. )
  433. def test_format_props(single_props, key_value_props, output):
  434. """Test the result of formatting a set of props (both single and keyvalue).
  435. Args:
  436. single_props: the list of single props
  437. key_value_props: the dict of key value props
  438. output: the expected output
  439. """
  440. assert format.format_props(*single_props, **key_value_props) == output
  441. @pytest.mark.parametrize(
  442. "input,output",
  443. [
  444. (EventHandler(fn=mock_event), ("", "mock_event")),
  445. ],
  446. )
  447. def test_get_handler_parts(input, output):
  448. assert format.get_event_handler_parts(input) == output
  449. @pytest.mark.parametrize(
  450. "input,output",
  451. [
  452. (TestState.do_something, f"{TestState.get_full_name()}.do_something"),
  453. (
  454. ChildState.change_both,
  455. f"{ChildState.get_full_name()}.change_both",
  456. ),
  457. (
  458. GrandchildState.do_nothing,
  459. f"{GrandchildState.get_full_name()}.do_nothing",
  460. ),
  461. ],
  462. )
  463. def test_format_event_handler(input, output):
  464. """Test formatting an event handler.
  465. Args:
  466. input: The event handler input.
  467. output: The expected output.
  468. """
  469. assert format.format_event_handler(input) == output # type: ignore
  470. @pytest.mark.parametrize(
  471. "input,output",
  472. [
  473. (
  474. EventSpec(handler=EventHandler(fn=mock_event)),
  475. '(Event("mock_event", ({ })))',
  476. ),
  477. ],
  478. )
  479. def test_format_event(input, output):
  480. assert str(LiteralVar.create(input)) == output
  481. @pytest.mark.parametrize(
  482. "input,output",
  483. [
  484. ({"query": {"k1": 1, "k2": 2}}, {"k1": 1, "k2": 2}),
  485. ({"query": {"k1": 1, "k-2": 2}}, {"k1": 1, "k_2": 2}),
  486. ],
  487. )
  488. def test_format_query_params(input, output):
  489. assert format.format_query_params(input) == output
  490. formatted_router = {
  491. "session": {"client_token": "", "client_ip": "", "session_id": ""},
  492. "headers": {
  493. "host": "",
  494. "origin": "",
  495. "upgrade": "",
  496. "connection": "",
  497. "cookie": "",
  498. "pragma": "",
  499. "cache_control": "",
  500. "user_agent": "",
  501. "sec_websocket_version": "",
  502. "sec_websocket_key": "",
  503. "sec_websocket_extensions": "",
  504. "accept_encoding": "",
  505. "accept_language": "",
  506. },
  507. "page": {
  508. "host": "",
  509. "path": "",
  510. "raw_path": "",
  511. "full_path": "",
  512. "full_raw_path": "",
  513. "params": {},
  514. },
  515. }
  516. @pytest.mark.parametrize(
  517. "input, output",
  518. [
  519. (
  520. TestState(_reflex_internal_init=True).dict(), # type: ignore
  521. {
  522. TestState.get_full_name(): {
  523. "array": [1, 2, 3.14],
  524. "complex": {
  525. 1: {"prop1": 42, "prop2": "hello"},
  526. 2: {"prop1": 42, "prop2": "hello"},
  527. },
  528. "dt": "1989-11-09 18:53:00+01:00",
  529. "fig": serialize_figure(go.Figure()),
  530. "key": "",
  531. "map_key": "a",
  532. "mapping": {"a": [1, 2, 3], "b": [4, 5, 6]},
  533. "num1": 0,
  534. "num2": 3.14,
  535. "obj": {"prop1": 42, "prop2": "hello"},
  536. "sum": 3.14,
  537. "upper": "",
  538. "router": formatted_router,
  539. },
  540. ChildState.get_full_name(): {
  541. "count": 23,
  542. "value": "",
  543. },
  544. ChildState2.get_full_name(): {"value": ""},
  545. ChildState3.get_full_name(): {"value": ""},
  546. GrandchildState.get_full_name(): {"value2": ""},
  547. GrandchildState2.get_full_name(): {"cached": ""},
  548. GrandchildState3.get_full_name(): {"computed": ""},
  549. },
  550. ),
  551. (
  552. DateTimeState(_reflex_internal_init=True).dict(), # type: ignore
  553. {
  554. DateTimeState.get_full_name(): {
  555. "d": "1989-11-09",
  556. "dt": "1989-11-09 18:53:00+01:00",
  557. "t": "18:53:00+01:00",
  558. "td": "11 days, 0:11:00",
  559. "router": formatted_router,
  560. },
  561. },
  562. ),
  563. ],
  564. )
  565. def test_format_state(input, output):
  566. """Test that the format state is correct.
  567. Args:
  568. input: The state to format.
  569. output: The expected formatted state.
  570. """
  571. assert format.format_state(input) == output
  572. @pytest.mark.parametrize(
  573. "input,output",
  574. [
  575. ("input1", "ref_input1"),
  576. ("input 1", "ref_input_1"),
  577. ("input-1", "ref_input_1"),
  578. ("input_1", "ref_input_1"),
  579. ("a long test?1! name", "ref_a_long_test_1_name"),
  580. ],
  581. )
  582. def test_format_ref(input, output):
  583. """Test formatting a ref.
  584. Args:
  585. input: The name to format.
  586. output: The expected formatted name.
  587. """
  588. assert format.format_ref(input) == output
  589. @pytest.mark.parametrize(
  590. "input,output",
  591. [
  592. (("my_array", None), "refs_my_array"),
  593. (("my_array", LiteralVar.create(0)), "refs_my_array[0]"),
  594. (("my_array", LiteralVar.create(1)), "refs_my_array[1]"),
  595. ],
  596. )
  597. def test_format_array_ref(input, output):
  598. assert format.format_array_ref(input[0], input[1]) == output
  599. @pytest.mark.parametrize(
  600. "input, output",
  601. [
  602. ("library@^0.1.2", "library"),
  603. ("library", "library"),
  604. ("@library@^0.1.2", "@library"),
  605. ("@library", "@library"),
  606. ],
  607. )
  608. def test_format_library_name(input: str, output: str):
  609. """Test formating a library name to remove the @version part.
  610. Args:
  611. input: the input string.
  612. output: the output string.
  613. """
  614. assert format.format_library_name(input) == output
  615. @pytest.mark.parametrize(
  616. "input,output",
  617. [
  618. (None, "null"),
  619. (True, "true"),
  620. (1, "1"),
  621. (1.0, "1.0"),
  622. ([], "[]"),
  623. ([1, 2, 3], "[1, 2, 3]"),
  624. ({}, "{}"),
  625. ({"k1": False, "k2": True}, '{"k1": false, "k2": true}'),
  626. (
  627. [datetime.timedelta(1, 1, 1), datetime.timedelta(1, 1, 2)],
  628. '["1 day, 0:00:01.000001", "1 day, 0:00:01.000002"]',
  629. ),
  630. (
  631. {"key1": datetime.timedelta(1, 1, 1), "key2": datetime.timedelta(1, 1, 2)},
  632. '{"key1": "1 day, 0:00:01.000001", "key2": "1 day, 0:00:01.000002"}',
  633. ),
  634. ],
  635. )
  636. def test_json_dumps(input, output):
  637. assert format.json_dumps(input) == output