1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3"""Tests for date and time precision helpers."""
4
5import decimal
6import unittest
7
8from dfdatetime import definitions
9from dfdatetime import precisions
10
11
12class DateTimePrecisionHelperTest(unittest.TestCase):
13  """Tests for the date time precision helper interface."""
14
15  def testCopyMicrosecondsToFractionOfSecond(self):
16    """Tests the CopyMicrosecondsToFractionOfSecond function."""
17    precision_helper = precisions.DateTimePrecisionHelper
18
19    with self.assertRaises(NotImplementedError):
20      precision_helper.CopyMicrosecondsToFractionOfSecond(0)
21
22  def testCopyToDateTimeString(self):
23    """Tests the CopyToDateTimeString function."""
24    precision_helper = precisions.DateTimePrecisionHelper
25
26    with self.assertRaises(NotImplementedError):
27      precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 0.5)
28
29
30class SecondsPrecisionHelperTest(unittest.TestCase):
31  """Tests for the seconds precision helper."""
32
33  def testCopyMicrosecondsToFractionOfSecond(self):
34    """Tests the CopyMicrosecondsToFractionOfSecond function."""
35    precision_helper = precisions.SecondsPrecisionHelper
36
37    fraction_of_second = precision_helper.CopyMicrosecondsToFractionOfSecond(
38        123456)
39    self.assertEqual(fraction_of_second, 0.0)
40
41    with self.assertRaises(ValueError):
42      precision_helper.CopyMicrosecondsToFractionOfSecond(-1)
43
44    with self.assertRaises(ValueError):
45      precision_helper.CopyMicrosecondsToFractionOfSecond(1000000)
46
47  def testCopyToDateTimeString(self):
48    """Tests the CopyToDateTimeString function."""
49    precision_helper = precisions.SecondsPrecisionHelper
50
51    date_time_string = precision_helper.CopyToDateTimeString(
52        (2018, 1, 2, 19, 45, 12), 0.123456)
53    self.assertEqual(date_time_string, '2018-01-02 19:45:12')
54
55    with self.assertRaises(ValueError):
56      precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456)
57
58
59class MillisecondsPrecisionHelperTest(unittest.TestCase):
60  """Tests for the milliseconds precision helper."""
61
62  def testCopyMicrosecondsToFractionOfSecond(self):
63    """Tests the CopyMicrosecondsToFractionOfSecond function."""
64    precision_helper = precisions.MillisecondsPrecisionHelper
65
66    fraction_of_second = precision_helper.CopyMicrosecondsToFractionOfSecond(
67        123456)
68    self.assertEqual(fraction_of_second, decimal.Decimal('0.123'))
69
70    with self.assertRaises(ValueError):
71      precision_helper.CopyMicrosecondsToFractionOfSecond(-1)
72
73    with self.assertRaises(ValueError):
74      precision_helper.CopyMicrosecondsToFractionOfSecond(1000000)
75
76  def testCopyToDateTimeString(self):
77    """Tests the CopyToDateTimeString function."""
78    precision_helper = precisions.MillisecondsPrecisionHelper
79
80    date_time_string = precision_helper.CopyToDateTimeString(
81        (2018, 1, 2, 19, 45, 12), 0.123456)
82    self.assertEqual(date_time_string, '2018-01-02 19:45:12.123')
83
84    with self.assertRaises(ValueError):
85      precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456)
86
87
88class MicrosecondsPrecisionHelperTest(unittest.TestCase):
89  """Tests for the milliseconds precision helper."""
90
91  def testCopyMicrosecondsToFractionOfSecond(self):
92    """Tests the CopyMicrosecondsToFractionOfSecond function."""
93    precision_helper = precisions.MicrosecondsPrecisionHelper
94
95    fraction_of_second = precision_helper.CopyMicrosecondsToFractionOfSecond(
96        123456)
97    self.assertEqual(fraction_of_second, decimal.Decimal('0.123456'))
98
99    with self.assertRaises(ValueError):
100      precision_helper.CopyMicrosecondsToFractionOfSecond(-1)
101
102    with self.assertRaises(ValueError):
103      precision_helper.CopyMicrosecondsToFractionOfSecond(1000000)
104
105  def testCopyToDateTimeString(self):
106    """Tests the CopyToDateTimeString function."""
107    precision_helper = precisions.MicrosecondsPrecisionHelper
108
109    date_time_string = precision_helper.CopyToDateTimeString(
110        (2018, 1, 2, 19, 45, 12), 0.123456)
111    self.assertEqual(date_time_string, '2018-01-02 19:45:12.123456')
112
113    with self.assertRaises(ValueError):
114      precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456)
115
116
117class PrecisionHelperFactoryTest(unittest.TestCase):
118  """Tests for the date time precision helper factory."""
119
120  def testCreatePrecisionHelper(self):
121    """Tests the CreatePrecisionHelper function."""
122    precision_helper = precisions.PrecisionHelperFactory.CreatePrecisionHelper(
123        definitions.PRECISION_1_MICROSECOND)
124
125    self.assertIsNotNone(precision_helper)
126
127    with self.assertRaises(ValueError):
128      precisions.PrecisionHelperFactory.CreatePrecisionHelper('bogus')
129
130
131if __name__ == '__main__':
132  unittest.main()
133