afscgap.test.test_model

Unit tests for data structures as part of afscgap.

(c) 2023 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"""
 2Unit tests for data structures as part of afscgap.
 3
 4(c) 2023 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
11
12import afscgap.model
13import afscgap.test.test_tools
14
15# pylint: disable=C0115, C0116
16
17
18class ModelTests(unittest.TestCase):
19
20    def test_get_opt_float_valid(self):
21        self.assertAlmostEquals(afscgap.model.get_opt_float('1.48E+002'), 148)
22
23    def test_get_opt_float_not_given(self):
24        self.assertIsNone(afscgap.model.get_opt_float(None))
25
26    def test_get_opt_float_na(self):
27        self.assertIsNone(afscgap.model.get_opt_float('NA'))
28
29    def test_get_opt_int_valid(self):
30        self.assertEquals(afscgap.model.get_opt_int('148'), 148)
31
32    def test_get_opt_int_not_given(self):
33        self.assertIsNone(afscgap.model.get_opt_int(None))
34
35    def test_get_opt_int_na(self):
36        self.assertIsNone(afscgap.model.get_opt_int('NA'))
37
38    def test_assert_float_present_true(self):
39        self.assertAlmostEquals(
40            afscgap.model.assert_float_present(1.23),
41            1.23
42        )
43
44    def test_assert_float_present_false(self):
45        with self.assertRaises(AssertionError):
46            afscgap.model.assert_float_present(None)
47
48    def test_assert_int_present_true(self):
49        self.assertEquals(
50            afscgap.model.assert_int_present(123),
51            123
52        )
53
54    def test_assert_int_present_false(self):
55        with self.assertRaises(AssertionError):
56            afscgap.model.assert_int_present(None)
class ModelTests(unittest.case.TestCase):
19class ModelTests(unittest.TestCase):
20
21    def test_get_opt_float_valid(self):
22        self.assertAlmostEquals(afscgap.model.get_opt_float('1.48E+002'), 148)
23
24    def test_get_opt_float_not_given(self):
25        self.assertIsNone(afscgap.model.get_opt_float(None))
26
27    def test_get_opt_float_na(self):
28        self.assertIsNone(afscgap.model.get_opt_float('NA'))
29
30    def test_get_opt_int_valid(self):
31        self.assertEquals(afscgap.model.get_opt_int('148'), 148)
32
33    def test_get_opt_int_not_given(self):
34        self.assertIsNone(afscgap.model.get_opt_int(None))
35
36    def test_get_opt_int_na(self):
37        self.assertIsNone(afscgap.model.get_opt_int('NA'))
38
39    def test_assert_float_present_true(self):
40        self.assertAlmostEquals(
41            afscgap.model.assert_float_present(1.23),
42            1.23
43        )
44
45    def test_assert_float_present_false(self):
46        with self.assertRaises(AssertionError):
47            afscgap.model.assert_float_present(None)
48
49    def test_assert_int_present_true(self):
50        self.assertEquals(
51            afscgap.model.assert_int_present(123),
52            123
53        )
54
55    def test_assert_int_present_false(self):
56        with self.assertRaises(AssertionError):
57            afscgap.model.assert_int_present(None)

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.
def test_get_opt_float_valid(self):
21    def test_get_opt_float_valid(self):
22        self.assertAlmostEquals(afscgap.model.get_opt_float('1.48E+002'), 148)
def test_get_opt_float_not_given(self):
24    def test_get_opt_float_not_given(self):
25        self.assertIsNone(afscgap.model.get_opt_float(None))
def test_get_opt_float_na(self):
27    def test_get_opt_float_na(self):
28        self.assertIsNone(afscgap.model.get_opt_float('NA'))
def test_get_opt_int_valid(self):
30    def test_get_opt_int_valid(self):
31        self.assertEquals(afscgap.model.get_opt_int('148'), 148)
def test_get_opt_int_not_given(self):
33    def test_get_opt_int_not_given(self):
34        self.assertIsNone(afscgap.model.get_opt_int(None))
def test_get_opt_int_na(self):
36    def test_get_opt_int_na(self):
37        self.assertIsNone(afscgap.model.get_opt_int('NA'))
def test_assert_float_present_true(self):
39    def test_assert_float_present_true(self):
40        self.assertAlmostEquals(
41            afscgap.model.assert_float_present(1.23),
42            1.23
43        )
def test_assert_float_present_false(self):
45    def test_assert_float_present_false(self):
46        with self.assertRaises(AssertionError):
47            afscgap.model.assert_float_present(None)
def test_assert_int_present_true(self):
49    def test_assert_int_present_true(self):
50        self.assertEquals(
51            afscgap.model.assert_int_present(123),
52            123
53        )
def test_assert_int_present_false(self):
55    def test_assert_int_present_false(self):
56        with self.assertRaises(AssertionError):
57            afscgap.model.assert_int_present(None)
Inherited Members
unittest.case.TestCase
TestCase
failureException
longMessage
maxDiff
addTypeEqualityFunc
addCleanup
enterContext
addClassCleanup
enterClassContext
setUp
tearDown
setUpClass
tearDownClass
countTestCases
defaultTestResult
shortDescription
id
subTest
run
doCleanups
doClassCleanups
debug
skipTest
fail
assertFalse
assertTrue
assertRaises
assertWarns
assertLogs
assertNoLogs
assertEqual
assertNotEqual
assertAlmostEqual
assertNotAlmostEqual
assertSequenceEqual
assertListEqual
assertTupleEqual
assertSetEqual
assertIn
assertNotIn
assertIs
assertIsNot
assertDictEqual
assertDictContainsSubset
assertCountEqual
assertMultiLineEqual
assertLess
assertLessEqual
assertGreater
assertGreaterEqual
assertIsNone
assertIsNotNone
assertIsInstance
assertNotIsInstance
assertRaisesRegex
assertWarnsRegex
assertRegex
assertNotRegex
failUnlessRaises
failIf
assertRaisesRegexp
assertRegexpMatches
assertNotRegexpMatches
failUnlessEqual
assertEquals
failIfEqual
assertNotEquals
failUnlessAlmostEqual
assertAlmostEquals
failIfAlmostEqual
assertNotAlmostEquals
failUnless
assert_