afscgap.test.test_convert
Tests for unit conversion.
(c) 2025 Regents of University of California / The Eric and Wendy Schmidt Center for Data Science and the Environment at UC Berkeley.
This file is part of afscgap released under the BSD 3-Clause License. See LICENSE.md.
1""" 2Tests for unit conversion. 3 4(c) 2025 Regents of University of California / The Eric and Wendy Schmidt Center 5for Data Science and the Environment at UC Berkeley. 6 7This file is part of afscgap released under the BSD 3-Clause License. See 8LICENSE.md. 9""" 10import unittest 11import unittest.mock 12 13import afscgap.convert 14 15 16class ConvertTests(unittest.TestCase): 17 18 def test_iso8601_regex_not_found(self): 19 self.assertIsNone( 20 afscgap.convert.ISO_8601_REGEX.match('07/16/2021 11:30:22') 21 ) 22 23 def test_iso8601_regex_found(self): 24 self.assertIsNotNone( 25 afscgap.convert.ISO_8601_REGEX.match('2021-07-16T11:30:22') 26 ) 27 28 def test_is_iso8601_success(self): 29 self.assertTrue(afscgap.convert.is_iso8601('2021-07-16T11:30:22')) 30 31 def test_is_iso8601_fail(self): 32 self.assertFalse(afscgap.convert.is_iso8601('07/16/2021 11:30:22')) 33 34 def test_convert_area(self): 35 self.assertAlmostEqual( 36 afscgap.convert.convert(123, 'km2', 'm2'), 37 123000000 38 ) 39 40 def test_unconvert_area(self): 41 self.assertAlmostEqual( 42 afscgap.convert.convert(123000000, 'm2', 'km2'), 43 123 44 ) 45 46 def test_convert_degrees(self): 47 self.assertAlmostEqual( 48 afscgap.convert.convert(123, 'dd', 'dd'), 49 123 50 ) 51 52 def test_convert_distance(self): 53 self.assertAlmostEqual( 54 afscgap.convert.convert(123, 'm', 'km'), 55 0.123 56 ) 57 58 def test_unconvert_distance(self): 59 self.assertAlmostEqual( 60 afscgap.convert.convert(123, 'km', 'm'), 61 123000 62 ) 63 64 def test_convert_temperature(self): 65 self.assertAlmostEqual( 66 afscgap.convert.convert(12, 'c', 'f'), 67 53.6 68 ) 69 70 def test_unconvert_temperature(self): 71 self.assertAlmostEqual( 72 afscgap.convert.convert(12, 'f', 'c'), 73 -11.111111111 74 ) 75 76 def test_convert_time(self): 77 self.assertAlmostEqual( 78 afscgap.convert.convert(123, 'hr', 'day'), 79 5.125 80 ) 81 82 def test_unconvert_time(self): 83 self.assertAlmostEqual( 84 afscgap.convert.convert(123, 'min', 'hr'), 85 2.05 86 ) 87 88 def test_convert_weight(self): 89 self.assertAlmostEqual( 90 afscgap.convert.convert(12, 'kg', 'g'), 91 12000 92 ) 93 94 def test_unconvert_weight(self): 95 self.assertAlmostEqual( 96 afscgap.convert.convert(12, 'g', 'kg'), 97 0.012 98 )
17class ConvertTests(unittest.TestCase): 18 19 def test_iso8601_regex_not_found(self): 20 self.assertIsNone( 21 afscgap.convert.ISO_8601_REGEX.match('07/16/2021 11:30:22') 22 ) 23 24 def test_iso8601_regex_found(self): 25 self.assertIsNotNone( 26 afscgap.convert.ISO_8601_REGEX.match('2021-07-16T11:30:22') 27 ) 28 29 def test_is_iso8601_success(self): 30 self.assertTrue(afscgap.convert.is_iso8601('2021-07-16T11:30:22')) 31 32 def test_is_iso8601_fail(self): 33 self.assertFalse(afscgap.convert.is_iso8601('07/16/2021 11:30:22')) 34 35 def test_convert_area(self): 36 self.assertAlmostEqual( 37 afscgap.convert.convert(123, 'km2', 'm2'), 38 123000000 39 ) 40 41 def test_unconvert_area(self): 42 self.assertAlmostEqual( 43 afscgap.convert.convert(123000000, 'm2', 'km2'), 44 123 45 ) 46 47 def test_convert_degrees(self): 48 self.assertAlmostEqual( 49 afscgap.convert.convert(123, 'dd', 'dd'), 50 123 51 ) 52 53 def test_convert_distance(self): 54 self.assertAlmostEqual( 55 afscgap.convert.convert(123, 'm', 'km'), 56 0.123 57 ) 58 59 def test_unconvert_distance(self): 60 self.assertAlmostEqual( 61 afscgap.convert.convert(123, 'km', 'm'), 62 123000 63 ) 64 65 def test_convert_temperature(self): 66 self.assertAlmostEqual( 67 afscgap.convert.convert(12, 'c', 'f'), 68 53.6 69 ) 70 71 def test_unconvert_temperature(self): 72 self.assertAlmostEqual( 73 afscgap.convert.convert(12, 'f', 'c'), 74 -11.111111111 75 ) 76 77 def test_convert_time(self): 78 self.assertAlmostEqual( 79 afscgap.convert.convert(123, 'hr', 'day'), 80 5.125 81 ) 82 83 def test_unconvert_time(self): 84 self.assertAlmostEqual( 85 afscgap.convert.convert(123, 'min', 'hr'), 86 2.05 87 ) 88 89 def test_convert_weight(self): 90 self.assertAlmostEqual( 91 afscgap.convert.convert(12, 'kg', 'g'), 92 12000 93 ) 94 95 def test_unconvert_weight(self): 96 self.assertAlmostEqual( 97 afscgap.convert.convert(12, 'g', 'kg'), 98 0.012 99 )
A class whose instances are single test cases.
By default, the test code itself should be placed in a method named 'runTest'.
If the fixture may be used for many test cases, create as many test methods as are needed. When instantiating such a TestCase subclass, specify in the constructor arguments the name of the test method that the instance is to execute.
Test authors should subclass TestCase for their own tests. Construction and deconstruction of the test's environment ('fixture') can be implemented by overriding the 'setUp' and 'tearDown' methods respectively.
If it is necessary to override the __init__ method, the base class __init__ method must always be called. It is important that subclasses should not change the signature of their __init__ method, since instances of the classes are instantiated automatically by parts of the framework in order to be run.
When subclassing TestCase, you can set these attributes:
- failureException: determines which exception will be raised when the instance's assertion methods fail; test methods raising this exception will be deemed to have 'failed' rather than 'errored'.
- longMessage: determines whether long messages (including repr of objects used in assert methods) will be printed on failure in addition to any explicit message passed.
- maxDiff: sets the maximum length of a diff in failure messages by assert methods using difflib. It is looked up as an instance attribute so can be configured by individual tests if required.