Skip to content

Commit

Permalink
fix tests and qa
Browse files Browse the repository at this point in the history
  • Loading branch information
francesconazzaro committed Oct 17, 2023
1 parent c6ce8de commit 683084f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
5 changes: 2 additions & 3 deletions cads_broker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ class SqlalchemySettings(pydantic_settings.BaseSettings):
"compute_db_host_read",
"compute_db_name",
)
def password_must_be_set(
def db_connection_env_vars_must_be_set(
cls: pydantic_settings.BaseSettings,
v: str | None,
info: pydantic_core.core_schema.FieldValidationInfo,
) -> str | None:
"""Check that password is explicitly set."""
"""Check that database connection environment variables are explicitly set."""
if v is None:
raise ValueError(f"{info.field_name} must be set")
return v


@property
def connection_string(self) -> str:
"""Create reader psql connection string."""
Expand Down
4 changes: 3 additions & 1 deletion cads_broker/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ def sync_database(self, session: sa.orm.Session) -> None:
# if it doesn't find the request: re-queue it
else:
# FIXME: check if request status has changed
refreshed_request = db.get_request(request_uid=request.request_uid, session=session)
refreshed_request = db.get_request(
request_uid=request.request_uid, session=session
)
if refreshed_request.status == "running":
db.set_request_status(
request_uid=request.request_uid,
Expand Down
4 changes: 3 additions & 1 deletion cads_broker/qos/Rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def dump(self, out):
out(self)

def get_uid(self, request):
return str(hash(f"{self.name} {self.info} {self.condition} : {self.evaluate(request)}"))
return str(
hash(f"{self.name} {self.info} {self.condition} : {self.evaluate(request)}")
)

def __repr__(self):
return f"{self.name} {self.info} {self.condition} : {self.conclusion}"
Expand Down
2 changes: 1 addition & 1 deletion cads_broker/rules.qos
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ priority "Priority for test user 2" (user == "00000000-0000-3000-abcd-0000000

# Functions examples

# Request contains any of the specified variable
# Request contains any of the specified variable
# priority "Priority for temperature and humidity" (request_contains_any("variable", ["temperature", "relative_humidity"])): -hour(1)

# Request contains all the specified months
Expand Down
35 changes: 29 additions & 6 deletions tests/test_01_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,46 @@
def test_sqlalchemysettings(temp_environ: Any) -> None:
# check settings must have a password set (no default)
temp_environ.pop("compute_db_password", default=None)
with pytest.raises(ValueError) as excinfo:
with pytest.raises(ValueError):
config.SqlalchemySettings()
assert "compute_db_password" in str(excinfo.value)
config.dbsettings = None

# also an empty password can be set
settings = config.SqlalchemySettings(compute_db_password="")
settings = config.SqlalchemySettings(
compute_db_password="",
compute_db_host="broker",
compute_db_host_read="broker",
compute_db_name="broker",
compute_db_user="broker",
)
assert settings.compute_db_password == ""
config.dbsettings = None

# also a not empty password can be set
temp_environ["compute_db_password"] = "a password"
temp_environ.update(
dict(
compute_db_password="a password",
compute_db_host="broker",
compute_db_host_read="broker",
compute_db_name="broker",
compute_db_user="broker",
)
)
settings = config.SqlalchemySettings()
assert settings.compute_db_password == "a password"
config.dbsettings = None


def test_ensure_settings(session_obj: sa.orm.sessionmaker, temp_environ: Any) -> None:
temp_environ["compute_db_password"] = "apassword"
temp_environ.update(
dict(
compute_db_password="apassword",
compute_db_host="compute-db",
compute_db_host_read="compute-db",
compute_db_name="broker",
compute_db_user="broker",
)
)

# initially global settings is importable, but it is None
assert config.dbsettings is None
Expand All @@ -36,7 +57,9 @@ def test_ensure_settings(session_obj: sa.orm.sessionmaker, temp_environ: Any) ->
effective_settings = config.ensure_settings()
assert (
effective_settings.connection_string
== "postgresql://broker:apassword@compute-db/broker"
== "postgresql://{compute_db_user}:{compute_db_password}@{compute_db_host}/{compute_db_name}".format(
**temp_environ
)
)
assert config.dbsettings == effective_settings
config.dbsettings = None
Expand Down
10 changes: 9 additions & 1 deletion tests/test_02_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,15 @@ def test_ensure_session_obj(
config.dbsettings = None

# case of session not set
temp_environ["compute_db_password"] = postgresql.info.password
temp_environ.update(
dict(
compute_db_password=postgresql.info.password,
compute_db_host=postgresql.info.host,
compute_db_host_read=postgresql.info.host,
compute_db_name=postgresql.info.dbname,
compute_db_user=postgresql.info.user,
)
)
ret_value = db.ensure_session_obj(None)
assert isinstance(ret_value, sessionmaker)
config.dbsettings = None

0 comments on commit 683084f

Please sign in to comment.