Skip to content

Commit

Permalink
Global variables (jsocol#106)
Browse files Browse the repository at this point in the history
* Back to global variables

* Migration

* Update tests/api/namespace/test_variables.py

Co-authored-by: Vincent GENTY <Wellan89@users.noreply.github.com>

* Update tests/api/namespace/test_variables.py

Co-authored-by: Vincent GENTY <Wellan89@users.noreply.github.com>

* Review + upgrade requirements

* Upgrade precommits

Co-authored-by: Vincent GENTY <Wellan89@users.noreply.github.com>
  • Loading branch information
Anogio and Wellan89 committed Mar 14, 2022
1 parent 2c898d2 commit 1a3cfc3
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 175 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
- id: blacken-docs

- repo: https://github.com/asottile/reorder_python_imports.git
rev: v2.7.1
rev: v3.0.1
hooks:
- id: reorder-python-imports
language_version: python3.10
Expand Down Expand Up @@ -60,7 +60,7 @@ repos:
- id: flake8

- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.931
rev: v0.940
hooks:
- id: mypy
args: [--ignore-missing-imports]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""add_instance_and_drop_use_case_from_variables
Revision ID: 7ed1ef8a6308
Revises: 0a64fe57718d
Create Date: 2022-03-10 10:37:46.956974
"""
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import UUID

from alembic import op


revision = "7ed1ef8a6308"
down_revision = "0a64fe57718d"
branch_labels = None
depends_on = None


def upgrade():
op.add_column(
"variables",
sa.Column(
"instance_id",
sa.Integer,
nullable=False,
),
)
op.drop_column("variables", "use_case_uuid")


def downgrade():
op.add_column(
"variables",
sa.Column(
"use_case_uuid",
UUID,
sa.ForeignKey("use_cases.uuid", ondelete="CASCADE"),
nullable=False,
),
)
op.drop_column("variables", "instance_id")
4 changes: 2 additions & 2 deletions requirements-base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# File generated automatically. Do not update it manually.
# Update files in the requirements folder instead.
pip==22.0.3
setuptools==60.8.2
pip==22.0.4
setuptools==60.9.3
wheel==0.37.1
54 changes: 27 additions & 27 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,53 @@ asgiref==3.5.0
astroid==2.9.3
attrs==21.4.0
black==22.1.0
boto3==1.20.52
botocore==1.23.52
boto3==1.21.18
botocore==1.24.18
callee==0.3.1
certifi==2021.10.8
cffi==1.15.0
cfgv==3.3.1
charset-normalizer==2.0.11
click==8.0.3
charset-normalizer==2.0.12
click==8.0.4
content-size-limit-asgi==0.1.5
cryptography==36.0.1
decopatch==1.4.9
decopatch==1.4.10
dependency-injector==4.38.0
distlib==0.3.4
factory-boy==3.2.1
Faker==12.2.0
fastapi==0.73.0
filelock==3.4.2
Flask==2.0.2
freezegun==1.1.0
Faker==13.3.2
fastapi==0.75.0
filelock==3.6.0
Flask==2.0.3
freezegun==1.2.0
funcy==1.17
greenlet==1.1.2
gunicorn==20.1.0
h11==0.13.0
identify==2.4.9
identify==2.4.11
idna==3.3
iniconfig==1.1.1
isort==5.10.1
itsdangerous==2.0.1
itsdangerous==2.1.1
Jinja2==3.0.3
jmespath==0.10.0
lazy-object-proxy==1.7.1
makefun==1.13.1
Mako==1.1.6
MarkupSafe==2.0.1
marshmallow==3.14.1
Mako==1.2.0
MarkupSafe==2.1.0
marshmallow==3.15.0
marshmallow-dataclass==8.5.3
marshmallow-enum==1.5.1
mccabe==0.6.1
more-itertools==8.12.0
moto==3.0.3
mypy==0.931
moto==3.1.0
mypy==0.940
mypy-extensions==0.4.3
nodeenv==1.6.0
packaging==21.3
pathspec==0.9.0
pika==1.2.0
platformdirs==2.5.0
platformdirs==2.5.1
pluggy==1.0.0
pre-commit==2.17.0
psycopg2-binary==2.9.3
Expand All @@ -63,8 +63,8 @@ pycparser==2.21
pydantic==1.9.0
pylint==2.12.2
pyparsing==3.0.7
pytest==7.0.0
pytest-cases==3.6.9
pytest==7.1.0
pytest-cases==3.6.10
pytest-freezegun==0.4.2
pytest-mock==3.7.0
pytest-pycharm==0.7.0
Expand All @@ -76,23 +76,23 @@ python-multipart==0.0.5
pytz==2021.3
PyYAML==6.0
requests==2.27.1
responses==0.18.0
s3transfer==0.5.1
sentry-sdk==1.5.4
responses==0.19.0
s3transfer==0.5.2
sentry-sdk==1.5.7
six==1.16.0
sniffio==1.2.0
SQLAlchemy==1.4.31
SQLAlchemy==1.4.32
sqlalchemy-repr==0.0.2
starlette==0.17.1
-e git+ssh://git@github.com/Destygo/pystatsd.git@c755b95da1f4692c49b8191f9ea0ef92f4c13c2c#egg=statsd
toml==0.10.2
tomli==2.0.1
typeguard==2.13.3
typing-inspect==0.7.1
typing_extensions==4.0.1
typing_extensions==4.1.1
urllib3==1.26.8
uvicorn==0.17.4
virtualenv==20.13.1
uvicorn==0.17.6
virtualenv==20.13.3
Werkzeug==2.0.3
wrapt==1.13.3
xmltodict==0.12.0
28 changes: 15 additions & 13 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,47 @@
alembic==1.7.6
anyio==3.5.0
asgiref==3.5.0
boto3==1.20.52
botocore==1.23.52
boto3==1.21.18
botocore==1.24.18
certifi==2021.10.8
charset-normalizer==2.0.11
click==8.0.3
charset-normalizer==2.0.12
click==8.0.4
content-size-limit-asgi==0.1.5
dependency-injector==4.38.0
fastapi==0.73.0
fastapi==0.75.0
funcy==1.17
greenlet==1.1.2
gunicorn==20.1.0
h11==0.13.0
idna==3.3
jmespath==0.10.0
Mako==1.1.6
MarkupSafe==2.0.1
marshmallow==3.14.1
Mako==1.2.0
MarkupSafe==2.1.0
marshmallow==3.15.0
marshmallow-dataclass==8.5.3
marshmallow-enum==1.5.1
more-itertools==8.12.0
mypy-extensions==0.4.3
packaging==21.3
pika==1.2.0
psycopg2-binary==2.9.3
pydantic==1.9.0
pyparsing==3.0.7
python-dateutil==2.8.2
python-dotenv==0.19.2
python-magic==0.4.25
python-multipart==0.0.5
requests==2.27.1
s3transfer==0.5.1
sentry-sdk==1.5.4
s3transfer==0.5.2
sentry-sdk==1.5.7
six==1.16.0
sniffio==1.2.0
SQLAlchemy==1.4.31
SQLAlchemy==1.4.32
sqlalchemy-repr==0.0.2
starlette==0.17.1
-e git+ssh://git@github.com/Destygo/pystatsd.git@c755b95da1f4692c49b8191f9ea0ef92f4c13c2c#egg=statsd
typeguard==2.13.3
typing-inspect==0.7.1
typing_extensions==4.0.1
typing_extensions==4.1.1
urllib3==1.26.8
uvicorn==0.17.4
uvicorn==0.17.6
67 changes: 27 additions & 40 deletions tests/api/namespace/test_variables.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# pylint: disable=redefined-outer-name
import uuid
from datetime import datetime
from datetime import timedelta

Expand All @@ -8,92 +6,81 @@
import pytest

from tests import test_patterns
from tests.factories.use_case_factory import UseCaseFactory
from tests.factories.variable_factory import VariableFactory
from use_case_executor.adapters.database_repository.models.variable import Variable


@pytest.fixture
def creation_payload():
return {"name": "My variable"}


@pytest.mark.parametrize("missing_key", ["name"])
def test_create_fails_if_missing_key(missing_key, creation_payload, client, session):
@pytest.mark.parametrize("missing_key", ["instance_id", "name"])
def test_create_fails_if_missing_key(missing_key, client, session):
response = client.post(
f"/use_cases/{uuid.uuid4()}/variables",
json=funcy.omit(creation_payload, [missing_key]),
"/variables",
json=funcy.omit({"instance_id": 123, "name": "My variable"}, [missing_key]),
)
assert response.status_code == 422


@pytest.mark.parametrize("key,wrong_value", [("name", 123)])
@pytest.mark.parametrize("key, wrong_value", [("instance_id", "abc"), ("name", 123)])
def test_create_fails_if_wrong_type_key(key, wrong_value, client, session):
response = client.post(
f"/use_cases/{uuid.uuid4()}/variables",
"/variables",
json=funcy.set_in(
{"instance_id": 123, "name": "My variable"}, [key], wrong_value
),
)
assert response.status_code == 422


def test_create_fails_if_unknown_use_case(creation_payload, client, session):
response = client.post(
f"/use_cases/{uuid.uuid4()}/variables",
json=creation_payload,
)
assert response.status_code == 404


def test_create_variable_with_correct_parameters_works(
creation_payload,
def test_create_variable_with_correct_parameters_work(
client,
session,
):
use_case = UseCaseFactory()
response = client.post(
f"/use_cases/{use_case.uuid}/variables", json=creation_payload
"/variables", json={"instance_id": 123, "name": "My variable"}
)
assert response.status_code == 200
assert response.json() == {
"uuid": callee.Regex(test_patterns.UUID_PATTERN),
"name": "My variable",
"use_case_uuid": str(use_case.uuid),
"instance_id": 123,
}

variable = session.query(Variable).one()

assert variable.uuid == callee.Regex(test_patterns.UUID_PATTERN)
assert variable.name == "My variable"
assert variable.use_case_uuid == use_case.uuid
assert variable.instance_id == 123
now = datetime.utcnow()
margin = timedelta(seconds=15)
assert now - margin <= variable.created_at <= now


def test_list_variables_fails_if_unknown_use_case(creation_payload, session, client):
response = client.get(
f"/use_cases/{uuid.uuid4()}/variables",
)
assert response.status_code == 404
def test_list_variables_fails_if_no_instance_scope(session, client):
response = client.get("/variables")
assert response.status_code == 422
assert response.json() == {
"detail": [
{
"loc": ["query", "instance_id"],
"msg": "field required",
"type": "value_error.missing",
}
]
}


def test_list_variables_filters_by_instance(session, client):
v = VariableFactory()
VariableFactory() # variable on another use case
variable = VariableFactory(instance_id=123)
VariableFactory(instance_id=456) # decoy

response = client.get(
f"/use_cases/{v.use_case_uuid}/variables",
)
response = client.get("/variables?instance_id=123")
assert response.status_code == 200

assert response.json() == {
"variables": [
{
"uuid": callee.Regex(test_patterns.UUID_PATTERN),
"name": v.name,
"use_case_uuid": str(v.use_case_uuid),
"name": variable.name,
"instance_id": 123,
}
]
}
3 changes: 1 addition & 2 deletions tests/factories/variable_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from factory import alchemy

from tests import common
from tests.factories.use_case_factory import UseCaseFactory
from use_case_executor.adapters.database_repository.models.variable import Variable


Expand All @@ -12,5 +11,5 @@ class Meta:
sqlalchemy_session = common.test_session
sqlalchemy_session_persistence = alchemy.SESSION_PERSISTENCE_COMMIT

use_case = factory.SubFactory(UseCaseFactory)
instance_id = factory.fuzzy.FuzzyInteger(1, 3)
name = factory.fuzzy.FuzzyText()
Loading

0 comments on commit 1a3cfc3

Please sign in to comment.