Changeset View
Standalone View
swh/web/ui/tests/test_service.py
Show All 10 Lines | |||||
from swh.core.hashutil import hex_to_hash, hash_to_hex | from swh.core.hashutil import hex_to_hash, hash_to_hex | ||||
from swh.web.ui import service | from swh.web.ui import service | ||||
from swh.web.ui.exc import BadInputExc, NotFoundExc | from swh.web.ui.exc import BadInputExc, NotFoundExc | ||||
from swh.web.ui.tests import test_app | from swh.web.ui.tests import test_app | ||||
class ServiceTestCase(test_app.SWHApiTestCase): | class ServiceTestCase(test_app.SWHApiTestCase): | ||||
def setUp(self): | |||||
self.SHA1_SAMPLE = '18d8be353ed3480476f032475e7c233eff7371d5' | |||||
self.SHA1_SAMPLE_BIN = hex_to_hash(self.SHA1_SAMPLE) | |||||
self.SHA256_SAMPLE = ('39007420ca5de7cb3cfc15196335507e' | |||||
'e76c98930e7e0afa4d2747d3bf96c926') | |||||
self.SHA256_SAMPLE_BIN = hex_to_hash(self.SHA256_SAMPLE) | |||||
self.SHA1GIT_SAMPLE = '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03' | |||||
self.SHA1GIT_SAMPLE_BIN = hex_to_hash(self.SHA1GIT_SAMPLE) | |||||
self.DIRECTORY_ID = '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6' | |||||
self.DIRECTORY_ID_BIN = hex_to_hash(self.DIRECTORY_ID) | |||||
self.AUTHOR_ID_BIN = { | |||||
'name': b'author', | |||||
'email': b'author@company.org', | |||||
} | |||||
self.AUTHOR_ID = { | |||||
'name': 'author', | |||||
'email': 'author@company.org', | |||||
} | |||||
self.COMMITTER_ID_BIN = { | |||||
'name': b'committer', | |||||
'email': b'committer@corp.org', | |||||
} | |||||
self.COMMITTER_ID = { | |||||
'name': 'committer', | |||||
'email': 'committer@corp.org', | |||||
} | |||||
self.SAMPLE_DATE_RAW = { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
} | |||||
self.SAMPLE_DATE = '2000-01-17T11:23:54+00:00' | |||||
self.SAMPLE_MESSAGE_BIN = b'elegant fix for bug 31415957' | |||||
self.SAMPLE_MESSAGE = 'elegant fix for bug 31415957' | |||||
self.SAMPLE_REVISION = { | |||||
'id': self.SHA1_SAMPLE, | |||||
'directory': self.DIRECTORY_ID, | |||||
'author': self.AUTHOR_ID, | |||||
'committer': self.COMMITTER_ID, | |||||
'message': self.SAMPLE_MESSAGE, | |||||
'date': self.SAMPLE_DATE, | |||||
'committer_date': self.SAMPLE_DATE, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
'merge': False | |||||
} | |||||
self.SAMPLE_REVISION_RAW = { | |||||
'id': self.SHA1_SAMPLE_BIN, | |||||
'directory': self.DIRECTORY_ID_BIN, | |||||
'author': self.AUTHOR_ID_BIN, | |||||
'committer': self.COMMITTER_ID_BIN, | |||||
'message': self.SAMPLE_MESSAGE_BIN, | |||||
'date': self.SAMPLE_DATE_RAW, | |||||
'committer_date': self.SAMPLE_DATE_RAW, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
} | |||||
self.SAMPLE_CONTENT = { | |||||
'sha1': self.SHA1_SAMPLE, | |||||
'sha256': self.SHA256_SAMPLE, | |||||
'sha1_git': self.SHA1GIT_SAMPLE, | |||||
'length': 190, | |||||
'status': 'absent' | |||||
} | |||||
self.SAMPLE_CONTENT_RAW = { | |||||
'sha1': self.SHA1_SAMPLE_BIN, | |||||
'sha256': self.SHA256_SAMPLE_BIN, | |||||
'sha1_git': self.SHA1GIT_SAMPLE_BIN, | |||||
'length': 190, | |||||
'status': 'hidden' | |||||
} | |||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_multiple_hashes_ball_missing(self, mock_backend): | def lookup_multiple_hashes_ball_missing(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.content_missing_per_sha1 = MagicMock(return_value=[]) | mock_backend.content_missing_per_sha1 = MagicMock(return_value=[]) | ||||
# when | # when | ||||
actual_lookup = service.lookup_multiple_hashes( | actual_lookup = service.lookup_multiple_hashes( | ||||
▲ Show 20 Lines • Show All 542 Lines • ▼ Show 20 Lines | def lookup_revision_with_context(self, mock_query, mock_backend): | ||||
sha1_git) | sha1_git) | ||||
# then | # then | ||||
self.assertEquals(actual_revision, { | self.assertEquals(actual_revision, { | ||||
'id': hash_to_hex(sha1_git_bin), | 'id': hash_to_hex(sha1_git_bin), | ||||
'parents': [], | 'parents': [], | ||||
'children': [hash_to_hex(b'999'), hash_to_hex(b'777')], | 'children': [hash_to_hex(b'999'), hash_to_hex(b'777')], | ||||
'directory': hash_to_hex(b'278'), | 'directory': hash_to_hex(b'278'), | ||||
'merge': False | |||||
}) | }) | ||||
mock_query.parse_hash_with_algorithms_or_throws.assert_has_calls( | mock_query.parse_hash_with_algorithms_or_throws.assert_has_calls( | ||||
[call(sha1_git, ['sha1'], 'Only sha1_git is supported.'), | [call(sha1_git, ['sha1'], 'Only sha1_git is supported.'), | ||||
call(sha1_git_root, ['sha1'], 'Only sha1_git is supported.')]) | call(sha1_git_root, ['sha1'], 'Only sha1_git is supported.')]) | ||||
mock_backend.revision_log.assert_called_with( | mock_backend.revision_log.assert_called_with( | ||||
sha1_git_root_bin, 100) | sha1_git_root_bin, 100) | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | def lookup_revision_with_context_sha1_git_root_already_retrieved_as_dict( | ||||
sha1_git) | sha1_git) | ||||
# then | # then | ||||
self.assertEquals(actual_revision, { | self.assertEquals(actual_revision, { | ||||
'id': hash_to_hex(sha1_git_bin), | 'id': hash_to_hex(sha1_git_bin), | ||||
'parents': [], | 'parents': [], | ||||
'children': [hash_to_hex(b'999'), hash_to_hex(b'777')], | 'children': [hash_to_hex(b'999'), hash_to_hex(b'777')], | ||||
'directory': hash_to_hex(b'278'), | 'directory': hash_to_hex(b'278'), | ||||
'merge': False | |||||
}) | }) | ||||
mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with( # noqa | mock_query.parse_hash_with_algorithms_or_throws.assert_called_once_with( # noqa | ||||
sha1_git, ['sha1'], 'Only sha1_git is supported.') | sha1_git, ['sha1'], 'Only sha1_git is supported.') | ||||
mock_backend.revision_get.assert_called_once_with(sha1_git_bin) | mock_backend.revision_get.assert_called_once_with(sha1_git_bin) | ||||
mock_backend.revision_log.assert_called_with( | mock_backend.revision_log.assert_called_with( | ||||
▲ Show 20 Lines • Show All 289 Lines • ▼ Show 20 Lines | def lookup_directory_with_revision_revision_with_path_to_file_with_data( | ||||
mock_backend.directory_entry_get_by_path.assert_called_once_with( | mock_backend.directory_entry_get_by_path.assert_called_once_with( | ||||
b'dir-id-as-sha1', 'some/path/to/file') | b'dir-id-as-sha1', 'some/path/to/file') | ||||
mock_backend.content_find.assert_called_once_with('sha1_git', b'789') | mock_backend.content_find.assert_called_once_with('sha1_git', b'789') | ||||
mock_backend.content_get.assert_called_once_with(b'content-sha1') | mock_backend.content_get.assert_called_once_with(b'content-sha1') | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision(self, mock_backend): | def lookup_revision(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.revision_get = MagicMock(return_value={ | mock_backend.revision_get = MagicMock( | ||||
zack: OK, //almost// there! The constants you added are great and reduce duplication a lot.
… but now… | |||||
'id': hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5'), | return_value=self.SAMPLE_REVISION_RAW) | ||||
'directory': hex_to_hash( | |||||
'7834ef7e7c357ce2af928115c6c6a42b7e2a44e6'), | |||||
'author': { | |||||
'name': b'bill & boule', | |||||
'email': b'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'boule & bill', | |||||
'email': b'boule@bill.org', | |||||
}, | |||||
'message': b'elegant fix for bug 31415957', | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}) | |||||
# when | # when | ||||
actual_revision = service.lookup_revision( | actual_revision = service.lookup_revision( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5') | self.SHA1_SAMPLE) | ||||
# then | # then | ||||
self.assertEqual(actual_revision, { | self.assertEqual(actual_revision, self.SAMPLE_REVISION) | ||||
Not Done Inline Actionsthis will become self.assertEqual(actual_revision, REVISION) zack: this will become
```
self.assertEqual(actual_revision, REVISION)
``` | |||||
'id': '18d8be353ed3480476f032475e7c233eff7371d5', | |||||
'directory': '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6', | |||||
'author': { | |||||
'name': 'bill & boule', | |||||
'email': 'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': 'boule & bill', | |||||
'email': 'boule@bill.org', | |||||
}, | |||||
'message': 'elegant fix for bug 31415957', | |||||
'date': "2000-01-17T11:23:54+00:00", | |||||
'committer_date': "2000-01-17T11:23:54+00:00", | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}) | |||||
mock_backend.revision_get.assert_called_with( | mock_backend.revision_get.assert_called_with( | ||||
hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | self.SHA1_SAMPLE_BIN) | ||||
Not Done Inline Actionsthis OTOH it is totally fine as is, as you need a finer grained version of the macro-constant zack: this OTOH it is totally fine as is, as you need a finer grained version of the macro-constant | |||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_invalid_msg(self, mock_backend): | def lookup_revision_invalid_msg(self, mock_backend): | ||||
# given | # given | ||||
stub_rev = { | stub_rev = self.SAMPLE_REVISION_RAW | ||||
Not Done Inline ActionsThis could remain as is, as you need to do something different for this case. stub_rev = REVISION_RAW stub_rev['id'] = ... stub_rev['message'] = ... which has the added benefit of making clear what is different in the test data of this test case from other ones. (Note: the above has potentially variable aliasing issues though, so you should either use a copy constructor, or make sure the constants are in a place that get refreshed for each test case.) zack: This //could// remain as is, as you need to do something different for this case.
But it'd be… | |||||
'id': hex_to_hash('123456'), | stub_rev['message'] = b'elegant fix for bug \xff' | ||||
'directory': hex_to_hash( | |||||
'7834ef7e7c357ce2af928115c6c6a42b7e2a44e6'), | expected_revision = self.SAMPLE_REVISION | ||||
'author': { | expected_revision['message'] = None | ||||
'name': b'bill & boule', | expected_revision['message_decoding_failed'] = True | ||||
'email': b'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'boule & bill', | |||||
'email': b'boule@bill.org', | |||||
}, | |||||
'message': b'elegant fix for bug \xff', | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
} | |||||
mock_backend.revision_get = MagicMock(return_value=stub_rev) | mock_backend.revision_get = MagicMock(return_value=stub_rev) | ||||
# when | # when | ||||
actual_revision = service.lookup_revision( | actual_revision = service.lookup_revision( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5') | self.SHA1_SAMPLE) | ||||
# then | # then | ||||
self.assertEqual(actual_revision, { | self.assertEqual(actual_revision, expected_revision) | ||||
Not Done Inline Actionsetc. zack: etc. | |||||
'id': '123456', | |||||
'directory': '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6', | |||||
'author': { | |||||
'name': 'bill & boule', | |||||
'email': 'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': 'boule & bill', | |||||
'email': 'boule@bill.org', | |||||
}, | |||||
'message': None, | |||||
'message_decoding_failed': True, | |||||
'date': "2000-01-17T11:23:54+00:00", | |||||
'committer_date': "2000-01-17T11:23:54+00:00", | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}) | |||||
mock_backend.revision_get.assert_called_with( | mock_backend.revision_get.assert_called_with( | ||||
hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | self.SHA1_SAMPLE_BIN) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_msg_ok(self, mock_backend): | def lookup_revision_msg_ok(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.revision_get.return_value = { | mock_backend.revision_get.return_value = self.SAMPLE_REVISION_RAW | ||||
'id': hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5'), | |||||
'directory': hex_to_hash( | |||||
'7834ef7e7c357ce2af928115c6c6a42b7e2a44e6'), | |||||
'author': { | |||||
'name': b'bill & boule', | |||||
'email': b'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'boule & bill', | |||||
'email': b'boule@bill.org', | |||||
}, | |||||
'message': b'elegant fix for bug 31415957', | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
} | |||||
# when | # when | ||||
rv = service.lookup_revision_message( | rv = service.lookup_revision_message( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5') | self.SHA1_SAMPLE) | ||||
# then | # then | ||||
self.assertEquals(rv, {'message': b'elegant fix for bug 31415957'}) | self.assertEquals(rv, {'message': self.SAMPLE_MESSAGE_BIN}) | ||||
mock_backend.revision_get.assert_called_with( | mock_backend.revision_get.assert_called_with( | ||||
hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | self.SHA1_SAMPLE_BIN) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_msg_absent(self, mock_backend): | def lookup_revision_msg_absent(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.revision_get.return_value = { | stub_revision = self.SAMPLE_REVISION_RAW | ||||
'id': hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5'), | del stub_revision['message'] | ||||
'directory': hex_to_hash( | mock_backend.revision_get.return_value = stub_revision | ||||
'7834ef7e7c357ce2af928115c6c6a42b7e2a44e6'), | |||||
'author': { | |||||
'name': b'bill & boule', | |||||
'email': b'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'boule & bill', | |||||
'email': b'boule@bill.org', | |||||
}, | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
} | |||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_revision_message( | service.lookup_revision_message( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5') | self.SHA1_SAMPLE) | ||||
# then | # then | ||||
mock_backend.revision_get.assert_called_with( | mock_backend.revision_get.assert_called_with( | ||||
hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | self.SHA1_SAMPLE_BIN) | ||||
self.assertEqual(cm.exception.args[0], 'No message for revision ' | self.assertEqual(cm.exception.args[0], 'No message for revision ' | ||||
'with sha1_git ' | 'with sha1_git ' | ||||
'18d8be353ed3480476f032475e7c233eff7371d5.') | '18d8be353ed3480476f032475e7c233eff7371d5.') | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_msg_norev(self, mock_backend): | def lookup_revision_msg_norev(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.revision_get.return_value = None | mock_backend.revision_get.return_value = None | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
service.lookup_revision_message( | service.lookup_revision_message( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5') | self.SHA1_SAMPLE) | ||||
# then | # then | ||||
mock_backend.revision_get.assert_called_with( | mock_backend.revision_get.assert_called_with( | ||||
hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | self.SHA1_SAMPLE_BIN) | ||||
self.assertEqual(cm.exception.args[0], 'Revision with sha1_git ' | self.assertEqual(cm.exception.args[0], 'Revision with sha1_git ' | ||||
'18d8be353ed3480476f032475e7c233eff7371d5 ' | '18d8be353ed3480476f032475e7c233eff7371d5 ' | ||||
'not found.') | 'not found.') | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_multiple(self, mock_backend): | def lookup_revision_multiple(self, mock_backend): | ||||
# given | # given | ||||
sha1 = self.SHA1_SAMPLE | |||||
sha1_bin = '18d8be353ed3480476f032475e7c233eff7371d5' | |||||
sha1_other = 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' | sha1_other = 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' | ||||
stub_revisions = [ | stub_revisions = [ | ||||
{ | self.SAMPLE_REVISION_RAW, | ||||
'id': hex_to_hash(sha1_bin), | |||||
'directory': '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6', | |||||
'author': { | |||||
'name': b'bill & boule', | |||||
'email': b'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'boule & bill', | |||||
'email': b'boule@bill.org', | |||||
}, | |||||
'message': b'elegant fix for bug 31415957', | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False | |||||
}, | |||||
'date_offset': 0, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False | |||||
}, | |||||
'committer_date_offset': 0, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}, | |||||
{ | { | ||||
'id': hex_to_hash(sha1_other), | 'id': hex_to_hash(sha1_other), | ||||
'directory': 'abcdbe353ed3480476f032475e7c233eff7371d5', | 'directory': 'abcdbe353ed3480476f032475e7c233eff7371d5', | ||||
'author': { | 'author': { | ||||
'name': b'name', | 'name': b'name', | ||||
'email': b'name@surname.org', | 'email': b'name@surname.org', | ||||
}, | }, | ||||
'committer': { | 'committer': { | ||||
Show All 23 Lines | def lookup_revision_multiple(self, mock_backend): | ||||
'metadata': [], | 'metadata': [], | ||||
} | } | ||||
] | ] | ||||
mock_backend.revision_get_multiple.return_value = stub_revisions | mock_backend.revision_get_multiple.return_value = stub_revisions | ||||
# when | # when | ||||
actual_revisions = service.lookup_revision_multiple( | actual_revisions = service.lookup_revision_multiple( | ||||
[sha1_bin, sha1_other]) | [sha1, sha1_other]) | ||||
# then | # then | ||||
self.assertEqual(list(actual_revisions), [ | self.assertEqual(list(actual_revisions), [ | ||||
{ | self.SAMPLE_REVISION, | ||||
Not Done Inline Actionshere's another one: this revision, or at least many metadata about it, are reused in multiple tests. Please factorize this out, … and DRY (Don't Repeat Yourself) :-) zack: here's another one: this revision, or at least many metadata about it, are reused in multiple… | |||||
Not Done Inline ActionsI should make a standalone task for this, since there are a lot of other objects that would benefit from factorizing in test_service.py. On the other hand, I believe @ardumont's rationale behind avoiding factorizing was that it much improves readability of the test itself. Since performance is far from being an issue in these tests, I'm tempted to argue against factorizing. jbertran: I should make a standalone task for this, since there are a lot of other objects that would… | |||||
Not Done Inline ActionsNothing about my comments is about performance. They're all about readability/maintanability of the tests. I'm not against a separate task for fixing this for old code, but your proposed change is contributing to the problem. So please avoid introducing even more test value duplication with this change, and submit a separate task for the old code. (I think it'd be easier to just fix this all at once, but YMMV.) zack: Nothing about my comments is about performance. They're all about readability/maintanability of… | |||||
Not Done Inline Actions
@jbertran Precision: avoiding factorizing in a test context. I do abide by the DRY principle but not fully in a test context. We have too much duplication now. Regarding this factorization, we could also create some intermediary helper functions make_revision, make_tree (or something) which takes as parameter the important part (in regard of that test, it seems to be the id here). ardumont: > @ardumont's rationale behind avoiding factorizing was that it much improves readability of… | |||||
'id': sha1_bin, | |||||
'directory': '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6', | |||||
'author': { | |||||
'name': 'bill & boule', | |||||
'email': 'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': 'boule & bill', | |||||
'email': 'boule@bill.org', | |||||
}, | |||||
'message': 'elegant fix for bug 31415957', | |||||
'date': '2000-01-17T11:23:54+00:00', | |||||
'date_offset': 0, | |||||
'committer_date': '2000-01-17T11:23:54+00:00', | |||||
'committer_date_offset': 0, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}, | |||||
{ | { | ||||
'id': sha1_other, | 'id': sha1_other, | ||||
'directory': 'abcdbe353ed3480476f032475e7c233eff7371d5', | 'directory': 'abcdbe353ed3480476f032475e7c233eff7371d5', | ||||
'author': { | 'author': { | ||||
'name': 'name', | 'name': 'name', | ||||
'email': 'name@surname.org', | 'email': 'name@surname.org', | ||||
}, | }, | ||||
'committer': { | 'committer': { | ||||
'name': 'name', | 'name': 'name', | ||||
'email': 'name@surname.org', | 'email': 'name@surname.org', | ||||
}, | }, | ||||
'message': 'ugly fix for bug 42', | 'message': 'ugly fix for bug 42', | ||||
'date': '2000-01-12T05:23:54+00:00', | 'date': '2000-01-12T05:23:54+00:00', | ||||
'date_offset': 0, | 'date_offset': 0, | ||||
'committer_date': '2000-01-12T05:23:54+00:00', | 'committer_date': '2000-01-12T05:23:54+00:00', | ||||
'committer_date_offset': 0, | 'committer_date_offset': 0, | ||||
'synthetic': False, | 'synthetic': False, | ||||
'type': 'git', | 'type': 'git', | ||||
'parents': [], | 'parents': [], | ||||
'metadata': [], | 'metadata': [], | ||||
'merge': False | |||||
} | } | ||||
]) | ]) | ||||
self.assertEqual( | self.assertEqual( | ||||
list(mock_backend.revision_get_multiple.call_args[0][0]), | list(mock_backend.revision_get_multiple.call_args[0][0]), | ||||
[hex_to_hash( | [hex_to_hash(sha1), | ||||
'18d8be353ed3480476f032475e7c233eff7371d5'), | hex_to_hash(sha1_other)]) | ||||
hex_to_hash( | |||||
'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc')]) | |||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_multiple_none_found(self, mock_backend): | def lookup_revision_multiple_none_found(self, mock_backend): | ||||
# given | # given | ||||
sha1_bin = '18d8be353ed3480476f032475e7c233eff7371d5' | sha1_bin = self.SHA1_SAMPLE | ||||
sha1_other = 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' | sha1_other = 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc' | ||||
mock_backend.revision_get_multiple.return_value = [] | mock_backend.revision_get_multiple.return_value = [] | ||||
# then | # then | ||||
actual_revisions = service.lookup_revision_multiple( | actual_revisions = service.lookup_revision_multiple( | ||||
[sha1_bin, sha1_other]) | [sha1_bin, sha1_other]) | ||||
self.assertEqual(list(actual_revisions), []) | self.assertEqual(list(actual_revisions), []) | ||||
self.assertEqual( | self.assertEqual( | ||||
list(mock_backend.revision_get_multiple.call_args[0][0]), | list(mock_backend.revision_get_multiple.call_args[0][0]), | ||||
[hex_to_hash( | [hex_to_hash(self.SHA1_SAMPLE), | ||||
'18d8be353ed3480476f032475e7c233eff7371d5'), | hex_to_hash(sha1_other)]) | ||||
hex_to_hash( | |||||
'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc')]) | |||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_log(self, mock_backend): | def lookup_revision_log(self, mock_backend): | ||||
# given | # given | ||||
stub_revision_log = [{ | stub_revision_log = [self.SAMPLE_REVISION_RAW] | ||||
'id': hex_to_hash('28d8be353ed3480476f032475e7c233eff7371d5'), | |||||
'directory': hex_to_hash( | |||||
'7834ef7e7c357ce2af928115c6c6a42b7e2a44e6'), | |||||
'author': { | |||||
'name': b'bill & boule', | |||||
'email': b'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'boule & bill', | |||||
'email': b'boule@bill.org', | |||||
}, | |||||
'message': b'elegant fix for bug 31415957', | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}] | |||||
mock_backend.revision_log = MagicMock(return_value=stub_revision_log) | mock_backend.revision_log = MagicMock(return_value=stub_revision_log) | ||||
# when | # when | ||||
actual_revision = service.lookup_revision_log( | actual_revision = service.lookup_revision_log( | ||||
'abcdbe353ed3480476f032475e7c233eff7371d5') | 'abcdbe353ed3480476f032475e7c233eff7371d5') | ||||
# then | # then | ||||
self.assertEqual(list(actual_revision), [{ | self.assertEqual(list(actual_revision), [self.SAMPLE_REVISION]) | ||||
'id': '28d8be353ed3480476f032475e7c233eff7371d5', | |||||
'directory': '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6', | |||||
'author': { | |||||
'name': 'bill & boule', | |||||
'email': 'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': 'boule & bill', | |||||
'email': 'boule@bill.org', | |||||
}, | |||||
'message': 'elegant fix for bug 31415957', | |||||
'date': "2000-01-17T11:23:54+00:00", | |||||
'committer_date': "2000-01-17T11:23:54+00:00", | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}]) | |||||
mock_backend.revision_log.assert_called_with( | mock_backend.revision_log.assert_called_with( | ||||
hex_to_hash('abcdbe353ed3480476f032475e7c233eff7371d5'), 100) | hex_to_hash('abcdbe353ed3480476f032475e7c233eff7371d5'), 100) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_log_by(self, mock_backend): | def lookup_revision_log_by(self, mock_backend): | ||||
# given | # given | ||||
stub_revision_log = [{ | stub_revision_log = [self.SAMPLE_REVISION_RAW] | ||||
'id': hex_to_hash('28d8be353ed3480476f032475e7c233eff7371d5'), | |||||
'directory': hex_to_hash( | |||||
'7834ef7e7c357ce2af928115c6c6a42b7e2a44e6'), | |||||
'author': { | |||||
'name': b'bill & boule', | |||||
'email': b'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'boule & bill', | |||||
'email': b'boule@bill.org', | |||||
}, | |||||
'message': b'elegant fix for bug 31415957', | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2000, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc, | |||||
).timestamp(), | |||||
'offset': 0, | |||||
'negative_utc': False, | |||||
}, | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}] | |||||
mock_backend.revision_log_by = MagicMock( | mock_backend.revision_log_by = MagicMock( | ||||
return_value=stub_revision_log) | return_value=stub_revision_log) | ||||
# when | # when | ||||
actual_log = service.lookup_revision_log_by( | actual_log = service.lookup_revision_log_by( | ||||
1, 'refs/heads/master', None) | 1, 'refs/heads/master', None) | ||||
# then | # then | ||||
self.assertEqual(list(actual_log), [{ | self.assertEqual(list(actual_log), [self.SAMPLE_REVISION]) | ||||
'id': '28d8be353ed3480476f032475e7c233eff7371d5', | |||||
'directory': '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6', | |||||
'author': { | |||||
'name': 'bill & boule', | |||||
'email': 'bill@boule.org', | |||||
}, | |||||
'committer': { | |||||
'name': 'boule & bill', | |||||
'email': 'boule@bill.org', | |||||
}, | |||||
'message': 'elegant fix for bug 31415957', | |||||
'date': "2000-01-17T11:23:54+00:00", | |||||
'committer_date': "2000-01-17T11:23:54+00:00", | |||||
'synthetic': False, | |||||
'type': 'git', | |||||
'parents': [], | |||||
'metadata': [], | |||||
}]) | |||||
mock_backend.revision_log_by.assert_called_with( | mock_backend.revision_log_by.assert_called_with( | ||||
1, 'refs/heads/master', None) | 1, 'refs/heads/master', None) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_log_by_nolog(self, mock_backend): | def lookup_revision_log_by_nolog(self, mock_backend): | ||||
# given | # given | ||||
Show All 16 Lines | def lookup_content_raw_not_found(self, mock_backend): | ||||
# when | # when | ||||
actual_content = service.lookup_content_raw( | actual_content = service.lookup_content_raw( | ||||
'sha1:18d8be353ed3480476f032475e7c233eff7371d5') | 'sha1:18d8be353ed3480476f032475e7c233eff7371d5') | ||||
# then | # then | ||||
self.assertIsNone(actual_content) | self.assertIsNone(actual_content) | ||||
mock_backend.content_find.assert_called_with( | mock_backend.content_find.assert_called_with( | ||||
'sha1', hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | 'sha1', hex_to_hash(self.SHA1_SAMPLE)) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_content_raw(self, mock_backend): | def lookup_content_raw(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.content_find = MagicMock(return_value={ | mock_backend.content_find = MagicMock(return_value={ | ||||
'sha1': '18d8be353ed3480476f032475e7c233eff7371d5', | 'sha1': self.SHA1_SAMPLE, | ||||
}) | }) | ||||
mock_backend.content_get = MagicMock(return_value={ | mock_backend.content_get = MagicMock(return_value={ | ||||
'data': b'binary data'}) | 'data': b'binary data'}) | ||||
# when | # when | ||||
actual_content = service.lookup_content_raw( | actual_content = service.lookup_content_raw( | ||||
'sha256:39007420ca5de7cb3cfc15196335507e' | 'sha256:%s' % self.SHA256_SAMPLE) | ||||
'e76c98930e7e0afa4d2747d3bf96c926') | |||||
# then | # then | ||||
self.assertEquals(actual_content, {'data': b'binary data'}) | self.assertEquals(actual_content, {'data': b'binary data'}) | ||||
mock_backend.content_find.assert_called_once_with( | mock_backend.content_find.assert_called_once_with( | ||||
'sha256', hex_to_hash('39007420ca5de7cb3cfc15196335507e' | 'sha256', self.SHA256_SAMPLE_BIN) | ||||
'e76c98930e7e0afa4d2747d3bf96c926')) | |||||
mock_backend.content_get.assert_called_once_with( | mock_backend.content_get.assert_called_once_with( | ||||
'18d8be353ed3480476f032475e7c233eff7371d5') | self.SHA1_SAMPLE) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_content_not_found(self, mock_backend): | def lookup_content_not_found(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.content_find = MagicMock(return_value=None) | mock_backend.content_find = MagicMock(return_value=None) | ||||
# when | # when | ||||
actual_content = service.lookup_content( | actual_content = service.lookup_content( | ||||
'sha1:18d8be353ed3480476f032475e7c233eff7371d5') | 'sha1:%s' % self.SHA1_SAMPLE) | ||||
# then | # then | ||||
self.assertIsNone(actual_content) | self.assertIsNone(actual_content) | ||||
mock_backend.content_find.assert_called_with( | mock_backend.content_find.assert_called_with( | ||||
'sha1', hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | 'sha1', self.SHA1_SAMPLE_BIN) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_content_with_sha1(self, mock_backend): | def lookup_content_with_sha1(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.content_find = MagicMock(return_value={ | mock_backend.content_find = MagicMock( | ||||
'sha1': hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5'), | return_value=self.SAMPLE_CONTENT_RAW) | ||||
'sha256': hex_to_hash('39007420ca5de7cb3cfc15196335507e' | |||||
'e76c98930e7e0afa4d2747d3bf96c926'), | |||||
'sha1_git': hex_to_hash('40e71b8614fcd89ccd17ca2b1d9e66' | |||||
'c5b00a6d03'), | |||||
'length': 190, | |||||
'status': 'hidden', | |||||
}) | |||||
# when | # when | ||||
actual_content = service.lookup_content( | actual_content = service.lookup_content( | ||||
'sha1:18d8be353ed3480476f032475e7c233eff7371d5') | 'sha1:%s' % self.SHA1_SAMPLE) | ||||
# then | # then | ||||
self.assertEqual(actual_content, { | self.assertEqual(actual_content, self.SAMPLE_CONTENT) | ||||
'sha1': '18d8be353ed3480476f032475e7c233eff7371d5', | |||||
'sha256': '39007420ca5de7cb3cfc15196335507ee76c98930e7e0afa4d274' | |||||
'7d3bf96c926', | |||||
'sha1_git': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', | |||||
'length': 190, | |||||
'status': 'absent', | |||||
}) | |||||
mock_backend.content_find.assert_called_with( | mock_backend.content_find.assert_called_with( | ||||
'sha1', hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5')) | 'sha1', hex_to_hash(self.SHA1_SAMPLE)) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_content_with_sha256(self, mock_backend): | def lookup_content_with_sha256(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.content_find = MagicMock(return_value={ | stub_content = self.SAMPLE_CONTENT_RAW | ||||
'sha1': hex_to_hash('18d8be353ed3480476f032475e7c233eff7371d5'), | stub_content['status'] = 'visible' | ||||
'sha256': hex_to_hash('39007420ca5de7cb3cfc15196335507e' | |||||
'e76c98930e7e0afa4d2747d3bf96c926'), | expected_content = self.SAMPLE_CONTENT | ||||
'sha1_git': hex_to_hash('40e71b8614fcd89ccd17ca2b1d9e66' | expected_content['status'] = 'visible' | ||||
'c5b00a6d03'), | mock_backend.content_find = MagicMock( | ||||
'length': 360, | return_value=stub_content) | ||||
'status': 'visible', | |||||
}) | |||||
# when | # when | ||||
actual_content = service.lookup_content( | actual_content = service.lookup_content( | ||||
'sha256:39007420ca5de7cb3cfc15196335507e' | 'sha256:%s' % self.SHA256_SAMPLE) | ||||
'e76c98930e7e0afa4d2747d3bf96c926') | |||||
# then | # then | ||||
self.assertEqual(actual_content, { | self.assertEqual(actual_content, expected_content) | ||||
'sha1': '18d8be353ed3480476f032475e7c233eff7371d5', | |||||
'sha256': '39007420ca5de7cb3cfc15196335507ee76c98930e7e0afa4d274' | |||||
'7d3bf96c926', | |||||
'sha1_git': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', | |||||
'length': 360, | |||||
'status': 'visible', | |||||
}) | |||||
mock_backend.content_find.assert_called_with( | mock_backend.content_find.assert_called_with( | ||||
'sha256', hex_to_hash('39007420ca5de7cb3cfc15196335507e' | 'sha256', self.SHA256_SAMPLE_BIN) | ||||
'e76c98930e7e0afa4d2747d3bf96c926')) | |||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_person(self, mock_backend): | def lookup_person(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.person_get = MagicMock(return_value={ | mock_backend.person_get = MagicMock(return_value={ | ||||
'id': 'person_id', | 'id': 'person_id', | ||||
'name': b'some_name', | 'name': b'some_name', | ||||
▲ Show 20 Lines • Show All 54 Lines • ▼ Show 20 Lines | def lookup_directory(self, mock_query, mock_backend): | ||||
'sha1', | 'sha1', | ||||
'directory-sha1-bin') | 'directory-sha1-bin') | ||||
# something that exists is all that matters here | # something that exists is all that matters here | ||||
mock_backend.directory_get.return_value = {'id': b'directory-sha1-bin'} | mock_backend.directory_get.return_value = {'id': b'directory-sha1-bin'} | ||||
# given | # given | ||||
stub_dir_entries = [{ | stub_dir_entries = [{ | ||||
'sha1': hex_to_hash('5c6f0e2750f48fa0bd0c4cf5976ba0b9e0' | 'sha1': self.SHA1_SAMPLE_BIN, | ||||
'2ebda5'), | 'sha256': self.SHA256_SAMPLE_BIN, | ||||
'sha256': hex_to_hash('39007420ca5de7cb3cfc15196335507e' | 'sha1_git': self.SHA1GIT_SAMPLE_BIN, | ||||
'e76c98930e7e0afa4d2747d3bf96c926'), | |||||
'sha1_git': hex_to_hash('40e71b8614fcd89ccd17ca2b1d9e66' | |||||
'c5b00a6d03'), | |||||
'target': hex_to_hash('40e71b8614fcd89ccd17ca2b1d9e66' | 'target': hex_to_hash('40e71b8614fcd89ccd17ca2b1d9e66' | ||||
'c5b00a6d03'), | 'c5b00a6d03'), | ||||
'dir_id': hex_to_hash('40e71b8614fcd89ccd17ca2b1d9e66' | 'dir_id': self.DIRECTORY_ID_BIN, | ||||
'c5b00a6d03'), | |||||
'name': b'bob', | 'name': b'bob', | ||||
'type': 10, | 'type': 10, | ||||
}] | }] | ||||
expected_dir_entries = [{ | expected_dir_entries = [{ | ||||
'sha1': '5c6f0e2750f48fa0bd0c4cf5976ba0b9e02ebda5', | 'sha1': self.SHA1_SAMPLE, | ||||
'sha256': '39007420ca5de7cb3cfc15196335507ee76c98930e7e0afa4d2747' | 'sha256': self.SHA256_SAMPLE, | ||||
'd3bf96c926', | 'sha1_git': self.SHA1GIT_SAMPLE, | ||||
'sha1_git': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', | |||||
'target': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', | 'target': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', | ||||
'dir_id': '40e71b8614fcd89ccd17ca2b1d9e66c5b00a6d03', | 'dir_id': self.DIRECTORY_ID, | ||||
'name': 'bob', | 'name': 'bob', | ||||
'type': 10, | 'type': 10, | ||||
}] | }] | ||||
mock_backend.directory_ls.return_value = stub_dir_entries | mock_backend.directory_ls.return_value = stub_dir_entries | ||||
# when | # when | ||||
actual_directory_ls = list(service.lookup_directory( | actual_directory_ls = list(service.lookup_directory( | ||||
Show All 20 Lines | def lookup_revision_by_nothing_found(self, mock_backend): | ||||
self.assertIsNone(actual_revisions) | self.assertIsNone(actual_revisions) | ||||
mock_backend.revision_get_by(1, 'master', None) | mock_backend.revision_get_by(1, 'master', None) | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_by(self, mock_backend): | def lookup_revision_by(self, mock_backend): | ||||
# given | # given | ||||
stub_rev = { | stub_rev = self.SAMPLE_REVISION_RAW | ||||
'id': hex_to_hash('28d8be353ed3480476f032475e7c233eff7371d5'), | |||||
'directory': hex_to_hash( | |||||
'7834ef7e7c357ce2af928115c6c6a42b7e2a44e6'), | |||||
'author': { | |||||
'name': b'ynot', | |||||
'email': b'ynot@blah.org', | |||||
}, | |||||
'committer': { | |||||
'name': b'ynot', | |||||
'email': b'ynot@blah.org', | |||||
}, | |||||
'message': b'elegant solution 31415', | |||||
'date': { | |||||
'timestamp': datetime.datetime( | |||||
2016, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc).timestamp(), | |||||
'offset': 420, | |||||
'negative_utc': None, | |||||
}, | |||||
'committer_date': { | |||||
'timestamp': datetime.datetime( | |||||
2016, 1, 17, 11, 23, 54, | |||||
tzinfo=datetime.timezone.utc).timestamp(), | |||||
'offset': 420, | |||||
'negative_utc': None, | |||||
}, | |||||
} | |||||
expected_rev = { | expected_rev = self.SAMPLE_REVISION | ||||
'id': '28d8be353ed3480476f032475e7c233eff7371d5', | |||||
'directory': '7834ef7e7c357ce2af928115c6c6a42b7e2a44e6', | mock_backend.revision_get_by.return_value = stub_rev | ||||
'author': { | |||||
'name': 'ynot', | # when | ||||
'email': 'ynot@blah.org', | actual_revision = service.lookup_revision_by(10, 'master2', 'some-ts') | ||||
}, | |||||
'committer': { | # then | ||||
'name': 'ynot', | self.assertEquals(actual_revision, expected_rev) | ||||
'email': 'ynot@blah.org', | |||||
}, | mock_backend.revision_get_by(1, 'master2', 'some-ts') | ||||
'message': 'elegant solution 31415', | |||||
'date': '2016-01-17T18:23:54+07:00', | @patch('swh.web.ui.service.backend') | ||||
'committer_date': '2016-01-17T18:23:54+07:00', | @istest | ||||
} | def lookup_revision_by_nomerge(self, mock_backend): | ||||
# given | |||||
stub_rev = self.SAMPLE_REVISION_RAW | |||||
stub_rev['parents'] = [ | |||||
hex_to_hash('adc83b19e793491b1c6ea0fd8b46cd9f32e592fc')] | |||||
expected_rev = self.SAMPLE_REVISION | |||||
expected_rev['parents'] = ['adc83b19e793491b1c6ea0fd8b46cd9f32e592fc'] | |||||
mock_backend.revision_get_by.return_value = stub_rev | |||||
# when | |||||
actual_revision = service.lookup_revision_by(10, 'master2', 'some-ts') | |||||
# then | |||||
self.assertEquals(actual_revision, expected_rev) | |||||
mock_backend.revision_get_by(1, 'master2', 'some-ts') | |||||
@patch('swh.web.ui.service.backend') | |||||
@istest | |||||
def lookup_revision_by_merge(self, mock_backend): | |||||
# given | |||||
stub_rev = self.SAMPLE_REVISION_RAW | |||||
stub_rev['parents'] = [ | |||||
hex_to_hash('adc83b19e793491b1c6ea0fd8b46cd9f32e592fc'), | |||||
hex_to_hash('ffff3b19e793491b1c6db0fd8b46cd9f32e592fc') | |||||
] | |||||
expected_rev = self.SAMPLE_REVISION | |||||
expected_rev['parents'] = [ | |||||
'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc', | |||||
'ffff3b19e793491b1c6db0fd8b46cd9f32e592fc' | |||||
] | |||||
expected_rev['merge'] = True | |||||
mock_backend.revision_get_by.return_value = stub_rev | mock_backend.revision_get_by.return_value = stub_rev | ||||
# when | # when | ||||
actual_revision = service.lookup_revision_by(10, 'master2', 'some-ts') | actual_revision = service.lookup_revision_by(10, 'master2', 'some-ts') | ||||
# then | # then | ||||
self.assertEquals(actual_revision, expected_rev) | self.assertEquals(actual_revision, expected_rev) | ||||
mock_backend.revision_get_by(1, 'master2', 'some-ts') | mock_backend.revision_get_by(1, 'master2', 'some-ts') | ||||
@patch('swh.web.ui.service.backend') | @patch('swh.web.ui.service.backend') | ||||
@istest | @istest | ||||
def lookup_revision_with_context_by_ko(self, mock_backend): | def lookup_revision_with_context_by_ko(self, mock_backend): | ||||
# given | # given | ||||
mock_backend.revision_get_by.return_value = None | mock_backend.revision_get_by.return_value = None | ||||
# when | # when | ||||
with self.assertRaises(NotFoundExc) as cm: | with self.assertRaises(NotFoundExc) as cm: | ||||
origin_id = 1 | origin_id = 1 | ||||
branch_name = 'master3' | branch_name = 'master3' | ||||
ts = None | ts = None | ||||
service.lookup_revision_with_context_by(origin_id, branch_name, ts, | service.lookup_revision_with_context_by(origin_id, branch_name, ts, | ||||
'sha1') | 'sha1') | ||||
Not Done Inline Actionsother stuff to be factored out zack: other stuff to be factored out | |||||
# then | # then | ||||
self.assertIn( | self.assertIn( | ||||
'Revision with (origin_id: %s, branch_name: %s' | 'Revision with (origin_id: %s, branch_name: %s' | ||||
', ts: %s) not found.' % (origin_id, | ', ts: %s) not found.' % (origin_id, | ||||
branch_name, | branch_name, | ||||
ts), cm.exception.args[0]) | ts), cm.exception.args[0]) | ||||
mock_backend.revision_get_by.assert_called_once_with( | mock_backend.revision_get_by.assert_called_once_with( | ||||
▲ Show 20 Lines • Show All 193 Lines • Show Last 20 Lines |
OK, almost there! The constants you added are great and reduce duplication a lot.
… but now what is duplicated is the above records, with all its 7 fields, which appears in several places :-)
I get there are two versions of it: one with _BIN/_RAW, the other with strings, but they are just 2 which is better than 10.
So please add two additional constants, e.g.: REVISION_RAW, and REVISION, defined using the other more fine grained constants, and in places like this just go with: