Ejemplo n.º 1
0
class RatesFmtTest(unittest.TestCase):
    def setUp(self):
        self.wf = Workflow()
        self.wf.clear_settings()
        self.wf.clear_data()
        self.wf.clear_cache()
        self.wf.settings[rates.SETTINGS_DEFAULT_CURRENCY] = 'BRL'
        rates.log = self.wf.logger

    def tearDown(self):
        pass

    def testFmtNumberComma(self):
        self.wf.settings[rates.SETTINGS_DEFAULT_NUMBER_DIVISOR] = ','
        result = rates.format_result(self.wf, Decimal('1001.0123'))
        self.assertEquals(result, '1.001,0123')

    def testFmtNumberDot(self):
        self.wf.settings[rates.SETTINGS_DEFAULT_NUMBER_DIVISOR] = '.'
        result = rates.format_result(self.wf, Decimal('1001.0123'))
        self.assertEquals(result, '1,001.0123')
Ejemplo n.º 2
0
class TestRatesConvert(unittest.TestCase):
    def setUp(self):
        self.wf = Workflow()
        self.wf.clear_settings()
        self.wf.clear_data()
        self.wf.clear_cache()
        self.wf.settings[rates.SETTINGS_DEFAULT_CURRENCY] = 'BRL'
        rates.log = self.wf.logger

    def tearDown(self):
        pass

    def test_full_convert(self):
        with patch.object(sys, 'argv', ['program', '100', 'BRL', 'CLP']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('BRL (Brazilian real) -> CLP (Chilean peso) with rate'), -1)

    def test_inverted_full_convert(self):
        with patch.object(sys, 'argv', ['program', '100', 'CLP', 'BRL']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('CLP (Chilean peso) -> BRL (Brazilian real) with rate'), -1)

    def test_convert_from_default_to_other(self):
        with patch.object(sys, 'argv', ['program', '100', 'CLP']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('BRL (Brazilian real) -> CLP (Chilean peso) with rate'), -1)

    def test_convert_from_other_to_default(self):
        with patch.object(sys, 'argv', ['program', 'CLP', '100']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('CLP (Chilean peso) -> BRL (Brazilian real) with rate'), -1)

    def test_convert_from_default_to_other_without_values(self):
        self.wf.settings[rates.SETTINGS_DEFAULT_CURRENCY] = 'USD'

        with patch.object(sys, 'argv', ['program', 'BRL']):
            rates.main(self.wf)

        self.assertTrue(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('USD (United States dollar) -> BRL (Brazilian real)'), -1)

    def test_invalid_currency_number_to_convert(self):
        with patch.object(sys, 'argv', 'program zupa BRL USD'.split()):
            ret = rates.main(self.wf)
        self.assertTrue(ret, 100)

    def test_invalid_currency_number_to_convert_using_default(self):
        with patch.object(sys, 'argv', 'program zupa BRL'.split()):
            ret = rates.main(self.wf)
        self.assertTrue(ret, 100)

    def test_invalid_argument(self):
        with patch.object(sys, 'argv', 'program zupa'.split()):
            ret = rates.main(self.wf)
        # 100 means auto complete
        self.assertEqual(ret, 100)

    def test_convert_case_insensitive(self):
        with patch.object(sys, 'argv', 'program 1 brl clp'.split()):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('BRL (Brazilian real) -> CLP (Chilean peso) with rate'), -1)

    def test_convert_eur(self):
        # This test is because of a bug with displaying EUR symbol
        with patch.object(sys, 'argv', 'program 1 USD EUR'.split()):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('USD (United States dollar) -> EUR (Euro) with rate'), -1)

    def test_extract_filter_params(self):
        currencies = rates.get_currencies()

        # List of tuple (Params, Expected Result)

        test_itens = [('100 Braz'.split(), ['Braz']),
                      (['Braz'], ['Braz']),
                      ('100 CLP United States'.split(), ['United', 'States']),
                      ([''], []),
                      ([], [])]

        for test in test_itens:
            resp = rates.extract_filter_params(test[0], currencies)
            self.assertEqual(resp, test[1])
Ejemplo n.º 3
0
class RatesCurrencyTest(unittest.TestCase):
    def setUp(self):
        self.wf = Workflow()
        self.wf.clear_settings()
        self.wf.clear_data()
        self.wf.clear_cache()
        rates.log = self.wf.logger

    def tearDown(self):
        pass

    def testLoadCurrencyInfo(self):
        currency_info = rates.get_currencies()

        # Checks if all itens have all info that is used by the script
        for currency, info in currency_info.iteritems():
            self.assertIn('Id', info, 'No ID for currency {}'.format(info))
            self.assertTrue(info['Id'], 'None ID specified for currency {}'.format(info))
            self.assertIn('Name', info, 'No Name for currency {}'.format(info))
            self.assertTrue(info['Name'], 'No Name for currency {}'.format(info))
            self.assertIn('Code', info, 'No Code for currency {}'.format(info))
            self.assertTrue(info['Code'], 'No Code for currency {}'.format(info))
            self.assertIn('Simbol', info, 'No Simbol for currency {}'.format(info))
            self.assertTrue(info['Simbol'], 'No Simbol for currency {}'.format(info))
            self.assertIn('Country', info, 'No Country for currency {}'.format(info))
            self.assertTrue(info['Country'], 'No Country for currency {}'.format(info))
            self.assertIn('Flag', info, 'No Flag for currency {}'.format(info))

    def test_is_float(self):
        tests = [(1, True),
                 ('asd', False),
                 (1.5, True),
                 ('1', True),
                 ('1', True)]

        for test in tests:
            self.assertEqual(rates.is_float(test[0]), test[1])

    def test_validate_currencies(self):
        currencies = rates.get_currencies()
        self.assertTrue(rates.validate_currencies([], 'BRL', 'USD', currencies, self.wf))
        self.assertFalse(rates.validate_currencies([], 'BRL', 'USDD', currencies, self.wf))
        self.assertFalse(rates.validate_currencies([], 'BRLL', 'USD', currencies, self.wf))

    def test_clear_caches(self):
        self.wf.cache_data('test_cache', 'testing cache')
        self.wf.store_data('test_store', 'testing store')
        with patch.object(sys, 'argv', 'program --clear'.split()):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertEqual(self.wf._items[0].title, 'Caches cleared!')
        self.assertIsNone(self.wf.stored_data('test_store'))
        self.assertIsNone(self.wf.cached_data('test_cache'))

    def test_evaluate_math(self):

        tests = [
            (['100*3', '100'], ['300', '100']),
            (['135.3*2', '234.5-5'], ['270.6', '229.5']),
            (['123/2', '61.5*50'], ['61.5', '3075.0']),
            (['123.5*2.5'], ['308.75']),
            # (['123', '/2', '61.5*50'], ['61.5', '3075.0']),
            # (['123', '/', '2', '61.5*50'], ['61.5', '3075.0']),
            # (['123', '/2', '/', '2', '61.5*50'], ['30.75', '3075.0']),
            # (['123', '/2', '/', '2', '61.5*50', '/3', '*2'], ['30.75', '2050.0'])
            # (['123*', '2'], ['246'])
            # (['100*2', 'usd', 'brl'], ['200', 'usd', 'brl'])
        ]

        for t in tests:
            result = rates.evaluate_math(t[0])
            self.assertEqual(result, t[1])

    def test_fmt_number(self):
        tests = [
            ('100.00', '.', '100.00'),
            ('100.00', ',', '100,00'),
            ('1.000,00', '.', '1,000.00'),
            ('100', None, '100')
        ]

        for t in tests:
            result = rates.fmt_number(t[0], t[1])
            self.assertEqual(result, t[2])
class TestRatesConvert(unittest.TestCase):
    def setUp(self):
        self.wf = Workflow()
        self.wf.clear_settings()
        self.wf.clear_data()
        self.wf.clear_cache()
        self.wf.settings[rates.SETTINGS_DEFAULT_CURRENCY] = 'BRL'
        rates.log = self.wf.logger

    def tearDown(self):
        pass

    def test_full_convert(self):
        with patch.object(sys, 'argv', ['program', '100 BRL CLP']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Brazilian real) -> (Chilean peso) with rate'), -1)

    def test_full_convert_multi_query(self):
        with patch.object(sys, 'argv', ['program', '100 BRL CLP; 100 USD BRL; 100 GBP CAD']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 3)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Brazilian real) -> (Chilean peso) with rate'), -1)
        self.assertNotEqual(self.wf._items[1].subtitle.find('(United States dollar) -> (Brazilian real) with rate'), -1)
        self.assertNotEqual(self.wf._items[2].subtitle.find('(British pound [B]) -> (Canadian dollar) with rate'), -1)

    def test_inverted_full_convert(self):
        with patch.object(sys, 'argv', ['program', '100 CLP BRL']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Chilean peso) -> (Brazilian real) with rate'), -1)

    def test_convert_from_default_to_other(self):
        with patch.object(sys, 'argv', ['program', '100 CLP']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Brazilian real) -> (Chilean peso) with rate'), -1)

    def test_convert_from_default_to_other_multi_query(self):
        with patch.object(sys, 'argv', ['program', '100 BRL CLP; 100 GBP; 100 GBP CAD']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 3)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Brazilian real) -> (Chilean peso) with rate'), -1)
        self.assertNotEqual(self.wf._items[1].subtitle.find('(Brazilian real) -> (British pound [B]) with rate'), -1)
        self.assertNotEqual(self.wf._items[2].subtitle.find('(British pound [B]) -> (Canadian dollar) with rate'), -1)

    def test_convert_from_other_to_default(self):
        with patch.object(sys, 'argv', ['program', 'CLP 100']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Chilean peso) -> (Brazilian real) with rate'), -1)

    def test_convert_from_other_to_default_multi_query(self):
        with patch.object(sys, 'argv', ['program', '100 BRL CLP; GBP 100; 100 GBP CAD']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 3)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Brazilian real) -> (Chilean peso) with rate'), -1)
        self.assertNotEqual(self.wf._items[1].subtitle.find('(British pound [B]) -> (Brazilian real) with rate'), -1)
        self.assertNotEqual(self.wf._items[2].subtitle.find('(British pound [B]) -> (Canadian dollar) with rate'), -1)

    def test_convert_from_default_to_other_without_values(self):
        self.wf.settings[rates.SETTINGS_DEFAULT_CURRENCY] = 'USD'

        with patch.object(sys, 'argv', ['program', 'BRL']):
            rates.main(self.wf)

        self.assertTrue(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(United States dollar) -> (Brazilian real)'), -1)

    def test_convert_from_detault_to_other_without_values_multi_query(self):
        self.wf.settings[rates.SETTINGS_DEFAULT_CURRENCY] = 'USD'

        with patch.object(sys, 'argv', ['program', '100 BRL CLP; GBP; 100 GBP CAD']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 3)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Brazilian real) -> (Chilean peso) with rate'), -1)
        self.assertNotEqual(self.wf._items[1].subtitle.find('(United States dollar) -> (British pound [B]) with rate'), -1)
        self.assertNotEqual(self.wf._items[2].subtitle.find('(British pound [B]) -> (Canadian dollar) with rate'), -1)

    def test_invalid_currency_number_to_convert(self):
        with patch.object(sys, 'argv', ['program', 'zupa BRL USD']):
            ret = rates.main(self.wf)
        self.assertTrue(ret, 100)

    def test_invalid_currency_number_to_convert_using_default(self):
        with patch.object(sys, 'argv', ['program', 'zupa BRL']):
            ret = rates.main(self.wf)
        self.assertTrue(ret, 100)

    def test_invalid_argument(self):
        with patch.object(sys, 'argv', ['program', 'zupa']):
            ret = rates.main(self.wf)
        # 100 means auto complete
        self.assertEqual(ret, 100)

    def test_convert_case_insensitive(self):
        with patch.object(sys, 'argv', ['program', '1 brl clp']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Brazilian real) -> (Chilean peso) with rate'), -1)

    def test_convert_eur(self):
        # This test is because of a bug with displaying EUR symbol
        with patch.object(sys, 'argv', ['program', '1 USD EUR']):
            rates.main(self.wf)
        self.assertEqual(len(self.wf._items), 1)
        self.assertNotEqual(self.wf._items[0].subtitle.find('(United States dollar) -> (Euro) with rate'), -1)

    def test_extract_filter_params(self):
        currencies = rates.get_currencies()

        # List of tuple (Params, Expected Result)

        test_itens = [('100 Braz'.split(), ['Braz']),
                      (['Braz'], ['Braz']),
                      ('100 CLP United States'.split(), ['United', 'States']),
                      ([''], []),
                      ([], [])]

        for test in test_itens:
            resp = rates.extract_filter_params(test[0], currencies)
            self.assertEqual(resp, test[1])

    def test_convert_high_rates(self):
        # Test the problem reported in #14, converting from idr to usd
        # Yahoo will return as rate 0.0001, because it's the minimum
        # but the rate is something around 0.000007...
        with patch.object(sys, 'argv', ['program', '1 idr usd']):
                rates.main(self.wf)
        # must have only 1 item
        self.assertEqual(len(self.wf._items), 1)
        # must not be rate 0.0001 for query
        self.assertEqual(self.wf._items[0].subtitle.find('(Indonesian rupiah) -> (United States dollar) with rate 0.0001 for query'), -1)
        # must be a conversion message
        self.assertNotEqual(self.wf._items[0].subtitle.find('(Indonesian rupiah) -> (United States dollar) with rate'), -1)