def test_get(_clean_specs_table): """ GIVEN user and database and storage with a single spec WHEN get is called with the user and spec id THEN the spec value is returned. """ user = "******" spec_name = "spec name 1" version = "1" package_database.get().create_update_spec( sub=user, name=spec_name, version=version, model_count=1 ) spec = {"components": {}} storage.get_storage_facade().create_update_spec( user=user, name=spec_name, version=version, spec_str=json.dumps(spec, separators=(",", ":")), ) response = specs.get(user=user, spec_name=spec_name) assert response.status_code == 200 assert response.mimetype == "application/json" response_data_json = json.loads(response.data.decode()) assert f"version: '{version}'" in response_data_json["value"] assert "components: {}" in response_data_json["value"] assert response_data_json["name"] == spec_name assert response_data_json["version"] == version
def test_get_database_miss(_clean_specs_table): """ GIVEN user and empty database WHEN get is called with the user and spec id THEN a 404 is returned. """ user = "******" spec_name = "spec name 1" response = specs.get(user=user, spec_name=spec_name) assert response.status_code == 404 assert response.mimetype == "text/plain" assert spec_name in response.data.decode() assert "not find" in response.data.decode()
def test_get_storage_facade_miss(_clean_specs_table): """ GIVEN user and database with a spec but empty storage WHEN get is called with the user and spec id THEN a 404 is returned. """ user = "******" spec_name = "spec name 1" version = "1" package_database.get().create_update_spec( sub=user, name=spec_name, version=version, model_count=1 ) response = specs.get(user=user, spec_name=spec_name) assert response.status_code == 404 assert response.mimetype == "text/plain" assert spec_name in response.data.decode() assert "not find" in response.data.decode()
def test_get_storage_facade_error(_clean_specs_table, monkeypatch): """ GIVEN user and database with a spec but storage that raises an error WHEN get is called with the user and spec id THEN a 500 is returned. """ user = "******" spec_name = "spec name 1" version = "1" package_database.get().create_update_spec( sub=user, name=spec_name, version=version, model_count=1 ) mock_storage_get_spec = mock.MagicMock() mock_storage_get_spec.side_effect = storage.exceptions.StorageError monkeypatch.setattr(storage.get_storage_facade(), "get_spec", mock_storage_get_spec) response = specs.get(user=user, spec_name=spec_name) assert response.status_code == 500 assert response.mimetype == "text/plain" assert "reading" in response.data.decode()
def test_get_database_error(_clean_specs_table, monkeypatch): """ GIVEN user and database that raises an error WHEN get is called with the user and spec id THEN a 500 is returned. """ user = "******" spec_name = "spec name 1" mock_database_get_latest_spec_version = mock.MagicMock() mock_database_get_latest_spec_version.side_effect = ( package_database.exceptions.BaseError ) monkeypatch.setattr( package_database.get(), "get_latest_spec_version", mock_database_get_latest_spec_version, ) response = specs.get(user=user, spec_name=spec_name) assert response.status_code == 500 assert response.mimetype == "text/plain" assert "database" in response.data.decode()