Skip to content

Commit

Permalink
Write some tests for the uplift.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
jhford committed Feb 25, 2014
1 parent 2cf1ef3 commit efbc682
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 19 deletions.
5 changes: 2 additions & 3 deletions gaia_uplift/uplift.py
Expand Up @@ -53,8 +53,7 @@ def order_commits(repo_dir, requirements):
return git.sort_commits(repo_dir, commits, "master")


def uplift_bug(repo_dir, bug_id, commit, to_branches, from_branch="master"):
"""Uplift bug_id from branch to to_branches. Return successful branches"""
def uplift_commit(repo_dir, commit, to_branches, from_branch="master"):
uplift_info = {'success': {},
'failure': []}
for branch in to_branches:
Expand Down Expand Up @@ -101,7 +100,7 @@ def uplift(repo_dir, gaia_url, requirements):
print "\n", "="*80
print "Attempting to uplift %s commit to %s" % (commit, util.e_join(needed_on))
uplift[commit]['needed_on'] = needed_on
result = uplift_bug(repo_dir, bug_id, commit, needed_on)
result = uplift_commit(repo_dir, commit, needed_on)
print "Sucess on %s" % util.e_join(result['success'].keys())
print "Failure on %s" % util.e_join(result['failure'])
uplift[commit]['uplift_status'] = result
Expand Down
153 changes: 137 additions & 16 deletions tests/uplift_tests.py
@@ -1,8 +1,9 @@
import unittest
import os
import json
import mock
from mock import patch

import gaia_uplift.git as git
import gaia_uplift.bzapi as bzapi
import gaia_uplift.uplift as subject

Expand Down Expand Up @@ -44,31 +45,151 @@ def test_is_skippable_skipable(self):
class FindBugsTest(unittest.TestCase):

def test_find_bugs(self):
with mock.patch('gaia_uplift.bzapi.search') as mock_search, \
mock.patch('gaia_uplift.bzapi.parse_bugzilla_query') as mock_parse:
with patch('gaia_uplift.bzapi.search') as search, \
patch('gaia_uplift.bzapi.parse_bugzilla_query') as parse:

mock_search.return_value = ['123456']
mock_parse.return_value = ['my_butt']
search.return_value = ['123456']
parse.return_value = ['my_butt']

actual = subject.find_bugs(['snow_storm'])
self.assertEqual(['123456'], actual)

mock_parse.assert_called_once_with('snow_storm')
mock_search.assert_called_once_with('my_butt')
parse.assert_called_once_with('snow_storm')
search.assert_called_once_with('my_butt')

def test_find_bugs_only_skipable(self):
with mock.patch('gaia_uplift.bzapi.search') as mock_search, \
mock.patch('gaia_uplift.bzapi.parse_bugzilla_query') as mock_parse, \
mock.patch('gaia_uplift.uplift.is_skipable') as mock_is_skipable:
with patch('gaia_uplift.bzapi.search') as search, \
patch('gaia_uplift.bzapi.parse_bugzilla_query') as parse, \
patch('gaia_uplift.uplift.is_skipable') as is_skipable:

mock_search.return_value = ['123456']
mock_parse.return_value = ['my_butt']
mock_is_skipable.return_value = True
search.return_value = ['123456']
parse.return_value = ['my_butt']
is_skipable.return_value = True

actual = subject.find_bugs(['snow_storm'])
self.assertEqual([], actual)

mock_parse.assert_called_once_with('snow_storm')
mock_is_skipable.assert_called_once_with('123456')
mock_search.assert_called_once_with('my_butt')
parse.assert_called_once_with('snow_storm')
is_skipable.assert_called_once_with('123456')
search.assert_called_once_with('my_butt')

class OrderCommits(unittest.TestCase):
def test_order_commits(self):
with patch('gaia_uplift.git.sort_commits') as sort:
expected = range(5)
requirements = dict([(x, {'commits': [x]}) for x in expected])
sort.return_value = expected
actual = subject.order_commits(None, requirements)
self.assertEqual(expected, actual)
sort.assert_called_once_with(None, expected, 'master')

def test_order_commits_no_commits(self):
with patch('gaia_uplift.git.sort_commits') as sort:
iterations = range(5)
expected = [x for x in iterations if x % 2 == 0]
requirements = dict([(x, {'commits': [x]}) if x % 2 == 0 else (x, {}) for x in iterations])
sort.return_value = expected
actual = subject.order_commits(None, requirements)
self.assertEqual(expected, actual)
sort.assert_called_once_with(None, expected, 'master')

class UpliftCommit(unittest.TestCase):
def test_success_two_branches(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
branches = ['v1', 'v2']

cherry_pick.side_effect = ["%ssha" % x for x in branches]

actual = subject.uplift_commit(None, 'commit', branches)
expected = {
'success': {'v1': 'v1sha', 'v2': 'v2sha'},
'failure': []
}
self.assertEqual(expected, actual)

def test_one_fail_one_pass(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
branches = ['v1', 'v2']

cherry_pick.side_effect = ['v1sha', git.GitError]

actual = subject.uplift_commit(None, 'commit', branches)
expected = {
'success': {'v1': 'v1sha'},
'failure': ['v2']
}
self.assertEqual(expected, actual)

def test_failure_two_branches(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
branches = ['v1', 'v2']

cherry_pick.side_effect = [git.GitError, git.GitError]

actual = subject.uplift_commit(None, 'commit', branches)
expected = {
'success': {},
'failure': ['v1', 'v2']
}
self.assertEqual(expected, actual)

def test_noop(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
commit = 'abc123'
cherry_pick.return_value = commit
actual = subject.uplift_commit(None, commit, ['v1'])
expected = {
'success': {'v1': commit},
'failure': []
}
self.assertEqual(expected, actual)

class Push(unittest.TestCase):
def test_success(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:
ask_yn.return_value = True
expected = {'url': None,
'branches': { 'master': ("a"*40, "b"*40) }
}

push.return_value = expected
actual = subject.push(None)
self.assertEqual(expected, actual)

def test_no_push(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:

preview = {'url': None,
'branches': { 'master': ("a"*40, "b"*40) }
}
push.return_value = preview
ask_yn.return_value = False
self.assertEqual(None, subject.push(None))

def test_abject_failure(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:

preview = {'url': None,
'branches': {'master': ("a"*40, "b"*40) }
}
ask_yn.return_value = False
push.side_effect = git.PushFailure
with self.assertRaises(git.PushFailure):
subject.push(None)

def test_recovery(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:
ask_yn.return_value = True
expected = {'url': None,
'branches': { 'master': ("a"*40, "b"*40) }
}

push.side_effect = [expected, git.PushFailure, expected]
actual = subject.push(None)
self.assertEqual(expected, actual)


0 comments on commit efbc682

Please sign in to comment.