• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

plasticuproject / tides / 10655120513

01 Sep 2024 03:10PM UTC coverage: 98.246% (-0.9%) from 99.115%
10655120513

Pull #5

github

web-flow
Merge edafd959d into 1eb7b3277
Pull Request #5: Update

17 of 17 new or added lines in 3 files covered. (100.0%)

1 existing line in 1 file now uncovered.

112 of 114 relevant lines covered (98.25%)

0.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.18
/test/test_api.py
1
"""test_api.py"""
1✔
2
from __future__ import annotations
1✔
3
import unittest
1✔
4
import json
1✔
5
from unittest import mock
1✔
6
import requests
1✔
7
from api.__main__ import app
1✔
8
from api.scraper.tide_scraper import low_tides_information
1✔
9

10

11
class MockResponse:  # pylint: disable=too-few-public-methods
1✔
12
    """Mock API responses."""
1✔
13

14
    def __init__(self, response_data: str | None, status_code: int) -> None:
1✔
15
        self.response_data = response_data
1✔
16
        self.status_code = status_code
1✔
17
        self.text = response_data
1✔
18

19

20
# pylint: disable=unused-argument
21
def mocked_requests_get(*args: str, **kwargs: str) -> MockResponse:
1✔
22
    """This method will be used by the mock to replace requests.get."""
23
    if args[0] == ("https://www.tide-forecast.com/locations/" +
1✔
24
                   "Huntington-Beach/tides/latest"):
25
        with open("./test/test.html", "r", encoding="utf-8") as infile:
1✔
26
            data = infile.read()
1✔
27
        return MockResponse(data, 200)
1✔
28
    return MockResponse(None, 404)
1✔
29

30

31
class ApiTests(unittest.TestCase):
1✔
32
    """Test endpoints and low tide scraper."""
1✔
33

34
    def setUp(self) -> None:
1✔
35
        """Set up flask app for testing."""
36
        app.config["DEBUG"] = False
1✔
37
        self.app = app.test_client()
1✔
38
        self.assertEqual(app.debug, False)
1✔
39

40
    def tearDown(self) -> None:
1✔
41
        """Tear down."""
42

43
    def test_live_site(self) -> None:
1✔
44
        """Test that tide-forecast site is live."""
45
        response = self.app.get("/api/v1/Huntington-Beach",
1✔
46
                                follow_redirects=True)
47
        print(response.status_code)
1✔
48
        self.assertEqual(response.status_code, 200)
1✔
49

50
    def test_index(self) -> None:
1✔
51
        """Test index/landing page."""
52
        with open("./test/index.html", "r", encoding="utf-8") as infile:
1✔
53
            index_page = infile.read()
1✔
54
        response = self.app.get("/")
1✔
55
        self.assertEqual(response.text, index_page)
1✔
56

57
    def test_redirect(self) -> None:
1✔
58
        """Test the web server is redirecting to the
59
        index page when given invalid endpoints."""
60
        with open("./test/index.html", "r", encoding="utf-8") as infile:
1✔
61
            index_page = infile.read()
1✔
62
        response_one = self.app.get("fart", follow_redirects=True)
1✔
63
        response_two = self.app.get("/api/v1/fart", follow_redirects=True)
1✔
64
        self.assertEqual(response_one.text, index_page)
1✔
65
        self.assertEqual(response_two.text, index_page)
1✔
66

67
    @mock.patch("api.scraper.tide_scraper.requests.get",
1✔
68
                side_effect=mocked_requests_get)
69
    def test_low_tides_infomation(self, mock_get: mock.MagicMock) -> None:
1✔
70
        """Test that the low_tides_information function
71
        returns the proper data given a mock request."""
72
        with open("./test/test.json", "r", encoding="utf-8") as infile:
1✔
73
            json_data = json.loads(infile.read())
1✔
74
        response = low_tides_information("Huntington-Beach")
1✔
75
        self.assertEqual(response, json_data)
1✔
76

77
    @mock.patch("requests.get", side_effect=mocked_requests_get)
1✔
78
    def test_request_mock_404(self, mock_get: mock.MagicMock) -> None:
1✔
79
        """Test that the request mock fails correctly."""
80
        response = requests.get("fart", timeout=60)
1✔
81
        self.assertEqual(response.status_code, 404)
1✔
82

83

84
if __name__ == "__main__":
1✔
UNCOV
85
    unittest.main()
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc