Exemplo n.º 1
0
 def test_get_prefix_from_user_input_abbr_internal(self):
     # Set up prefix group
     test_repo = ConvertRepo()
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     prefix_name1 = "test_name1"
     prefix_abbr1 = "test_abbr1"
     prefix_mult1 = 1001
     test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1)
     prefix_name2 = "test_name2"
     prefix_abbr2 = "test_abbr2"
     prefix_mult2 = 1002
     test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2)
     prefix_group.add_prefix(test_prefix1)
     prefix_group.add_prefix(test_prefix2)
     # Set up test object
     test_type = ConvertType(test_repo, "test_type")
     test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1)
     test_unit_names = ["name1", "name2", "NaMe3"]
     test_value = 1337
     test_unit = ConvertUnit(test_type, test_unit_names, test_value)
     test_unit.add_abbr("ABbr1")
     test_unit.add_abbr("cubic {X}abbr2")
     test_unit.valid_prefix_group = prefix_group
     # Get prefix from input
     assert test_unit.get_prefix_from_user_input("cubic test_abbr1abbr2") == test_prefix1
     assert test_unit.get_prefix_from_user_input("cubic test_abbr1not_a_unit") is False
     assert test_unit.get_prefix_from_user_input("cubic not_a_prefixabbr2") is False
     assert test_unit.get_prefix_from_user_input("cubic abbr2") is None
     # Remove prefix group and see what happens
     test_unit.valid_prefix_group = None
     assert test_unit.get_prefix_from_user_input("cubic test_abbr1abbr2") is False
Exemplo n.º 2
0
 def test_json(self):
     # Set up stuff
     test_repo = ConvertRepo()
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     # Collapse to XML and rebuild
     test_json = prefix_group.to_json()
     rebuild_group = ConvertPrefixGroup.from_json(test_repo, test_json)
     assert rebuild_group.repo == test_repo
     assert len(rebuild_group.prefix_list) == 0
     assert rebuild_group.name == "test_group"
Exemplo n.º 3
0
class ConvertFunctionTestBase(TestBase, unittest.TestCase):

    def setUp(self):
        super().setUp()
        # Create test repo
        self.test_repo = ConvertRepo()
        self.test_type1 = ConvertType(self.test_repo, "test_type1")
        self.test_repo.add_type(self.test_type1)
        self.test_unit1a = ConvertUnit(self.test_type1, ["unit1a"], 1)
        self.test_type1.base_unit = self.test_unit1a
        self.test_unit1b = ConvertUnit(self.test_type1, ["unit1b", "same_name"], 2)
        self.test_unit1b.abbr_list = ["abbr1b", "abbr1bz"]
        self.test_type1.add_unit(self.test_unit1b)
        self.test_unit1c = ConvertUnit(self.test_type1, ["unit1c"], 4)
        self.test_unit1b.abbr_list = ["abbr1c"]
        self.test_type1.add_unit(self.test_unit1c)
        # Add a second type
        self.test_type2 = ConvertType(self.test_repo, "test_type2")
        self.test_repo.add_type(self.test_type2)
        self.test_unit2a = ConvertUnit(self.test_type2, ["unit2a"], 1)
        self.test_type2.base_unit = self.test_unit2a
        self.test_unit2b = ConvertUnit(self.test_type2, ["unit2b", "same_name"], 5)
        self.test_type2.add_unit(self.test_unit2b)
        # Add a prefix group
        self.test_group1 = ConvertPrefixGroup(self.test_repo, "test_group1")
        self.test_repo.add_prefix_group(self.test_group1)
        self.test_prefix1a = ConvertPrefix(self.test_group1, "prefix1a", "pref1a", 5)
        self.test_group1.add_prefix(self.test_prefix1a)
        self.test_prefix1b = ConvertPrefix(self.test_group1, "prefixb", "pref1b", 15)
        self.test_group1.add_prefix(self.test_prefix1b)
        # Add a second prefix group
        self.test_group2 = ConvertPrefixGroup(self.test_repo, "test_group2")
        self.test_repo.add_prefix_group(self.test_group2)
        self.test_prefix2a = ConvertPrefix(self.test_group2, "prefix2a", "pref2a", 7)
        self.test_group2.add_prefix(self.test_prefix2a)
        self.test_prefix2b = ConvertPrefix(self.test_group2, "prefixb", "pref2b", 42)
        self.test_group2.add_prefix(self.test_prefix2b)
        # Move current convert.json
        try:
            os.rename("store/convert.json", "store/convert.json.tmp")
        except OSError:
            pass
        # Put this test repo into the Convert object
        convert_function = self.function_dispatcher.get_function_by_name("convert")
        convert_function_obj = self.function_dispatcher.get_function_object(convert_function)  # type: Convert
        convert_function_obj.convert_repo = self.test_repo

    def tearDown(self):
        super().tearDown()
        # Put all the normal convert json back where it should be
        try:
            os.remove("store/convert.json")
        except OSError:
            pass
        try:
            os.rename("store/convert.json.tmp", "store/convert.json")
        except OSError:
            pass
Exemplo n.º 4
0
 def test_to_string(self):
     # Setup test objects
     test_repo = ConvertRepo()
     test_type = ConvertType(test_repo, "test_type")
     test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1)
     test_type.decimals = 3
     test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337)
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     test_prefix = ConvertPrefix(prefix_group, "ten", "10", 10)
     prefix_group.add_prefix(test_prefix)
     test_unit.valid_prefix_group = prefix_group
     measure1 = ConvertMeasure(17.5, test_unit)
     # Get string
     measure_str = measure1.to_string()
     # Check
     assert str(measure1) == measure_str
     assert measure_str == "1.750 tenname1"
Exemplo n.º 5
0
 def test_json_contents(self):
     # Set up stuff
     test_repo = ConvertRepo()
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     prefix_name1 = "test_name1"
     prefix_abbr1 = "test_abbr1"
     prefix_mult1 = 1001
     test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1)
     prefix_group.add_prefix(test_prefix1)
     prefix_name2 = "test_name2"
     prefix_abbr2 = "test_abbr2"
     prefix_mult2 = 1002
     test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2)
     prefix_group.add_prefix(test_prefix2)
     # Collapse to XML and rebuild
     test_json = prefix_group.to_json()
     rebuild_group = ConvertPrefixGroup.from_json(test_repo, test_json)
     assert rebuild_group.repo == test_repo
     assert len(rebuild_group.prefix_list) == 2
     assert rebuild_group.name == "test_group"
     count1 = 0
     count2 = 0
     for prefix in rebuild_group.prefix_list:
         assert prefix.prefix_group == rebuild_group
         assert prefix.prefix in [prefix_name1, prefix_name2]
         assert prefix.abbreviation in [prefix_abbr1, prefix_abbr2]
         assert prefix.multiplier in [prefix_mult1, prefix_mult2]
         count1 += prefix.prefix == prefix_name1
         count2 += prefix.prefix == prefix_name2
     assert count1 == 1
     assert count2 == 1
Exemplo n.º 6
0
 def test_build_list_from_user_input_prefix(self):
     # Setup test objects
     test_repo = ConvertRepo()
     test_type = ConvertType(test_repo, "test_type")
     test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1)
     test_repo.add_type(test_type)
     test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337)
     test_type.add_unit(test_unit)
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     test_prefix = ConvertPrefix(prefix_group, "ten", "10", 10)
     prefix_group.add_prefix(test_prefix)
     test_unit.valid_prefix_group = prefix_group
     # Run method
     data = ConvertMeasure.build_list_from_user_input(test_repo, "tenname2 27")
     # Check results
     assert len(data) == 1
     assert data[0].amount == 270
     assert data[0].unit == test_unit
Exemplo n.º 7
0
 def test_to_string_with_prefix(self):
     # Setup test objects
     test_repo = ConvertRepo()
     test_type = ConvertType(test_repo, "test_type")
     test_type.base_unit = ConvertUnit(test_type, ["base_unit"], 1)
     test_type.decimals = 3
     test_unit = ConvertUnit(test_type, ["name1", "name2"], 1337)
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     test_prefix1 = ConvertPrefix(prefix_group, "ten", "10", 10)
     test_prefix2 = ConvertPrefix(prefix_group, "hundred", "100", 100)
     prefix_group.add_prefix(test_prefix1)
     prefix_group.add_prefix(test_prefix2)
     test_unit.valid_prefix_group = prefix_group
     measure1 = ConvertMeasure(17.5, test_unit)
     # Get string
     measure_str = measure1.to_string_with_prefix(test_prefix2)
     # Check
     assert measure_str == "0.175 hundredname1"
Exemplo n.º 8
0
 def test_add_prefix(self):
     # Set up stuff
     test_repo = ConvertRepo()
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     prefix_name1 = "test_name1"
     prefix_abbr1 = "test_abbr1"
     prefix_mult1 = 1001
     test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1)
     prefix_name2 = "test_name2"
     prefix_abbr2 = "test_abbr2"
     prefix_mult2 = 1002
     test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2)
     # Add some prefixes and test
     assert len(prefix_group.prefix_list) == 0
     prefix_group.add_prefix(test_prefix1)
     assert len(prefix_group.prefix_list) == 1
     assert test_prefix1 in prefix_group.prefix_list
     prefix_group.add_prefix(test_prefix2)
     assert len(prefix_group.prefix_list) == 2
     assert test_prefix2 in prefix_group.prefix_list
Exemplo n.º 9
0
 def test_get_appropriate_prefix(self):
     # Set up stuff
     test_repo = ConvertRepo()
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     prefix_name1 = "test_name1"
     prefix_abbr1 = "test_abbr1"
     prefix_mult1 = 1000
     test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1)
     prefix_name2 = "test_name2"
     prefix_abbr2 = "test_abbr2"
     prefix_mult2 = 10
     test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2)
     prefix_name3 = "test_name3"
     prefix_abbr3 = "test_abbr3"
     prefix_mult3 = 0.01
     test_prefix3 = ConvertPrefix(prefix_group, prefix_name3, prefix_abbr3, prefix_mult3)
     prefix_group.add_prefix(test_prefix1)
     prefix_group.add_prefix(test_prefix2)
     prefix_group.add_prefix(test_prefix3)
     # Test things
     assert prefix_group.get_appropriate_prefix(2) is None
     assert prefix_group.get_appropriate_prefix(24) == test_prefix2
     assert prefix_group.get_appropriate_prefix(1234) == test_prefix1
     assert prefix_group.get_appropriate_prefix(0.5) == test_prefix3
Exemplo n.º 10
0
 def test_get_prefix_by_abbr(self):
     # Set up stuff
     test_repo = ConvertRepo()
     prefix_group = ConvertPrefixGroup(test_repo, "test_group")
     prefix_name1 = "test_name1"
     prefix_abbr1 = "test_abbr1"
     prefix_mult1 = 1001
     test_prefix1 = ConvertPrefix(prefix_group, prefix_name1, prefix_abbr1, prefix_mult1)
     prefix_name2 = "test_name2"
     prefix_abbr2 = "test_abbr2"
     prefix_mult2 = 1002
     test_prefix2 = ConvertPrefix(prefix_group, prefix_name2, prefix_abbr2, prefix_mult2)
     prefix_group.add_prefix(test_prefix1)
     prefix_group.add_prefix(test_prefix2)
     # Test things
     assert prefix_group.get_prefix_by_abbr(prefix_abbr1) == test_prefix1
     assert prefix_group.get_prefix_by_abbr(prefix_abbr2) == test_prefix2
     assert prefix_group.get_prefix_by_abbr(prefix_abbr1.upper()) == test_prefix1
     assert prefix_group.get_prefix_by_abbr(prefix_abbr2.upper()) == test_prefix2
     assert prefix_group.get_prefix_by_abbr(prefix_abbr1.capitalize()) == test_prefix1
     assert prefix_group.get_prefix_by_abbr(prefix_abbr2.capitalize()) == test_prefix2