Skip to content

Migrate integration tests to testcontainers #252

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 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions .github/actions/test-backend/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ inputs:
runs:
using: composite
steps:
- uses: browser-actions/setup-chrome@latest
- name: Set up Python 3.9
uses: actions/setup-python@v5
with:
Expand All @@ -18,16 +17,6 @@ runs:
python -m pip install --upgrade pip
pip install -r requirements.txt
shell: bash
- name: Install Elasticsearch
run: |
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.6-amd64.deb -q
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.6-amd64.deb.sha512
shasum -a 512 -c elasticsearch-7.17.6-amd64.deb.sha512
sudo dpkg -i elasticsearch-7.17.6-amd64.deb
shell: bash
- name: Start Elasticsearch
run: sudo systemctl start elasticsearch.service
shell: bash
- name: Write Google Drive API credentials to file
uses: jsdaniell/create-json@v1.2.3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/auto-author-assign.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ jobs:
assign-author:
runs-on: ubuntu-latest
steps:
- uses: toshimaru/auto-author-assign@v2.2.1
- uses: toshimaru/auto-author-assign@v2.1.1
3 changes: 2 additions & 1 deletion MPCAutofill/MPCAutofill/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@

# elasticsearch DSL settings
ELASTICSEARCH_HOST = env("ELASTICSEARCH_HOST", default="localhost")
ELASTICSEARCH_PORT = env("ELASTICSEARCH_PORT", default="9200")
ELASTICSEARCH_DSL = {
"default": {"hosts": f"{ELASTICSEARCH_HOST}:9200"},
"default": {"hosts": f"{ELASTICSEARCH_HOST}:{ELASTICSEARCH_PORT}"},
}

ELASTICSEARCH_DSL_AUTOSYNC = False
Expand Down
2 changes: 1 addition & 1 deletion MPCAutofill/cardpicker/search/search_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def __init__(self) -> None:

def get_elasticsearch_connection() -> Elasticsearch:
if (es := getattr(thread_local, "elasticsearch", None)) is None:
es = Elasticsearch([settings.ELASTICSEARCH_HOST], port=9200)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yuck, who wrote this 🗿

es = Elasticsearch([settings.ELASTICSEARCH_HOST], port=settings.ELASTICSEARCH_PORT)
return es


Expand Down
12 changes: 6 additions & 6 deletions MPCAutofill/cardpicker/tests/__snapshots__/test_sources.ambr
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# name: TestUpdateDatabase.test_comprehensive_snapshot[cards]
list([
<Card: [Example Drive 1] <EN> Mountain [Type: CARD, Identifier: 1-dcs0FEE05MTGiYbKqs9HnRdhXkgtIJG, Uploaded: 22/09/2022, Priority: 7]>,
<Card: [Example Drive 1] <EN> Plains [Type: CARD, Identifier: 1743iShpNEhY5xQ2PCUNpca4UL5t4rG3g, Uploaded: 22/09/2022, Priority: 7]>,
<Card: [Example Drive 1] <EN> Delver of Secrets [Type: CARD, Identifier: 17fopRCNRge72U8Hac8pApHZtEalx5kHy, Uploaded: 09/10/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Huntmaster of the Fells [Type: CARD, Identifier: 1991MWCur9NdAFi-tQQD5YbQj2oqV_WRy, Uploaded: 22/09/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Brainstorm [Type: CARD, Identifier: 1c4M-sK9gd0Xju0NXCPtqeTW_DQTldVU5, Uploaded: 22/09/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Mountain [Type: CARD, Identifier: 1-dcs0FEE05MTGiYbKqs9HnRdhXkgtIJG, Uploaded: 22/09/2022, Priority: 7]>,
<Card: [Example Drive 2] <EN> Past in Flames [Type: CARD, Identifier: 1dxSLHtw-VwwE09pZCA8OA6LbuWRZPEoU, Uploaded: 24/09/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Island (William Bradford) [Type: CARD, Identifier: 1HsvTYs1jFGe1c8U1PnNZ9aB8jkAW7KU0, Uploaded: 22/09/2022, Priority: 6]>,
<Card: [Example Drive 1] <EN> Island [Type: CARD, Identifier: 1IDtqSjJ4Yo45AnNA4SplOiN7ewibifMa, Uploaded: 22/09/2022, Priority: 7]>,
<Card: [Example Drive 1] <EN> Simple Cube [Type: CARDBACK, Identifier: 1JtXL6Ca9nQkvhwZZRR9ZuKA9_DzsFf1V, Uploaded: 22/09/2022, Priority: 17]>,
<Card: [Example Drive 1] <EN> Ravager of the Fells [Type: CARD, Identifier: 1lv8WC1Xf1qxA7VHSc8jOtT5up6FwaBPH, Uploaded: 22/09/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Insectile Aberration [Type: CARD, Identifier: 1mO73GTYlieP0kiZEkF58pJSrZTmC9lNh, Uploaded: 09/10/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Swamp [Type: CARD, Identifier: 1O5Y_lBOBJK1D9Tw6pcsQJXYoTZXhR3mV, Uploaded: 22/09/2022, Priority: 7]>,
<Card: [Example Drive 1] <EN> Simple Lotus [Type: CARDBACK, Identifier: 1oigI6wz0zA--pNMuExKTs40kBNH6VRP_, Uploaded: 22/09/2022, Priority: 7]>,
<Card: [Example Drive 1] <EN> Forest [Type: CARD, Identifier: 1Sne_vn7TlohIbsjt933Kd5k2ff6vFS1I, Uploaded: 22/09/2022, Priority: 7]>,
<Card: [Example Drive 1] <EN> Pást in Flames [Type: CARD, Identifier: 1UPdh7J7hScg4ZnxSPJ-EeBYHLp2s3Oz1, Uploaded: 24/09/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Goblin [Type: TOKEN, Identifier: 1V5E0avDmNyEUuFfYwx3nA05aj-1HY0rA, Uploaded: 09/10/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Brainstorm [Type: CARD, Identifier: 1c4M-sK9gd0Xju0NXCPtqeTW_DQTldVU5, Uploaded: 22/09/2022, Priority: 2]>,
<Card: [Example Drive 2] <EN> Past in Flames [Type: CARD, Identifier: 1dxSLHtw-VwwE09pZCA8OA6LbuWRZPEoU, Uploaded: 24/09/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Ravager of the Fells [Type: CARD, Identifier: 1lv8WC1Xf1qxA7VHSc8jOtT5up6FwaBPH, Uploaded: 22/09/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Insectile Aberration [Type: CARD, Identifier: 1mO73GTYlieP0kiZEkF58pJSrZTmC9lNh, Uploaded: 09/10/2022, Priority: 2]>,
<Card: [Example Drive 1] <EN> Simple Lotus [Type: CARDBACK, Identifier: 1oigI6wz0zA--pNMuExKTs40kBNH6VRP_, Uploaded: 22/09/2022, Priority: 7]>,
])
# ---
44 changes: 39 additions & 5 deletions MPCAutofill/cardpicker/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

import pytest
from pytest_elasticsearch import factories
from testcontainers.elasticsearch import ElasticSearchContainer
from testcontainers.postgres import PostgresContainer

from django.conf import settings as conf_settings
from django.core.management import call_command

from cardpicker.integrations.base import GameIntegration
Expand All @@ -16,6 +19,37 @@
TagFactory,
)

POSTGRES_PORT = 47000
ELASTICSEARCH_PORT = 9300 # this is the default expected by `elasticsearch_nooproc`


@pytest.fixture(scope="session")
def postgres_container():
postgres = PostgresContainer("postgres:16.0-alpine").with_bind_ports(5432, POSTGRES_PORT)
postgres.start()
yield postgres
postgres.stop()


@pytest.fixture(scope="session")
def elasticsearch_container():
elasticsearch = ElasticSearchContainer("elasticsearch:7.17.23", mem_limit="1G").with_bind_ports(
9200, ELASTICSEARCH_PORT
)
elasticsearch.start()
yield elasticsearch
elasticsearch.stop()


@pytest.fixture(scope="session")
def django_db_modify_db_settings(postgres_container):
# customise settings to point to testcontainers db
conf_settings.DATABASES["default"]["HOST"] = postgres_container.get_container_host_ip()
conf_settings.DATABASES["default"]["PORT"] = POSTGRES_PORT
conf_settings.DATABASES["default"]["NAME"] = postgres_container.dbname
conf_settings.DATABASES["default"]["USER"] = postgres_container.username
conf_settings.DATABASES["default"]["PASSWORD"] = postgres_container.password


@pytest.fixture()
def django_settings(db, settings):
Expand All @@ -42,11 +76,11 @@ def dummy_integration(integration_setter) -> Type[GameIntegration]:


@pytest.fixture(scope="session", autouse=True)
def elasticsearch():
"""
This fixture expects elasticsearch to be running on your machine.
"""

def elasticsearch(elasticsearch_container):
conf_settings.ELASTICSEARCH_DSL["default"][
"hosts"
] = f"{elasticsearch_container.get_container_host_ip()}:{ELASTICSEARCH_PORT}"
conf_settings.ELASTICSEARCH_PORT = ELASTICSEARCH_PORT
return factories.elasticsearch("elasticsearch_nooproc")


Expand Down
1 change: 1 addition & 0 deletions MPCAutofill/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ ratelimit~=2.2.1
requests~=2.31.0
sentry-sdk~=1.30.0
syrupy~=3.0
testcontainers[postgres,elasticsearch]
tqdm~=4.65.0
tzdata