def testBasicUsage(): f = FractionValue(3, Fraction(5, 3)) assert f.number == 3 assert f.fraction == Fraction(5, 3) f.number = 5.5 f.fraction = Fraction(6, 5) assert f.number == 5.5 assert f.fraction == Fraction(6, 5) with pytest.raises(TypeError): f.SetNumber("hello") with pytest.raises(TypeError): f.SetFraction("hello") with pytest.raises(ValueError): f.SetFraction((1, 2, 3)) assert FractionValue(3).GetFraction() == Fraction(0, 1)
def ConvertFractionValue(cls, fraction_value, quantity, from_unit, to_unit): """ Converts the given fraction value to the given unit. :type fraction_value: L{FractionValue} :param fraction_value: the fraction value to convert :type quantity: IQuantity or str :param quantity: the IQuantity object to use in the conversion, or the quantity type itself :type from_unit: L{FractionValue} :param from_unit: current unit of the given fraction value :type to_unit: L{FractionValue} :param to_unit: the unit to convert the fraction value to :rtype: L{FractionValue} :returns: the converted fraction value """ # check if a quantity type str was passed if quantity.__class__ == str: # Note: actually ignoring the initial quantity type in this case because we # do the operation just using the from unit which may have any category (i.e. # the important thing is the quantity type, so, it can be created with the # default category). quantity = ObtainQuantity(from_unit) convert_to_quantity = ObtainQuantity(from_unit, quantity.GetComposingCategories()) converted_number = convert_to_quantity.ConvertScalarValue( fraction_value.GetNumber(), to_unit ) # convert the number result = FractionValue(number=converted_number) # convert fraction's numerator if fraction_value.GetFraction() is not None: converted_numerator = convert_to_quantity.ConvertScalarValue( fraction_value.GetFraction().numerator, to_unit ) converted_fraction = copy.copy(fraction_value.GetFraction()) converted_fraction.numerator = converted_numerator result.SetFraction(converted_fraction) return result
def testPartsArentNone(): """ FractionValue can't be initialized nor modified to have None as number or fraction part. """ with pytest.raises(TypeError): FractionValue(1, None) with pytest.raises(TypeError): FractionValue(None, (0 / 1)) with pytest.raises(TypeError): FractionValue(None, None) f = FractionValue(1, Fraction(0, 1)) with pytest.raises(TypeError): f.SetNumber(None) with pytest.raises(TypeError): f.SetFraction(None)