gerrit_client_test.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env vpython3
  2. # coding=utf-8
  3. # Copyright 2020 The Chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """Unit tests for gerrit_client.py."""
  7. import logging
  8. import os
  9. import sys
  10. import unittest
  11. from unittest import mock
  12. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  13. import gerrit_client
  14. class TestGerritClient(unittest.TestCase):
  15. @mock.patch('gerrit_util.GetGerritBranch', return_value='')
  16. def test_branch_info(self, util_mock):
  17. gerrit_client.main([
  18. 'branchinfo', '--host', 'https://example.org/foo', '--project',
  19. 'projectname', '--branch', 'branchname'
  20. ])
  21. util_mock.assert_called_once_with('example.org', 'projectname',
  22. 'branchname')
  23. @mock.patch('gerrit_util.CreateGerritBranch', return_value='')
  24. def test_branch(self, util_mock):
  25. gerrit_client.main([
  26. 'branch', '--host', 'https://example.org/foo', '--project',
  27. 'projectname', '--branch', 'branchname', '--commit', 'commitname'
  28. ])
  29. util_mock.assert_called_once_with('example.org', 'projectname',
  30. 'branchname', 'commitname')
  31. @mock.patch('gerrit_util.QueryChanges', return_value='')
  32. def test_changes(self, util_mock):
  33. gerrit_client.main([
  34. 'changes', '--host', 'https://example.org/foo', '-p', 'foo=bar',
  35. '-p', 'baz=qux', '--limit', '10', '--start', '20', '-o', 'op1',
  36. '-o', 'op2'
  37. ])
  38. util_mock.assert_called_once_with('example.org', [('foo', 'bar'),
  39. ('baz', 'qux')],
  40. first_param=None,
  41. limit=10,
  42. start=20,
  43. o_params=['op1', 'op2'])
  44. @mock.patch('gerrit_util.QueryChanges', return_value='')
  45. def test_changes_query(self, util_mock):
  46. gerrit_client.main([
  47. 'changes',
  48. '--host',
  49. 'https://example.org/foo',
  50. '--query',
  51. 'is:owner is:open',
  52. '--limit',
  53. '10',
  54. '--start',
  55. '20',
  56. ])
  57. util_mock.assert_called_once_with('example.org', [],
  58. first_param='is:owner is:open',
  59. limit=10,
  60. start=20,
  61. o_params=None)
  62. @mock.patch('gerrit_util.QueryChanges', return_value='')
  63. def test_changes_params_query(self, util_mock):
  64. gerrit_client.main([
  65. 'changes',
  66. '--host',
  67. 'https://example.org/foo',
  68. '--query',
  69. 'is:owner is:open',
  70. '-p',
  71. 'foo=bar',
  72. '--limit',
  73. '10',
  74. '--start',
  75. '20',
  76. ])
  77. util_mock.assert_called_once_with('example.org', [('foo', 'bar')],
  78. first_param='is:owner is:open',
  79. limit=10,
  80. start=20,
  81. o_params=None)
  82. @mock.patch('gerrit_util.GetRelatedChanges', return_value='')
  83. def test_relatedchanges(self, util_mock):
  84. gerrit_client.main([
  85. 'relatedchanges', '--host', 'https://example.org/foo', '--change',
  86. 'foo-change-id', '--revision', 'foo-revision-id'
  87. ])
  88. util_mock.assert_called_once_with('example.org',
  89. change='foo-change-id',
  90. revision='foo-revision-id')
  91. @mock.patch('gerrit_util.CreateChange', return_value={})
  92. def test_createchange(self, util_mock):
  93. gerrit_client.main([
  94. 'createchange', '--host', 'https://example.org/foo', '--project',
  95. 'project', '--branch', 'main', '--subject', 'subject', '-p',
  96. 'work_in_progress=true'
  97. ])
  98. util_mock.assert_called_once_with('example.org',
  99. 'project',
  100. branch='main',
  101. subject='subject',
  102. params=[('work_in_progress', 'true')])
  103. @mock.patch('builtins.open', mock.mock_open())
  104. @mock.patch('gerrit_util.ChangeEdit', return_value='')
  105. def test_changeedit(self, util_mock):
  106. open().read.return_value = 'test_data'
  107. gerrit_client.main([
  108. 'changeedit', '--host', 'https://example.org/foo', '--change', '1',
  109. '--path', 'path/to/file', '--file', '/my/foo'
  110. ])
  111. util_mock.assert_called_once_with('example.org', 1, 'path/to/file',
  112. 'test_data')
  113. @mock.patch('gerrit_util.PublishChangeEdit', return_value='')
  114. def test_publishchangeedit(self, util_mock):
  115. gerrit_client.main([
  116. 'publishchangeedit', '--host', 'https://example.org/foo',
  117. '--change', '1', '--notify', 'yes'
  118. ])
  119. util_mock.assert_called_once_with('example.org', 1, 'yes')
  120. @mock.patch('gerrit_util.AbandonChange', return_value='')
  121. def test_abandon(self, util_mock):
  122. gerrit_client.main([
  123. 'abandon', '--host', 'https://example.org/foo', '-c', '1', '-m',
  124. 'bar'
  125. ])
  126. util_mock.assert_called_once_with('example.org', 1, 'bar')
  127. @mock.patch('gerrit_util.SetReview', return_value='')
  128. def test_setlabel(self, util_mock):
  129. gerrit_client.main([
  130. 'setlabel',
  131. '--host',
  132. 'https://example.org/foo',
  133. '-c',
  134. '1',
  135. '-l',
  136. 'some-label',
  137. '-2',
  138. ])
  139. util_mock.assert_called_once_with('example.org',
  140. 1,
  141. labels={'some-label': '-2'})
  142. if __name__ == '__main__':
  143. logging.basicConfig(
  144. level=logging.DEBUG if '-v' in sys.argv else logging.ERROR)
  145. unittest.main()