Skip to content
Snippets Groups Projects
Commit 6202c27a authored by Thorge Petersen's avatar Thorge Petersen
Browse files

Fixed usage of builtins and some other tests that failed

parent e79e9f60
Branches
Tags
2 merge requests!41Version 2.0.0,!38Merge py3 into dev
...@@ -18,19 +18,19 @@ class testHashException(unittest.TestCase): ...@@ -18,19 +18,19 @@ class testHashException(unittest.TestCase):
def test_without_hash(self): def test_without_hash(self):
resource = {'package_id':'Test_id',} resource = {'package_id':'Test_id',}
with patch("__builtin__.open", mock_open(read_data=self.text)) as mock_file: with patch("builtins.open", mock_open(read_data=self.text)) as mock_file:
with open('some/file') as f: with open('some/file') as f:
assert _raise_validation_error_if_hash_values_differ(f, resource) == None assert _raise_validation_error_if_hash_values_differ(f, resource) == None
def test_with_correct_hash(self): def test_with_correct_hash(self):
resource = {'package_id':'Test_id', 'hash':self.hash_for_text} resource = {'package_id':'Test_id', 'hash':self.hash_for_text}
with patch("__builtin__.open", mock_open(read_data=self.text)) as mock_file: with patch("builtins.open", mock_open(read_data=self.text)) as mock_file:
with open('some/file') as f: with open('some/file') as f:
assert _raise_validation_error_if_hash_values_differ(f, resource) == None assert _raise_validation_error_if_hash_values_differ(f, resource) == None
def test_with_wrong_hash(self): def test_with_wrong_hash(self):
resource = {'package_id':'Test_id', 'hash':'incorrect_hash'} resource = {'package_id':'Test_id', 'hash':'incorrect_hash'}
with patch("__builtin__.open", mock_open(read_data=self.text)) as mock_file: with patch("builtins.open", mock_open(read_data=self.text)) as mock_file:
with open('some/file') as f: with open('some/file') as f:
with pytest.raises(logic.ValidationError) as e: with pytest.raises(logic.ValidationError) as e:
_raise_validation_error_if_hash_values_differ(f, resource) _raise_validation_error_if_hash_values_differ(f, resource)
...@@ -38,23 +38,19 @@ class testHashException(unittest.TestCase): ...@@ -38,23 +38,19 @@ class testHashException(unittest.TestCase):
assert exception_upload[0] == 'Berechneter Hash und mitgelieferter Hash sind unterschiedlich' assert exception_upload[0] == 'Berechneter Hash und mitgelieferter Hash sind unterschiedlich'
def test_mock_file(self): def test_mock_file(self):
with patch("__builtin__.open", mock_open(read_data=self.text)) as mock_file: with patch("builtins.open", mock_open(read_data=self.text)) as mock_file:
with open('some/file') as f: with open('some/file') as f:
file_content = f.read() file_content = f.read()
assert file_content == self.text assert file_content == self.text
def test_hash_of_empty_string(self): def test_hash_of_empty_string(self):
hash_empty_string = 'd41d8cd98f00b204e9800998ecf8427e' hash_empty_string = 'd41d8cd98f00b204e9800998ecf8427e'
assert hash_empty_string == hashlib.md5('').hexdigest() assert hash_empty_string == hashlib.md5(''.encode('utf-8')).hexdigest()
def test_pdf(self): def test_pdf(self):
# expected_hash_pdf produced by following command in bash: # expected_hash_pdf produced by following command in bash:
# md5sum test.pdf # md5sum test.pdf
expected_hash_pdf = '66123edf64fabf1c073fc45478bf4a57' expected_hash_pdf = '66123edf64fabf1c073fc45478bf4a57'
with open(dir_path + '/resources/test.pdf') as f: with open(dir_path + '/resources/test.pdf', 'rb') as f:
hash = calculate_hash(f) hash = calculate_hash(f)
assert hash == expected_hash_pdf assert hash == expected_hash_pdf
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment