Search.py
1 ############################ Copyrights and license ############################ 2 # # 3 # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> # 4 # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> # 5 # Copyright 2018 Agor Maxime <maxime.agor23@gmail.com> # 6 # Copyright 2018 Joel Koglin <JoelKoglin@gmail.com> # 7 # Copyright 2018 sfdye <tsfdye@gmail.com> # 8 # # 9 # This file is part of PyGithub. # 10 # http://pygithub.readthedocs.io/ # 11 # # 12 # PyGithub is free software: you can redistribute it and/or modify it under # 13 # the terms of the GNU Lesser General Public License as published by the Free # 14 # Software Foundation, either version 3 of the License, or (at your option) # 15 # any later version. # 16 # # 17 # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY # 18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # 19 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # 20 # details. # 21 # # 22 # You should have received a copy of the GNU Lesser General Public License # 23 # along with PyGithub. If not, see <http://www.gnu.org/licenses/>. # 24 # # 25 ################################################################################ 26 27 from . import Framework 28 29 30 class Search(Framework.TestCase): 31 def setUp(self): 32 super().setUp() 33 34 def testSearchUsers(self): 35 users = self.g.search_users("vincent", sort="followers", order="desc") 36 self.assertEqual(users.totalCount, 2781) 37 38 def testPaginateSearchUsers(self): 39 users = self.g.search_users("", location="Berlin") 40 self.assertListKeyBegin( 41 users, 42 lambda u: u.login, 43 [ 44 "cloudhead", 45 "felixge", 46 "sferik", 47 "rkh", 48 "jezdez", 49 "janl", 50 "marijnh", 51 "nikic", 52 "igorw", 53 "froschi", 54 "svenfuchs", 55 "omz", 56 "chad", 57 "bergie", 58 "roidrage", 59 "pcalcado", 60 "durran", 61 "hukl", 62 "mttkay", 63 "aFarkas", 64 "ole", 65 "hagenburger", 66 "jberkel", 67 "naderman", 68 "joshk", 69 "pudo", 70 "robb", 71 "josephwilk", 72 "hanshuebner", 73 "txus", 74 "paulasmuth", 75 "splitbrain", 76 "langalex", 77 "bendiken", 78 "stefanw", 79 ], 80 ) 81 self.assertEqual(users.totalCount, 6038) 82 83 def testGetPageOnSearchUsers(self): 84 users = self.g.search_users("", location="Berlin") 85 self.assertEqual( 86 [u.login for u in users.get_page(7)], 87 [ 88 "ursachec", 89 "bitboxer", 90 "fs111", 91 "michenriksen", 92 "witsch", 93 "booo", 94 "mortice", 95 "r0man", 96 "MikeBild", 97 "mhagger", 98 "bkw", 99 "fwbrasil", 100 "mschneider", 101 "lydiapintscher", 102 "asksven", 103 "iamtimm", 104 "sneak", 105 "kr1sp1n", 106 "Feh", 107 "GordonLesti", 108 "annismckenzie", 109 "eskimoblood", 110 "tsujigiri", 111 "riethmayer", 112 "lauritzthamsen", 113 "scotchi", 114 "peritor", 115 "toto", 116 "hwaxxer", 117 "lukaszklis", 118 ], 119 ) 120 121 def testSearchRepos(self): 122 repos = self.g.search_repositories("github", sort="stars", order="desc", language="Python") 123 self.assertListKeyBegin( 124 repos, 125 lambda r: r.full_name, 126 [ 127 "kennethreitz/legit", 128 "RuudBurger/CouchPotatoV1", 129 "gelstudios/gitfiti", 130 "gpjt/webgl-lessons", 131 "jacquev6/PyGithub", 132 "aaasen/github_globe", 133 "hmason/gitmarks", 134 "dnerdy/factory_boy", 135 "binaryage/drydrop", 136 "bgreenlee/sublime-github", 137 "karan/HackerNewsAPI", 138 "mfenniak/pyPdf", 139 "skazhy/github-decorator", 140 "llvmpy/llvmpy", 141 "lexrupy/gmate", 142 "ask/python-github2", 143 "audreyr/cookiecutter-pypackage", 144 "tabo/django-treebeard", 145 "dbr/tvdb_api", 146 "jchris/couchapp", 147 "joeyespo/grip", 148 "nigelsmall/py2neo", 149 "ask/chishop", 150 "sigmavirus24/github3.py", 151 "jsmits/github-cli", 152 "lincolnloop/django-layout", 153 "amccloud/django-project-skel", 154 "Stiivi/brewery", 155 "webpy/webpy.github.com", 156 "dustin/py-github", 157 "logsol/Github-Auto-Deploy", 158 "cloudkick/libcloud", 159 "berkerpeksag/github-badge", 160 "bitprophet/ssh", 161 "azavea/OpenTreeMap", 162 ], 163 ) 164 165 def testSearchReposWithNoResults(self): 166 repos = self.g.search_repositories("doesnotexist") 167 self.assertEqual(repos.totalCount, 0) 168 169 def testSearchIssues(self): 170 issues = self.g.search_issues("compile", sort="comments", order="desc", language="C++") 171 self.assertListKeyBegin( 172 issues, 173 lambda i: i.id, 174 [ 175 12068673, 176 23250111, 177 14371957, 178 9423897, 179 24277400, 180 2408877, 181 11338741, 182 13980502, 183 27697165, 184 23102422, 185 ], 186 ) 187 188 def testPaginateSearchCommits(self): 189 commits = self.g.search_commits(query="hash:5b0224e868cc9242c9450ef02efbe3097abd7ba2") 190 self.assertEqual(commits.totalCount, 3) 191 192 def testSearchCommits(self): 193 commits = self.g.search_commits( 194 query="hash:1265747e992ba7d34a469b6b2f527809f8bf7067", 195 sort="author-date", 196 order="asc", 197 merge="false", 198 ) 199 self.assertEqual(commits.totalCount, 2) 200 201 def testSearchTopics(self): 202 topics = self.g.search_topics("python", repositories=">950") 203 self.assertListKeyBegin( 204 topics, 205 lambda r: r.name, 206 ["python", "django", "flask", "ruby", "scikit-learn", "wagtail"], 207 ) 208 209 def testPaginateSearchTopics(self): 210 repos = self.g.search_topics("python", repositories=">950") 211 self.assertEqual(repos.totalCount, 6) 212 213 def testSearchCode(self): 214 files = self.g.search_code("toto", sort="indexed", order="asc", user="jacquev6") 215 self.assertListKeyEqual( 216 files, 217 lambda f: f.name, 218 [ 219 "Commit.setUp.txt", 220 "PullRequest.testGetFiles.txt", 221 "NamedUser.testGetEvents.txt", 222 "PullRequest.testCreateComment.txt", 223 "PullRequestFile.setUp.txt", 224 "Repository.testGetIssuesWithWildcards.txt", 225 "Repository.testGetIssuesWithArguments.txt", 226 "test_ebnf.cpp", 227 "test_abnf.cpp", 228 "PullRequestFile.py", 229 "SystemCalls.py", 230 "tests.py", 231 "LexerTestCase.py", 232 "ParserTestCase.py", 233 ], 234 ) 235 self.assertEqual(files[0].repository.full_name, "jacquev6/PyGithub") 236 content = files[0].decoded_content 237 if isinstance(content, bytes): 238 content = content.decode("utf-8") 239 self.assertEqual(content[:30], "https\nGET\napi.github.com\nNone\n") 240 241 def testSearchHighlightingCode(self): 242 files = self.g.search_code("toto", sort="indexed", order="asc", user="jacquev6", highlight=True) 243 self.assertTrue(files[0].text_matches) 244 245 def testUrlquotingOfQualifiers(self): 246 # Example taken from #236 247 issues = self.g.search_issues("repo:saltstack/salt-api type:Issues", updated=">2014-03-04T18:28:11Z") 248 self.assertEqual(issues[0].id, 29138794) 249 250 def testUrlquotingOfQuery(self): 251 # Example taken from #236 252 issues = self.g.search_issues("repo:saltstack/salt-api type:Issues updated:>2014-03-04T18:28:11Z") 253 self.assertEqual(issues[0].id, 29138794)