/ tests / Gist.py
Gist.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net>                 #
  4  # Copyright 2012 Zearin <zearin@gonk.net>                                      #
  5  # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net>                 #
  6  # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net>                 #
  7  # Copyright 2016 Jannis Gebauer <ja.geb@me.com>                                #
  8  # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com>          #
  9  # Copyright 2018 sfdye <tsfdye@gmail.com>                                      #
 10  #                                                                              #
 11  # This file is part of PyGithub.                                               #
 12  # http://pygithub.readthedocs.io/                                              #
 13  #                                                                              #
 14  # PyGithub is free software: you can redistribute it and/or modify it under    #
 15  # the terms of the GNU Lesser General Public License as published by the Free  #
 16  # Software Foundation, either version 3 of the License, or (at your option)    #
 17  # any later version.                                                           #
 18  #                                                                              #
 19  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 20  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 21  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 22  # details.                                                                     #
 23  #                                                                              #
 24  # You should have received a copy of the GNU Lesser General Public License     #
 25  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 26  #                                                                              #
 27  ################################################################################
 28  
 29  from datetime import datetime, timezone
 30  
 31  import github
 32  
 33  from . import Framework
 34  
 35  
 36  class Gist(Framework.TestCase):
 37      def testAttributes(self):
 38          gist = self.g.get_gist("6296732")
 39          self.assertEqual(gist.comments, 0)
 40          self.assertEqual(
 41              gist.created_at,
 42              datetime(2013, 8, 21, 16, 28, 24, tzinfo=timezone.utc),
 43          )
 44          self.assertEqual(gist.description, "Github API")
 45          self.assertEqual(list(gist.files.keys()), ["GithubAPI.lua"])
 46          self.assertEqual(gist.files["GithubAPI.lua"].size, 21229)
 47          self.assertEqual(gist.files["GithubAPI.lua"].filename, "GithubAPI.lua")
 48          self.assertEqual(gist.files["GithubAPI.lua"].language, "Lua")
 49          self.assertEqual(gist.files["GithubAPI.lua"].content[:10], "-- GithubA")
 50          self.assertEqual(
 51              gist.files["GithubAPI.lua"].raw_url,
 52              "https://gist.githubusercontent.com/jacquev6/6296732/raw/88aafa25fb28e17013054a117354a37f0d78963c/GithubAPI.lua",
 53          )
 54          self.assertEqual(gist.forks, [])
 55          self.assertEqual(gist.git_pull_url, "https://gist.github.com/6296732.git")
 56          self.assertEqual(gist.git_push_url, "https://gist.github.com/6296732.git")
 57          self.assertEqual(len(gist.history), 1)
 58          self.assertEqual(gist.history[0].change_status.additions, 793)
 59          self.assertEqual(gist.history[0].change_status.deletions, 0)
 60          self.assertEqual(gist.history[0].change_status.total, 793)
 61          self.assertEqual(
 62              gist.history[0].committed_at,
 63              datetime(2013, 8, 21, 16, 12, 27, tzinfo=timezone.utc),
 64          )
 65          self.assertEqual(
 66              gist.history[0].url,
 67              "https://api.github.com/gists/6296732/c464aecd7fea16684e935607eeea7ae4f8caa0e2",
 68          )
 69          self.assertEqual(gist.history[0].user, None)
 70          self.assertEqual(gist.history[0].owner.login, "jacquev6")
 71          self.assertEqual(gist.history[0].version, "c464aecd7fea16684e935607eeea7ae4f8caa0e2")
 72          self.assertEqual(gist.html_url, "https://gist.github.com/6296732")
 73          self.assertEqual(gist.id, "6296732")
 74          self.assertTrue(gist.public)
 75          self.assertEqual(
 76              gist.updated_at,
 77              datetime(2013, 8, 21, 16, 28, 24, tzinfo=timezone.utc),
 78          )
 79          self.assertEqual(gist.url, "https://api.github.com/gists/6296732")
 80          self.assertEqual(gist.user, None)
 81          self.assertEqual(gist.owner.login, "jacquev6")
 82          self.assertEqual(gist.git_pull_url, "https://gist.github.com/6296732.git")
 83          self.assertEqual(gist.git_push_url, "https://gist.github.com/6296732.git")
 84          self.assertEqual(gist.html_url, "https://gist.github.com/6296732")
 85          self.assertEqual(gist.url, "https://api.github.com/gists/6296732")
 86          self.assertEqual(repr(gist), 'Gist(id="6296732")')
 87          self.assertEqual(repr(gist.files["GithubAPI.lua"]), 'GistFile(filename="GithubAPI.lua")')
 88  
 89      def testEditWithoutParameters(self):
 90          gist = self.g.get_gist("2729810")
 91          gist.edit()
 92          self.assertEqual(gist.description, "Gist created by PyGithub")
 93          self.assertEqual(
 94              gist.updated_at,
 95              datetime(2012, 5, 19, 7, 0, 58, tzinfo=timezone.utc),
 96          )
 97  
 98      def testEditWithAllParameters(self):
 99          gist = self.g.get_gist("2729810")
100          gist.edit(
101              "Description edited by PyGithub",
102              {"barbaz.txt": github.InputFileContent("File also created by PyGithub")},
103          )
104          self.assertEqual(gist.description, "Description edited by PyGithub")
105          self.assertEqual(
106              gist.updated_at,
107              datetime(2012, 5, 19, 7, 6, 10, tzinfo=timezone.utc),
108          )
109          self.assertEqual(set(gist.files.keys()), {"foobar.txt", "barbaz.txt"})
110  
111      def testDeleteFile(self):
112          gist = self.g.get_gist("5339374")
113          self.assertEqual(sorted(gist.files.keys()), ["bar.txt", "foo.txt"])
114          gist.edit(files={"foo.txt": None})
115          self.assertEqual(list(gist.files.keys()), ["bar.txt"])
116  
117      def testRenameFile(self):
118          gist = self.g.get_gist("5339374")
119          self.assertEqual(list(gist.files.keys()), ["bar.txt"])
120          gist.edit(files={"bar.txt": github.InputFileContent(gist.files["bar.txt"].content, new_name="baz.txt")})
121          self.assertEqual(list(gist.files.keys()), ["baz.txt"])
122  
123      def testCreateComment(self):
124          gist = self.g.get_gist("2729810")
125          comment = gist.create_comment("Comment created by PyGithub")
126          self.assertEqual(comment.id, 323629)
127  
128      def testGetComments(self):
129          gist = self.g.get_gist("2729810")
130          self.assertListKeyEqual(gist.get_comments(), lambda c: c.id, [323637])
131  
132      def testStarring(self):
133          gist = self.g.get_gist("2729810")
134          self.assertFalse(gist.is_starred())
135          gist.set_starred()
136          self.assertTrue(gist.is_starred())
137          gist.reset_starred()
138          self.assertFalse(gist.is_starred())
139  
140      def testFork(self):
141          gist = self.g.get_gist("6296553")  # Random gist
142          myGist = gist.create_fork()
143          self.assertEqual(myGist.id, "6296732")
144          self.assertEqual(myGist.fork_of, None)  # WTF
145          sameGist = self.g.get_gist("6296732")
146          self.assertEqual(sameGist.fork_of.id, "6296553")
147  
148      def testDelete(self):
149          gist = self.g.get_gist("2729810")
150          gist.delete()