Refactor test_prepare_trigger to make it easier to add tests

This commit is contained in:
Joseph Schorr 2015-09-25 15:09:47 -04:00
parent 6673266328
commit 1bba472c14

View file

@ -9,10 +9,19 @@ from buildtrigger.githubhandler import get_transformed_webhook_payload as gh_web
from buildtrigger.gitlabhandler import get_transformed_webhook_payload as gl_webhook
class TestPrepareTrigger(unittest.TestCase):
def test_bitbucket_customer_payload_noauthor(self):
with open('test/triggerjson/bitbucket_customer_example_noauthor.json') as f:
def assertSchema(self, filename, expected, processor, *args, **kwargs):
with open('test/triggerjson/%s.json' % filename) as f:
payload = json.loads(f.read())
nargs = [payload]
nargs.extend(args)
created = processor(*nargs, **kwargs)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
def test_bitbucket_customer_payload_noauthor(self):
expected = {
"commit": "a0ec139843b2bb281ab21a433266ddc498e605dc",
"ref": "refs/heads/master",
@ -29,15 +38,10 @@ class TestPrepareTrigger(unittest.TestCase):
},
}
created = bb_webhook(payload)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('bitbucket_customer_example_noauthor', expected, bb_webhook)
def test_bitbucket_customer_payload_tag(self):
with open('test/triggerjson/bitbucket_customer_example_tag.json') as f:
payload = json.loads(f.read())
expected = {
"commit": "a0ec139843b2bb281ab21a433266ddc498e605dc",
"ref": "refs/tags/0.1.2",
@ -54,15 +58,10 @@ class TestPrepareTrigger(unittest.TestCase):
},
}
created = bb_webhook(payload)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('bitbucket_customer_example_tag', expected, bb_webhook)
def test_bitbucket_commit(self):
with open('test/triggerjson/bitbucket_commit.json') as f:
commit = json.loads(f.read())
ref = 'refs/heads/somebranch'
default_branch = 'somebranch'
repository_name = 'foo/bar'
@ -92,15 +91,10 @@ class TestPrepareTrigger(unittest.TestCase):
}
}
created = bb_commit(commit, ref, default_branch, repository_name, lookup_author)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('bitbucket_commit', expected, bb_commit, ref, default_branch,
repository_name, lookup_author)
def test_bitbucket_webhook_payload(self):
with open('test/triggerjson/bitbucket_webhook.json') as f:
payload = json.loads(f.read())
expected = {
"commit": u"af64ae7188685f8424040b4735ad12941b980d75",
"ref": u"refs/heads/master",
@ -122,15 +116,10 @@ class TestPrepareTrigger(unittest.TestCase):
},
}
created = bb_webhook(payload)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('bitbucket_webhook', expected, bb_webhook)
def test_github_webhook_payload(self):
with open('test/triggerjson/github_webhook.json') as f:
payload = json.loads(f.read())
expected = {
'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c',
'ref': u'refs/heads/master',
@ -148,15 +137,10 @@ class TestPrepareTrigger(unittest.TestCase):
},
}
created = gh_webhook(payload)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('github_webhook', expected, gh_webhook)
def test_github_webhook_payload_with_lookup(self):
with open('test/triggerjson/github_webhook.json') as f:
payload = json.loads(f.read())
expected = {
'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c',
'ref': u'refs/heads/master',
@ -184,15 +168,10 @@ class TestPrepareTrigger(unittest.TestCase):
'avatar_url': 'http://some/avatar/url'
}
created = gh_webhook(payload, lookup_user=lookup_user)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('github_webhook', expected, gh_webhook, lookup_user=lookup_user)
def test_gitlab_webhook_payload(self):
with open('test/triggerjson/gitlab_webhook.json') as f:
payload = json.loads(f.read())
expected = {
'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e',
'ref': u'refs/heads/master',
@ -204,15 +183,10 @@ class TestPrepareTrigger(unittest.TestCase):
},
}
created = gl_webhook(payload)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('gitlab_webhook', expected, gl_webhook)
def test_github_webhook_payload_missing_fields(self):
with open('test/triggerjson/github_webhook_missing.json') as f:
payload = json.loads(f.read())
expected = {
'commit': u'410f4cdf8ff09b87f245b13845e8497f90b90a4c',
'ref': u'refs/heads/master',
@ -224,15 +198,10 @@ class TestPrepareTrigger(unittest.TestCase):
},
}
created = gh_webhook(payload)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('github_webhook_missing', expected, gh_webhook)
def test_gitlab_webhook_payload_with_lookup(self):
with open('test/triggerjson/gitlab_webhook.json') as f:
payload = json.loads(f.read())
expected = {
'commit': u'fb88379ee45de28a0a4590fddcbd8eff8b36026e',
'ref': u'refs/heads/master',
@ -256,9 +225,8 @@ class TestPrepareTrigger(unittest.TestCase):
'avatar_url': 'http://some/avatar/url',
}
created = gl_webhook(payload, lookup_user=lookup_user)
self.assertEquals(expected, created)
validate(created, METADATA_SCHEMA)
self.assertSchema('gitlab_webhook', expected, gl_webhook, lookup_user=lookup_user)
if __name__ == '__main__':
unittest.main()