Permalink
Browse files

100% test coverage.

  • Loading branch information...
1 parent e51e0c3 commit 6f5866dadb4aee43a7f8897ef34a9d04c29b9a9b @carljm carljm committed Jun 23, 2012
Showing with 99 additions and 3 deletions.
  1. +9 −3 bughub.py
  2. +90 −0 test_bughub.py
View
@@ -33,7 +33,7 @@
class IssueSource(object):
def get_all(self):
"""Yield all issues as standardized dictionaries."""
- raise NotImplemented
+ raise NotImplemented # pragma: no cover
@@ -173,7 +173,7 @@ def parse_source(definition):
-if __name__ == "__main__":
+def main(output):
parser = argparse.ArgumentParser(
description="Pull bugs from github/bugzilla and dump to CSV.")
@@ -210,8 +210,14 @@ def parse_source(definition):
]
- writer = csv.DictWriter(sys.stdout, fieldnames)
+ writer = csv.DictWriter(output, fieldnames)
writer.writerow(dict(zip(fieldnames, fieldnames)))
for issue in chain(*[source.get_all() for source in sources]):
writer.writerow(issue)
+
+
+
+if __name__ == "__main__":
+ main(sys.stdout) # pragma: no cover
+
View
@@ -2,9 +2,36 @@
Tests for bughub.
"""
+import json
+import sys
+from StringIO import StringIO
+
import bughub
+def test_main(monkeypatch):
+ monkeypatch.setattr(bughub, "urlopen", fake_urlopen)
+ argv = [
+ "prog",
+ "github:user:repo:state=open",
+ "bugzilla:state=NEW",
+ ]
+ monkeypatch.setattr(sys, "argv", argv)
+
+ stdout = StringIO()
+
+ bughub.main(stdout)
+
+ stdout.seek(0)
+ output = stdout.read()
+
+ assert output.replace("\r\n", "\n") == """source,id,url,assigned,status,title,product,module,patch
+github,123,https://github.com/user/repo/issues/123,,open,Fix the things,repo,repo,n
+github,123,https://github.com/user/repo/issues/123,,open,Fix the things,repo,repo,n
+bugzilla,123,https://bugzilla.mozilla.org/show_bug.cgi?id=123,foo,NEW,Fix the things,Core,Component,n
+"""
+
+
def test_parse_source_github():
s = bughub.parse_source("github:user:repo:foo=bar:foo=baz")
@@ -17,3 +44,66 @@ def test_parse_source_bugzilla():
s = bughub.parse_source("bugzilla:foo=bar:foo=baz")
assert s.filters == {"foo": ["bar", "baz"]}
+
+
+
+class FakeResponse(object):
+ def __init__(self, body, headers):
+ self.body = body
+ self.headers = headers
+
+
+ def read(self):
+ return self.body
+
+
+
+def fake_github_issue(**kwargs):
+ defaults = {
+ "number": 123,
+ "assignee": None,
+ "state": "open",
+ "title": "Fix the things",
+ "pull_request": {"html_url": None},
+ }
+ defaults.update(kwargs)
+
+ defaults.setdefault(
+ "html_url",
+ "https://github.com/user/repo/issues/{0}".format(defaults["number"]),
+ )
+
+ return defaults
+
+
+
+def fake_bugzilla_issue(**kwargs):
+ defaults = {
+ "id": 123,
+ "assigned_to": {"name": "foo"},
+ "status": "NEW",
+ "summary": "Fix the things",
+ "product": "Core",
+ "component": "Component",
+ }
+ defaults.update(kwargs)
+
+ return defaults
+
+
+def fake_urlopen(url):
+ if "github" in url:
+ body = json.dumps([fake_github_issue()])
+ if "page=2" in url:
+ headers = {
+ "Link": 'Link: <https://github.com/user/repo/issues?page=1>; rel="first"'
+ }
+ else:
+ headers = {
+ "Link": 'Link: <https://github.com/user/repo/issues?page=2&state=open>; rel="next", <https://github.com/user/repo/issues?page=2&state=open>; rel="last"'
+ }
+ return FakeResponse(body, headers)
+ elif "bugzilla" in url:
+ return FakeResponse(json.dumps({"bugs": [fake_bugzilla_issue()]}), {})
+ else:
+ raise NotImplementedError

0 comments on commit 6f5866d

Please sign in to comment.