forked from ammaritiz/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models.py
More file actions
108 lines (88 loc) · 3.54 KB
/
Copy pathtest_models.py
File metadata and controls
108 lines (88 loc) · 3.54 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""
This modules contains tests for pulp_python.plugins.models.
"""
import hashlib
import unittest
import mock
from pulp_python.plugins import models
def make_package():
return models.Package(
name='foo',
version='1.0.0',
author='Mr Foo',
filename='foo-1.0.0.tar.gz',
summary='Foo!',
_checksum='abc123',
_checksum_type='md5',
)
class TestPackage(unittest.TestCase):
"""
This class contains tests for the Package class.
"""
@mock.patch('__builtin__.open')
def test_checksum_default_sum(self, mock_open):
"""
Assert that the checksum method works correctly with the default sum.
"""
file_chunks = ['Hello', ' World!', '']
def mock_read(size):
"""
A fake file read() that will return "Hello", " World!" and "" when called three times.
"""
self.assertEqual(size, 32 * 1024 * 1024)
return file_chunks.pop(0)
mock_open.return_value.__enter__.return_value.read.side_effect = mock_read
path = '/some/path'
checksum = models.Package.checksum(path)
mock_open.assert_called_once_with(path)
hasher = hashlib.sha512()
hasher.update('Hello World!')
self.assertEqual(checksum, hasher.hexdigest())
@mock.patch('__builtin__.open')
def test_checksum_md5(self, mock_open):
"""
Assert that the checksum method works correctly with md5.
"""
file_chunks = ['Hello', ' World!', '']
def mock_read(size):
"""
A fake file read() that will return "Hello", " World!" and "" when called three times.
"""
self.assertEqual(size, 32 * 1024 * 1024)
return file_chunks.pop(0)
mock_open.return_value.__enter__.return_value.read.side_effect = mock_read
path = '/some/path'
checksum = models.Package.checksum(path, 'md5')
mock_open.assert_called_once_with(path)
hasher = hashlib.md5()
hasher.update('Hello World!')
self.assertEqual(checksum, hasher.hexdigest())
def test_from_json(self):
"""
Test that the data needed to instantiate a package comes from the right part of the JSON.
"""
mock_pkg_attrs = {'filename': "m_file", "url": "earl", "packagetype": "mocktype",
'md5_digest': 'fleventyfive'}
mock_release = "1.0.2"
mock_dist_data = {"author": "me", "name": "test", "summary": "does stuff"}
package = models.Package.from_json(mock_pkg_attrs, mock_release, mock_dist_data)
self.assertEqual(package.filename, 'm_file')
self.assertEqual(package.version, '1.0.2')
self.assertEqual(package.author, 'me')
self.assertEqual(package.summary, 'does stuff')
@mock.patch('pulp_python.plugins.models.PackageFile')
def test_from_file(self, m_twine):
"""
Ensure that before init, metadata from twine is filtered leaving only required fields.
"""
twine_to_dict = m_twine.from_filename.return_value.metadata_dictionary
twine_to_dict.return_value = {'name': 'necessary', 'extra_field': 'do not include'}
package = models.Package.from_archive('mock_path')
self.assertEqual(package.name, 'necessary')
self.assertFalse(hasattr(package, 'extra_field'))
def test___repr__(self):
"""
Assert correct behavior with the __repr__() method.
"""
pp = make_package()
self.assertEqual(repr(pp), 'Package(name=foo, version=1.0.0)')