Skip to content

Commit

Permalink
tests: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lengau committed Jun 12, 2024
1 parent dc8e9d4 commit db748e8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
8 changes: 7 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,14 @@ def mock_store_client():
return client


@pytest.fixture()
def mock_store_anonymous_client() -> mock.Mock:
return mock.Mock(spec_set=store.AnonymousClient)


@pytest.fixture()
def service_factory(
fs, fake_project_dir, fake_prime_dir, simple_charm, mock_store_client
fs, fake_project_dir, fake_prime_dir, simple_charm, mock_store_client, mock_store_anonymous_client
) -> services.CharmcraftServiceFactory:
factory = services.CharmcraftServiceFactory(app=APP_METADATA)

Expand All @@ -78,6 +83,7 @@ def service_factory(
factory.project = simple_charm

factory.store.client = mock_store_client
factory.store.anonymous_client = mock_store_anonymous_client

return factory

Expand Down
14 changes: 7 additions & 7 deletions tests/unit/commands/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_fetch_libs_no_charm_libs(
Could not find the following libraries on charmhub:
- lib: mysql.mysql
version: '1'
- lib: some_charm.lib
- lib: some-charm.lib
version: '1.2'
"""
),
Expand All @@ -173,7 +173,7 @@ def test_fetch_libs_no_charm_libs(
)
def test_fetch_libs_missing_from_store(service_factory, libs, expected):
service_factory.project.charm_libs = libs
service_factory.store.client.fetch_libraries_metadata.return_value = []
service_factory.store.anonymous_client.fetch_libraries_metadata.return_value = []
fetch_libs = FetchLibs({"app": APP_METADATA, "services": service_factory})

with pytest.raises(errors.CraftError) as exc_info:
Expand Down Expand Up @@ -213,8 +213,8 @@ def test_fetch_libs_missing_from_store(service_factory, libs, expected):
)
def test_fetch_libs_no_content(new_path, service_factory, libs, store_libs, dl_lib, expected):
service_factory.project.charm_libs = libs
service_factory.store.client.fetch_libraries_metadata.return_value = store_libs
service_factory.store.client.get_library.return_value = dl_lib
service_factory.store.anonymous_client.fetch_libraries_metadata.return_value = store_libs
service_factory.store.anonymous_client.get_library.return_value = dl_lib
fetch_libs = FetchLibs({"app": APP_METADATA, "services": service_factory})

with pytest.raises(errors.CraftError, match=expected) as exc_info:
Expand Down Expand Up @@ -254,10 +254,10 @@ def test_fetch_libs_no_content(new_path, service_factory, libs, store_libs, dl_l
)
def test_fetch_libs_success(
new_path, emitter, service_factory, libs, store_libs, dl_lib, expected
):
) -> None:
service_factory.project.charm_libs = libs
service_factory.store.client.fetch_libraries_metadata.return_value = store_libs
service_factory.store.client.get_library.return_value = dl_lib
service_factory.store.anonymous_client.fetch_libraries_metadata.return_value = store_libs
service_factory.store.anonymous_client.get_library.return_value = dl_lib
fetch_libs = FetchLibs({"app": APP_METADATA, "services": service_factory})

fetch_libs.run(argparse.Namespace())
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/services/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
def store(service_factory) -> services.StoreService:
store = services.StoreService(app=application.APP_METADATA, services=service_factory)
store.client = mock.Mock(spec_set=client.Client)
store.anonymous_client = mock.Mock(spec_set=client.AnonymousClient)
return store


Expand Down Expand Up @@ -232,16 +233,16 @@ def test_get_credentials(monkeypatch, store):
([], []),
(
[CharmLib(lib="my_charm.my_lib", version="1")],
[{"charm-name": "my_charm", "library-name": "my_lib", "api": 1}],
[{"charm-name": "my-charm", "library-name": "my_lib", "api": 1}],
),
(
[CharmLib(lib="my_charm.my_lib", version="1.0")],
[{"charm-name": "my_charm", "library-name": "my_lib", "api": 1, "patch": 0}],
[{"charm-name": "my-charm", "library-name": "my_lib", "api": 1, "patch": 0}],
),
],
)
def test_fetch_libraries_metadata(monkeypatch, store, libs, expected_call):

store.get_libraries_metadata(libs)

store.client.fetch_libraries_metadata.assert_called_once_with(expected_call)
store.anonymous_client.fetch_libraries_metadata.assert_called_once_with(expected_call)

0 comments on commit db748e8

Please sign in to comment.