"Fossies" - the Fresh Open Source Software Archive

Member "cloudkitty-13.0.0/cloudkitty/tests/utils_tests/test_tz.py" (14 Oct 2020, 5986 Bytes) of package /linux/misc/openstack/cloudkitty-13.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_tz.py": 12.1.0_vs_13.0.0.

    1 # Copyright 2019 Objectif Libre
    2 #
    3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    4 #    not use this file except in compliance with the License. You may obtain
    5 #    a copy of the License at
    6 #
    7 #         http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 #    Unless required by applicable law or agreed to in writing, software
   10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 #    License for the specific language governing permissions and limitations
   13 #    under the License.
   14 #
   15 import datetime
   16 import unittest
   17 from unittest import mock
   18 
   19 from dateutil import tz
   20 from oslo_utils import timeutils
   21 
   22 from cloudkitty import utils
   23 from cloudkitty.utils import tz as tzutils
   24 
   25 
   26 class TestTZUtils(unittest.TestCase):
   27 
   28     def setUp(self):
   29         self.local_now = tzutils.localized_now()
   30         self.naive_now = utils.utcnow().replace(microsecond=0)
   31 
   32     def test_localized_now(self):
   33         self.assertEqual(
   34             self.local_now.astimezone(tz.tzutc()).replace(tzinfo=None),
   35             self.naive_now)
   36         self.assertIsNotNone(self.local_now.tzinfo)
   37 
   38     def test_local_to_utc_naive(self):
   39         naive_local = tzutils.local_to_utc(self.local_now, naive=True)
   40         naive_naive = tzutils.local_to_utc(self.naive_now, naive=True)
   41         self.assertIsNone(naive_local.tzinfo)
   42         self.assertIsNone(naive_naive.tzinfo)
   43         self.assertEqual(naive_local, naive_naive)
   44 
   45     def test_local_to_utc_not_naive(self):
   46         local = tzutils.local_to_utc(self.local_now)
   47         naive = tzutils.local_to_utc(self.naive_now)
   48         self.assertIsNotNone(local.tzinfo)
   49         self.assertIsNotNone(naive.tzinfo)
   50         self.assertEqual(local, naive)
   51 
   52     def test_utc_to_local(self):
   53         self.assertEqual(tzutils.utc_to_local(self.naive_now), self.local_now)
   54 
   55     def test_dt_from_iso(self):
   56         tester = '2019-06-06T16:30:54+02:00'
   57         tester_utc = '2019-06-06T14:30:54+00:00'
   58 
   59         dt = tzutils.dt_from_iso(tester)
   60         self.assertIsNotNone(dt.tzinfo)
   61         self.assertEqual(tzutils.dt_from_iso(tester, as_utc=True).isoformat(),
   62                          tester_utc)
   63 
   64     def _test_add_substract_delta(self, obj, tzone):
   65         delta = datetime.timedelta(seconds=3600)
   66         naive = obj.astimezone(tz.tzutc()).replace(tzinfo=None)
   67 
   68         self.assertEqual(
   69             tzutils.add_delta(obj, delta).astimezone(tzone),
   70             (naive + delta).replace(tzinfo=tz.tzutc()).astimezone(tzone),
   71         )
   72         self.assertEqual(
   73             tzutils.substract_delta(obj, delta).astimezone(tzone),
   74             (naive - delta).replace(tzinfo=tz.tzutc()).astimezone(tzone),
   75         )
   76 
   77     def test_add_substract_delta_summertime(self):
   78         tzone = tz.gettz('Europe/Paris')
   79         obj = datetime.datetime(2019, 3, 31, 1, tzinfo=tzone)
   80         self._test_add_substract_delta(obj, tzone)
   81 
   82     def test_add_substract_delta(self):
   83         tzone = tz.gettz('Europe/Paris')
   84         obj = datetime.datetime(2019, 1, 1, tzinfo=tzone)
   85         self._test_add_substract_delta(obj, tzone)
   86 
   87     def test_get_month_start_no_arg(self):
   88         naive_utc_now = timeutils.utcnow()
   89         naive_month_start = datetime.datetime(
   90             naive_utc_now.year, naive_utc_now.month, 1)
   91         month_start = tzutils.get_month_start()
   92         self.assertIsNotNone(month_start.tzinfo)
   93         self.assertEqual(
   94             naive_month_start,
   95             month_start.replace(tzinfo=None))
   96 
   97     def test_get_month_start_with_arg(self):
   98         param = datetime.datetime(2019, 1, 3, 4, 5)
   99         month_start = tzutils.get_month_start(param)
  100         self.assertIsNotNone(month_start.tzinfo)
  101         self.assertEqual(month_start.replace(tzinfo=None),
  102                          datetime.datetime(2019, 1, 1))
  103 
  104     def test_get_month_start_with_arg_naive(self):
  105         param = datetime.datetime(2019, 1, 3, 4, 5)
  106         month_start = tzutils.get_month_start(param, naive=True)
  107         self.assertIsNone(month_start.tzinfo)
  108         self.assertEqual(month_start, datetime.datetime(2019, 1, 1))
  109 
  110     def test_diff_seconds_positive_arg_naive_objects(self):
  111         one = datetime.datetime(2019, 1, 1, 1, 1, 30)
  112         two = datetime.datetime(2019, 1, 1, 1, 1)
  113         self.assertEqual(tzutils.diff_seconds(one, two), 30)
  114 
  115     def test_diff_seconds_negative_arg_naive_objects(self):
  116         one = datetime.datetime(2019, 1, 1, 1, 1, 30)
  117         two = datetime.datetime(2019, 1, 1, 1, 1)
  118         self.assertEqual(tzutils.diff_seconds(two, one), 30)
  119 
  120     def test_diff_seconds_positive_arg_aware_objects(self):
  121         one = datetime.datetime(2019, 1, 1, 1, 1, 30, tzinfo=tz.tzutc())
  122         two = datetime.datetime(2019, 1, 1, 1, 1, tzinfo=tz.tzutc())
  123         self.assertEqual(tzutils.diff_seconds(one, two), 30)
  124 
  125     def test_diff_seconds_negative_arg_aware_objects(self):
  126         one = datetime.datetime(2019, 1, 1, 1, 1, 30, tzinfo=tz.tzutc())
  127         two = datetime.datetime(2019, 1, 1, 1, 1, tzinfo=tz.tzutc())
  128         self.assertEqual(tzutils.diff_seconds(two, one), 30)
  129 
  130     def test_diff_seconds_negative_arg_aware_objects_on_summer_change(self):
  131         one = datetime.datetime(2019, 3, 31, 1,
  132                                 tzinfo=tz.gettz('Europe/Paris'))
  133         two = datetime.datetime(2019, 3, 31, 3,
  134                                 tzinfo=tz.gettz('Europe/Paris'))
  135         self.assertEqual(tzutils.diff_seconds(two, one), 3600)
  136 
  137     def test_cloudkitty_dt_from_ts_as_utc(self):
  138         ts = 1569902400
  139         dt = datetime.datetime(2019, 10, 1, 4, tzinfo=tz.tzutc())
  140         self.assertEqual(dt, tzutils.dt_from_ts(ts, as_utc=True))
  141 
  142     def test_cloudkitty_dt_from_ts_local_tz(self):
  143         ts = 1569902400
  144         timezone = tz.gettz('Europe/Paris')
  145         dt = datetime.datetime(2019, 10, 1, 6, tzinfo=timezone)
  146         with mock.patch.object(tzutils, '_LOCAL_TZ', new=timezone):
  147             self.assertEqual(dt, tzutils.dt_from_ts(ts))