summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAndy Doan <andy.doan@linaro.org>2015-07-09 14:38:52 -0500
committerAndy Doan <andy.doan@linaro.org>2015-07-15 14:21:04 -0500
commit8886e0e161b42c991c77f83d59ad6d09ff100527 (patch)
treebb3927ed8463349887db033e0c1ebfc4ae8f45fe /tests
parentf97c8946afd77bc635686725e3cacb0bd2029d82 (diff)
add unit test for update_commited_patches
More testing framework to help make the hard parts of our code easier to test out locally. Change-Id: Ia1f504c948d4801d60f14b83b2883f4d198d116d
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py39
-rw-r--r--tests/test_update_commited_patches.py73
2 files changed, 112 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index e69de29..66efdfe 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -0,0 +1,39 @@
+import os
+import subprocess
+
+
+class TestRepo(object):
+ '''Simple class to make it easy to create/manipulate test repos'''
+
+ @staticmethod
+ def create(path):
+ os.mkdir(path)
+ subprocess.check_call(['git', 'init'], cwd=path)
+ return TestRepo(path)
+
+ @staticmethod
+ def clone(repo_url, path):
+ subprocess.check_call(['git', 'clone', repo_url, path])
+ return TestRepo(path)
+
+ def __init__(self, path):
+ self.path = path
+
+ def add_commit(self, fname, content, message):
+ with open(os.path.join(self.path, fname), 'w') as f:
+ f.write(content)
+ subprocess.check_call(['git', 'add', '.'], cwd=self.path)
+ subprocess.check_call(
+ ['git', 'commit', '-a', '-m', message], cwd=self.path)
+ return self.last_commit()
+
+ def last_commit(self):
+ out = subprocess.check_output(
+ ['git', 'log', '--format=oneline', '-1'], cwd=self.path)
+ return out.split(' ')[0]
+
+ def create_patch(self, commit):
+ patch = subprocess.check_output(
+ ['git', 'format-patch', '%s^..%s' % (commit, commit)],
+ cwd=self.path)
+ return os.path.join(self.path, patch.strip())
diff --git a/tests/test_update_commited_patches.py b/tests/test_update_commited_patches.py
new file mode 100644
index 0000000..726d565
--- /dev/null
+++ b/tests/test_update_commited_patches.py
@@ -0,0 +1,73 @@
+import os
+import shutil
+import tempfile
+
+import import_emails
+import update_commited_patches
+
+from django.test import TestCase
+from patchwork.models import Patch, Project, State
+from tests import TestRepo
+
+
+class TestUpdateCommitedPatches(TestCase):
+ '''This class does a bunch of mocking and setup, but the code it tests is
+ is complex enough to make this quite useful for sanity checking'''
+ fixtures = ['default_states']
+
+ def setUp(self):
+ self.tmpdir = tempfile.mkdtemp()
+ self.addCleanup(shutil.rmtree, self.tmpdir)
+ path = os.path.join(self.tmpdir, 'upstream')
+ self.upstream_repo = TestRepo.create(path)
+ self.upstream_repo.add_commit('file.txt', 'linel', 'commit 1')
+ self.upstream_repo.add_commit('file.txt', 'line1', 'commit 2')
+
+ path = os.path.join(self.tmpdir, 'patchwork_copy')
+ self.local_repo = TestRepo.clone(self.upstream_repo.path, path)
+
+ self.project = Project.objects.create(
+ listid='lng-odp.lists.linaro.org',
+ linkname='patchwork_copy')
+ self.addCleanup(self.project.delete)
+
+ commit = self.upstream_repo.add_commit(
+ 'file.txt', 'line1\nline2', 'testing update_commited_patches')
+ self.patchmail = self.upstream_repo.create_patch(commit)
+
+ def test_update_project(self):
+ '''Ensure an exact patch match gets marked accepted'''
+ # create a patch for our project
+ with open(self.patchmail) as f:
+ mail = 'Message-Id: 1234\nList-Id: %s\n' % self.project.listid
+ mail += f.read()
+ import_emails.process_message(mail)
+ patches = Patch.objects.all()
+ self.assertEqual(1, patches.count())
+ self.assertEqual(State.objects.get(name='New'), patches[0].state)
+
+ update_commited_patches._update_project(
+ self.tmpdir, self.project, [], False)
+
+ patches = Patch.objects.all()
+ self.assertEqual(1, patches.count())
+ self.assertEqual(State.objects.get(name='Accepted'), patches[0].state)
+
+ def test_update_project_fuzzy(self):
+ '''Ensure a patch that's a close match is marked accepted'''
+ # create a patch for our project
+ with open(self.patchmail) as f:
+ mail = 'Message-Id: 1234\nList-Id: %s\n' % self.project.listid
+ mail += f.read()
+ mail = mail.replace('+line2', '+Line2##') # make this an inexact patch
+ import_emails.process_message(mail)
+ patches = Patch.objects.all()
+ self.assertEqual(1, patches.count())
+ self.assertEqual(State.objects.get(name='New'), patches[0].state)
+
+ update_commited_patches._update_project(
+ self.tmpdir, self.project, [], False)
+
+ patches = Patch.objects.all()
+ self.assertEqual(1, patches.count())
+ self.assertEqual(State.objects.get(name='Accepted'), patches[0].state)