Files
tryton/ci/tests/test_upstream_pr.py
T

61 lines
2.3 KiB
Python

import importlib.util
import os
import pathlib
import unittest
from unittest.mock import ANY, patch
SCRIPT = pathlib.Path(__file__).parents[1] / 'upstream_pr.py'
class UpstreamPullRequestTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
environment = {
'GITEA_URL': 'https://gitea.example.test',
'CUSTOM_REPOSITORY': 'tryton-do/tryton',
'GITEA_TOKEN': 'test-token',
'INTEGRATION_BRANCH': 'integration/8.0',
'UPDATE_BRANCH': 'automation/upstream-8.0',
}
with patch.dict(os.environ, environment, clear=False):
spec = importlib.util.spec_from_file_location('upstream_pr', SCRIPT)
cls.module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(cls.module)
def test_reuses_existing_pull_request(self):
existing = [{
'head': {'ref': self.module.HEAD},
'base': {'ref': self.module.TARGET},
'html_url': 'https://gitea.example.test/pulls/1',
}]
with patch.object(self.module, 'open_pulls', return_value=existing), \
patch.object(self.module, 'request') as request:
self.module.ensure_pull_request('a' * 40)
request.assert_not_called()
def test_creates_pull_request_with_review_target(self):
created = {'html_url': 'https://gitea.example.test/pulls/2'}
with patch.object(self.module, 'open_pulls', return_value=[]), \
patch.object(self.module, 'request', return_value=created) as request:
self.module.ensure_pull_request('b' * 40)
request.assert_called_once_with(
'POST', '/repos/tryton-do/tryton/pulls', ANY)
payload = request.call_args.args[2]
self.assertEqual(payload['head'], 'automation/upstream-8.0')
self.assertEqual(payload['base'], 'integration/8.0')
def test_reuses_existing_conflict_issue(self):
existing = [{
'title': 'Upstream Tryton 8.0 synchronization conflict',
'html_url': 'https://gitea.example.test/issues/1',
}]
with patch.object(self.module, 'request', return_value=existing) as request:
self.module.ensure_conflict_issue('trytond/file.py')
request.assert_called_once()
if __name__ == '__main__':
unittest.main()