Skip to content

Commit

Permalink
Add some test coverage
Browse files Browse the repository at this point in the history
(There could always be more, but I think this covers the basics.)
  • Loading branch information
dbutenhof committed Mar 20, 2023
1 parent 5ae8663 commit 1946628
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
3 changes: 1 addition & 2 deletions lib/pbench/server/database/models/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,12 +851,11 @@ def getvalue(
raise MetadataBadStructure(dataset, key, name)
if i in value:
name = i
value = value[i]
elif i.lower() in value:
name = i.lower()
value = value[name]
else:
return None
value = value[name]
return value

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def test_metadata(
much of the setup.
"""

# In order to test that this API support getting non-alphanumeric
# In order to test that this API supports getting non-alphanumeric
# metalog metadata keys, we decorate the dataset with one. To keep it
# simple, just remove the existing Metadata row and create a new one.
drb = Dataset.query(name="drb")
Expand Down
25 changes: 18 additions & 7 deletions lib/pbench/test/unit/server/test_datasets_metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from http import HTTPStatus
import re

import pytest
import requests
Expand Down Expand Up @@ -329,17 +330,27 @@ def test_put_no_dataset(self, client, server_config, attach_dataset):
assert response.status_code == HTTPStatus.NOT_FOUND
assert response.json == {"message": "Dataset 'foobar' not found"}

def test_put_bad_keys(self, client, server_config, attach_dataset):
@pytest.mark.parametrize(
"keys,message",
(
(
{"xyzzy": "private", "what": "sup", "global.saved": True},
"'what', 'xyzzy'",
),
({"global.AbC@foo=y": True}, "'global.AbC@foo=y'"),
({"global..foo": True}, "'global..foo'"),
),
)
def test_put_bad_keys(self, client, server_config, attach_dataset, keys, message):
response = client.put(
f"{server_config.rest_uri}/datasets/drb/metadata",
json={
"metadata": {"xyzzy": "private", "what": "sup", "global.saved": True}
},
json={"metadata": keys},
)
assert response.status_code == HTTPStatus.BAD_REQUEST
assert response.json == {
"message": "Unrecognized JSON keys ['what', 'xyzzy'] for parameter metadata."
}
assert re.match(
f"Unrecognized JSON keys? \\[{message}\\] for parameter metadata.",
response.json["message"],
)

def test_put_reserved_metadata(self, client, server_config, attach_dataset):
response = client.put(
Expand Down
52 changes: 43 additions & 9 deletions lib/pbench/test/unit/server/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,21 +326,55 @@ def test_boolean(self, value, expected):
assert x.normalize(value) == expected

@pytest.mark.parametrize(
"input,expected",
"input,funky,expected",
(
("yes", "yes"),
("Yes", "yes"),
("me.you", "me.you"),
("me.You.him", "me.you.him"),
("ME.US.HER.THEM", "me.us.her.them"),
("yes", False, "yes"),
("Yes", False, "yes"),
("me.you", False, "me.you"),
("me.You.him", False, "me.you.him"),
("ME.US.HER.THEM", False, "me.us.her.them"),
("me.a.b.fooBar:test@y=z", True, "me.a.b.fooBar:test@y=z"),
("yes.a7.b2.foo", False, "yes.a7.b2.foo"),
("me.;baR.#f0o.x@y=10", True, "me.;baR.#f0o.x@y=10"),
),
)
def test_keyword_namespace(self, input, expected):
def test_keyword_namespace(self, input, funky, expected):
"""Test parameter normalization for a keyword parameter."""
x = Parameter(
"data",
ParamType.KEYWORD,
keywords=["yes", "me"],
key_path=True,
metalog_ok=funky,
)
assert x.normalize(input) == expected

@pytest.mark.parametrize(
"input,funky",
(
("me..you", False),
("me.#You.him", False),
("them..us", True),
("a.b.fooBar:test@y=z", True),
("wow.a7.b2.foo", False),
),
)
def test_keyword_namespace_bad(self, input, funky):
"""
Test parameter normalization for a keyword parameter.
"""
x = Parameter("data", ParamType.KEYWORD, keywords=["yes", "me"], key_path=True)
assert x.normalize(input) == expected
x = Parameter(
"data",
ParamType.KEYWORD,
keywords=["yes", "me"],
key_path=True,
metalog_ok=funky,
)
with pytest.raises(
KeywordError,
match=f"Unrecognized keyword \\[{input!r}\\] for parameter data.",
):
x.normalize(input)

@pytest.mark.parametrize(
"input,expected",
Expand Down
3 changes: 2 additions & 1 deletion lib/pbench/test/unit/server/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,15 @@ def test_bad_metadata_upload(
"Content-Length": "STRING",
},
query_string={
"metadata": "foobar.badpath:data,server.deletion:3000-12-25T23:59:59+00:00"
"metadata": "global.xyz#A@b=z:y,foobar.badpath:data,server.deletion:3000-12-25T23:59:59+00:00"
},
)
assert response.status_code == HTTPStatus.BAD_REQUEST
json = response.json
assert "errors" in json and "message" in json
assert json["message"] == "at least one specified metadata key is invalid"
assert json["errors"] == [
"Key global.xyz#a@b=z is invalid or isn't settable",
"Key foobar.badpath is invalid or isn't settable",
"Metadata key 'server.deletion' value '3000-12-25T23:59:59+00:00' for dataset must be a date/time before 1979-12-30",
]
Expand Down

0 comments on commit 1946628

Please sign in to comment.