From 4056352036e459e549692a13044058d0e3e28915 Mon Sep 17 00:00:00 2001 From: Clark Boylan Date: Fri, 15 Feb 2019 09:16:30 -0800 Subject: [PATCH] Test GithubShaCache This adds a small test class for the GithubShaCache. Change-Id: I5d6763e10bf1f8228af038fcbfa7c2ad65778508 --- tests/unit/test_github_driver.py | 87 +++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_github_driver.py b/tests/unit/test_github_driver.py index 77f53a6eb2..7e5db6bbec 100644 --- a/tests/unit/test_github_driver.py +++ b/tests/unit/test_github_driver.py @@ -23,9 +23,10 @@ from unittest import mock, skip import git import github3.exceptions +from zuul.driver.github.githubconnection import GithubShaCache import zuul.rpcclient -from tests.base import ZuulTestCase, simple_layout, random_sha1 +from tests.base import BaseTestCase, ZuulTestCase, simple_layout, random_sha1 from tests.base import ZuulWebFixture @@ -1308,3 +1309,87 @@ class TestGithubWebhook(ZuulTestCase): self.waitUntilSettled() self.assertEqual(2, len(self.history)) + + +class TestGithubShaCache(BaseTestCase): + + def testInsert(self): + cache = GithubShaCache() + pr_dict = { + 'head': { + 'sha': '123456', + }, + 'number': 1, + 'state': 'open', + } + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('foo/bar', '123456'), set({1})) + + def testRemoval(self): + cache = GithubShaCache() + pr_dict = { + 'head': { + 'sha': '123456', + }, + 'number': 1, + 'state': 'open', + } + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('foo/bar', '123456'), set({1})) + pr_dict['state'] = 'closed' + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('foo/bar', '123456'), set()) + + def testMultiInsert(self): + cache = GithubShaCache() + pr_dict = { + 'head': { + 'sha': '123456', + }, + 'number': 1, + 'state': 'open', + } + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('foo/bar', '123456'), set({1})) + pr_dict['number'] = 2 + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('foo/bar', '123456'), set({1, 2})) + + def testMultiProjectInsert(self): + cache = GithubShaCache() + pr_dict = { + 'head': { + 'sha': '123456', + }, + 'number': 1, + 'state': 'open', + } + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('foo/bar', '123456'), set({1})) + cache.update('foo/baz', pr_dict) + self.assertEqual(cache.get('foo/baz', '123456'), set({1})) + + def testNoMatch(self): + cache = GithubShaCache() + pr_dict = { + 'head': { + 'sha': '123456', + }, + 'number': 1, + 'state': 'open', + } + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('bar/foo', '789'), set()) + self.assertEqual(cache.get('foo/bar', '789'), set()) + + def testNoUpdate(self): + cache = GithubShaCache() + pr_dict = { + 'head': { + 'sha': '123456', + }, + 'number': 1, + 'state': 'closed', + } + cache.update('foo/bar', pr_dict) + self.assertEqual(cache.get('bar/foo', '123456'), set())