Skip to content

Commit

Permalink
Test rates.py
Browse files Browse the repository at this point in the history
  • Loading branch information
merc1er committed Jun 10, 2024
1 parent 29cf34c commit b64d220
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ requests==2.32.3

# Dev only packages.
black==24.4.2
coverage==7.5.3
11 changes: 10 additions & 1 deletion run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@


def main():
subprocess.call(["python", "-m", "unittest"])
# Run tests with coverage
subprocess.call(["python", "-m", "coverage", "run", "-m", "unittest", "discover"])

# Generate the coverage report in the terminal
subprocess.call(["python", "-m", "coverage", "report", "-m"])

# Generate the HTML coverage report
subprocess.call(["python", "-m", "coverage", "html"])

print("\n\nTests completed.")
print("HTML report generated at 'htmlcov/index.html'")


if __name__ == "__main__":
Expand Down
51 changes: 50 additions & 1 deletion tests/test_rates.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
import json
import unittest
from unittest.mock import patch, MagicMock

import requests

from tipbot import rates
from .samples import UPDATE


class TestRate(unittest.TestCase):
def test_get_rate(self):
rate = rates.get_rate(UPDATE)
self.assertIs(type(rate), float)
self.assertIsInstance(rate, float)

def test_get_rate_unsupported_currency(self):
mock_update = MagicMock()
rates.get_rate(mock_update, "XYZ")

mock_update.message.reply_text.assert_called_once_with(
"XYZ is not a supported currency."
)

@patch("tipbot.rates.requests.get")
def test_get_rate_api_failure(self, mock_requests_get):
mock_requests_get.side_effect = requests.exceptions.RequestException
mock_update = MagicMock()
rates.get_rate(mock_update, "USD")

mock_update.message.reply_text.assert_called_once_with(
f"Unable to contact {rates.RATE_API}"
)

@patch("tipbot.rates.requests.get")
def test_get_rate_http_error(self, mock_requests_get):
mock_requests_get.side_effect = requests.exceptions.HTTPError
mock_response = MagicMock()
mock_response.text = "Error message"
mock_requests_get.return_value = mock_response
mock_update = MagicMock()
rates.get_rate(mock_update, "USD")

mock_update.message.reply_text.assert_called_once_with(
f"Unable to contact {rates.RATE_API}"
)

@patch("tipbot.rates.requests.get")
def test_get_rate_parse_failure(self, mock_requests_get):
mock_response = MagicMock()
mock_response.raise_for_status = MagicMock()
mock_response.json.side_effect = json.JSONDecodeError("Expecting value", "", 0)
mock_requests_get.return_value = mock_response
mock_update = MagicMock()
rates.get_rate(mock_update, "USD")

mock_update.message.reply_text.assert_called_once_with(
f"Unable to parse rate data: {mock_response.text}"
)

0 comments on commit b64d220

Please sign in to comment.