diff options
| author | Hunternif | 2019-04-18 00:24:02 +0700 |
|---|---|---|
| committer | Hunternif | 2019-04-18 00:24:02 +0700 |
| commit | a5d7897cec2a57333ab479d58ececd5e9bdaa9bb (patch) | |
| tree | 8284b0d3abf40ad86df02042e9e1cfb5b8b70dc9 /server/szurubooru/tests/func/test_metrics.py | |
| parent | 4e8f9aa771f868dc848dff564f6d0e5a102780bb (diff) | |
server: unit tests for metrics
Diffstat (limited to 'server/szurubooru/tests/func/test_metrics.py')
| -rw-r--r-- | server/szurubooru/tests/func/test_metrics.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/server/szurubooru/tests/func/test_metrics.py b/server/szurubooru/tests/func/test_metrics.py new file mode 100644 index 0000000..9b0bf02 --- /dev/null +++ b/server/szurubooru/tests/func/test_metrics.py @@ -0,0 +1,66 @@ +import pytest +from szurubooru import db, model +from szurubooru.func import metrics + + +def test_serialize_metric(tag_factory): + tag = tag_factory() + metric = model.Metric(tag=tag, min=1, max=2) + result = metrics.serialize_metric(metric) + assert result == { + 'min': 1, + 'max': 2, + } + + +def test_create_metric(tag_factory): + tag = tag_factory() + db.session.add(tag) + new_metric = metrics.create_metric(tag, 1, 2) + assert new_metric is not None + db.session.flush() + db.session.refresh(tag) + assert tag.metric is not None + assert tag.metric.min == 1 + assert tag.metric.max == 2 + + +def test_create_metric_with_existing_metric(tag_factory): + tag = tag_factory() + tag.metric = model.Metric() + with pytest.raises(metrics.MetricAlreadyExistsError): + metrics.create_metric(tag, 1, 2) + + +def test_create_metric_with_invalid_params(tag_factory): + tag = tag_factory() + with pytest.raises(metrics.InvalidMetricError): + metrics.create_metric(tag, 2, 1) + + +def test_update_or_create_metric(tag_factory): + tag = tag_factory() + db.session.add(tag) + new_metric = metrics.update_or_create_metric(tag, {'min': 1, 'max': 2}) + assert new_metric is not None + db.session.flush() + db.session.refresh(tag) + assert tag.metric is not None + assert tag.metric.min == 1 + assert tag.metric.max == 2 + + new_metric = metrics.update_or_create_metric(tag, {'min': 3, 'max': 4}) + assert new_metric is None + db.session.flush() + db.session.refresh(tag) + assert tag.metric.min == 3 + assert tag.metric.max == 4 + + +@pytest.mark.parametrize('params', [ + {'min': 1}, {'max': 2}, {'min': 2, 'max': 1} +]) +def test_update_or_create_metric_with_invalid_params(tag_factory, params): + tag = tag_factory() + with pytest.raises(metrics.InvalidMetricError): + metrics.update_or_create_metric(tag, params) |