-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
89 lines (65 loc) · 2.21 KB
/
test_app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import gzip
from collections import Counter
from unittest.mock import Mock, patch
import pytest
import requests
from app import BASE_URL, get_contents, get_counts, show_top
@pytest.fixture
def mock_contents():
return """
bin/file1 packageA
bin/file2 packageA,packageB
bin/file3 packageA,packageB,packageC
MALFORMED/FILE/PACKAGE
"""
# Happy paths with 200 responses:
@patch("requests.get")
def test_get_contents_200(mock_get):
"""Test `get_contents` with a successful response."""
# Mock the 200 response.
mock_response = Mock()
mock_response.status_code = 200
mock_response.content = gzip.compress(b"valid contents")
mock_get.return_value = mock_response
# Call TUF and check results.
actual = get_contents("amd64")
expected = "valid contents"
assert actual == expected
mock_get.assert_called_once_with(f"{BASE_URL}/Contents-amd64.gz")
def test_get_counts_200(mock_contents):
"""Test `get_counts` showing correct counts."""
result = get_counts(mock_contents)
expected = Counter({"packageA": 3, "packageB": 2, "packageC": 1})
assert result == expected
def test_show_top_200(mock_contents, capsys):
"""Test `show_top` showing correct top result."""
result = get_counts(mock_contents)
show_top(result)
captured = capsys.readouterr()
assert "01. packageA" in captured.out
assert "" in captured.err
# Unhappy paths with 404 responses:
@patch("requests.get")
def test_get_contents_404(mock_get):
"""Test `get_contents` with a failed response."""
# Mock the 404 response.
mock_response = Mock()
mock_response.status_code = 404
mock_get.return_value = mock_response
# Call real function and check the results.
actual = get_contents("amd64")
expected = None
assert actual == expected
mock_get.assert_called_once_with(f"{BASE_URL}/Contents-amd64.gz")
def test_get_counts_404():
"""Test `get_counts` of None."""
result = get_counts(None)
expected = Counter()
assert result == expected
def test_show_top_404(capsys):
"""Test `show_top` of empty counter."""
result = get_counts(None)
show_top(result)
captured = capsys.readouterr()
assert "" in captured.out
assert "" in captured.err