æ¬è¨äºã¯ãµã¼ãã¼ã¯ã¼ã¯ã¹ Advent Calendar 2023 ã® 13æ¥ç®ã®ã¨ã³ããªã¼ã§ãã
ã¯ããã«
æè¿ãAWS CDK ã使ã£ãéçºããã¦ããã®ã§ãããAWS CDK ã®ãã¹ãææ³ã¨ãã¦ã¹ãããã·ã§ãããã¹ãã¨å¼ã°ããææ³ããããããã AWS CDK 以å¤ã®éçºã«ãå¿ç¨ã§ãããã ãªã¨çæ³ãå¾ãã®ãæ¬è¨äºãæ¸ããã£ããã§ãã
ã¹ãããã·ã§ãããã¹ãã¨ã¯
ChatGPT ã«ããã¨ä»¥ä¸ã®ãããªãã¹ãææ³ãæãã¾ãã
ã¹ãããã·ã§ãããã¹ãã¯ãããã°ã©ã ãã³ã¼ãã®ãã¹ãææ³ã®ä¸ã¤ã§ãããã®ãã¹ãã§ã¯ãç¹å®ã®æç¹ã§ã®ã¢ããªã±ã¼ã·ã§ã³ã®åºåãç¶æ ããã¹ãããã·ã§ãããã¨ãã¦è¨é²ãããã®å¾ã®ã¢ããªã±ã¼ã·ã§ã³ã®å¤æ´ãæå¾ éãã®çµæãããããã¦ãããã確èªãã¾ãã
AWS CDK ã®å ´åãAWS ãªã½ã¼ã¹ãããã°ã©ãã³ã°è¨èªã§è¨è¿°ããå¾ãSynthesizeï¼åæï¼ã¨å¼ã°ããæä½ã«ã¦ CloudFormation ã«å¤æãããããªæµããã¨ãã¾ããå¤æãã CloudFormation ãã³ãã¬ã¼ããã¹ãããã·ã§ããã¨ã㦠Git 管çãã¦ããã次å以éã®å¤æçµæã¨æ¯è¼ãããã¨ã§ã¬ã°ãªãã·ã§ã³ã®ç¢ºèªããããã¯æå³ããå¤æ´ãã§ãã¦ãããã¨ã確èªãããã¨ãã§ãã¾ãã
AWS CDK ã®ä»ã«ããããã³ãã¨ã³ãéçºã®é åã§ã³ã³ãã¼ãã³ãã®ã¹ãããã·ã§ãããã¹ããå®æ½ãã¦ãããã¨ã観測ãã¦ãã¾ããã
ä»ã®é åã§ã使ããã®ã§ã¯
çè ã¯ä¸è¿°ã® AWS CDK ã使ã£ãéçºã®ä»ã«ããPython ã使ã£ããµã¼ãã¼ãµã¤ãã»ãããå¦ççã®éçºã«ãå¾äºãã¦ããããã®é åã«ãã¹ãããã·ã§ãããã¹ããæ´»ããå ´é¢ãããã¨èãã¾ãããä¾ãã°ä»¥ä¸ã®ãããªã¢ããªã±ã¼ã·ã§ã³ã§ãã
説æã®ããé常ã«ç°¡åãªä¾ã¨ãã¦ãã¾ããããã¡ã¤ã«ãå ¥åãå å·¥ãã¦åºåãããããªã¢ããªã±ã¼ã·ã§ã³ã§ãããã®ãããªã¢ããªã±ã¼ã·ã§ã³ãä¿å®ãã¦ããå ´åã以ä¸ã®ãããªç¹ã注æããå¿ è¦ãããã¾ãã
- ä»æ§å¤æ´æãå¤æ´ãã¦ããªãé¨åã«å½±é¿ããªããã¨ãæ ä¿ããå¿ è¦ããã
- ã©ã³ã¿ã¤ã ãä¾åã©ã¤ãã©ãªã®ã¢ãããã¼ãæãåºåçµæãå¤ãããªããã¨ãæ ä¿ããå¿ è¦ããã
åºåãã¡ã¤ã«ãå¥ããã°ã©ã ã®å ¥åã¨ãªãçã確å®ã«åä¸æ§ãæ ä¿ããå¿ è¦ãããå ´åã¯ç¹ã«æ³¨æãå¿ è¦ã§ãã
ãã¡ã¤ã«åºåããã°ã©ã ã«ã¹ãããã·ã§ãããã¹ããå°å ¥ãã
以ä¸ã®ãããªããã°ã©ã ã«ã¹ãããã·ã§ãããã¹ããå°å ¥ãã¦ã¿ã¾ããjson ãã¡ã¤ã«ãå ¥åã¨ãããã®å 容ãåºã« csv ãã¡ã¤ã«ãåºåããã¢ããªã±ã¼ã·ã§ã³ã§ãã
from pathlib import Path import json from csv import DictWriter def json_to_csv(input_json_path: Path, output_csv_path: Path): with open(input_json_path, "r") as infile: json_data = json.load(infile) users = json_data["users"] with open(output_csv_path, "w") as outfile: writer = DictWriter(outfile, fieldnames=users[0].keys()) writer.writerows(users) if __name__ == "__main__": json_to_csv(Path("input.json"), Path("output.csv"))
以ä¸ã®ãã㪠json ãã¡ã¤ã«ãå ¥åã¨ãã¾ãã
{ "users": [ { "name": "John", "age": 22, "height": 1.75 }, { "name": "Peter", "age": 30, "height": 1.8 }, { "name": "Mary", "age": 25, "height": 1.65 } ] }
åºåçµæã¯ä»¥ä¸ã®éãã«ãªãã¾ãã
John,22,1.75 Peter,30,1.8 Mary,25,1.65
ä»åã¯Python ã®ã¹ãããã·ã§ãããã¹ãã©ã¤ãã©ãªã¨ãã¦ãpytest ã®ãã©ã°ã¤ã³ã¨ãã¦ä½¿ç¨å¯è½ãª pytest-snapshot ã使ç¨ãã¾ãã
åè¿°ã®ããã°ã©ã å
ã® json_to_csv
ã«å¯¾ããã¹ãããã·ã§ãããã¹ãã¯ä»¥ä¸ã®éãã§ãã
from src.main import json_to_csv from pathlib import Path # pytest-snapshot ãå°å ¥ãã㨠`snapshot` ã¨ããååã® fixture ãå©ç¨å¯è½ def test_json_to_csv(snapshot, tmp_path): output_csv_path = Path(tmp_path / "output.csv") # ãã¹ã対象ã¡ã½ããã®å®è¡ json_to_csv(Path("input.json"), output_csv_path) # ã¹ãããã·ã§ãããã¡ã¤ã«ã®åºåå ãæå® snapshot.snapshot_dir = Path("tests/snapshots") # ã¹ãããã·ã§ããå·®åã®æç¡ã assert # tests/snapshots/snapshot.csv ã«ã¹ãããã·ã§ãããåºåããã snapshot.assert_match(output_csv_path.read_text(), "snapshot.csv")
ãã®ãã¹ãã³ã¼ããå®è¡ãã¾ãã
$ pytest ============================================================== test session starts ============================================================== platform darwin -- Python 3.11.4, pytest-7.4.3, pluggy-1.3.0 rootdir: /Users/t3yamoto/dev/src/github.com/t3yamoto/snapshot-testing-for-file-write plugins: snapshot-0.9.0 collected 1 item tests/test_main.py F [100%] =================================================================== FAILURES ==================================================================== _______________________________________________________________ test_json_to_csv ________________________________________________________________ snapshot = <pytest_snapshot.plugin.Snapshot object at 0x105731950> tmp_path = PosixPath('/private/var/folders/_3/v6w_pffd2zg106nh1040xjnw0000gn/T/pytest-of-t3yamoto/pytest-10/test_json_to_csv0') def test_json_to_csv(snapshot, tmp_path): output_csv_path = Path(tmp_path / "output.csv") json_to_csv(Path("input.json"), output_csv_path) snapshot.snapshot_dir = Path("tests/snapshots") > snapshot.assert_match(output_csv_path.read_text(), "snapshot.csv") E AssertionError: snapshot tests/snapshots/snapshot.csv doesn't exist. (run pytest with --snapshot-update to create it) tests/test_main.py:10: AssertionError ============================================================ short test summary info ============================================================ FAILED tests/test_main.py::test_json_to_csv - AssertionError: snapshot tests/snapshots/snapshot.csv doesn't exist. (run pytest with --snapshot-update to create it) =============================================================== 1 failed in 0.02s ===============================================================
E AssertionError: snapshot tests/snapshots/snapshot.csv doesn't exist. (run pytest with --snapshot-update to create it)
ååã¯ã¹ãããã·ã§ãããåå¨ããªãããã¨ã©ã¼ã«ãªãã¾ããã¹ãããã·ã§ããä½æã®ããã--snapshot-update
ãªãã·ã§ã³ãã¤ãã¦å®è¡ãã¾ãã
$ pytest --snapshot-update ============================================================== test session starts ============================================================== platform darwin -- Python 3.11.4, pytest-7.4.3, pluggy-1.3.0 rootdir: /Users/t3yamoto/dev/src/github.com/t3yamoto/snapshot-testing-for-file-write plugins: snapshot-0.9.0 collected 1 item tests/test_main.py .E [100%] ==================================================================== ERRORS ===================================================================== _____________________________________________________ ERROR at teardown of test_json_to_csv _____________________________________________________ Snapshot directory was modified: tests/snapshots (verify that the changes are expected before committing them to version control) Created snapshots: snapshot.csv ============================================================ short test summary info ============================================================ ERROR tests/test_main.py::test_json_to_csv - Failed: Snapshot directory was modified: tests/snapshots ========================================================== 1 passed, 1 error in 0.01s ===========================================================
tests/snapshots/snapshot.csv
ã«ã¹ãããã·ã§ãããåºåããã¾ãããããã Git ã®ç®¡ç対象ã¨ãã¦ããã¾ãã
ãã®ç¶æ
ã§å度ãã¹ããå®è¡ããã¨...
$ pytest ============================================================== test session starts ============================================================== platform darwin -- Python 3.11.4, pytest-7.4.3, pluggy-1.3.0 rootdir: /Users/t3yamoto/dev/src/github.com/t3yamoto/snapshot-testing-for-file-write plugins: snapshot-0.9.0 collected 1 item tests/test_main.py . [100%] =============================================================== 1 passed in 0.01s ===============================================================
ãã¹ãããã¹ãã¾ãããããã¯ååä¿åããã¹ãããã·ã§ãã㨠json_to_csv
ã¡ã½ããã®åºåçµæã«å·®åããªãã£ãããã§ãã
ã§ã¯ãjson_to_csv
ã¡ã½ããã«å¤æ´ãå ãã¦ã¿ã¾ãã
from pathlib import Path import json from csv import DictWriter def json_to_csv(input_json_path: Path, output_csv_path: Path): with open(input_json_path, "r") as infile: json_data = json.load(infile) users = json_data["users"] with open(output_csv_path, "w") as outfile: writer = DictWriter(outfile, fieldnames=users[0].keys()) writer.writeheader() # è¦åºãè¡ãåºåããã writer.writerows(users) if __name__ == "__main__": json_to_csv(Path("input.json"), Path("output.csv"))
10è¡ç®ã®éããè¦åºãè¡ãåºåãããããã«å¤æ´ãã¾ãããããã¯æå³ããªãå¤æ´ã§ãã
ã¹ãããã·ã§ãããã¹ããå®è¡ãã¾ãã
$ pytest ============================================================== test session starts ============================================================== platform darwin -- Python 3.11.4, pytest-7.4.3, pluggy-1.3.0 rootdir: /Users/t3yamoto/dev/src/github.com/t3yamoto/snapshot-testing-for-file-write plugins: snapshot-0.9.0 collected 1 item tests/test_main.py F [100%] =================================================================== FAILURES ==================================================================== _______________________________________________________________ test_json_to_csv ________________________________________________________________ snapshot = <pytest_snapshot.plugin.Snapshot object at 0x102499cd0> tmp_path = PosixPath('/private/var/folders/_3/v6w_pffd2zg106nh1040xjnw0000gn/T/pytest-of-t3yamoto/pytest-15/test_json_to_csv0') def test_json_to_csv(snapshot, tmp_path): output_csv_path = Path(tmp_path / "output.csv") json_to_csv(Path("input.json"), output_csv_path) snapshot.snapshot_dir = Path("tests/snapshots") > snapshot.assert_match(output_csv_path.read_text(), "snapshot.csv") E AssertionError: value does not match the expected value in snapshot tests/snapshots/snapshot.csv E (run pytest with --snapshot-update to update snapshots) E assert 'name,age,hei...ary,25,1.65\n' == 'John,22,1.75...ary,25,1.65\n' E + name,age,height E John,22,1.75 E Peter,30,1.8 E Mary,25,1.65 tests/test_main.py:10: AssertionError ============================================================ short test summary info ============================================================ FAILED tests/test_main.py::test_json_to_csv - AssertionError: value does not match the expected value in snapshot tests/snapshots/snapshot.csv =============================================================== 1 failed in 0.02s ===============================================================
ã¹ãããã·ã§ãããã¹ãã失æããæå³ããªãå·®åãæ¤ç¥ãããã¨ãã§ãã¾ãããE + name,age,height
ã®é¨åãå·®åã§ãã
ã¾ã¨ã
ãã¡ã¤ã«åºåãè¡ãã¢ããªã±ã¼ã·ã§ã³ã«ãã¹ãããã·ã§ãããã¹ããå°å ¥ãã¾ãããé·æçã«ä¿å®ããã¢ããªã±ã¼ã·ã§ã³ã®ã¬ã°ãªãã·ã§ã³ãã¹ãã¨ãã¦ç¹ã«å¹æãçºæ®ããã®ã§ã¯ãªããã¨æãã¾ãã
以ä¸ãæ¤è¨¼ãå®æ½ãããªãã¸ããªã§ããå ¨ä½åã®ææ¡ã«ãå©ç¨ãã ããã github.com