/ tests / GithubObject.py
GithubObject.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2023 Enrico Minack <github@enrico.minack.dev>                      #
  4  #                                                                              #
  5  # This file is part of PyGithub.                                               #
  6  # http://pygithub.readthedocs.io/                                              #
  7  #                                                                              #
  8  # PyGithub is free software: you can redistribute it and/or modify it under    #
  9  # the terms of the GNU Lesser General Public License as published by the Free  #
 10  # Software Foundation, either version 3 of the License, or (at your option)    #
 11  # any later version.                                                           #
 12  #                                                                              #
 13  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 14  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 15  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 16  # details.                                                                     #
 17  #                                                                              #
 18  # You should have received a copy of the GNU Lesser General Public License     #
 19  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 20  #                                                                              #
 21  ################################################################################
 22  
 23  import unittest
 24  from datetime import datetime, timedelta, timezone
 25  
 26  from dateutil.tz.tz import tzoffset
 27  
 28  from . import Framework
 29  
 30  gho = Framework.github.GithubObject
 31  
 32  
 33  class GithubObject(unittest.TestCase):
 34      def testMakeDatetimeAttribute(self):
 35          for value, expected in [
 36              (None, None),
 37              (
 38                  "2021-01-23T12:34:56Z",
 39                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone.utc),
 40              ),
 41              (
 42                  "2021-01-23T12:34:56.000Z",
 43                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone.utc),
 44              ),
 45              (
 46                  "2021-01-23T12:34:56.000Z",
 47                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone.utc),
 48              ),
 49              (
 50                  "2021-01-23T12:34:56+00:00",
 51                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone.utc),
 52              ),
 53              (
 54                  "2021-01-23T12:34:56+01:00",
 55                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone(timedelta(hours=1))),
 56              ),
 57              (
 58                  "2021-01-23T12:34:56-06:30",
 59                  datetime(
 60                      2021,
 61                      1,
 62                      23,
 63                      12,
 64                      34,
 65                      56,
 66                      tzinfo=timezone(timedelta(hours=-6, minutes=-30)),
 67                  ),
 68              ),
 69              (
 70                  "2021-01-23T12:34:56.000+00:00",
 71                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone.utc),
 72              ),
 73              (
 74                  "2021-01-23T12:34:56.000+01:00",
 75                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone(timedelta(hours=1))),
 76              ),
 77              (
 78                  "2021-01-23T12:34:56.000-06:00",
 79                  datetime(2021, 1, 23, 12, 34, 56, tzinfo=tzoffset(None, -21600)),
 80              ),
 81          ]:
 82              actual = gho.GithubObject._makeDatetimeAttribute(value)
 83              self.assertEqual(gho._ValuedAttribute, type(actual), value)
 84              self.assertEqual(expected, actual.value, value)
 85  
 86      def testMakeDatetimeAttributeBadValues(self):
 87          for value in ["not a timestamp", 1234]:
 88              actual = gho.GithubObject._makeDatetimeAttribute(value)
 89  
 90              self.assertEqual(gho._BadAttribute, type(actual))
 91              with self.assertRaises(Framework.github.BadAttributeException) as e:
 92                  value = actual.value
 93              self.assertEqual(value, e.exception.actual_value)
 94              self.assertEqual(str, e.exception.expected_type)
 95              if isinstance(value, str):
 96                  self.assertIsNotNone(e.exception.transformation_exception)
 97              else:
 98                  self.assertIsNone(e.exception.transformation_exception)
 99  
100      def testMakeTimestampAttribute(self):
101          actual = gho.GithubObject._makeTimestampAttribute(None)
102          self.assertEqual(gho._ValuedAttribute, type(actual))
103          self.assertIsNone(actual.value)
104  
105          actual = gho.GithubObject._makeTimestampAttribute(1611405296)
106          self.assertEqual(gho._ValuedAttribute, type(actual))
107          self.assertEqual(datetime(2021, 1, 23, 12, 34, 56, tzinfo=timezone.utc), actual.value)
108  
109      def testMakeTimetsampAttributeBadValues(self):
110          for value in ["1611405296", 1234.567]:
111              actual = gho.GithubObject._makeTimestampAttribute(value)
112  
113              self.assertEqual(gho._BadAttribute, type(actual))
114              with self.assertRaises(Framework.github.BadAttributeException) as e:
115                  value = actual.value
116              self.assertEqual(value, e.exception.actual_value)
117              self.assertEqual(int, e.exception.expected_type)
118              self.assertIsNone(e.exception.transformation_exception)