Example #1
0
    def test_sub_known_Values(self):
        #subtraction should give known result with known input
        #__rsub__ should give the negative of __sub__
        #the units of the results should be the same as the units of the calling instance

        known_sub_Values=(('1m', '5m',-4), ('1cm', '1cm',0), ('1cm', '5m',-499.0),
                          ('7km', '1m',6.999))

        for q1,q2, sum in known_sub_Values:
            x = PhysicalQuantity(q1)
            y = PhysicalQuantity(q2)
            self.assertEqual((x-y).value, sum)
            self.assertEqual((x-y).unit, x.unit)
            self.assertEqual(x.__rsub__(y).value, -sum)
            self.assertEqual(x.__rsub__(y).unit, x.unit)

        #test for error if incompatible units
        q1 = PhysicalQuantity('1cm')
        q2 = PhysicalQuantity('1kg')

        try:
            q1 - q2
        except TypeError as err:
            self.assertEqual(str(err), "Incompatible units")
        else:
            self.fail("expecting TypeError")

        #test offset units
        q1 = PhysicalQuantity('1degK')
        q2 = PhysicalQuantity('1degR')
        q3 = PhysicalQuantity('1degC')

        result = q1 - q2
        self.assertAlmostEqual(result.value, .444,3)
        self.assertEqual(result.unit, q1.unit)

        try:
            q3 - q2
        except TypeError as err:
            msg = "Unit conversion (degR to degC) cannot be expressed as a simple multiplicative factor"
            self.assertEqual(str(err), msg)
        else:
            self.fail('expecting TypeError')