Skip to content

Commit

Permalink
Merge pull request #702 from fractal-analytics-platform/696-use-postg…
Browse files Browse the repository at this point in the history
…res-in-ci

Use Postgres in CI
  • Loading branch information
tcompa authored Sep 18, 2024
2 parents cc751c7 + 7815572 commit e3bc40a
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 36 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ jobs:
matrix:
python-version: ["3.9", "3.10", "3.11"]

services:
postgres:
image: postgres
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: fractal_client_test
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:

- uses: actions/checkout@v4
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Add `fractal group` command (\#695).
* Testing
* Update GitHub actions for upload/download/coverage (\#690, \#691).
* Switch from SQLite to Postgres in CI (\#702).

# 2.0.3

Expand Down
115 changes: 112 additions & 3 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pre-commit = "^2.19.0"
pytest = "^7.1.2"
bumpver = "^2022.1118"
coverage = {extras = ["toml"], version = "^6.5.0"}
fractal-server = { git = "https://github.com/fractal-analytics-platform/fractal-server.git", branch = "main"}
fractal-server = { git = "https://github.com/fractal-analytics-platform/fractal-server.git", branch = "main", extras = ["postgres-psycopg-binary"] }


[tool.poetry.group.docs]
Expand Down
40 changes: 25 additions & 15 deletions tests/fixtures_testserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ def override_server_settings(tmp_path):

settings = Settings()

tmp_db_path = tmp_path / "db/test.db"
tmp_db_path.parent.mkdir()
settings.DB_ENGINE = "sqlite"
settings.SQLITE_PATH = tmp_db_path
settings.DB_ENGINE = "postgres-psycopg"
settings.POSTGRES_DB = "fractal_client_test"
settings.POSTGRES_USER = "postgres"
settings.POSTGRES_PASSWORD = "postgres"

settings.FRACTAL_RUNNER_BACKEND = "local"

settings.JWT_SECRET_KEY = "secret_key"
Expand All @@ -50,6 +51,7 @@ def _get_settings():

@pytest.fixture(scope="function", autouse=True)
def testserver(override_server_settings):

from fractal_server.app.db import DB
from fractal_server.app.models.security import SQLModel
from fractal_server.app.models.security import UserOAuth
Expand All @@ -58,13 +60,13 @@ def testserver(override_server_settings):
from fractal_server.app.security import _create_first_group

# INIT DB
DB.set_sync_db()
logger.debug(DB.engine_sync().url)
SQLModel.metadata.create_all(DB.engine_sync())
engine_sync = DB.engine_sync()
logger.debug(engine_sync.url)
SQLModel.metadata.create_all(engine_sync)

# Create default group and first superuser
# NOTE: we have to do it here, because we are not calling the `set_db` function
# from fractal-server. This would change with
# NOTE: we have to do it here, because we are not calling the `set_db`
# function from fractal-server. This would change with
# https://github.com/fractal-analytics-platform/fractal-client/issues/697
# NOTE: `hashed_password` is the bcrypt hash of "1234", see
# https://github.com/fractal-analytics-platform/fractal-server/issues/1750
Expand Down Expand Up @@ -123,6 +125,19 @@ def run_server():

logger.debug(environ["FRACTAL_SERVER"])
yield environ["FRACTAL_SERVER"]

# Cleanup DB
engine_sync.dispose()
try:
DB._engine_async
raise
except AttributeError:
# we show here that we do not need to dispose of `engine_async`,
# because it is never used.
pass
SQLModel.metadata.drop_all(engine_sync)
logger.debug("Dropped all tables from the database.")

proc.kill()


Expand Down Expand Up @@ -268,8 +283,7 @@ def __register_user(


@pytest.fixture
def register_user(user_factory, db):
from fractal_server.app.models.security import UserOAuth
def register_user(user_factory):

created_user = user_factory(
email=environ["FRACTAL_USER"],
Expand All @@ -278,7 +292,3 @@ def register_user(user_factory, db):
)

yield created_user

db_user = db.get(UserOAuth, created_user["id"])
db.delete(db_user)
db.commit()
29 changes: 12 additions & 17 deletions tests/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,9 @@ def test_group_commands(user_factory, invoke_as_superuser):

res = invoke_as_superuser("group list --user-ids")
assert len(res.data) == 1
assert res.data[0]["user_ids"] == [
superuser_id,
user1_id,
user2_id,
user3_id,
]
assert set(res.data[0]["user_ids"]) == set(
[superuser_id, user1_id, user2_id, user3_id]
)

# Create 2 new empty groups (`group new`)

Expand Down Expand Up @@ -102,29 +99,27 @@ def test_group_commands(user_factory, invoke_as_superuser):
)
assert res.retcode == 0
assert res.data["id"] == group2_id
assert res.data["user_ids"] == [user2_id, user3_id] # order
assert set(res.data["user_ids"]) == set([user3_id, user2_id])
# add also `superuser` to `group2`
res = invoke_as_superuser(
f"group update {group2_id} --new-user-ids {superuser_id}"
)
assert res.data["user_ids"] == [superuser_id, user2_id, user3_id] # order
assert set(res.data["user_ids"]) == set([user3_id, user2_id, superuser_id])

# Check groups are updated

res = invoke_as_superuser("group list --user-ids")
assert len(res.data) == 3
assert res.data[0]["id"] == default_group_id
assert res.data[0]["user_ids"] == [
superuser_id,
user1_id,
user2_id,
user3_id,
]
assert set(res.data[0]["user_ids"]) == set(
[superuser_id, user1_id, user2_id, user3_id]
)
assert res.data[1]["id"] == group1_id
assert res.data[1]["user_ids"] == [user1_id, user2_id]
assert set(res.data[1]["user_ids"]) == set([user1_id, user2_id])
assert res.data[2]["id"] == group2_id
# sorted in order of insertion
assert res.data[2]["user_ids"] == [user3_id, user2_id, superuser_id]
assert set(res.data[2]["user_ids"]) == set(
[user3_id, user2_id, superuser_id]
)

# Test `group get` command

Expand Down

0 comments on commit e3bc40a

Please sign in to comment.