Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Fully test cover auto merge pull requests
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 29, 2016
1 parent c3fe6c7 commit 3f444fb
Showing 1 changed file with 147 additions and 1 deletion.
148 changes: 147 additions & 1 deletion tests/test_auto_merge_pull_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@

def test_auto_merge_not_enabled(app, test_client):
app.config['AUTO_MERGE_ENABLED'] = False
payload = '{}'
payload = json.dumps({
'repository': {
'full_name': 'deepanalyzer/badwolf',
},
'pullrequest': {
'id': 1,
'title': 'PR 1',
'description': 'This is PR1',
},
})
with mock.patch.object(bitbucket.PullRequest, 'get') as pr_get:
test_client.post(
url_for('webhook.webhook_push'),
Expand All @@ -26,6 +35,30 @@ def test_auto_merge_not_enabled(app, test_client):
app.config['AUTO_MERGE_ENABLED'] = True


def test_auto_merge_failure_pr_get_error(app, test_client):
payload = json.dumps({
'repository': {
'full_name': 'deepanalyzer/badwolf',
},
'pullrequest': {
'id': 1,
'title': 'PR 1',
'description': 'This is PR1',
},
})
with mock.patch.object(bitbucket.PullRequest, 'get') as pr_get:
pr_get.side_effect = bitbucket.BitbucketAPIError(404, 'not found', 'PR not found')
test_client.post(
url_for('webhook.webhook_push'),
data=payload,
headers={
'X-Event-Key': 'pullrequest:approved',
}
)
with mock.patch.object(bitbucket.BuildStatus, 'get') as status_get:
status_get.assert_not_called()


def test_auto_merge_skip_title(test_client):
payload = json.dumps({
'repository': {
Expand Down Expand Up @@ -94,3 +127,116 @@ def test_auto_merge_skip_pr_not_in_open_state(test_client):
)
with mock.patch.object(bitbucket.BuildStatus, 'get') as status_get:
status_get.assert_not_called()


def test_auto_merge_skip_not_enough_approval(test_client):
payload = json.dumps({
'repository': {
'full_name': 'deepanalyzer/badwolf',
},
'pullrequest': {
'id': 1,
'title': 'PR 1',
'description': 'This is PR1',
},
})
with mock.patch.object(bitbucket.PullRequest, 'get') as pr_get:
pr_get.return_value = {
'state': 'OPEN',
'participants': [
{'approved': False},
{'approved': True},
{'approved': False},
]
}
test_client.post(
url_for('webhook.webhook_push'),
data=payload,
headers={
'X-Event-Key': 'pullrequest:approved',
}
)
with mock.patch.object(bitbucket.BuildStatus, 'get') as status_get:
status_get.assert_not_called()


def test_auto_merge_success(test_client):
payload = json.dumps({
'repository': {
'full_name': 'deepanalyzer/badwolf',
},
'pullrequest': {
'id': 1,
'title': 'PR 1',
'description': 'This is PR1',
},
})
with mock.patch.object(bitbucket.PullRequest, 'get') as pr_get, \
mock.patch.object(bitbucket.PullRequest, 'merge') as pr_merge, \
mock.patch.object(bitbucket.BuildStatus, 'get') as status_get:
pr_get.return_value = {
'state': 'OPEN',
'participants': [
{'approved': True},
{'approved': True},
{'approved': True},
],
'source': {
'repository': {'full_name': 'deepanalyzer/badwolf'},
'commit': {'hash': '0000000'},
},
}
status_get.return_value = {
'state': 'SUCCESSFUL',
}
pr_merge.return_value = None
test_client.post(
url_for('webhook.webhook_push'),
data=payload,
headers={
'X-Event-Key': 'pullrequest:approved',
}
)
assert status_get.called
assert pr_merge.called


def test_auto_merge_call_error(test_client):
payload = json.dumps({
'repository': {
'full_name': 'deepanalyzer/badwolf',
},
'pullrequest': {
'id': 1,
'title': 'PR 1',
'description': 'This is PR1',
},
})
with mock.patch.object(bitbucket.PullRequest, 'get') as pr_get, \
mock.patch.object(bitbucket.PullRequest, 'merge') as pr_merge, \
mock.patch.object(bitbucket.BuildStatus, 'get') as status_get:
pr_get.return_value = {
'state': 'OPEN',
'participants': [
{'approved': True},
{'approved': True},
{'approved': True},
],
'source': {
'repository': {'full_name': 'deepanalyzer/badwolf'},
'commit': {'hash': '0000000'},
},
}
status_get.return_value = {
'state': 'SUCCESSFUL',
}
pr_merge.side_effect = bitbucket.BitbucketAPIError(401, 'access denied', 'access denied')
test_client.post(
url_for('webhook.webhook_push'),
data=payload,
headers={
'X-Event-Key': 'pullrequest:approved',
}
)
assert status_get.called
assert pr_merge.called

0 comments on commit 3f444fb

Please sign in to comment.