Skip to content

Commit 18bc355

Browse files
committed
Add FetchLicenseTest for basic test coverage #280
Signed-off-by: Thomas Druez <[email protected]>
1 parent a6c74ea commit 18bc355

File tree

4 files changed

+47
-23
lines changed

4 files changed

+47
-23
lines changed

src/attributecode/model.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,7 @@ def pre_process_and_fetch_license_dict(abouts, api_url, api_key):
14161416
msg = u'Network problem. Please check your Internet connection. License generation is skipped.'
14171417
errors.append(Error(ERROR, msg))
14181418
for about in abouts:
1419-
# No need to go thru all the about objects for license extraction if we detected
1419+
# No need to go through all the about objects for license extraction if we detected
14201420
# invalid '--api_key'
14211421
auth_error = Error(ERROR, u"Authorization denied. Invalid '--api_key'. License generation is skipped.")
14221422
if auth_error in errors:
@@ -1472,7 +1472,7 @@ def valid_api_url(api_url):
14721472
request = Request(api_url)
14731473
# This will always goes to exception as no key are provided.
14741474
# The purpose of this code is to validate the provided api_url is correct
1475-
response = urlopen(request)
1475+
urlopen(request)
14761476
except HTTPError as http_e:
14771477
# The 403 error code is refer to "Authentication credentials were not provided.".
14781478
# This is correct as no key are provided.

tests/test_api.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,30 @@
2020

2121
import unittest
2222

23-
from mock import patch
23+
import mock
2424

2525
from attributecode import api
2626
from attributecode import ERROR
2727
from attributecode import Error
28-
29-
30-
class FakeResponse(object):
31-
response_content = None
32-
33-
def __init__(self, response_content):
34-
self.response_content = response_content
35-
36-
def read(self):
37-
return self.response_content
28+
from testing_utils import FakeResponse
3829

3930

4031
class ApiTest(unittest.TestCase):
41-
@patch.object(api, 'request_license_data')
42-
def test_api_get_license_details_from_api(self, mock_data):
32+
@mock.patch.object(api, 'request_license_data')
33+
def test_api_get_license_details_from_api(self, request_license_data):
4334
license_data = {
4435
'name': 'Apache License 2.0',
4536
'full_text': 'Apache License Version 2.0 ...',
4637
'key': 'apache-2.0',
4738
}
4839
errors = []
49-
mock_data.return_value = license_data, errors
40+
request_license_data.return_value = license_data, errors
5041

5142
expected = ('Apache License 2.0', 'apache-2.0', 'Apache License Version 2.0 ...', [])
5243
result = api.get_license_details_from_api('url', 'api_key', 'license_key')
5344
assert expected == result
5445

55-
@patch.object(api, 'urlopen')
46+
@mock.patch.object(api, 'urlopen')
5647
def test_api_request_license_data(self, mock_data):
5748
response_content = (
5849
b'{"count":1,"results":[{"name":"Apache 2.0","key":"apache-2.0","text":"Text"}]}'

tests/test_model.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525
import unittest
2626
from unittest.case import expectedFailure
2727

28-
from testing_utils import extract_test_loc
29-
from testing_utils import get_temp_file
30-
from testing_utils import get_test_loc
31-
from testing_utils import get_test_lines
32-
from testing_utils import get_unicode_content
28+
import mock
3329

3430
import attributecode
3531
from attributecode import CRITICAL
@@ -41,6 +37,11 @@
4137
from attributecode import util
4238
from attributecode.util import add_unc
4339
from attributecode.util import load_csv
40+
from testing_utils import extract_test_loc
41+
from testing_utils import get_temp_file
42+
from testing_utils import get_test_loc
43+
from testing_utils import get_test_lines
44+
from testing_utils import get_unicode_content
4445

4546

4647
def check_csv(expected, result):
@@ -1313,3 +1314,25 @@ def test_by_name(self):
13131314
('eclipse', [d]),
13141315
])
13151316
assert expected == results
1317+
1318+
1319+
class FetchLicenseTest(unittest.TestCase):
1320+
@mock.patch.object(model, 'urlopen')
1321+
def test_valid_api_url(self, mock_data):
1322+
mock_data.return_value = ''
1323+
assert model.valid_api_url('non_valid_url') is False
1324+
1325+
@mock.patch('attributecode.util.have_network_connection')
1326+
@mock.patch('attributecode.model.valid_api_url')
1327+
def test_pre_process_and_fetch_license_dict(self, have_network_connection, valid_api_url):
1328+
have_network_connection.return_value = True
1329+
1330+
valid_api_url.return_value = False
1331+
error_msg = ('Network problem. Please check your Internet connection. '
1332+
'License generation is skipped.')
1333+
expected = ({}, [Error(ERROR, error_msg)])
1334+
assert model.pre_process_and_fetch_license_dict([], '', '') == expected
1335+
1336+
valid_api_url.return_value = True
1337+
expected = ({}, [])
1338+
assert model.pre_process_and_fetch_license_dict([], '', '') == expected

tests/testing_utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,14 @@ def extract_test_loc(path, extract_func=extract_zip):
155155
else:
156156
target_dir = get_temp_dir()
157157
extract_func(archive, target_dir)
158-
return target_dir
158+
return target_dir
159+
160+
161+
class FakeResponse(object):
162+
response_content = None
163+
164+
def __init__(self, response_content):
165+
self.response_content = response_content
166+
167+
def read(self):
168+
return self.response_content

0 commit comments

Comments
 (0)