diff --git a/backend/services/resouce.py b/backend/services/resouce.py new file mode 100644 index 0000000..ead6780 --- /dev/null +++ b/backend/services/resouce.py @@ -0,0 +1,9 @@ +from fastapi import Depends +from ..database import db_session +from sqlalchemy.orm import Session + + +class ResourceService: + + def __init__(self, session: Session = Depends(db_session)): + self._session = session diff --git a/backend/services/service.py b/backend/services/service.py new file mode 100644 index 0000000..28a9e3c --- /dev/null +++ b/backend/services/service.py @@ -0,0 +1,9 @@ +from fastapi import Depends +from ..database import db_session +from sqlalchemy.orm import Session + + +class ServiceService: + + def __init__(self, session: Session = Depends(db_session)): + self._session = session diff --git a/backend/services/tag.py b/backend/services/tag.py new file mode 100644 index 0000000..b66f1fe --- /dev/null +++ b/backend/services/tag.py @@ -0,0 +1,9 @@ +from fastapi import Depends +from ..database import db_session +from sqlalchemy.orm import Session + + +class TagService: + + def __init__(self, session: Session = Depends(db_session)): + self._session = session diff --git a/backend/services/user.py b/backend/services/user.py new file mode 100644 index 0000000..629e22b --- /dev/null +++ b/backend/services/user.py @@ -0,0 +1,9 @@ +from fastapi import Depends +from ..database import db_session +from sqlalchemy.orm import Session + + +class UserService: + + def __init__(self, session: Session = Depends(db_session)): + self._session = session diff --git a/backend/test/README.md b/backend/test/README.md new file mode 100644 index 0000000..0599adb --- /dev/null +++ b/backend/test/README.md @@ -0,0 +1,53 @@ +# Testing + +## Backend + +### Organization + +Tests for `backend` code use [Pytest](https://doc.pytest.org/) and are organized in the `backend/test` directory +with subdirectories that mirror the package structure. + +The file `backend/test/conftest.py` defines fixtures for automatically setting up and tearing down a test database for backend services to use. + +At present, we do not have automated front-end testing instrumented; this remains a goal. + +### Pytest CLI + +The `pytest` command-line program will run all tests in the command-line. + +To see `print` output, run Pytest with the special extra output flag `pytest -rP`. + +To limit the scope of your tests to a specific file, include the path to the file following the command, eg: + +`pytest backend/test/services/user_test.py` + +To run a specific test within a test suite, use the [`-k` option of `pytest`](https://docs.pytest.org/en/latest/example/markers.html#using-k-expr-to-select-tests-based-on-their-name) to match all or part of the filtered test name(s): + +`pytest backend/test/services/user_test.py -k test_get` + +### Pytest VSCode with Debugger + +VSCode's Python plugin has great support for testing. Click the test tube icon, configure VSCode to use Pytest and select the workspace. + +When you refresh, you will see tests you can run individually, or in the debugger and with breakpoints. When you encounter a bug or failing test and having a difficult time pinning down exactly why it is failing, developing the instinct to run the test in the VSCode debugger, setting a break point, and stepping through is encouraged. + +For more, see the [official documentation](https://code.visualstudio.com/docs/python/testing). + +### Code Coverage + +We expect 100% test coverage of backend services code and as much coverage for other code in the backend. + +To generate a test coverage report, run the following command in your development container: + +`pytest --cov-report html:coverage --cov=backend/services backend/test/services` + +This command generates a directory with an HTML report. To view it, on your _host machine_, open the `coverage` directory's `index.html` file. Click on the service file you are working on to see the lines not covered by test cases if you are below 100%. After adding test cases that cover the missing lines, rerun the coverage command to generate a new report and confirm your progress. + +## Writing Tests + +1. Depending on what you are writing tests for, create the testing file in the associated directory (e.g. writing tests for services should be in the backend/tests/services directory +2. Name the file as [tested_item]\_test.py and any functions inside the file should be prefixed with test\_[tested_function] to be recognized by **pytest** +3. Tests should be created in a way to test the main functionality, edge cases, and error handling (look at test's on the csxl repo for inspiration) +4. Run specific functions by running this command while in the /workspace directory + - pytest backend/test/[test_directory]/[test_file]::[function_name] -s + - -s flag allows you to show print statements in the console which is defaulted to not show diff --git a/backend/test/entities/sample_test.py b/backend/test/entities/sample_test.py index 637769c..d0cab09 100644 --- a/backend/test/entities/sample_test.py +++ b/backend/test/entities/sample_test.py @@ -9,7 +9,7 @@ from ...entities.sample_entity import SampleEntity def test_entity_count(): """Checks the number of entities to be inserted""" print(entities.EntityBase.metadata.tables.keys()) - assert len(entities.EntityBase.metadata.tables.keys()) == 1 + assert len(entities.EntityBase.metadata.tables.keys()) == 7 def test_add_sample_data(session: Engine): diff --git a/backend/test/services/resource_test.py b/backend/test/services/resource_test.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/test/services/service_test.py b/backend/test/services/service_test.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/test/services/tag_test.py b/backend/test/services/tag_test.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/test/services/user_test.py b/backend/test/services/user_test.py new file mode 100644 index 0000000..e69de29