diff --git a/tests/pull_requests.json b/tests/pull_requests.json new file mode 100644 index 0000000..3768245 --- /dev/null +++ b/tests/pull_requests.json @@ -0,0 +1,370 @@ +[ + { + "url": "https://api.github.com/repos/simonw/datasette/pulls/571", + "id": 313384926, + "node_id": "MDExOlB1bGxSZXF1ZXN0MzEzMzg0OTI2", + "html_url": "https://github.com/simonw/datasette/pull/571", + "diff_url": "https://github.com/simonw/datasette/pull/571.diff", + "patch_url": "https://github.com/simonw/datasette/pull/571.patch", + "issue_url": "https://api.github.com/repos/simonw/datasette/issues/571", + "number": 571, + "state": "closed", + "locked": false, + "title": "detect_fts now works with alternative table escaping", + "user": { + "login": "simonw", + "id": 9599, + "node_id": "MDQ6VXNlcjk1OTk=", + "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/simonw", + "html_url": "https://github.com/simonw", + "followers_url": "https://api.github.com/users/simonw/followers", + "following_url": "https://api.github.com/users/simonw/following{/other_user}", + "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", + "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", + "organizations_url": "https://api.github.com/users/simonw/orgs", + "repos_url": "https://api.github.com/users/simonw/repos", + "events_url": "https://api.github.com/users/simonw/events{/privacy}", + "received_events_url": "https://api.github.com/users/simonw/received_events", + "type": "User", + "site_admin": false + }, + "body": "Fixes #570", + "created_at": "2019-09-03T00:23:39Z", + "updated_at": "2019-09-03T00:32:28Z", + "closed_at": "2019-09-03T00:32:28Z", + "merged_at": "2019-09-03T00:32:28Z", + "merge_commit_sha": "2dc5c8dc259a0606162673d394ba8cc1c6f54428", + "assignee": null, + "assignees": [ + + ], + "requested_reviewers": [ + + ], + "requested_teams": [ + + ], + "labels": [ + + ], + "milestone": null, + "draft": false, + "commits_url": "https://api.github.com/repos/simonw/datasette/pulls/571/commits", + "review_comments_url": "https://api.github.com/repos/simonw/datasette/pulls/571/comments", + "review_comment_url": "https://api.github.com/repos/simonw/datasette/pulls/comments{/number}", + "comments_url": "https://api.github.com/repos/simonw/datasette/issues/571/comments", + "statuses_url": "https://api.github.com/repos/simonw/datasette/statuses/a85239f69261c10f1a9f90514c8b5d113cb94585", + "head": { + "label": "simonw:detect-fts", + "ref": "detect-fts", + "sha": "a85239f69261c10f1a9f90514c8b5d113cb94585", + "user": { + "login": "simonw", + "id": 9599, + "node_id": "MDQ6VXNlcjk1OTk=", + "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/simonw", + "html_url": "https://github.com/simonw", + "followers_url": "https://api.github.com/users/simonw/followers", + "following_url": "https://api.github.com/users/simonw/following{/other_user}", + "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", + "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", + "organizations_url": "https://api.github.com/users/simonw/orgs", + "repos_url": "https://api.github.com/users/simonw/repos", + "events_url": "https://api.github.com/users/simonw/events{/privacy}", + "received_events_url": "https://api.github.com/users/simonw/received_events", + "type": "User", + "site_admin": false + }, + "repo": { + "id": 107914493, + "node_id": "MDEwOlJlcG9zaXRvcnkxMDc5MTQ0OTM=", + "name": "datasette", + "full_name": "simonw/datasette", + "private": false, + "owner": { + "login": "simonw", + "id": 9599, + "node_id": "MDQ6VXNlcjk1OTk=", + "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/simonw", + "html_url": "https://github.com/simonw", + "followers_url": "https://api.github.com/users/simonw/followers", + "following_url": "https://api.github.com/users/simonw/following{/other_user}", + "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", + "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", + "organizations_url": "https://api.github.com/users/simonw/orgs", + "repos_url": "https://api.github.com/users/simonw/repos", + "events_url": "https://api.github.com/users/simonw/events{/privacy}", + "received_events_url": "https://api.github.com/users/simonw/received_events", + "type": "User", + "site_admin": false + }, + "html_url": "https://github.com/simonw/datasette", + "description": "An open source multi-tool for exploring and publishing data", + "fork": false, + "url": "https://api.github.com/repos/simonw/datasette", + "forks_url": "https://api.github.com/repos/simonw/datasette/forks", + "keys_url": "https://api.github.com/repos/simonw/datasette/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/simonw/datasette/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/simonw/datasette/teams", + "hooks_url": "https://api.github.com/repos/simonw/datasette/hooks", + "issue_events_url": "https://api.github.com/repos/simonw/datasette/issues/events{/number}", + "events_url": "https://api.github.com/repos/simonw/datasette/events", + "assignees_url": "https://api.github.com/repos/simonw/datasette/assignees{/user}", + "branches_url": "https://api.github.com/repos/simonw/datasette/branches{/branch}", + "tags_url": "https://api.github.com/repos/simonw/datasette/tags", + "blobs_url": "https://api.github.com/repos/simonw/datasette/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/simonw/datasette/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/simonw/datasette/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/simonw/datasette/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/simonw/datasette/statuses/{sha}", + "languages_url": "https://api.github.com/repos/simonw/datasette/languages", + "stargazers_url": "https://api.github.com/repos/simonw/datasette/stargazers", + "contributors_url": "https://api.github.com/repos/simonw/datasette/contributors", + "subscribers_url": "https://api.github.com/repos/simonw/datasette/subscribers", + "subscription_url": "https://api.github.com/repos/simonw/datasette/subscription", + "commits_url": "https://api.github.com/repos/simonw/datasette/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/simonw/datasette/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/simonw/datasette/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/simonw/datasette/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/simonw/datasette/contents/{+path}", + "compare_url": "https://api.github.com/repos/simonw/datasette/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/simonw/datasette/merges", + "archive_url": "https://api.github.com/repos/simonw/datasette/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/simonw/datasette/downloads", + "issues_url": "https://api.github.com/repos/simonw/datasette/issues{/number}", + "pulls_url": "https://api.github.com/repos/simonw/datasette/pulls{/number}", + "milestones_url": "https://api.github.com/repos/simonw/datasette/milestones{/number}", + "notifications_url": "https://api.github.com/repos/simonw/datasette/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/simonw/datasette/labels{/name}", + "releases_url": "https://api.github.com/repos/simonw/datasette/releases{/id}", + "deployments_url": "https://api.github.com/repos/simonw/datasette/deployments", + "created_at": "2017-10-23T00:39:03Z", + "updated_at": "2020-07-27T20:42:15Z", + "pushed_at": "2020-07-26T01:21:05Z", + "git_url": "git://github.com/simonw/datasette.git", + "ssh_url": "git@github.com:simonw/datasette.git", + "clone_url": "https://github.com/simonw/datasette.git", + "svn_url": "https://github.com/simonw/datasette", + "homepage": "http://datasette.readthedocs.io/", + "size": 3487, + "stargazers_count": 3642, + "watchers_count": 3642, + "language": "Python", + "has_issues": true, + "has_projects": false, + "has_downloads": true, + "has_wiki": true, + "has_pages": false, + "forks_count": 206, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 190, + "license": { + "key": "apache-2.0", + "name": "Apache License 2.0", + "spdx_id": "Apache-2.0", + "url": "https://api.github.com/licenses/apache-2.0", + "node_id": "MDc6TGljZW5zZTI=" + }, + "forks": 206, + "open_issues": 190, + "watchers": 3642, + "default_branch": "master" + } + }, + "base": { + "label": "simonw:master", + "ref": "master", + "sha": "f04deebec4f3842f7bd610cd5859de529f77d50e", + "user": { + "login": "simonw", + "id": 9599, + "node_id": "MDQ6VXNlcjk1OTk=", + "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/simonw", + "html_url": "https://github.com/simonw", + "followers_url": "https://api.github.com/users/simonw/followers", + "following_url": "https://api.github.com/users/simonw/following{/other_user}", + "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", + "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", + "organizations_url": "https://api.github.com/users/simonw/orgs", + "repos_url": "https://api.github.com/users/simonw/repos", + "events_url": "https://api.github.com/users/simonw/events{/privacy}", + "received_events_url": "https://api.github.com/users/simonw/received_events", + "type": "User", + "site_admin": false + }, + "repo": { + "id": 107914493, + "node_id": "MDEwOlJlcG9zaXRvcnkxMDc5MTQ0OTM=", + "name": "datasette", + "full_name": "simonw/datasette", + "private": false, + "owner": { + "login": "simonw", + "id": 9599, + "node_id": "MDQ6VXNlcjk1OTk=", + "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/simonw", + "html_url": "https://github.com/simonw", + "followers_url": "https://api.github.com/users/simonw/followers", + "following_url": "https://api.github.com/users/simonw/following{/other_user}", + "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", + "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", + "organizations_url": "https://api.github.com/users/simonw/orgs", + "repos_url": "https://api.github.com/users/simonw/repos", + "events_url": "https://api.github.com/users/simonw/events{/privacy}", + "received_events_url": "https://api.github.com/users/simonw/received_events", + "type": "User", + "site_admin": false + }, + "html_url": "https://github.com/simonw/datasette", + "description": "An open source multi-tool for exploring and publishing data", + "fork": false, + "url": "https://api.github.com/repos/simonw/datasette", + "forks_url": "https://api.github.com/repos/simonw/datasette/forks", + "keys_url": "https://api.github.com/repos/simonw/datasette/keys{/key_id}", + "collaborators_url": "https://api.github.com/repos/simonw/datasette/collaborators{/collaborator}", + "teams_url": "https://api.github.com/repos/simonw/datasette/teams", + "hooks_url": "https://api.github.com/repos/simonw/datasette/hooks", + "issue_events_url": "https://api.github.com/repos/simonw/datasette/issues/events{/number}", + "events_url": "https://api.github.com/repos/simonw/datasette/events", + "assignees_url": "https://api.github.com/repos/simonw/datasette/assignees{/user}", + "branches_url": "https://api.github.com/repos/simonw/datasette/branches{/branch}", + "tags_url": "https://api.github.com/repos/simonw/datasette/tags", + "blobs_url": "https://api.github.com/repos/simonw/datasette/git/blobs{/sha}", + "git_tags_url": "https://api.github.com/repos/simonw/datasette/git/tags{/sha}", + "git_refs_url": "https://api.github.com/repos/simonw/datasette/git/refs{/sha}", + "trees_url": "https://api.github.com/repos/simonw/datasette/git/trees{/sha}", + "statuses_url": "https://api.github.com/repos/simonw/datasette/statuses/{sha}", + "languages_url": "https://api.github.com/repos/simonw/datasette/languages", + "stargazers_url": "https://api.github.com/repos/simonw/datasette/stargazers", + "contributors_url": "https://api.github.com/repos/simonw/datasette/contributors", + "subscribers_url": "https://api.github.com/repos/simonw/datasette/subscribers", + "subscription_url": "https://api.github.com/repos/simonw/datasette/subscription", + "commits_url": "https://api.github.com/repos/simonw/datasette/commits{/sha}", + "git_commits_url": "https://api.github.com/repos/simonw/datasette/git/commits{/sha}", + "comments_url": "https://api.github.com/repos/simonw/datasette/comments{/number}", + "issue_comment_url": "https://api.github.com/repos/simonw/datasette/issues/comments{/number}", + "contents_url": "https://api.github.com/repos/simonw/datasette/contents/{+path}", + "compare_url": "https://api.github.com/repos/simonw/datasette/compare/{base}...{head}", + "merges_url": "https://api.github.com/repos/simonw/datasette/merges", + "archive_url": "https://api.github.com/repos/simonw/datasette/{archive_format}{/ref}", + "downloads_url": "https://api.github.com/repos/simonw/datasette/downloads", + "issues_url": "https://api.github.com/repos/simonw/datasette/issues{/number}", + "pulls_url": "https://api.github.com/repos/simonw/datasette/pulls{/number}", + "milestones_url": "https://api.github.com/repos/simonw/datasette/milestones{/number}", + "notifications_url": "https://api.github.com/repos/simonw/datasette/notifications{?since,all,participating}", + "labels_url": "https://api.github.com/repos/simonw/datasette/labels{/name}", + "releases_url": "https://api.github.com/repos/simonw/datasette/releases{/id}", + "deployments_url": "https://api.github.com/repos/simonw/datasette/deployments", + "created_at": "2017-10-23T00:39:03Z", + "updated_at": "2020-07-27T20:42:15Z", + "pushed_at": "2020-07-26T01:21:05Z", + "git_url": "git://github.com/simonw/datasette.git", + "ssh_url": "git@github.com:simonw/datasette.git", + "clone_url": "https://github.com/simonw/datasette.git", + "svn_url": "https://github.com/simonw/datasette", + "homepage": "http://datasette.readthedocs.io/", + "size": 3487, + "stargazers_count": 3642, + "watchers_count": 3642, + "language": "Python", + "has_issues": true, + "has_projects": false, + "has_downloads": true, + "has_wiki": true, + "has_pages": false, + "forks_count": 206, + "mirror_url": null, + "archived": false, + "disabled": false, + "open_issues_count": 190, + "license": { + "key": "apache-2.0", + "name": "Apache License 2.0", + "spdx_id": "Apache-2.0", + "url": "https://api.github.com/licenses/apache-2.0", + "node_id": "MDc6TGljZW5zZTI=" + }, + "forks": 206, + "open_issues": 190, + "watchers": 3642, + "default_branch": "master" + } + }, + "_links": { + "self": { + "href": "https://api.github.com/repos/simonw/datasette/pulls/571" + }, + "html": { + "href": "https://github.com/simonw/datasette/pull/571" + }, + "issue": { + "href": "https://api.github.com/repos/simonw/datasette/issues/571" + }, + "comments": { + "href": "https://api.github.com/repos/simonw/datasette/issues/571/comments" + }, + "review_comments": { + "href": "https://api.github.com/repos/simonw/datasette/pulls/571/comments" + }, + "review_comment": { + "href": "https://api.github.com/repos/simonw/datasette/pulls/comments{/number}" + }, + "commits": { + "href": "https://api.github.com/repos/simonw/datasette/pulls/571/commits" + }, + "statuses": { + "href": "https://api.github.com/repos/simonw/datasette/statuses/a85239f69261c10f1a9f90514c8b5d113cb94585" + } + }, + "author_association": "OWNER", + "active_lock_reason": null, + "merged": true, + "mergeable": null, + "rebaseable": null, + "mergeable_state": "unknown", + "merged_by": { + "login": "simonw", + "id": 9599, + "node_id": "MDQ6VXNlcjk1OTk=", + "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", + "gravatar_id": "", + "url": "https://api.github.com/users/simonw", + "html_url": "https://github.com/simonw", + "followers_url": "https://api.github.com/users/simonw/followers", + "following_url": "https://api.github.com/users/simonw/following{/other_user}", + "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", + "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", + "organizations_url": "https://api.github.com/users/simonw/orgs", + "repos_url": "https://api.github.com/users/simonw/repos", + "events_url": "https://api.github.com/users/simonw/events{/privacy}", + "received_events_url": "https://api.github.com/users/simonw/received_events", + "type": "User", + "site_admin": false + }, + "comments": 0, + "review_comments": 0, + "maintainer_can_modify": false, + "commits": 1, + "additions": 7, + "deletions": 3, + "changed_files": 2 + } +] diff --git a/tests/test_pull_requests.py b/tests/test_pull_requests.py new file mode 100644 index 0000000..1d3fd6e --- /dev/null +++ b/tests/test_pull_requests.py @@ -0,0 +1,128 @@ +from github_to_sqlite import utils +import pytest +import pathlib +import sqlite_utils +from sqlite_utils.db import ForeignKey +import json + + +@pytest.fixture +def pull_requests(): + return json.load(open(pathlib.Path(__file__).parent / "pull_requests.json")) + + +@pytest.fixture +def db(pull_requests): + db = sqlite_utils.Database(memory=True) + db["repos"].insert( + {"id": 1}, + pk="id", + columns={"organization": int, "topics": str, "name": str, "description": str}, + ) + utils.save_pull_requests(db, pull_requests, {"id": 1}) + return db + + +def test_tables(db): + assert {"pull_requests", "users", "repos", "milestones"} == set(db.table_names()) + assert { + ForeignKey( + table="pull_requests", column="repo", other_table="repos", other_column="id" + ), + ForeignKey( + table="pull_requests", + column="milestone", + other_table="milestones", + other_column="id", + ), + ForeignKey( + table="pull_requests", + column="assignee", + other_table="users", + other_column="id", + ), + ForeignKey( + table="pull_requests", column="user", other_table="users", other_column="id" + ), + } == set(db["pull_requests"].foreign_keys) + + +def test_pull_requests(db): + pull_request_rows = list(db["pull_requests"].rows) + assert [ + { + "id": 313384926, + "node_id": "MDExOlB1bGxSZXF1ZXN0MzEzMzg0OTI2", + "number": 571, + "state": "closed", + "locked": 0, + "title": "detect_fts now works with alternative table escaping", + "user": 9599, + "body": "Fixes #570", + "created_at": "2019-09-03T00:23:39Z", + "updated_at": "2019-09-03T00:32:28Z", + "closed_at": "2019-09-03T00:32:28Z", + "merged_at": "2019-09-03T00:32:28Z", + "merge_commit_sha": "2dc5c8dc259a0606162673d394ba8cc1c6f54428", + "assignee": None, + "milestone": None, + "draft": 0, + "head": "a85239f69261c10f1a9f90514c8b5d113cb94585", + "base": "f04deebec4f3842f7bd610cd5859de529f77d50e", + "author_association": "OWNER", + "merged": 1, + "mergeable": None, + "rebaseable": None, + "mergeable_state": "unknown", + "merged_by": '{"login": "simonw", "id": 9599, "node_id": "MDQ6VXNlcjk1OTk=", "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", "gravatar_id": "", "url": "https://api.github.com/users/simonw", "html_url": "https://github.com/simonw", "followers_url": "https://api.github.com/users/simonw/followers", "following_url": "https://api.github.com/users/simonw/following{/other_user}", "gists_url": "https://api.github.com/users/simonw/gists{/gist_id}", "starred_url": "https://api.github.com/users/simonw/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/simonw/subscriptions", "organizations_url": "https://api.github.com/users/simonw/orgs", "repos_url": "https://api.github.com/users/simonw/repos", "events_url": "https://api.github.com/users/simonw/events{/privacy}", "received_events_url": "https://api.github.com/users/simonw/received_events", "type": "User", "site_admin": false}', + "comments": 0, + "review_comments": 0, + "maintainer_can_modify": 0, + "commits": 1, + "additions": 7, + "deletions": 3, + "changed_files": 2, + "repo": 1, + "url": "https://github.com/simonw/datasette/pull/571", + } + ] == pull_request_rows + + +def test_users(db): + user_rows = list(db["users"].rows) + assert [ + { + "login": "simonw", + "id": 9599, + "node_id": "MDQ6VXNlcjk1OTk=", + "avatar_url": "https://avatars0.githubusercontent.com/u/9599?v=4", + "gravatar_id": "", + "html_url": "https://github.com/simonw", + "type": "User", + "site_admin": 0, + "name": "simonw", + } + ] == user_rows + + +def test_foreign_keys(db): + assert [ + ForeignKey( + table="pull_requests", column="repo", other_table="repos", other_column="id" + ), + ForeignKey( + table="pull_requests", + column="milestone", + other_table="milestones", + other_column="id", + ), + ForeignKey( + table="pull_requests", + column="assignee", + other_table="users", + other_column="id", + ), + ForeignKey( + table="pull_requests", column="user", other_table="users", other_column="id" + ), + ] == db["pull_requests"].foreign_keys