"Fossies" - the Fresh Open Source Software Archive

Member "cloudkitty-13.0.0/cloudkitty/tests/utils_tests/test_utils.py" (14 Oct 2020, 7070 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_utils.py": 12.1.0_vs_13.0.0.

    1 # -*- coding: utf-8 -*-
    2 # Copyright 2014 Objectif Libre
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 #
   16 import datetime
   17 import decimal
   18 import fractions
   19 import itertools
   20 import unittest
   21 from unittest import mock
   22 
   23 from oslo_utils import timeutils
   24 
   25 from cloudkitty import utils as ck_utils
   26 
   27 
   28 def iso2dt(iso_str):
   29     return timeutils.parse_isotime(iso_str)
   30 
   31 
   32 class UtilsTimeCalculationsTest(unittest.TestCase):
   33     def setUp(self):
   34         self.date_ts = 1416219015
   35         self.date_iso = '2014-11-17T10:10:15Z'
   36         self.date_params = {'year': 2014,
   37                             'month': 11,
   38                             'day': 17,
   39                             'hour': 10,
   40                             'minute': 10,
   41                             'second': 15}
   42         self.date_tz_params = {'year': 2014,
   43                                'month': 10,
   44                                'day': 26,
   45                                'hour': 2,
   46                                'minute': 00,
   47                                'second': 00}
   48 
   49     def test_dt2ts(self):
   50         date = datetime.datetime(**self.date_params)
   51         trans_ts = ck_utils.dt2ts(date)
   52         self.assertEqual(self.date_ts, trans_ts)
   53 
   54     def test_iso2dt(self):
   55         date = datetime.datetime(**self.date_params)
   56         trans_dt = ck_utils.iso2dt(self.date_iso)
   57         self.assertEqual(date, trans_dt)
   58 
   59     def test_ts2iso(self):
   60         trans_iso = ck_utils.ts2iso(self.date_ts)
   61         self.assertEqual(self.date_iso, trans_iso)
   62 
   63     def test_dt2iso(self):
   64         date = datetime.datetime(**self.date_params)
   65         trans_iso = ck_utils.dt2iso(date)
   66         self.assertEqual(self.date_iso, trans_iso)
   67 
   68     @mock.patch.object(ck_utils, 'utcnow',
   69                        return_value=iso2dt('2014-01-31T00:00:00Z'))
   70     def test_month_start_without_dt(self, patch_utcnow_mock):
   71         date = datetime.datetime(2014, 1, 1)
   72         trans_dt = ck_utils.get_month_start()
   73         self.assertEqual(date, trans_dt)
   74         patch_utcnow_mock.assert_called_once_with()
   75 
   76     @mock.patch.object(ck_utils, 'utcnow',
   77                        return_value=iso2dt('2014-01-15T00:00:00Z'))
   78     def test_month_end_without_dt(self, patch_utcnow_mock):
   79         date = datetime.datetime(2014, 1, 31)
   80         trans_dt = ck_utils.get_month_end()
   81         self.assertEqual(date, trans_dt)
   82         patch_utcnow_mock.assert_called_once_with()
   83 
   84     @mock.patch.object(ck_utils, 'utcnow',
   85                        return_value=iso2dt('2014-01-31T00:00:00Z'))
   86     def test_get_last_month_without_dt(self, patch_utcnow_mock):
   87         date = datetime.datetime(2013, 12, 1)
   88         trans_dt = ck_utils.get_last_month()
   89         self.assertEqual(date, trans_dt)
   90         patch_utcnow_mock.assert_called_once_with()
   91 
   92     @mock.patch.object(ck_utils, 'utcnow',
   93                        return_value=iso2dt('2014-01-31T00:00:00Z'))
   94     def test_get_next_month_without_dt(self, patch_utcnow_mock):
   95         date = datetime.datetime(2014, 2, 1)
   96         trans_dt = ck_utils.get_next_month()
   97         self.assertEqual(date, trans_dt)
   98         patch_utcnow_mock.assert_called_once_with()
   99 
  100     def test_get_last_month_leap(self):
  101         base_date = datetime.datetime(2016, 3, 31)
  102         date = datetime.datetime(2016, 2, 1)
  103         trans_dt = ck_utils.get_last_month(base_date)
  104         self.assertEqual(date, trans_dt)
  105 
  106     def test_get_next_month_leap(self):
  107         base_date = datetime.datetime(2016, 1, 31)
  108         date = datetime.datetime(2016, 2, 1)
  109         trans_dt = ck_utils.get_next_month(base_date)
  110         self.assertEqual(date, trans_dt)
  111 
  112     def test_add_month_leap(self):
  113         base_date = datetime.datetime(2016, 1, 31)
  114         date = datetime.datetime(2016, 3, 3)
  115         trans_dt = ck_utils.add_month(base_date, False)
  116         self.assertEqual(date, trans_dt)
  117 
  118     def test_add_month_keep_leap(self):
  119         base_date = datetime.datetime(2016, 1, 31)
  120         date = datetime.datetime(2016, 2, 29)
  121         trans_dt = ck_utils.add_month(base_date)
  122         self.assertEqual(date, trans_dt)
  123 
  124     def test_sub_month_leap(self):
  125         base_date = datetime.datetime(2016, 3, 31)
  126         date = datetime.datetime(2016, 3, 3)
  127         trans_dt = ck_utils.sub_month(base_date, False)
  128         self.assertEqual(date, trans_dt)
  129 
  130     def test_sub_month_keep_leap(self):
  131         base_date = datetime.datetime(2016, 3, 31)
  132         date = datetime.datetime(2016, 2, 29)
  133         trans_dt = ck_utils.sub_month(base_date)
  134         self.assertEqual(date, trans_dt)
  135 
  136     def test_load_timestamp(self):
  137         calc_dt = ck_utils.iso2dt(self.date_iso)
  138         check_dt = ck_utils.ts2dt(self.date_ts)
  139         self.assertEqual(calc_dt, check_dt)
  140 
  141 
  142 class ConvertUnitTest(unittest.TestCase):
  143     """Class testing the convert_unit and num2decimal function"""
  144     possible_args = [
  145         None,  # Use default arg
  146         '2/3',
  147         decimal.Decimal(1.23),
  148         '1.23',
  149         2,
  150         '2',
  151         2.3,
  152     ]
  153 
  154     def test_arg_types(self):
  155         """Test function with several arg combinations of different types"""
  156         for fac, off in itertools.product(self.possible_args, repeat=2):
  157             factor = fac if fac else 1
  158             offset = off if off else 0
  159             ck_utils.convert_unit(10, factor, offset)
  160 
  161     def test_str_str_str(self):
  162         result = ck_utils.convert_unit('1/2', '1/2', '1/2')
  163         self.assertEqual(result, decimal.Decimal(0.5 * 0.5 + 0.5))
  164 
  165     def test_str_float_float(self):
  166         result = ck_utils.convert_unit('1/2', 0.5, 0.5)
  167         self.assertEqual(result, decimal.Decimal(0.5 * 0.5 + 0.5))
  168 
  169     def test_convert_str_float(self):
  170         result = ck_utils.num2decimal('2.0')
  171         self.assertEqual(result, decimal.Decimal(2.0))
  172 
  173     def test_convert_str_int(self):
  174         result = ck_utils.num2decimal('2')
  175         self.assertEqual(result, decimal.Decimal(2))
  176 
  177     def test_convert_str_fraction(self):
  178         result = ck_utils.num2decimal('2/3')
  179         self.assertEqual(result, decimal.Decimal(2.0 / 3))
  180 
  181     def test_convert_fraction(self):
  182         result = ck_utils.num2decimal(fractions.Fraction(1, 2))
  183         self.assertEqual(result, decimal.Decimal(1.0 / 2))
  184 
  185     def test_convert_float(self):
  186         result = ck_utils.num2decimal(0.5)
  187         self.assertEqual(result, decimal.Decimal(0.5))
  188 
  189     def test_convert_int(self):
  190         result = ck_utils.num2decimal(2)
  191         self.assertEqual(result, decimal.Decimal(2))
  192 
  193     def test_convert_decimal(self):
  194         result = ck_utils.num2decimal(decimal.Decimal(2))
  195         self.assertEqual(result, decimal.Decimal(2))