Example #1
0
def testSetFloatValue():
    """
    The fraction scalar should accept a float-convertible value. Since the fraction is a subclass
    from scalar, it should be able to respect the same interface.
    """
    f = units.FractionScalar("length", value=FractionValue(0), unit="in")
    f = f.CreateCopy(value=0.75)
    assert f.GetValue("in") == FractionValue(0.75)
Example #2
0
def testStr():
    f = FractionValue(3, Fraction(5, 3))
    assert str(f) == "3 5/3"
    assert repr(f) == "FractionValue(3, 5/3)"

    f = FractionValue(3)
    assert str(f) == "3"
    assert repr(f) == "FractionValue(3, 0/1)"
Example #3
0
def testCopy():
    f = FractionValue(3, (5, 3))
    cf = copy.copy(f)
    assert f == cf
    assert f is not cf

    cf.fraction.numerator = 10
    cf.fraction.denominator = 4
    assert f == FractionValue(3, (5, 3))
Example #4
0
def testFractionScalarConversion():
    db = units.UnitDatabase()
    db.AddUnit("length", "milimeters", "mm", "%f * 1000.0", "%f / 1000.0")
    db.AddUnitBase("length", "meters", "m")

    f = units.FractionScalar("length",
                             value=FractionValue(3, (1, 2)),
                             unit="m")

    converted = db.Convert("length", "m", "mm", f.value)
    assert converted == FractionValue(3500)
Example #5
0
def testComparison():

    f1 = units.FractionScalar("length", value=FractionValue(10), unit="in")
    f3 = units.FractionScalar("volume", value=FractionValue(4), unit="m3")
    with pytest.raises(TypeError):
        f1 < f3

    f2 = units.FractionScalar("length", unit="in")
    f1 = f1.CreateCopy(value=FractionValue(250, (1, 2)), unit="m")
    f2 = f2.CreateCopy(value=FractionValue(220, (3, 4)), unit="m")

    assert f1 > f2
    assert f2 < f1
Example #6
0
def testFractionScalarWithDefaultValueOnCategory(unit_database_len):
    """
    FractionScalar is not considering the default value from category on initialization
    """
    db = unit_database_len

    db.AddCategory("my length",
                   "length",
                   default_value=FractionValue(5, (1, 2)))

    scalar = units.FractionScalar("my length", unit="m")
    assert scalar.GetValue("m") == FractionValue(5, (1, 2))
    assert scalar.GetUnit() == "m"
    assert scalar.GetFormatted() == "5 1/2 [m]"
Example #7
0
    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
Example #8
0
    def _InternalCreateWithQuantity(self, quantity, value, unit_database=None):
        """
        For internal use only. Is used to initialize the actual quantity.

        :type quantity: str or IQuantity
        :param quantity:
            The quantity of this scalar.

        :param FractionValue value:
            The initial value
        """
        # Considering fraction values values are easily coerced from float values (though it is
        # important to note the opposite is not true) if the input value is not a fraction already
        # try to convert value to float. This also makes this subclass SetValue interface compatible
        # with superclass interface.
        try:
            if type(value) != FractionValue:
                value = FractionValue(number=float(value))
        except Exception:
            # If not a fraction and coercion to float fails, use CheckType to provide a better error
            # message.
            CheckType(value, (FractionValue, float))

        self._value = value
        self._quantity = quantity
        self._unit_database = unit_database or UnitDatabase.GetSingleton()
Example #9
0
 def ConvertFractionScalar(db_unit, quantity_type, from_unit, to_unit,
                           value):
     """
     Converts the given Fraction Scalar by applying the converts method of this class.
     """
     converted = cls.ConvertFractionValue(value, quantity_type,
                                          from_unit, to_unit)
     return FractionValue(number=float(converted))
Example #10
0
def testConvertFractionValues(unit_database_custom_conversion) -> None:
    """
    Test unit database handling fraction values.
    """
    db = unit_database_custom_conversion

    # Converting half meter to cm
    value = FractionValue(0, (1, 2))

    value = db.Convert("length", "m", "cm", value)
    assert float(value) == 50
Example #11
0
def testRegisterTwoFunctionsForTheSameClass(unit_database_custom_conversion):
    """
    Test the behavior when we attempt to register two convert functions for the same class
    """
    def ConvertFunction1(*args, **kwargs):
        return 0

    db = unit_database_custom_conversion
    # There is a function specialized in convert fraction values. Attempting to register another
    # function should raise an error

    value = FractionValue(0, (1, 2))
    db.Convert("length", "m", "cm", value)

    with pytest.raises(AssertionError):
        db.RegisterAdditionalConversionType(FractionValue, ConvertFunction1)
Example #12
0
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)
Example #13
0
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)
Example #14
0
def testFractionScalarInvalidValue(unit_database_len):
    db = unit_database_len

    db.AddCategory("another-length", "length", min_value=5, max_value=15)

    scalar = units.FractionScalar("another-length",
                                  value=FractionValue(1, (1, 5)),
                                  unit="m")
    assert not scalar.IsValid()
    with pytest.raises(ValueError):
        scalar.CheckValidity()

    # By default the validation will be performed, 10 is a valid value
    scalar = scalar.CreateCopy(value=FractionValue(10))
    assert scalar.IsValid()

    # Even invalid ,the scalar returns the value, unit and a formatted text.
    another = units.FractionScalar("another-length",
                                   value=FractionValue(3000),
                                   unit="m")
    assert not another.IsValid()
    assert another.GetValue("m") == FractionValue(3000)
    assert another.GetUnit() == "m"
    assert another.GetFormatted() == "3000 [m]"

    # By default the validation will be performed, and in this cases will raise ValueError.
    another_2 = scalar.CreateCopy(value=FractionValue(5000))
    assert not another_2.IsValid()

    another_3 = units.FractionScalar("another-length",
                                     unit="m",
                                     value=FractionValue(5000))
    assert not another_3.IsValid()

    # Performing copy between invalid fraction scalars. The validation is not performed on copy.
    copied = another.Copy()
    assert not copied.IsValid()
    assert copied.GetValue("m") == FractionValue(3000)
    assert copied.GetUnit() == "m"
    assert copied.GetFormatted() == "3000 [m]"
Example #15
0
 def AssertCreateFromString(text, whole, fraction=None):
     assert (FractionValue.CreateFromString(text) == FractionValue(
         whole, fraction) if fraction is not None else FractionValue(whole))
Example #16
0
def testDefault():
    f = FractionValue()
    assert f.number == 0.0
    assert f.fraction == Fraction(0, 1)
Example #17
0
def testEquality():
    assert FractionValue(3, Fraction(5, 3)) == FractionValue(3, Fraction(5, 3))
    assert not FractionValue(3, Fraction(5, 3)) != FractionValue(
        3, Fraction(5, 3))

    assert FractionValue(3) == FractionValue(3)
    assert not FractionValue(3) != FractionValue(3)

    assert FractionValue(10, Fraction(5, 3)) != FractionValue(
        3, Fraction(5, 3))
    assert not FractionValue(10, Fraction(5, 3)) == FractionValue(
        3, Fraction(5, 3))

    assert FractionValue(10, (5, 3)) == FractionValue(10, Fraction(5, 3))
Example #18
0
def testFormatValue():
    f = units.FractionScalar("length",
                             value=FractionValue(250.0, (3, 4)),
                             unit="m")
    assert f.GetFormattedValue() == "250 3/4"
Example #19
0
def testFractionScalar():
    # create our scalar
    f = units.FractionScalar("length",
                             value=FractionValue(250, (3, 4)),
                             unit="m")

    # check formatting
    assert f.GetFormatted() == "250 3/4 [m]"

    # check conversion
    # f.unit = 'km'
    assert f.GetValue("km") == FractionValue(0.25, (3 / 1000.0, 4))
    assert f.GetValue("m") == FractionValue(250.0, (3, 4))

    # test no fraction part
    f = f.CreateCopy(value=FractionValue(0.25), unit="km")
    assert f.value == FractionValue(0.25)
    assert f.GetFormatted() == "0.25 [km]"

    # set fraction again
    f = f.CreateCopy(value=FractionValue(250, (3, 4)), unit="m")
    assert f.GetValue("km") == FractionValue(0.25, (3 / 1000.0, 4))
    assert f.GetFormatted() == "250 3/4 [m]"
    assert f.GetValue("m") == FractionValue(250, (3, 4))

    f = f.CreateCopy(value=FractionValue(0.25, (3, 4)), unit="km")
    assert f.value == FractionValue(0.25, (3, 4))
    assert f.GetFormatted() == "0.25 3/4 [km]"
    assert f.GetValue("m") == FractionValue(250.0, (3000, 4))

    with pytest.raises(AttributeError):
        setattr(f, "value", 10)
Example #20
0
def testRepr():
    assert repr(FractionValue(250, (3, 4))) == "FractionValue(250, 3/4)"
Example #21
0
 def Create(number, fraction):
     return units.FractionScalar("length",
                                 value=FractionValue(number, fraction),
                                 unit="m")
Example #22
0
def testCopy():
    f = units.FractionScalar("length",
                             value=FractionValue(250.0, (3, 4)),
                             unit="m")
    c = copy.copy(f)
    assert c.value == FractionValue(250.0, (3, 4))
Example #23
0
def testComparison():
    assert FractionValue(3) < FractionValue(3, (3, 4))
    assert FractionValue(3) <= FractionValue(3, (3, 4))
    assert FractionValue(3, (3, 4)) > FractionValue(3)
    assert FractionValue(3, (3, 4)) >= FractionValue(3)
Example #24
0
def testMatchFractionPart():
    # Text Ok, should not raise error
    FractionValue.MatchFractionPart("3/4")
    with pytest.raises(ValueError):
        FractionValue.MatchFractionPart("2 3/4")
Example #25
0
def testCreateFromFloat():
    """
    Allow the user enter only the fraction value
    """
    assert FractionValue.CreateFromFloat(1.375) == FractionValue(1, (3, 8))
    assert FractionValue.CreateFromFloat(2.5) == FractionValue(2, (1, 2))
Example #26
0
def testFloat():
    assert float(FractionValue(3, Fraction(5, 3))) == 3 + 5 / 3.0
    assert float(FractionValue(3)) == 3.0