| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Tests for date and time precision helpers.""" |
| 3 |
|
| 4 |
import decimal |
| 5 |
import unittest |
| 6 |
|
| 7 |
from dfdatetime import definitions |
| 8 |
from dfdatetime import precisions |
| 9 |
|
| 10 |
|
| 11 |
class DateTimePrecisionHelperTest(unittest.TestCase): |
| 12 |
"""Tests for the date time precision helper interface.""" |
| 13 |
|
| 14 |
def testCopyNanosecondsToFractionOfSecond(self): |
| 15 |
"""Tests the CopyNanosecondsToFractionOfSecond function.""" |
| 16 |
precision_helper = precisions.DateTimePrecisionHelper |
| 17 |
|
| 18 |
with self.assertRaises(NotImplementedError): |
| 19 |
precision_helper.CopyNanosecondsToFractionOfSecond(0) |
| 20 |
|
| 21 |
def testCopyToDateTimeString(self): |
| 22 |
"""Tests the CopyToDateTimeString function.""" |
| 23 |
precision_helper = precisions.DateTimePrecisionHelper |
| 24 |
|
| 25 |
with self.assertRaises(NotImplementedError): |
| 26 |
precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 0.5) |
| 27 |
|
| 28 |
|
| 29 |
class SecondsPrecisionHelperTest(unittest.TestCase): |
| 30 |
"""Tests for the seconds precision helper.""" |
| 31 |
|
| 32 |
def testCopyNanosecondsToFractionOfSecond(self): |
| 33 |
"""Tests the CopyNanosecondsToFractionOfSecond function.""" |
| 34 |
precision_helper = precisions.SecondsPrecisionHelper |
| 35 |
|
| 36 |
fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond(123456) |
| 37 |
self.assertEqual(fraction_of_second, 0.0) |
| 38 |
|
| 39 |
with self.assertRaises(ValueError): |
| 40 |
precision_helper.CopyNanosecondsToFractionOfSecond(-1) |
| 41 |
|
| 42 |
with self.assertRaises(ValueError): |
| 43 |
precision_helper.CopyNanosecondsToFractionOfSecond(1000000000) |
| 44 |
|
| 45 |
def testCopyToDateTimeString(self): |
| 46 |
"""Tests the CopyToDateTimeString function.""" |
| 47 |
precision_helper = precisions.SecondsPrecisionHelper |
| 48 |
|
| 49 |
date_time_string = precision_helper.CopyToDateTimeString( |
| 50 |
(2018, 1, 2, 19, 45, 12), 0.123456 |
| 51 |
) |
| 52 |
self.assertEqual(date_time_string, "2018-01-02 19:45:12") |
| 53 |
|
| 54 |
with self.assertRaises(ValueError): |
| 55 |
precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456) |
| 56 |
|
| 57 |
|
| 58 |
class CentisecondsPrevisionHelperTest(unittest.TestCase): |
| 59 |
"""Tests for the centiseconds prevision helper.""" |
| 60 |
|
| 61 |
def testCopyNanosecondsToFractionOfSecond(self): |
| 62 |
"""Tests the CopyNanosecondsToFractionOfSecond function.""" |
| 63 |
precision_helper = precisions.CentisecondsPrecisionHelper |
| 64 |
|
| 65 |
fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond( |
| 66 |
123456789 |
| 67 |
) |
| 68 |
self.assertEqual(fraction_of_second, decimal.Decimal("0.12")) |
| 69 |
|
| 70 |
with self.assertRaises(ValueError): |
| 71 |
precision_helper.CopyNanosecondsToFractionOfSecond(-1) |
| 72 |
|
| 73 |
with self.assertRaises(ValueError): |
| 74 |
precision_helper.CopyNanosecondsToFractionOfSecond(1000000000) |
| 75 |
|
| 76 |
def testCopyToDateTimeString(self): |
| 77 |
"""Tests the CopyToDateTimeString function.""" |
| 78 |
precision_helper = precisions.CentisecondsPrecisionHelper |
| 79 |
|
| 80 |
date_time_string = precision_helper.CopyToDateTimeString( |
| 81 |
(2018, 1, 2, 19, 45, 12), 0.123456 |
| 82 |
) |
| 83 |
self.assertEqual(date_time_string, "2018-01-02 19:45:12.12") |
| 84 |
|
| 85 |
with self.assertRaises(ValueError): |
| 86 |
precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456) |
| 87 |
|
| 88 |
|
| 89 |
class MillisecondsPrecisionHelperTest(unittest.TestCase): |
| 90 |
"""Tests for the milliseconds precision helper.""" |
| 91 |
|
| 92 |
def testCopyNanosecondsToFractionOfSecond(self): |
| 93 |
"""Tests the CopyNanosecondsToFractionOfSecond function.""" |
| 94 |
precision_helper = precisions.MillisecondsPrecisionHelper |
| 95 |
|
| 96 |
fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond( |
| 97 |
123456789 |
| 98 |
) |
| 99 |
self.assertEqual(fraction_of_second, decimal.Decimal("0.123")) |
| 100 |
|
| 101 |
with self.assertRaises(ValueError): |
| 102 |
precision_helper.CopyNanosecondsToFractionOfSecond(-1) |
| 103 |
|
| 104 |
with self.assertRaises(ValueError): |
| 105 |
precision_helper.CopyNanosecondsToFractionOfSecond(1000000000) |
| 106 |
|
| 107 |
def testCopyToDateTimeString(self): |
| 108 |
"""Tests the CopyToDateTimeString function.""" |
| 109 |
precision_helper = precisions.MillisecondsPrecisionHelper |
| 110 |
|
| 111 |
date_time_string = precision_helper.CopyToDateTimeString( |
| 112 |
(2018, 1, 2, 19, 45, 12), 0.123456 |
| 113 |
) |
| 114 |
self.assertEqual(date_time_string, "2018-01-02 19:45:12.123") |
| 115 |
|
| 116 |
with self.assertRaises(ValueError): |
| 117 |
precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456) |
| 118 |
|
| 119 |
|
| 120 |
class DeciMillisecondsPrevisionHelperTest(unittest.TestCase): |
| 121 |
"""Tests for the decimilliseconds precision helper.""" |
| 122 |
|
| 123 |
def testCopyNanosecondsToFractionOfSecond(self): |
| 124 |
"""Tests the CopyNanosecondsToFractionOfSecond function.""" |
| 125 |
precision_helper = precisions.DecimillisecondsPrecisionHelper |
| 126 |
|
| 127 |
fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond( |
| 128 |
123456789 |
| 129 |
) |
| 130 |
self.assertEqual(fraction_of_second, decimal.Decimal("0.1234")) |
| 131 |
|
| 132 |
with self.assertRaises(ValueError): |
| 133 |
precision_helper.CopyNanosecondsToFractionOfSecond(-1) |
| 134 |
|
| 135 |
with self.assertRaises(ValueError): |
| 136 |
precision_helper.CopyNanosecondsToFractionOfSecond(1000000000) |
| 137 |
|
| 138 |
def testCopyToDateTimeString(self): |
| 139 |
"""Tests the CopyToDateTimeString function.""" |
| 140 |
precision_helper = precisions.DecimillisecondsPrecisionHelper |
| 141 |
|
| 142 |
date_time_string = precision_helper.CopyToDateTimeString( |
| 143 |
(2018, 1, 2, 19, 45, 12), 0.123456 |
| 144 |
) |
| 145 |
self.assertEqual(date_time_string, "2018-01-02 19:45:12.1234") |
| 146 |
|
| 147 |
with self.assertRaises(ValueError): |
| 148 |
precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456) |
| 149 |
|
| 150 |
|
| 151 |
class MicrosecondsPrecisionHelperTest(unittest.TestCase): |
| 152 |
"""Tests for the microseconds precision helper.""" |
| 153 |
|
| 154 |
def testCopyNanosecondsToFractionOfSecond(self): |
| 155 |
"""Tests the CopyNanosecondsToFractionOfSecond function.""" |
| 156 |
precision_helper = precisions.MicrosecondsPrecisionHelper |
| 157 |
|
| 158 |
fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond( |
| 159 |
123456789 |
| 160 |
) |
| 161 |
self.assertEqual(fraction_of_second, decimal.Decimal("0.123456")) |
| 162 |
|
| 163 |
with self.assertRaises(ValueError): |
| 164 |
precision_helper.CopyNanosecondsToFractionOfSecond(-1) |
| 165 |
|
| 166 |
with self.assertRaises(ValueError): |
| 167 |
precision_helper.CopyNanosecondsToFractionOfSecond(1000000000) |
| 168 |
|
| 169 |
def testCopyToDateTimeString(self): |
| 170 |
"""Tests the CopyToDateTimeString function.""" |
| 171 |
precision_helper = precisions.MicrosecondsPrecisionHelper |
| 172 |
|
| 173 |
date_time_string = precision_helper.CopyToDateTimeString( |
| 174 |
(2018, 1, 2, 19, 45, 12), 0.123456 |
| 175 |
) |
| 176 |
self.assertEqual(date_time_string, "2018-01-02 19:45:12.123456") |
| 177 |
|
| 178 |
with self.assertRaises(ValueError): |
| 179 |
precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456) |
| 180 |
|
| 181 |
|
| 182 |
class NanosecondsPrecisionHelperTest(unittest.TestCase): |
| 183 |
"""Tests for the nanoseconds precision helper.""" |
| 184 |
|
| 185 |
def testCopyNanosecondsToFractionOfSecond(self): |
| 186 |
"""Tests the CopyNanosecondsToFractionOfSecond function.""" |
| 187 |
precision_helper = precisions.NanosecondsPrecisionHelper |
| 188 |
|
| 189 |
fraction_of_second = precision_helper.CopyNanosecondsToFractionOfSecond( |
| 190 |
123456789 |
| 191 |
) |
| 192 |
self.assertEqual(fraction_of_second, decimal.Decimal("0.123456789")) |
| 193 |
|
| 194 |
with self.assertRaises(ValueError): |
| 195 |
precision_helper.CopyNanosecondsToFractionOfSecond(-1) |
| 196 |
|
| 197 |
with self.assertRaises(ValueError): |
| 198 |
precision_helper.CopyNanosecondsToFractionOfSecond(1000000000) |
| 199 |
|
| 200 |
def testCopyToDateTimeString(self): |
| 201 |
"""Tests the CopyToDateTimeString function.""" |
| 202 |
precision_helper = precisions.NanosecondsPrecisionHelper |
| 203 |
|
| 204 |
date_time_string = precision_helper.CopyToDateTimeString( |
| 205 |
(2018, 1, 2, 19, 45, 12), 0.123456789 |
| 206 |
) |
| 207 |
self.assertEqual(date_time_string, "2018-01-02 19:45:12.123456789") |
| 208 |
|
| 209 |
with self.assertRaises(ValueError): |
| 210 |
precision_helper.CopyToDateTimeString((2018, 1, 2, 19, 45, 12), 4.123456789) |
| 211 |
|
| 212 |
|
| 213 |
class PrecisionHelperFactoryTest(unittest.TestCase): |
| 214 |
"""Tests for the date time precision helper factory.""" |
| 215 |
|
| 216 |
def testCreatePrecisionHelper(self): |
| 217 |
"""Tests the CreatePrecisionHelper function.""" |
| 218 |
precision_helper = precisions.PrecisionHelperFactory.CreatePrecisionHelper( |
| 219 |
definitions.PRECISION_1_MICROSECOND |
| 220 |
) |
| 221 |
|
| 222 |
self.assertIsNotNone(precision_helper) |
| 223 |
|
| 224 |
with self.assertRaises(ValueError): |
| 225 |
precisions.PrecisionHelperFactory.CreatePrecisionHelper("bogus") |
| 226 |
|
| 227 |
|
| 228 |
if __name__ == "__main__": |
| 229 |
unittest.main() |