Skip to content

Commit

Permalink
add tests for interfaces view (#1768)
Browse files Browse the repository at this point in the history
* add tests for interfaces view

* fix failing tests

* chore: update interfaces test

* fix python  lint

* fix failing test

* remove unused variable
  • Loading branch information
codeEmpress1 authored Nov 8, 2024
1 parent 621e9ab commit 8cd8170
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ PyGithub==2.2.0
black==24.3.0
flake8==6.1.0
Flask-Caching==2.1.0
responses==0.22.0
responses==0.22.0
87 changes: 87 additions & 0 deletions tests/interfaces/test_single_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import json
from unittest.mock import patch
from unittest import TestCase

import responses
from webapp.app import app
from webapp.integrations.logic import Interfaces


class TestSingleInterface(TestCase):

def setUp(self):
app.config["TESTING"] = True
app.config["DEBUG"] = True
self.client = app.test_client()
self.test_interfaces = Interfaces()
# mock_github_client = patch("webapp.integrations.logic.github_client")

self.github_interfaces_url = "".join(
[
"https://api.github.com/repos/canonical/",
"charm-relation-interfaces/contents/interfaces",
]
)
self.charmhub_api_get_requirers = "".join(
[
"https://api.charmhub.io/v2/charms/find?",
"q=&category=&publisher=&requires=test_interface",
]
)
self.charmhub_api_get_providers = "".join(
[
"https://api.charmhub.io/v2/charms/find?",
"q=&category=&publisher=&provides=test_interface",
]
)

@responses.activate
def test_single_interface(self):
responses.add(
responses.Response(
method="GET",
url=f"{self.github_interfaces_url}/test_interface",
body="test_interface",
status=200,
)
)
responses.add(
responses.Response(
method="GET",
url=self.charmhub_api_get_requirers,
body=json.dumps({"results": ["test_interface1"]}),
status=200,
)
)
responses.add(
responses.Response(
method="GET",
url=self.charmhub_api_get_providers,
body=json.dumps({"results": ["test_interface2"]}),
status=200,
)
)
response = self.client.get("/integrations/test_interface.json")
self.assertEqual(response.status_code, 200)
self.assertDictEqual(
response.json,
{
"other_charms": {
"providers": ["test_interface2"],
"requirers": ["test_interface1"],
}
},
)

@patch("webapp.integrations.logic.Interfaces")
@patch("webapp.app.app.store_api.find")
def test_repo_has_no_interface(self, mock_find, mock_interfaces):
mock_find.return_value = {
"results": ["mock_providers", "mock_requirers"]
}

mock_interface_logic = mock_interfaces()
mock_interface_logic.get_interface_list.return_value = []
response = self.client.get("/integrations/test_interface.json")
self.assertEqual(response.status_code, 200)
self.assertIn(b"other_charms", response.data)

0 comments on commit 8cd8170

Please sign in to comment.