test.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import os
  2. import httpx
  3. BASE_URL = "http://localhost:8000"
  4. EXPORT_PATH = "downloaded"
  5. def test_full_pipeline():
  6. with open("sample.csv", "rb") as f:
  7. upload = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  8. session_id = upload.json()["session_id"]
  9. filter_ = httpx.post(f"{BASE_URL}/filter/", data={
  10. "session_id": session_id,
  11. "dt": 1.125e-7,
  12. "center_freq": 2.97e6,
  13. "lower_freq": 2.92e6,
  14. "higher_freq": 3.02e6,
  15. "low_freq": 600e3
  16. })
  17. assert filter_.status_code == 200
  18. fft_ = httpx.post(f"{BASE_URL}/fft/", data={"session_id": session_id})
  19. assert fft_.status_code == 200
  20. result = httpx.get(f"{BASE_URL}/result/", params={"session_id": session_id})
  21. assert result.status_code == 200
  22. data = result.json()
  23. assert "fid" in data and "spectrum" in data and "frequency" in data
  24. def test_export_and_save():
  25. os.makedirs(EXPORT_PATH, exist_ok=True)
  26. # upload CSV
  27. with open("sample.csv", "rb") as f:
  28. r = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  29. session_id = r.json()["session_id"]
  30. # filter
  31. r = httpx.post(f"{BASE_URL}/filter/", data={
  32. "session_id": session_id,
  33. "dt": 1.125e-7,
  34. "center_freq": 2.97e6,
  35. "lower_freq": 2.92e6,
  36. "higher_freq": 3.02e6,
  37. "low_freq": 600e3
  38. })
  39. assert r.status_code == 200
  40. r = httpx.post(f"{BASE_URL}/fft/", data={"session_id": session_id})
  41. assert r.status_code == 200
  42. r = httpx.post(f"{BASE_URL}/export/", data={"session_id": session_id})
  43. assert r.status_code == 200
  44. data = r.json()
  45. for name, url in data.items():
  46. filename = url.split("/")[-1]
  47. full_url = f"{BASE_URL}{url}"
  48. response = httpx.get(full_url)
  49. assert response.status_code == 200
  50. file_path = os.path.join(EXPORT_PATH, filename)
  51. with open(file_path, "wb") as f:
  52. f.write(response.content)
  53. print(f"Saved: {file_path}")
  54. for filename in [f"{session_id}_fid.png", f"{session_id}_spectrum.png"]:
  55. assert os.path.exists(os.path.join(EXPORT_PATH, filename))
  56. def test_export_and_save():
  57. os.makedirs(EXPORT_PATH, exist_ok=True)
  58. with open("sample2.csv", "rb") as f:
  59. r = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  60. session_id = r.json()["session_id"]
  61. r = httpx.post(f"{BASE_URL}/filter/", data={
  62. "session_id": session_id,
  63. "dt": 1.125e-7,
  64. "center_freq": 2.97e6,
  65. "lower_freq": 2.92e6,
  66. "higher_freq": 3.02e6,
  67. "low_freq": 600e3
  68. })
  69. assert r.status_code == 200
  70. r = httpx.post(f"{BASE_URL}/fft/", data={"session_id": session_id})
  71. assert r.status_code == 200
  72. r = httpx.post(f"{BASE_URL}/export/", data={"session_id": session_id})
  73. assert r.status_code == 200
  74. data = r.json()
  75. for name, url in data.items():
  76. filename = url.split("/")[-1]
  77. full_url = f"{BASE_URL}{url}"
  78. response = httpx.get(full_url)
  79. assert response.status_code == 200
  80. with open(os.path.join(EXPORT_PATH, filename), "wb") as f:
  81. f.write(response.content)
  82. for name in ["fid", "spectrum"]:
  83. assert os.path.exists(os.path.join(EXPORT_PATH, f"{session_id}_{name}.png"))
  84. def test_plot_raw_generation():
  85. os.makedirs(EXPORT_PATH, exist_ok=True)
  86. with open("sample2.csv", "rb") as f:
  87. r = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  88. assert r.status_code == 200
  89. session_id = r.json()["session_id"]
  90. r = httpx.post(f"{BASE_URL}/plot-raw/", data={"session_id": session_id})
  91. assert r.status_code == 200
  92. raw_plot_url = r.json()["raw_plot"]
  93. filename = raw_plot_url.split("/")[-1]
  94. full_url = f"{BASE_URL}{raw_plot_url}"
  95. response = httpx.get(full_url)
  96. assert response.status_code == 200
  97. file_path = os.path.join(EXPORT_PATH, filename)
  98. with open(file_path, "wb") as f:
  99. f.write(response.content)
  100. assert os.path.exists(file_path)
  101. print(f"✓ Plot saved to {file_path}")
  102. def test_export_and_save_json():
  103. os.makedirs(EXPORT_PATH, exist_ok=True)
  104. with open("test.json", "rb") as f:
  105. r = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  106. assert r.status_code == 200
  107. session_id = r.json()["session_id"]
  108. r = httpx.post(f"{BASE_URL}/filter/", data={
  109. "session_id": session_id,
  110. "dt": 1.125e-7,
  111. "center_freq": 2.97e6,
  112. "lower_freq": 2.92e6,
  113. "higher_freq": 3.02e6,
  114. "low_freq": 600e3
  115. })
  116. assert r.status_code == 200
  117. r = httpx.post(f"{BASE_URL}/fft/", data={"session_id": session_id})
  118. assert r.status_code == 200
  119. r = httpx.post(f"{BASE_URL}/export/", data={"session_id": session_id})
  120. assert r.status_code == 200
  121. data = r.json()
  122. for name, url in data.items():
  123. filename = url.split("/")[-1]
  124. full_url = f"{BASE_URL}{url}"
  125. response = httpx.get(full_url)
  126. assert response.status_code == 200
  127. with open(os.path.join(EXPORT_PATH, filename), "wb") as f:
  128. f.write(response.content)
  129. for name in ["fid", "spectrum"]:
  130. file_path = os.path.join(EXPORT_PATH, f"{session_id}_{name}.png")
  131. assert os.path.exists(file_path)
  132. print(f"✓ Exported: {file_path}")
  133. def test_plot_raw_from_json():
  134. os.makedirs(EXPORT_PATH, exist_ok=True)
  135. with open("test.json", "rb") as f:
  136. r = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  137. assert r.status_code == 200
  138. session_id = r.json()["session_id"]
  139. r = httpx.post(f"{BASE_URL}/plot-raw/", data={"session_id": session_id})
  140. assert r.status_code == 200
  141. plot_url = r.json()["raw_plot"]
  142. filename = plot_url.split("/")[-1]
  143. full_url = f"{BASE_URL}{plot_url}"
  144. response = httpx.get(full_url)
  145. assert response.status_code == 200
  146. path = os.path.join(EXPORT_PATH, filename)
  147. with open(path, "wb") as f:
  148. f.write(response.content)
  149. assert os.path.exists(path)
  150. print(f"✓ Raw plot saved: {path}")
  151. def test_plot_raw_from_json():
  152. os.makedirs(EXPORT_PATH, exist_ok=True)
  153. with open("test.json", "rb") as f:
  154. r = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  155. assert r.status_code == 200, f"Upload failed: {r.text}"
  156. session_id = r.json()["session_id"]
  157. r = httpx.post(f"{BASE_URL}/plot-raw/", data={"session_id": session_id})
  158. assert r.status_code == 200, f"Plot raw failed: {r.text}"
  159. raw_plot_url = r.json()["raw_plot"]
  160. filename = raw_plot_url.split("/")[-1]
  161. response = httpx.get(f"{BASE_URL}{raw_plot_url}")
  162. assert response.status_code == 200, f"Download failed: {response.text}"
  163. file_path = os.path.join(EXPORT_PATH, filename)
  164. with open(file_path, "wb") as f:
  165. f.write(response.content)
  166. assert os.path.exists(file_path), f"File {file_path} not found"
  167. print(f"✓ Raw plot saved to {file_path}")
  168. def test_filter_and_fft_from_json():
  169. os.makedirs(EXPORT_PATH, exist_ok=True)
  170. with open("test2.json", "rb") as f:
  171. r = httpx.post(f"{BASE_URL}/upload/", files={"file": f})
  172. assert r.status_code == 200, f"Upload failed: {r.text}"
  173. session_id = r.json()["session_id"]
  174. r = httpx.post(f"{BASE_URL}/filter/", data={
  175. "session_id": session_id,
  176. "dt": 1.125e-7,
  177. "center_freq": 2.97e6,
  178. "lower_freq": 2.92e6,
  179. "higher_freq": 3.02e6,
  180. "low_freq": 600e3
  181. })
  182. assert r.status_code == 200, f"Filter failed: {r.text}"
  183. r = httpx.post(f"{BASE_URL}/fft/", data={"session_id": session_id})
  184. assert r.status_code == 200, f"FFT failed: {r.text}"
  185. r = httpx.post(f"{BASE_URL}/export/", data={"session_id": session_id})
  186. assert r.status_code == 200, f"Export failed: {r.text}"
  187. data = r.json()
  188. for name, url in data.items():
  189. filename = url.split("/")[-1]
  190. full_url = f"{BASE_URL}{url}"
  191. response = httpx.get(full_url)
  192. assert response.status_code == 200, f"Download failed for {filename}"
  193. file_path = os.path.join(EXPORT_PATH, filename)
  194. with open(file_path, "wb") as f:
  195. f.write(response.content)
  196. print(f"✓ Saved {file_path}")
  197. for name in ["fid", "spectrum"]:
  198. file_path = os.path.join(EXPORT_PATH, f"{session_id}_{name}.png")
  199. assert os.path.exists(file_path), f"{file_path} not found"