-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Populate Application DB instead of the Test DB #1731
Conversation
…cation. - Don't set FIDES__TEST_MODE in docker-compose - this should be set by pytest only - Update pytest's setting of FIDES__TEST_MODE to be the correct variable - Rename the ops "get_api_session" to "get_sync_session" to mirror the language that ctl is using. It is a session that uses a synchronous engine. - Fix logging test to remove need for unnecessary toggle_testing_envvar fixture - Update the ctl database crud methods to take in an async_session as a variable - Inject an async session as a dependency into the crud, datamap, and generate routes. Pass that session onto the crud methods. - Update ctl route utils to take in an async_session parameter - Update ops and ctl setup_db in tests to not use db_action method - instead use the TestClient as to not not wipe the application db - Add an async_session to the ctl conftest for use in testing - Have logging_sessions use debug logs on dev mode, not test mode (test mode is false now, when running application locally) - Have load_default_organization and load_default_taxonomy take in an async_session (ctl) - Load_default_dsr_policies (ops) still uses a sync session - For ctl, create an async_db generator for API endpoint dependency injection. Also create an async_session and async_sessionmaker that shares the same engine - Use the api client in several of the ctl tests, which is aware of test mode, instead of hitting the server.
- This verifies that if we're in test mode, we should be supplying a TestClient to these methods and we'll use that client to make the requests. Otherwise, we use the requests library which makes requests to the running webserver. (Also explored using httpx.Client to have both prod and testing use a "client" but this seems to just add an extra requirement. - A lot of methods were refactored to optionally pass in this client for "test mode" only.
…t fides_test db. - Fix AnalyticsClient developer_mode to reference dev mode instead of incorrect test mode (FIDES_TEST_MODE was incorrect variable anyway - it's FIDES__TEST_MODE)
after talking in a 1:1 chat, we discussed trying to monkey-patch |
…methods." This reverts commit 0255f59.
This reverts commit a89f0bb.
…tion level and ensure it is called before setup db to prevent wiping the application db during testing. - Remove changes that are no longer necessary with the monkeypatch changes in an attempt to reduce the size of the diff.
- Remove monkeypatching requests on ops side. Ping the reset URL in setup_db with the api_client instead of the requests library.
# Conflicts: # tests/ctl/api/test_seed.py
Issues still existing from the merge ^ will address |
…ctl side which is now being used in the seed tests.
…yncClient that was instantiated with the application so it is aware that we are in test mode and talks to the correct test db instead of the application db
…lication_fixtures to be used elsewhere. Monkeypatch Session.send to be TestClient.send (which is instantiated with the application and is test-mode aware) in testing.
^ I still have one test hanging for the FidesConnector work: tests/ops/service/connectors/test_fides_connector.py::TestFidesConnectorIntegration::test_retrieve_data after merging in main The FidesConnector/FidesClient use their own requests.Sessions and httpx.AsyncClient()s to make requests against the running webserver. I tried to monkeypatch similar to what we did on the -ctl side to use TestClients instead. This test is currently creating/queueing a privacy request but the privacy request status is not getting updated. EDIT Going back into draft mode |
Sounds good, lmk if I can help! |
a4123b2
to
7f42881
Compare
Conflicts: src/fides/api/ctl/routes/util.py src/fides/api/ops/service/connectors/fides/fides_client.py src/fides/cli/utils.py src/fides/ctl/core/config/logging_settings.py src/fides/ctl/core/config/utils.py tests/ctl/api/test_seed.py tests/ctl/cli/test_cli_utils.py tests/ctl/conftest.py tests/ops/conftest.py tests/ops/util/test_logger.py
…ConnectorIntegration.test_retrieve_data. Both of these will just make requests to the running webserver (which is talking to the application db), but we need them to talk to the test db. - If no privacy request found in poll_server_for_completion, raise a specific error instead of throwing a vague IndexError: list index out of range.
OK I think we're ready to go again! |
@ThomasLaPiana this PR has sat for awhile, as I deprioritized to work on unified-fides-resources, but with this last commit 834aae1, I think it's good to go. |
@pattisdr you're a hero, thank you! I've been anxiously awaiting! I'll re-review/retest 🙂 |
hooray! I think this functionality 0882943 that Adam merged while this was sitting might have helped tests pass too. Previously in trying to monkeypatch async clients in the fides connector test I had been running into errors with multiple event loops. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic work here! Excited to see this merged and making development easier 🙂
Appreciate the re-review @ThomasLaPiana! |
❗ This could create breaking changes in the -plus repos that would need to be handled before they are upgraded to use this
Closes #1175
Code Changes
Mostly ctl-side changes:
Environment Variables
FIDES__TEST_MODE
in thedocker-compose.yml
- this should be set by pytest only. So this is only True when running pytest.TESTING
toFIDES__TEST_MODE
inpyproject.toml
to set test mode to true for pytest only.TESTING
was the old variable we used in ops.fides
db instead of thefides_test
db in the.fides/fides.toml
. Runningfides scan dataset db --credentials-id app_postgres
locally should scan the application db not the test db.Sessions
crud
,datamap
,view
, andgenerate
routes. Pass that session onto the crud methods. (Ops already injects a session into its routes. ) The admin route doesn't seem to need a session because it is using database_uri's that have test_mode awareness. Thehealth
endpoint is already injecting the sync session from ops. The validate routes don't seem to need a session either.async_db
generator for API endpoint dependency injection.async_session
as a variable (api/ctl/database/crud.py
), which is test_mode awareasync_session
parameter (api/ctl/routes/util.py
) that is test mode awareTest refactors
requests
library to use theTestClient
(which calls functions directly instead of sending a request to a running webserver). This is primarily for the methods insrc/fides/ctl/core/api.py
. Now all the CLI-related tests and all methods callingapi.get
orapi.pos
t etc. should be usingthe TestClient instead of the requests library.Ops changes
Ops was already using a
TestClient
to test its endpoints, which does not ping a running webserver.setup_db
to reset the test db, not the application db (by using the test client).Misc
toggle_testing_envvar
fixturelogging_sessions
log debug logs on dev mode, not test mode (test mode is false now, when running application locally). Running the application had been resulting in fewer logs now that test mode was false.load_default_organization
andload_default_taxonomy
take in an async_session (ctl-side)Steps to Confirm
nox -s dev -- postgres
to bring up the webserverfides
application dbpsql -h localhost -p 5432 -U postgres -d fides
locally and verify that this application db is up and populatedpsql -h localhost -p 5432 -U postgres -d fides_test
nox -s dev -- postgres shell
pytest tests/ctl/database/test_crud.py
, put a breakpoint in the test after resources are createdfides get data_category user
- observe successful return from application dbPre-Merge Checklist
CHANGELOG.md
Description Of Changes
Fides is currently writing to the test db both when running the application and when we're running pytest. This is difficult for local development - when you run tests in pytest, the test db is wiped and populated with fixture data. It would be useful to populate the application db when running the application locally and run the test db when running pytest.
Note that before the merge of fidesctl and fidesops, fidesops already had this division, writing to the application db or the test db depending on the context . Fidesctl differed in that it spun up a webserver and the tests made requests against that running webserver. Fidesops used test clients. This refactor primarily injects sessions into ctl-routes, and then uses test clients that have awareness of test mode to talk to the test db during testing. However, for the CLI tests that are talking to a running webserver, we're monkeypatching the
requests
library to use the TestClient instead.Note
I want to make sure we're not being too aggressive with the monkey patching - ops uses the requests library to make connector requests in many places. I am only monkeypatching the requests library ctl side.