-
Notifications
You must be signed in to change notification settings - Fork 8
Integration tests #520
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
Merged
Merged
Integration tests #520
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c642a7c
Add some integration tests that have access to the database
hardbyte b53b4d1
CI can run integration tests using docker-compose.
hardbyte 9e2ae67
Insertion tests should also fetch data afterwards
hardbyte 95b7415
Update docs regarding integration testing
hardbyte 17f87e5
Basic integration tests for redis
hardbyte e63cb5c
Update integration tests following code review
hardbyte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
99 changes: 99 additions & 0 deletions
99
backend/entityservice/integrationtests/dbtests/test_insertions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import datetime | ||
| import time | ||
|
|
||
| import psycopg2 | ||
| from pytest import raises | ||
|
|
||
| from entityservice.database import insert_dataprovider, insert_new_project, \ | ||
| insert_encodings_into_blocks, insert_blocking_metadata, get_project, get_encodingblock_ids | ||
| from entityservice.models import Project | ||
| from entityservice.tests.util import generate_bytes | ||
| from entityservice.utils import generate_code | ||
| from entityservice.settings import Config as config | ||
|
|
||
|
|
||
| class TestInsertions: | ||
|
|
||
| def _get_conn_and_cursor(self): | ||
| db = config.DATABASE | ||
| host = config.DATABASE_SERVER | ||
| user = config.DATABASE_USER | ||
| password = config.DATABASE_PASSWORD | ||
| conn = psycopg2.connect(host=host, dbname=db, user=user, password=password) | ||
| cursor = conn.cursor() | ||
| return conn, cursor | ||
|
|
||
| def _create_project_and_dp(self): | ||
| project, dp_ids = self._create_project() | ||
| dp_id = dp_ids[0] | ||
| dp_auth_token = project.update_tokens[0] | ||
|
|
||
| conn, cur = self._get_conn_and_cursor() | ||
| # create a default block | ||
| insert_blocking_metadata(conn, dp_id, {'1': 99}) | ||
| conn.commit() | ||
|
|
||
| assert len(dp_auth_token) == 48 | ||
| return project.project_id, project.result_token, dp_id, dp_auth_token | ||
|
|
||
| def _create_project(self): | ||
| project = Project('groups', {}, name='', notes='', parties=2, uses_blocking=False) | ||
| conn, cur = self._get_conn_and_cursor() | ||
| dp_ids = project.save(conn) | ||
| return project, dp_ids | ||
|
|
||
| def test_insert_project(self): | ||
| before = datetime.datetime.now() | ||
| project, _ = self._create_project() | ||
| assert len(project.result_token) == 48 | ||
| # check we can fetch the inserted project back from the database | ||
| conn, cur = self._get_conn_and_cursor() | ||
| project_response = get_project(conn, project.project_id) | ||
| assert 'time_added' in project_response | ||
| assert project_response['time_added'] - before >= datetime.timedelta(seconds=0) | ||
| assert not project_response['marked_for_deletion'] | ||
| assert not project_response['uses_blocking'] | ||
| assert project_response['parties'] == 2 | ||
| assert project_response['notes'] == '' | ||
| assert project_response['name'] == '' | ||
| assert project_response['result_type'] == 'groups' | ||
| assert project_response['schema'] == {} | ||
| assert project_response['encoding_size'] is None | ||
|
|
||
| def test_insert_dp_no_project_fails(self): | ||
| conn, cur = self._get_conn_and_cursor() | ||
| project_id = generate_code() | ||
| dp_auth = generate_code() | ||
| with raises(psycopg2.errors.ForeignKeyViolation): | ||
| insert_dataprovider(cur, auth_token=dp_auth, project_id=project_id) | ||
|
|
||
| def test_insert_many_clks(self): | ||
| data = [generate_bytes(128) for _ in range(100)] | ||
| project_id, project_auth_token, dp_id, dp_auth_token = self._create_project_and_dp() | ||
| conn, cur = self._get_conn_and_cursor() | ||
| num_entities = 10_000 | ||
| blocks = [['1'] for _ in range(num_entities)] | ||
| encodings = [data[i % 100] for i in range(num_entities)] | ||
| start_time = time.perf_counter() | ||
| insert_encodings_into_blocks(conn, dp_id, | ||
| block_ids=blocks, | ||
| encoding_ids=list(range(num_entities)), | ||
| encodings=encodings | ||
| ) | ||
| end_time = time.perf_counter() | ||
| elapsed_time = end_time - start_time | ||
| # This takes ~0.5s using docker compose on a ~5yo desktop. | ||
| # If the database is busy - e.g. if you're running integration | ||
| # tests and e2e tests at the same time, this assertion could fail. | ||
| assert elapsed_time < 2 | ||
|
|
||
| stored_encoding_ids = list(get_encodingblock_ids(conn, dp_id, '1')) | ||
| fetch_time = time.perf_counter() - end_time | ||
| # retrieval of encoding ids should be much faster than insertion | ||
| assert fetch_time < elapsed_time | ||
|
|
||
| assert len(stored_encoding_ids) == num_entities | ||
| for stored_encoding_id, original in zip(stored_encoding_ids, range(num_entities)): | ||
| assert stored_encoding_id == original | ||
|
|
||
| # TODO fetch binary encodings and verify against uploaded |
Empty file.
53 changes: 53 additions & 0 deletions
53
backend/entityservice/integrationtests/redistests/test_progress.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import datetime | ||
| import time | ||
|
|
||
| import pytest | ||
| import redis | ||
|
|
||
| from entityservice.settings import Config as config | ||
| from entityservice.cache import connect_to_redis, clear_progress, save_current_progress, get_progress | ||
|
|
||
|
|
||
| class TestProgress: | ||
|
|
||
| def _get_redis_rw(self): | ||
| return connect_to_redis() | ||
|
|
||
| def test_clear_missing_progress(self): | ||
| clear_progress('test_clear_missing_progress') | ||
|
|
||
| def test_clear_progress(self): | ||
| config.CACHE_EXPIRY = datetime.timedelta(seconds=1) | ||
| runid = 'runtest_clear_progress' | ||
| save_current_progress(1, runid, config) | ||
| assert 1 == get_progress(runid) | ||
| clear_progress(runid) | ||
| assert get_progress(runid) is None | ||
|
|
||
| def test_storing_wrong_type(self): | ||
| config.CACHE_EXPIRY = datetime.timedelta(seconds=1) | ||
| runid = 'test_storing_wrong_type' | ||
| with pytest.raises(redis.exceptions.ResponseError): | ||
| save_current_progress(1.5, runid, config) | ||
|
|
||
| def test_progress_expires(self): | ||
| # Uses the minimum expiry of 1 second | ||
| config.CACHE_EXPIRY = datetime.timedelta(seconds=1) | ||
| runid = 'test_progress_expires' | ||
| save_current_progress(42, runid, config) | ||
| cached_progress = get_progress(runid) | ||
| assert cached_progress == 42 | ||
| time.sleep(1) | ||
| # After expiry the progress should be reset to None | ||
| assert get_progress(runid) is None | ||
|
|
||
| def test_progress_increments(self): | ||
| config.CACHE_EXPIRY = datetime.timedelta(seconds=1) | ||
| runid = 'test_progress_increments' | ||
| save_current_progress(1, runid, config) | ||
| cached_progress = get_progress(runid) | ||
| assert cached_progress == 1 | ||
| for i in range(99): | ||
| save_current_progress(1, runid, config) | ||
|
|
||
| assert 100 == get_progress(runid) |
33 changes: 33 additions & 0 deletions
33
backend/entityservice/integrationtests/redistests/test_status.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import time | ||
|
|
||
| import redis | ||
| from pytest import raises | ||
|
|
||
| from entityservice.cache import connect_to_redis, get_status, set_status | ||
|
|
||
|
|
||
| class TestStatus: | ||
|
|
||
| def _get_redis_rw(self): | ||
| return connect_to_redis() | ||
|
|
||
| def test_get_missing_status(self): | ||
| r = self._get_redis_rw() | ||
| r.delete('entityservice-status') | ||
| status = get_status() | ||
| assert status is None | ||
|
|
||
|
|
||
| def test_set_status(self): | ||
| original_status = get_status() | ||
| if original_status is None: | ||
| new_status = {} | ||
| else: | ||
| new_status = original_status | ||
|
|
||
| new_status['testkey'] = 'testvalue' | ||
| set_status(new_status) | ||
| time.sleep(0.2) | ||
| updated_status = get_status() | ||
| assert 'testkey' in updated_status | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I find it confusing to be able to pass
configin here, as almost all of it gets ignored anyway. It'd be clearer if it's just a cache expiry value. But then, why would you want to set a different expiry value in the first place?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.
Yeah you'd only want to do that for testing. My logic is that it would be nice to slowly move away from using the global
Configas we add tests.