Skip to content

Commit

Permalink
test: fix failing tests #256
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Bozhinov committed Nov 9, 2021
1 parent be64faf commit f64a94c
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 23 deletions.
5 changes: 4 additions & 1 deletion test/datagateway_api/icat/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

@pytest.fixture(scope="package")
def icat_client():
client = Client(config.icat_url, checkCert=config.icat_check_cert)
client = Client(
config.datagateway_api.icat_url,
checkCert=config.datagateway_api.icat_check_cert,
)
client.login(
config.test_mechanism, config.test_user_credentials.dict(),
)
Expand Down
4 changes: 3 additions & 1 deletion test/datagateway_api/icat/filters/test_skip_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def test_valid_skip_value(self, icat_query, skip_value):

assert icat_query.limit == (
skip_value,
get_icat_properties(config.icat_url, config.icat_check_cert)["maxEntities"],
get_icat_properties(
config.datagateway_api.icat_url, config.datagateway_api.icat_check_cert,
)["maxEntities"],
)

@pytest.mark.parametrize(
Expand Down
9 changes: 6 additions & 3 deletions test/datagateway_api/icat/test_lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@
class TestLRUCache:
def test_valid_cache_creation(self):
test_cache = ExtendedLRUCache()
assert test_cache.maxsize == config.client_cache_size
assert test_cache.maxsize == config.datagateway_api.client_cache_size

def test_valid_popitem(self):
test_cache = ExtendedLRUCache()
test_pool = create_client_pool()
test_client = Client(config.icat_url, checkCert=config.icat_check_cert)
test_client = Client(
config.datagateway_api.icat_url,
checkCert=config.datagateway_api.icat_check_cert,
)

test_cache.popitem = MagicMock(side_effect=test_cache.popitem)

@cached(cache=test_cache)
def get_cached_client(cache_number, client_pool):
return test_client

for cache_number in range(config.client_cache_size + 1):
for cache_number in range(config.datagateway_api.client_cache_size + 1):
get_cached_client(cache_number, test_pool)

assert test_cache.popitem.called
5 changes: 4 additions & 1 deletion test/datagateway_api/icat/test_session_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ def test_expired_session(self):
test_backend.get_session_details("session id", client_pool=client_pool)

def test_valid_logout(self, flask_test_app_icat):
client = Client(config.icat_url, checkCert=config.icat_check_cert)
client = Client(
config.datagateway_api.icat_url,
checkCert=config.datagateway_api.icat_check_cert,
)
client.login(
config.test_mechanism, config.test_user_credentials.dict(),
)
Expand Down
44 changes: 28 additions & 16 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
@pytest.fixture()
def test_config_data():
return {
"backend": "db",
"client_cache_size": 5,
"client_pool_init_size": 2,
"client_pool_max_size": 5,
"db_url": "mysql+pymysql://icatdbuser:icatdbuserpw@localhost:3306/icatdb",
"datagateway_api": {
"extension": "/datagateway-api",
"backend": "db",
"client_cache_size": 5,
"client_pool_init_size": 2,
"client_pool_max_size": 5,
"db_url": "mysql+pymysql://icatdbuser:icatdbuserpw@localhost:3306/icatdb",
"icat_url": "https://localhost:8181",
"icat_check_cert": False,
},
"search_api": {
"extension": "/search-api",
"icat_url": "https://localhost:8181",
"icat_check_cert": False,
"client_pool_init_size": 2,
"client_pool_max_size": 5,
},
"flask_reloader": False,
"icat_url": "https://localhost:8181",
"icat_check_cert": False,
"log_level": "WARN",
"log_location": "/home/runner/work/datagateway-api/datagateway-api/logs.log",
"debug_mode": False,
Expand All @@ -36,7 +46,7 @@ def test_config(test_config_data):

class TestAPIConfig:
def test_load_with_valid_config_data(self, test_config):
backend_type = test_config.backend
backend_type = test_config.datagateway_api.backend
assert backend_type == "db"

def test_load_with_no_config_data(self):
Expand All @@ -45,27 +55,29 @@ def test_load_with_no_config_data(self):
APIConfig.load("test/path")

def test_load_with_missing_mandatory_config_data(self, test_config_data):
del test_config_data["backend"]
del test_config_data["host"]
with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))):
with pytest.raises(SystemExit):
APIConfig.load("test/path")

def test_load_with_db_backend_and_missing_db_config_data(self, test_config_data):
del test_config_data["db_url"]
def test_load_with_datagateway_api_db_backend_and_missing_db_config_data(
self, test_config_data,
):
del test_config_data["datagateway_api"]["db_url"]
with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))):
with pytest.raises(SystemExit):
APIConfig.load("test/path")

def test_load_with_python_icat_backend_and_missing_python_icat_config_data(
def test_load_with_datagateway_api_icat_backend_and_missing_icat_config_data(
self, test_config_data,
):
test_config_data["backend"] = "python_icat"
del test_config_data["icat_url"]
test_config_data["datagateway_api"]["backend"] = "python_icat"
del test_config_data["datagateway_api"]["icat_url"]
with patch("builtins.open", mock_open(read_data=json.dumps(test_config_data))):
with pytest.raises(SystemExit):
APIConfig.load("test/path")

def test_set_backend_type(self, test_config):
test_config.set_backend_type("backend_name_changed")
test_config.datagateway_api.set_backend_type("backend_name_changed")

assert test_config.backend == "backend_name_changed"
assert test_config.datagateway_api.backend == "backend_name_changed"
2 changes: 1 addition & 1 deletion test/test_query_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ class DummyQueryFilter(QueryFilter):
assert qf.apply_filter(apply_filter) is None

def test_invalid_query_filter_getter(self):
config.config.backend = "invalid_backend"
config.config.datagateway_api.backend = "invalid_backend"
with pytest.raises(ApiError):
QueryFilterFactory.get_query_filter({"order": "id DESC"})

0 comments on commit f64a94c

Please sign in to comment.