Skip to content

Commit

Permalink
Test/pattern for backend integration testing (#117)
Browse files Browse the repository at this point in the history
* chore: Sperated tests into unit and integration tests

* chore: Added common test utility files.

* chore: Moved unit tests into tests/unit dir

* feat: Added backend integration tests.

* chore: Added Test PDF file.

* chore: Added poe script to run integration tests
Updated GitHub build action file.

* core: Added env config for test files.

* chore: Remove .ini env template formatting

* chore: Restore DOCQ_OPENAI_API_KEY name in env template files.

* chore: Load test env configs using python-dotenv

* chore: Restore DOCQ_OPENAI_API_KEY name

* chore: Separate unit tests from integration tests.
Fix local linting errors for test modules.

* chore: Update manual upload test.

* chore: Update integration settings to work with model selection.

* chore: Update manage_spaces.reindex to handle exceptions when model settings are not available.

* fix: Fix key error when trying to retrieve system settings before they are saved.

* chore: Re-organize imports.

* doc: Update getting started docs on how to run integration tests.

* fix: Fix circular dependency between m_users and m_organisation.
  • Loading branch information
osala-eng authored Oct 6, 2023
1 parent 8c75b6b commit ead571d
Show file tree
Hide file tree
Showing 23 changed files with 334 additions and 60 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ venv/
ENV/
env.bak/
venv.bak/
pytest.env

# Spyder project settings
.spyderproject
Expand Down Expand Up @@ -186,4 +187,4 @@ docker.env

# ignore docs dynamically generated at build time.
/docs/api
/docs/tests
/docs/tests
15 changes: 15 additions & 0 deletions docs/developer-guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,24 @@ Some useful examples of how this project can be used:
```

- Run tests

- Unit tests

```sh
poe test
```

- Integration tests

- Prepare env vars by supplying a pytest.env file required for the integration tests

```sh
cp misc/pytest.env.template pytest.env
## Make edits on pytest.env with the correct values for the test environment then run the tests
```

```sh
poe test-integration
```

- Run the project
Expand Down
8 changes: 8 additions & 0 deletions misc/pytest.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copy and place the following file to the root of the project as pytest.env
# To setup enviroment variables for integration testing
DOCQ_DATA=./.persisted-test-integration/
DOCQ_OPENAI_API_KEY=YOUR_OPEN_AI_KEY
DOCQ_COOKIE_HMAC_SECRET_KEY=32_char_secret_used_to_encrypt

# This will use the DOCQ_OPENAI_API_KEY for now.
OPENAI_API_KEY=${DOCQ_OPENAI_API_KEY}
Binary file added misc/test_files/integration_test.pdf
Binary file not shown.
46 changes: 24 additions & 22 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ black = { version = "^22.3.0", allow-prereleases = true }
xdoctest = "^1.0.0"
poethepoet = "^0.16.4"
ruff = "^0.0.253"
pytest = "7.1.1"
pytest = "7.3.1"
pytest-html = "^3.1.1"
pytest-sugar = "^0.9.6"
pytest-reverse = "^1.5.0"
Expand All @@ -44,6 +44,7 @@ mkdocs-awesome-pages-plugin = "^2.8.0"
mkapi = "^1.0.14"
pillow = "^9.5.0"
cairosvg = "^2.7.0"
python-dotenv = "^1.0.0"

[tool.poetry.scripts]
-c = "poethepoet:main"
Expand Down Expand Up @@ -123,7 +124,8 @@ exclude_lines = [
[tool.poe.tasks]
install = "poetry install --only main"
install-dev = "poetry install"
test = "pytest"
test = "pytest tests/unit"
test-integration = "pytest tests/integration"
pre-commit = "pre-commit run --all-files"
lint-ruff = "ruff check **/*.py --fix"
lint-black = "black **/*.py"
Expand Down
5 changes: 2 additions & 3 deletions source/docq/manage_organisations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
from datetime import datetime
from typing import List, Tuple

from . import manage_settings
from .manage_users import _add_organisation_member_sql
from . import manage_settings, manage_users
from .support.store import get_sqlite_system_file

SQL_CREATE_ORGS_TABLE = """
Expand Down Expand Up @@ -146,7 +145,7 @@ def create_organisation(name: str, creating_user_id: int) -> int | None:
_create_organisation_sql(cursor, name)
org_id = cursor.lastrowid
is_default_org_admin = True
_add_organisation_member_sql(cursor, org_id, creating_user_id, is_default_org_admin)
manage_users._add_organisation_member_sql(cursor, org_id, creating_user_id, is_default_org_admin)
connection.commit()
log.info("Created organization %s with member %s", org_id, creating_user_id)
except Exception as e:
Expand Down
6 changes: 2 additions & 4 deletions source/docq/manage_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ def _persist_index(index: GPTVectorStoreIndex, space: SpaceKey) -> None:

def reindex(space: SpaceKey) -> None:
"""Reindex documents in a space."""
(ds_type, ds_configs) = get_space_data_source(space)

saved_model_settings = get_saved_model_settings_collection(space.org_id)

try:
(ds_type, ds_configs) = get_space_data_source(space)
saved_model_settings = get_saved_model_settings_collection(space.org_id)
log.debug("get datasource instance")
documents = SpaceDataSources[ds_type].value.load(space, ds_configs)
log.debug("docs to index, %s", len(documents))
Expand Down
4 changes: 2 additions & 2 deletions source/docq/manage_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ def rnd():

personal_org_id = cursor.lastrowid

manage_organisations._add_organisation_member_sql(cursor, personal_org_id, user_id, True)
_add_organisation_member_sql(cursor, personal_org_id, user_id, True)

if org_id:
log.info("Adding user %s to org %s", user_id, org_id)
manage_organisations._add_organisation_member_sql(cursor, org_id, user_id, org_admin)
_add_organisation_member_sql(cursor, org_id, user_id, org_admin)

connection.commit()
log.info("Created user %s", user_id)
Expand Down
4 changes: 4 additions & 0 deletions tests/integration/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""Integration tests."""
from dotenv import load_dotenv

load_dotenv("pytest.env")
Loading

0 comments on commit ead571d

Please sign in to comment.