Example #1
0
 def test_value_greater_equal_than_value(self):
     val1 = Value(19, 0.4)
     val2 = Value(19, 0.5)
     val3 = Value(17, 0.5)
     self.assertTrue(val2 >= val1)
     self.assertTrue(val1 <= val2)
     self.assertTrue(val2 >= val3)
Example #2
0
 def test_can_add_values(self):
     val1 = Value(23, 5)
     val2 = Value(19, 1)
     val3 = val1 + val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 42)
     self.assertAlmostEqual(val3._error, 5.1, delta=0.05)
Example #3
0
 def test_error_requires_numbers(self):
     Value(23, 1)
     Value(23, 0.5)
     with self.assertRaises(TypeError):
         Value(23, "100")
     with self.assertRaises(TypeError):
         Value(23, True)
Example #4
0
 def test_value_requires_numbers(self):
     Value(23)
     Value(23.5)
     with self.assertRaises(TypeError):
         Value("100")
     with self.assertRaises(TypeError):
         Value(True)
Example #5
0
 def test_can_divide_negative_values(self, mock_err):
     mock_err.side_effect = (4, 3)
     val1 = Value(12, 48)
     val2 = Value(-4, 12)
     val3 = val1 / val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, -3)
     self.assertEqual(val3._error, 15)
Example #6
0
 def test_can_multiply_negative_values(self, mock_err):
     mock_err.side_effect = (0.025, 0.06)
     val1 = Value(-2, 0.08)
     val2 = Value(3, 0.05)
     val3 = val1 * val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, -6)
     self.assertAlmostEqual(val3._error, 0.39)
Example #7
0
 def test_can_get_consistency_with_non_value(self):
     val1 = Value(9.87, 0.09)
     val2 = 9.81
     self.assertTrue(val1.consistent_with(val2))
     val2 = 9.78
     self.assertTrue(val1.consistent_with(val2))
     val2 = 9.7799
     self.assertFalse(val1.consistent_with(val2))
Example #8
0
 def test_can_create_value_from_value(self):
     value = Mock(Value)
     value._value = 10
     value._error = 2
     val = Value(value)
     self.assertEqual(val._value, 10)
     self.assertEqual(val._error, 0)
Example #9
0
 def test_can_add_number_to_value(self):
     val1 = 23
     val2 = Value(19, 0.4)
     val3 = val1 + val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 42)
     self.assertEqual(val3._error, 0.4)
Example #10
0
 def test_can_subtract_value_from_number(self):
     val1 = Value(23, 0.5)
     val2 = 19.0
     val3 = val2 - val1
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, -4)
     self.assertEqual(val3._error, 0.5)
Example #11
0
 def test_can_add_value_to_number(self):
     val1 = Value(23, 0.5)
     val2 = 19.0
     val3 = val1 + val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 42)
     self.assertEqual(val3._error, 0.5)
Example #12
0
 def test_can_subtract_number_from_value(self):
     val1 = 23
     val2 = Value(19, 0.4)
     val3 = val2 - val1
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, -4)
     self.assertEqual(val3._error, 0.4)
Example #13
0
 def test_can_raise_value_to_negative_power(self, mock_err):
     mock_err.return_value = 0.01
     val1 = Value(2, 0.02)
     val = val1**-2
     self.assertIsInstance(val, Value)
     self.assertEqual(val._value, 0.25)
     self.assertEqual(val._error, 0.005)
Example #14
0
 def test_can_divide_value_by_number(self, mock_err):
     mock_err.return_value = 0.025
     val1 = Value(12, 48)
     val2 = 4.0
     val3 = val1 / val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 3)
     self.assertAlmostEqual(val3._error, 0.075, delta=0.000005)
Example #15
0
 def test_can_divide_number_by_value(self, mock_err):
     mock_err.return_value = 0.06
     val1 = 12
     val2 = Value(4, 12)
     val3 = val1 / val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 3)
     self.assertAlmostEqual(val3._error, 0.18, delta=0.000005)
Example #16
0
 def test_can_raise_value_to_power(self, mock_err):
     mock_err.return_value = 0.01
     val1 = Value(2, 0.02)
     val2 = 3
     val3 = val1**val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 8)
     self.assertEqual(val3._error, 0.24)
Example #17
0
 def test_can_multiply_number_with_value(self, mock_err):
     mock_err.return_value = 0.06
     val1 = 2
     val2 = Value(3, 0.05)
     val3 = val1 * val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 6)
     self.assertAlmostEqual(val3._error, 0.36)
Example #18
0
 def test_can_multiply_value_with_number(self, mock_err):
     mock_err.return_value = 0.025
     val1 = Value(2, 0.08)
     val2 = 3
     val3 = val1 * val2
     self.assertIsInstance(val3, Value)
     self.assertEqual(val3._value, 6)
     self.assertAlmostEqual(val3._error, 0.15)
Example #19
0
 def test_can_raise_negative_value_to_power(self, mock_err):
     mock_err.return_value = 0.01
     val1 = Value(-2, 0.02)
     val = val1**2
     self.assertIsInstance(val, Value)
     self.assertEqual(val._value, 4)
     self.assertEqual(val._error, 0.08)
     val = val1**3
     self.assertIsInstance(val, Value)
     self.assertEqual(val._value, -8)
     self.assertEqual(val._error, 0.24)
Example #20
0
 def test_repr_with_error(self):
     val = Value(23, 0.5)
     self.assertEqual(str(val), '23 ± 0.5')
Example #21
0
 def test_cannot_get_consistency_with_non_number(self):
     val1 = Value(9.87, 0.09)
     with self.assertRaises(TypeError):
         val1.consistent_with("value")
Example #22
0
 def test_can_create_simple_value(self):
     val = Value(10)
     self.assertEqual(val._value, 10)
     self.assertEqual(val._error, 0)
Example #23
0
 def test_repr_with_no_error(self):
     val = Value(23)
     self.assertEqual(str(val), '23')
Example #24
0
 def test_can_ignore_non_numbers(self):
     s = "value"
     val = Value.create(s, 0.2)
     self.assertIs(val, s)
Example #25
0
 def test_can_get_error_value_safely(self):
     val = Value.create(10.5, 0.2)
     self.assertIsInstance(val, Value)
     self.assertEqual(val._value, 10.5)
     self.assertEqual(val._error, 0.2)
Example #26
0
 def test_can_get_simple_value_safely(self):
     val = Value.create(10)
     self.assertIsInstance(val, Value)
     self.assertEqual(val._value, 10)
     self.assertEqual(val._error, 0)
Example #27
0
 def test_can_get_consistent_results(self):
     val1 = Value(3.3, 0.2)
     val2 = Mock(Value)
     val2.value.return_value = 3.1
     val2.error.return_value = 0.1
     self.assertTrue(val1.consistent_with(val2))
Example #28
0
 def test_can_get_inconsistent_results(self):
     val1 = Value(3.30001, 0.2)
     val2 = Mock(Value)
     val2.value.return_value = 3.0
     val2.error.return_value = 0.1
     self.assertFalse(val1.consistent_with(val2))
Example #29
0
 def test_can_get_error_range(self):
     val = Value(100, 2)
     self.assertEqual(val.error_range(), (98, 102))
Example #30
0
 def test_error_must_be_positive(self):
     with self.assertRaises(ValueError):
         Value(23, -0.01)