|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +####### |
| 4 | +# actinia-python-client is a python client for actinia - an open source REST |
| 5 | +# API for scalable, distributed, high performance processing of geographical |
| 6 | +# data that uses GRASS GIS for computational tasks. |
| 7 | +# |
| 8 | +# Copyright (c) 2023 mundialis GmbH & Co. KG |
| 9 | +# |
| 10 | +# This program is free software: you can redistribute it and/or modify |
| 11 | +# it under the terms of the GNU General Public License as published by |
| 12 | +# the Free Software Foundation, either version 3 of the License, or |
| 13 | +# (at your option) any later version. |
| 14 | +# |
| 15 | +# This program is distributed in the hope that it will be useful, |
| 16 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +# GNU General Public License for more details. |
| 19 | +# |
| 20 | +# You should have received a copy of the GNU General Public License |
| 21 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | +# |
| 23 | +####### |
| 24 | + |
| 25 | +__license__ = "GPLv3" |
| 26 | +__author__ = "Anika Weinmann, Markus Metz" |
| 27 | +__copyright__ = "Copyright 2023, mundialis GmbH & Co. KG" |
| 28 | +__maintainer__ = "Anika Weinmann" |
| 29 | + |
| 30 | +import pytest |
| 31 | + |
| 32 | +from actinia.utils import ( |
| 33 | + create_actinia_pc_item, |
| 34 | + request_and_check, |
| 35 | + set_job_names, |
| 36 | +) |
| 37 | +from .actinia_config import ACTINIA_BASEURL, ACTINIA_VERSION, ACTINIA_AUTH |
| 38 | + |
| 39 | + |
| 40 | +class TestActiniaUtils(object): |
| 41 | + def test_request_and_check(self): |
| 42 | + """Test request_and_check utils function.""" |
| 43 | + url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version" |
| 44 | + resp = request_and_check(url, ACTINIA_AUTH, status_code=200) |
| 45 | + assert "version" in resp |
| 46 | + |
| 47 | + def test_request_and_check_wrong_url(self): |
| 48 | + """Test request_and_check utils function with .""" |
| 49 | + url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/version_fail" |
| 50 | + err_msg = "The requested URL was not found on the server." |
| 51 | + with pytest.raises(Exception) as excinfo: |
| 52 | + request_and_check(url, ACTINIA_AUTH, status_code=200) |
| 53 | + assert err_msg in str(excinfo.value) |
| 54 | + |
| 55 | + def test_request_and_check_wrong_auth(self): |
| 56 | + """Test request_and_check utils function with wrong auth.""" |
| 57 | + url = f"{ACTINIA_BASEURL}api/{ACTINIA_VERSION}/locations" |
| 58 | + err_msg = "Unauthorized Access" |
| 59 | + wrong_auth = ("actinia-gdi", "wrong_pw") |
| 60 | + with pytest.raises(Exception) as excinfo: |
| 61 | + request_and_check(url, wrong_auth, status_code=200) |
| 62 | + assert err_msg in str(excinfo.value) |
| 63 | + |
| 64 | + def test_set_job_names(self): |
| 65 | + """Test set_job_names utils function.""" |
| 66 | + def_name = "def_job_name" |
| 67 | + name_to_set = "test" |
| 68 | + orig_name, name = set_job_names(name_to_set, default_name=def_name) |
| 69 | + assert name_to_set == orig_name |
| 70 | + assert name_to_set in name |
| 71 | + |
| 72 | + def test_set_job_names_using_own_default(self): |
| 73 | + """Test set_job_names utils function with using own default name.""" |
| 74 | + def_name = "def_job_name" |
| 75 | + name_to_set = None |
| 76 | + orig_name, name = set_job_names(name_to_set, default_name=def_name) |
| 77 | + assert def_name == orig_name |
| 78 | + assert "job" in name |
| 79 | + |
| 80 | + def test_set_job_names_using_default(self): |
| 81 | + """Test set_job_names utils function with using default name.""" |
| 82 | + name_to_set = None |
| 83 | + orig_name, name = set_job_names(name_to_set) |
| 84 | + assert "unknown_job" == orig_name |
| 85 | + assert "job" in name |
| 86 | + |
| 87 | + def test_create_actinia_pc_item(self): |
| 88 | + """Test set_job_names utils function with using default name.""" |
| 89 | + module = "g.region" |
| 90 | + id = "pc_item_ide" |
| 91 | + inputs = { |
| 92 | + "raster": "elevation@PERMANENT", |
| 93 | + "res": "5000", |
| 94 | + } |
| 95 | + pc_item = create_actinia_pc_item( |
| 96 | + id=id, |
| 97 | + module="g.region", |
| 98 | + inputs=inputs, |
| 99 | + flags="g", |
| 100 | + ) |
| 101 | + assert "module" in pc_item |
| 102 | + assert "id" in pc_item |
| 103 | + assert "inputs" in pc_item |
| 104 | + assert pc_item["module"] == module |
| 105 | + assert pc_item["id"] == id |
| 106 | + assert isinstance(pc_item["inputs"], list) |
| 107 | + for inp in pc_item["inputs"]: |
| 108 | + param = inp["param"] |
| 109 | + value = inp["value"] |
| 110 | + assert param in inputs |
| 111 | + assert inputs[param] == value |
0 commit comments