Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for interfaces view #1768

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ PyGithub==2.2.0
black==24.1.1
flake8==6.1.0
Flask-Caching==2.1.0
Flask-Testing==0.8.1
Empty file added tests/interfaces/__init__.py
Empty file.
59 changes: 59 additions & 0 deletions tests/interfaces/test_all_interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from flask import make_response
from unittest.mock import patch
from flask_testing import TestCase
from webapp.app import app


class TestAllInterfaces(TestCase):
def create_app(self):
return app

def setUp(self):
self.app = self.create_app()
self.client = self.app.test_client()

def test_interfaces_json(self):
response = self.client.get("/interfaces.json")
self.assertEqual(response.status_code, 200)
self.assertIn(b"interfaces", response.data)

@patch("webapp.interfaces.views.get_interfaces")
def test_all_interfaces(self, mock_get_interfaces):
mock_get_interfaces.return_value = make_response(
{
"interfaces": [
{
"name": "test_interface1",
"summary": "test_summary1",
"description": "test_description1",
},
{
"name": "test_interface2",
"summary": "test_summary2",
"description": "test_description2",
},
{
"name": "test_interface3",
"summary": "test_summary3",
"description": "test_description3",
},
{
"name": "test_interface4",
"summary": "test_summary4",
"description": "test_description4",
},
{
"name": "test_interface5",
"summary": "test_summary5",
"description": "test_description5",
},
],
"size": 5,
}
)

response = self.client.get("/interfaces")
self.assertEqual(response.status_code, 200)
self.assertIn(b"test_interface1", response.data)
self.assertIn(b"test_interface3", response.data)
self.assert_template_used("interfaces/index.html")
44 changes: 44 additions & 0 deletions tests/interfaces/test_single_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from unittest.mock import patch
from flask_testing import TestCase
from webapp.app import app


class TestSingleInterface(TestCase):
def create_app(self):
return app

def setUp(self):
self.app = self.create_app()
self.client = self.app.test_client()

def test_single_interface(self):
response = self.client.get("/interfaces/test_interface")
self.assertEqual(response.status_code, 200)
self.assertIn(b"test_interface", response.data)

@patch("webapp.interfaces.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("/interfaces/test_interface.json")
self.assertEqual(response.status_code, 200)
self.assertIn(b"other_charms", response.data)

def test_get_single_live_interface(self):
response = self.client.get("/interfaces/test_interface/live.json")
self.assertEqual(response.status_code, 200)

def test_get_single_draft_interface(self):
response = self.client.get("/interfaces/test_interface/draft.json")
self.assertEqual(response.status_code, 200)

def test_single_interface_invalid_status(self):
response = self.client.get(
"/interfaces/test_interface/invalid_status.json"
)
self.assertEqual(response.status_code, 200)
Loading