← 메인 화면프로젝트 자료·코드
← 자료 목록

cc/tests/test_app.py

프로그램·코드 · 2.5 KB원본 다운로드

원본 내용을 읽기 전용으로 표시합니다.

  1. import math
  2. import numpy as np
  3. import pytest
  4. from startup_app.app import create_app, 숫자정리
  5. class FakeEngine:
  6. now = [1] * 20
  7. 모델 = {}
  8. boundary = None
  9. mid_to_cat = {"커피 전문점": "음식"}
  10. def 위치확인(self, lat, lon):
  11. return math.isfinite(lat) and math.isfinite(lon) and 36.4 < lat < 36.7 and 127.1 < lon < 127.4
  12. def 메타(self):
  13. return {"업종": {"음식": ["커피 전문점"]}}
  14. def 분석(self, lat, lon, category):
  15. return {"업종": category, "count": np.int64(3), "missing": float("nan")}
  16. def 업종추천(self, lat, lon):
  17. return {"상태": "표시대상없음", "적은편": [], "많은편": []}
  18. @pytest.fixture
  19. def client():
  20. return create_app(FakeEngine()).test_client()
  21. @pytest.mark.parametrize("payload", [None, [], 1, "text", {}, {"lat": True, "lon": 127.2}, {"lat": "NaN", "lon": 127.2}, {"lat": 36.5, "lon": "Infinity"}, {"lat": 36.5, "lon": 127.2, "mid": []}, {"lat": 36.5, "lon": 127.2, "mid": {}}, {"lat": 36.5, "lon": 127.2, "mid": "없는 업종"}, {"lat": 37, "lon": 127.2, "mid": "커피 전문점"}])
  22. def test_invalid_requests_are_client_errors(client, payload):
  23. response = client.post("/api/analyze", json=payload)
  24. assert response.status_code == 400
  25. assert "오류" in response.json
  26. def test_valid_request_and_strict_json(client):
  27. response = client.post("/api/analyze", json={"lat": 36.5, "lon": 127.2, "mid": "커피 전문점"})
  28. assert response.status_code == 200
  29. assert response.json == {"업종": "커피 전문점", "count": 3, "missing": None}
  30. assert "NaN" not in response.text
  31. assert response.headers["Cache-Control"] == "no-store"
  32. def test_bad_json_and_size_limit(client):
  33. assert client.post("/api/analyze", data="{", content_type="application/json").status_code == 400
  34. assert client.post("/api/analyze", data="x" * 17000, content_type="application/json").status_code == 413
  35. def test_pages_and_methods(client):
  36. assert client.get("/").status_code == 200
  37. assert client.get("/healthz").json["status"] == "ok"
  38. assert client.get("/api/meta").json["업종"]["음식"] == ["커피 전문점"]
  39. assert client.get("/api/analyze").status_code == 405
  40. assert client.get("/not-here").status_code == 404
  41. assert client.get("/api/boundary").json["features"] == []
  42. assert client.post("/api/recommend", json={"lat": 36.5, "lon": 127.2}).json["상태"] == "표시대상없음"
  43. def test_json_cleanup():
  44. assert 숫자정리({"values": (np.float64("inf"), np.int32(2))}) == {"values": [None, 2]}
파일 확인 정보

SHA-256: 318aa50e97bb76af25c85ec726527878a53e39a5b32291b6c92d648a22d81a07