Skip to content

Commit

Permalink
Implement safeguard against problematic meta keys
Browse files Browse the repository at this point in the history
  • Loading branch information
danielhuppmann committed Nov 20, 2024
1 parent 70366d4 commit 5b2945d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ixmp4/data/db/meta/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ixmp4 import db
from ixmp4.core.decorators import check_types
from ixmp4.core.exceptions import InvalidRunMeta
from ixmp4.data import abstract
from ixmp4.data.auth.decorators import guard
from ixmp4.data.db.model import Model
Expand All @@ -16,6 +17,8 @@
from .. import base
from .model import RunMetaEntry

ILLEGAL_META_KEYS = {"model", "scenario", "id", "version", "is_default"}


class RemoveRunMetaEntryFrameSchema(pa.DataFrameModel):
key: Series[pa.String] = pa.Field(coerce=True)
Expand Down Expand Up @@ -60,6 +63,8 @@ def add(
default_only=False,
)

if key in ILLEGAL_META_KEYS:
raise InvalidRunMeta("Illegal meta key: " + key)
entry = RunMetaEntry(run__id=run__id, key=key, value=value)
self.session.add(entry)
return entry
Expand Down
7 changes: 6 additions & 1 deletion tests/data/test_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

import ixmp4
from ixmp4.core.exceptions import SchemaError
from ixmp4.core.exceptions import InvalidRunMeta, SchemaError
from ixmp4.data.abstract.meta import RunMetaEntry

from ..utils import assert_unordered_equality
Expand Down Expand Up @@ -37,6 +37,11 @@ def test_create_get_entry(self, platform: ixmp4.Platform):
assert entry.value == value
assert entry.type == type

def test_illegal_key(self, platform: ixmp4.Platform):
run = platform.runs.create("Model", "Scenario")
with pytest.raises(InvalidRunMeta):
platform.backend.meta.create(run.id, "version", "foo")

def test_entry_unique(self, platform: ixmp4.Platform):
run = platform.runs.create("Model", "Scenario")
platform.backend.meta.create(run.id, "Key", "Value")
Expand Down

0 comments on commit 5b2945d

Please sign in to comment.