alessandro trinca tornidor commited on
Commit
552d671
·
1 Parent(s): 37f509f

test: refactored test execution

Browse files
.coveragerc CHANGED
@@ -1,9 +1,9 @@
1
  [run]
2
  source = samgis
3
- omit = ./venv/*,__version__.py,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py,./tests/*
4
 
5
  [report]
6
  omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py,./tests/*
7
 
8
  exclude_lines =
9
- if __name__ == .__main__.:
 
1
  [run]
2
  source = samgis
3
+ omit = ./venv/*,__version__.py,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py,./tests/*,./scripts/extract-*.py
4
 
5
  [report]
6
  omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py,./tests/*
7
 
8
  exclude_lines =
9
+ if __name__\s*==\s*['"]__main__['"]\s*:
pyproject.toml CHANGED
@@ -50,7 +50,7 @@ myst-parser = "^4.0.0"
50
  sphinx-autodoc-defaultargs = "^0.1.2"
51
 
52
  [tool.pytest.ini_options]
53
- addopts = "--cov=my_ghost_writer --cov-report html"
54
 
55
  [build-system]
56
  requires = ['poetry-core (>=2.0.0,<3.0.0)']
 
50
  sphinx-autodoc-defaultargs = "^0.1.2"
51
 
52
  [tool.pytest.ini_options]
53
+ addopts = " --cov=scripts --cov=app --cov-report html"
54
 
55
  [build-system]
56
  requires = ['poetry-core (>=2.0.0,<3.0.0)']
pytest.ini DELETED
@@ -1,7 +0,0 @@
1
- [pytest]
2
- env_override_existing_values = 1
3
- env_files =
4
- test/.test.env
5
- [path]
6
- source = app
7
- omit = ./venv/*,*tests*,*apps.py,*manage.py,*__init__.py,*migrations*,*asgi*,*wsgi*,*admin.py,*urls.py,./tests/*
 
 
 
 
 
 
 
 
scripts/client_health.py CHANGED
@@ -18,10 +18,6 @@ ch.setFormatter(formatter)
18
  logger.addHandler(ch)
19
 
20
 
21
- # def aa():
22
- # pass
23
-
24
-
25
  def simple_formatter_ex(ex: Exception):
26
  logger.error(type(ex))
27
  logger.error(ex)
 
18
  logger.addHandler(ch)
19
 
20
 
 
 
 
 
21
  def simple_formatter_ex(ex: Exception):
22
  logger.error(type(ex))
23
  logger.error(ex)
scripts/extract-openapi-fastapi.py CHANGED
@@ -10,13 +10,11 @@ from uvicorn.importer import import_from_string
10
  from app import project_root_folder
11
 
12
 
13
- parser = argparse.ArgumentParser(prog="extract-openapi-fastapi.py")
14
- parser.add_argument("app", help='App import string. Eg. "main:app"', default="main:app")
15
- parser.add_argument("--app-dir", help="Directory containing the app", default=None)
16
-
17
-
18
- if __name__ == "__main__":
19
  # python scripts/extract-openapi.py fastapi_wrapper:app --app-dir wrappers --out docs/specs/openapi_new.yaml
 
 
 
20
  args = parser.parse_args()
21
 
22
  if args.app_dir is not None:
 
10
  from app import project_root_folder
11
 
12
 
13
+ if __name__ == "__main__": # pragma: no cover
 
 
 
 
 
14
  # python scripts/extract-openapi.py fastapi_wrapper:app --app-dir wrappers --out docs/specs/openapi_new.yaml
15
+ parser = argparse.ArgumentParser(prog="extract-openapi-fastapi.py")
16
+ parser.add_argument("app", help='App import string. Eg. "main:app"', default="main:app")
17
+ parser.add_argument("--app-dir", help="Directory containing the app", default=None)
18
  args = parser.parse_args()
19
 
20
  if args.app_dir is not None:
tests/test_app.py CHANGED
@@ -7,7 +7,7 @@ from unittest.mock import patch
7
  from fastapi.testclient import TestClient
8
  from samgis_web.utilities.local_tiles_http_server import LocalTilesHttpServer
9
  from samgis_web.web import web_helpers
10
- from . import test_health
11
 
12
  import app
13
 
@@ -51,6 +51,7 @@ response_bodies_post_test = {
51
  }
52
  }
53
 
 
54
  def check_body(body: dict, expected_body: dict):
55
  if body != expected_body:
56
  logging.error(f"Wrong test: body not {body}.")
@@ -60,18 +61,18 @@ def check_body(body: dict, expected_body: dict):
60
  class TestFastapiApp(unittest.TestCase):
61
  def test_fastapi_handler_health_200(self):
62
  response = client.get("/health")
63
- test_health.check_for_statuscode(response.status_code, 200, response)
64
  body = response.json()
65
  check_body(body, {"msg": "still alive..."})
66
 
67
  def test_404(self):
68
  response = client.get("/404")
69
- test_health.check_for_statuscode(response.status_code, 404, response)
70
 
71
  def test_infer_samgis_empty_body_422(self):
72
  response = client.post(infer_samgis, json={})
73
  print(response_status_code.format(response.status_code))
74
- test_health.check_for_statuscode(response.status_code, 422, response)
75
  body = response.json()
76
  logging.info(response_body_loaded.format(body))
77
  check_body(body, {"msg": "Error - Unprocessable Entity"})
@@ -83,7 +84,7 @@ class TestFastapiApp(unittest.TestCase):
83
  local_event["source_type"] = "source_fake"
84
  response = client.post(infer_samgis, json=local_event)
85
  logging.info(response_status_code.format(response.status_code))
86
- test_health.check_for_statuscode(response.status_code, 422, response)
87
  body = response.json()
88
  logging.info(response_body_loaded.format(body))
89
  check_body(body, {"msg": "Error - Unprocessable Entity"})
@@ -94,7 +95,7 @@ class TestFastapiApp(unittest.TestCase):
94
 
95
  response = client.post(infer_samgis, json=event)
96
  logging.info(response_status_code.format(response.status_code))
97
- test_health.check_for_statuscode(response.status_code, 500, response)
98
  body = response.json()
99
  logging.info(response_body_loaded.format(body))
100
  check_body(body, {"msg": "Error - Internal Server Error"})
@@ -115,7 +116,7 @@ class TestFastapiApp(unittest.TestCase):
115
  with LocalTilesHttpServer.http_server("localhost", listen_port, directory=TEST_EVENTS_FOLDER):
116
  response = client.post(infer_samgis, json=event)
117
  logging.info(response_status_code.format(response.status_code))
118
- test_health.check_for_statuscode(response.status_code, 200, response)
119
  body_string = response.json()["body"]
120
  body_loaded = json.loads(body_string)
121
  logging.info(response_body_loaded.format(body_loaded))
@@ -123,7 +124,7 @@ class TestFastapiApp(unittest.TestCase):
123
  raise ValueError("Missing value in body")
124
  output = body_loaded["output"]
125
  if not ('n_predictions' in output and "n_shapes_geojson" in output):
126
- raise ValueError("Missing value in body/2")
127
  geojson = output["geojson"]
128
  output_geojson = shapely.from_geojson(geojson)
129
  logging.info("output_geojson::{}.".format(output_geojson))
@@ -150,7 +151,7 @@ class TestFastapiApp(unittest.TestCase):
150
 
151
  response = client.post(infer_samgis, json=event)
152
  logging.info(response_status_code.format(response.status_code))
153
- test_health.check_for_statuscode(response.status_code, 200, response)
154
  response_json = response.json()
155
  body_loaded = json.loads(response_json["body"])
156
  logging.info(response_body_loaded.format(body_loaded))
 
7
  from fastapi.testclient import TestClient
8
  from samgis_web.utilities.local_tiles_http_server import LocalTilesHttpServer
9
  from samgis_web.web import web_helpers
10
+ from . import test_client_health
11
 
12
  import app
13
 
 
51
  }
52
  }
53
 
54
+
55
  def check_body(body: dict, expected_body: dict):
56
  if body != expected_body:
57
  logging.error(f"Wrong test: body not {body}.")
 
61
  class TestFastapiApp(unittest.TestCase):
62
  def test_fastapi_handler_health_200(self):
63
  response = client.get("/health")
64
+ test_client_health.check_for_statuscode(response.status_code, 200, response)
65
  body = response.json()
66
  check_body(body, {"msg": "still alive..."})
67
 
68
  def test_404(self):
69
  response = client.get("/404")
70
+ test_client_health.check_for_statuscode(response.status_code, 404, response)
71
 
72
  def test_infer_samgis_empty_body_422(self):
73
  response = client.post(infer_samgis, json={})
74
  print(response_status_code.format(response.status_code))
75
+ test_client_health.check_for_statuscode(response.status_code, 422, response)
76
  body = response.json()
77
  logging.info(response_body_loaded.format(body))
78
  check_body(body, {"msg": "Error - Unprocessable Entity"})
 
84
  local_event["source_type"] = "source_fake"
85
  response = client.post(infer_samgis, json=local_event)
86
  logging.info(response_status_code.format(response.status_code))
87
+ test_client_health.check_for_statuscode(response.status_code, 422, response)
88
  body = response.json()
89
  logging.info(response_body_loaded.format(body))
90
  check_body(body, {"msg": "Error - Unprocessable Entity"})
 
95
 
96
  response = client.post(infer_samgis, json=event)
97
  logging.info(response_status_code.format(response.status_code))
98
+ test_client_health.check_for_statuscode(response.status_code, 500, response)
99
  body = response.json()
100
  logging.info(response_body_loaded.format(body))
101
  check_body(body, {"msg": "Error - Internal Server Error"})
 
116
  with LocalTilesHttpServer.http_server("localhost", listen_port, directory=TEST_EVENTS_FOLDER):
117
  response = client.post(infer_samgis, json=event)
118
  logging.info(response_status_code.format(response.status_code))
119
+ test_client_health.check_for_statuscode(response.status_code, 200, response)
120
  body_string = response.json()["body"]
121
  body_loaded = json.loads(body_string)
122
  logging.info(response_body_loaded.format(body_loaded))
 
124
  raise ValueError("Missing value in body")
125
  output = body_loaded["output"]
126
  if not ('n_predictions' in output and "n_shapes_geojson" in output):
127
+ raise ValueError("Missing value in body")
128
  geojson = output["geojson"]
129
  output_geojson = shapely.from_geojson(geojson)
130
  logging.info("output_geojson::{}.".format(output_geojson))
 
151
 
152
  response = client.post(infer_samgis, json=event)
153
  logging.info(response_status_code.format(response.status_code))
154
+ test_client_health.check_for_statuscode(response.status_code, 200, response)
155
  response_json = response.json()
156
  body_loaded = json.loads(response_json["body"])
157
  logging.info(response_body_loaded.format(body_loaded))
tests/{test_health.py → test_client_health.py} RENAMED
File without changes