コード例 #1
0
class TestConvert(unittest.TestCase):
    """Tests the convert method."""
    def setUp(self):
        self.converter = CurrencyConverter()
        self.rates = {"USD": 0.87, "EUR": 1, "CZK": 0.04}

    @patch("src.CurrencyConverter.CurrencyConverter.call_rates_api")
    def test_single_conversion(self, mock_rates):
        """Test conversion 1:1."""
        mock_rates.return_value = self.rates

        conversion = self.converter.convert(1, "USD", "EUR")
        expected = {
            "input": {
                "amount": 1,
                "currency": "USD"
            },
            "output": {
                "EUR": 0.87
            }
        }
        self.assertDictEqual(conversion, expected)

        conversion = self.converter.convert(1, "CZK", "USD")
        expected = {
            "input": {
                "amount": 1,
                "currency": "CZK"
            },
            "output": {
                "USD": 0.05
            }
        }
        self.assertDictEqual(conversion, expected)

    @patch("src.CurrencyConverter.CurrencyConverter.call_rates_api")
    def test_all_conversion(self, mock_rates):
        """Test conversion 1:ALL."""
        mock_rates.return_value = self.rates

        conversion = self.converter.convert(1, "USD")
        expected = {
            "input": {
                "amount": 1,
                "currency": "USD"
            },
            "output": {
                "USD": 1,
                "EUR": 0.87,
                "CZK": 21.75
            },
        }
        self.assertDictEqual(conversion, expected)
コード例 #2
0
class TestParseSymbol(unittest.TestCase):
    """Tests the parse_symbol method."""
    def setUp(self):
        self.converter = CurrencyConverter()

    def test_symbols(self):
        self.assertEqual(self.converter.parse_symbol("$"), "USD")
        self.assertEqual(self.converter.parse_symbol("€"), "EUR")
        self.assertEqual(self.converter.parse_symbol("Kč"), "CZK")
        self.assertEqual(self.converter.parse_symbol("£"), "GBP")
        self.assertEqual(self.converter.parse_symbol("¥"), "CNY")
        self.assertEqual(self.converter.parse_symbol("₽"), "RUB")

    def test_not_symbols(self):
        not_symbol = ["USD", "1x", ".23"]
        for ns in not_symbol:
            self.assertEqual(self.converter.parse_symbol(ns), ns)
コード例 #3
0
 def setUp(self):
     self.converter = CurrencyConverter()
     self.rates = {"USD": 0.87, "EUR": 1, "CZK": 0.04}
コード例 #4
0
 def setUp(self):
     self.converter = CurrencyConverter()
コード例 #5
0
class TestCalculateConversion(unittest.TestCase):
    """Tests the test_conversion method."""
    def setUp(self):
        self.converter = CurrencyConverter()
        # rates relative to EUR, actual on 11.1.2019
        self.USD_rate = 0.87  # 1USD = 0.86764 EUR
        self.EUR_rate = 1
        self.CZK_rate = 0.04

    def test_conversion_trivial(self):
        """Test trivial conversions to EUR."""
        self.assertAlmostEqual(
            self.converter.calculate_conversion(1, self.USD_rate,
                                                self.EUR_rate),
            self.USD_rate,
        )
        self.assertAlmostEqual(
            self.converter.calculate_conversion(1, self.CZK_rate,
                                                self.EUR_rate),
            self.CZK_rate,
        )

    def test_conversion_reverse(self):
        """Test reverse conversions."""
        amount = 1
        conversion = self.converter.calculate_conversion(
            amount, self.USD_rate, self.CZK_rate)
        reverse_conversion = self.converter.calculate_conversion(
            conversion, self.CZK_rate, self.USD_rate)
        self.assertAlmostEqual(reverse_conversion, amount)

        amount = 5.2
        conversion = self.converter.calculate_conversion(
            amount, self.EUR_rate, self.CZK_rate)
        reverse_conversion = self.converter.calculate_conversion(
            conversion, self.CZK_rate, self.EUR_rate)
        self.assertAlmostEqual(reverse_conversion, amount)

    def test_conversion_explicit(self):
        """Tests conversion with explicit values."""
        usd_to_eur = {
            3.21: 2.79,
            6.12: 5.32,
            5.18: 4.51,
            658.33: 572.75,
            0.12: 0.1
        }
        for amount, expected in usd_to_eur.items():
            self.assertAlmostEqual(
                self.converter.calculate_conversion(amount, self.USD_rate,
                                                    self.EUR_rate),
                expected,
            )

        czk_to_usd = {
            3.21: 0.15,
            6.12: 0.28,
            5.18: 0.24,
            658.33: 30.27,
            0.12: 0.01
        }
        for amount, expected in czk_to_usd.items():
            self.assertAlmostEqual(
                self.converter.calculate_conversion(amount, self.CZK_rate,
                                                    self.USD_rate),
                expected,
            )
コード例 #6
0
 def setUp(self):
     self.converter = CurrencyConverter()
     # rates relative to EUR, actual on 11.1.2019
     self.USD_rate = 0.87  # 1USD = 0.86764 EUR
     self.EUR_rate = 1
     self.CZK_rate = 0.04
コード例 #7
0
 def test_construct_5(self):
     """Rates file is set and exists."""
     try:
         CurrencyConverter(app_id, 'file', symbols_filepath, rates_filepath)
     except Exception, e:
         self.fail(e)
コード例 #8
0
 def test_construct_6(self):
     """Rates file is missing and rates_rad_mode is set to 'api'."""
     try:
         CurrencyConverter(app_id, 'api', symbols_filepath)
     except Exception, e:
         self.fail(e)
コード例 #9
0
 def test_construct_3(self):
     """If currency symbols file does not exist, raise an exception."""
     with self.assertRaises(IOError) as context:
         CurrencyConverter(app_id, 'file', 'XYZ.XXX', rates_filepath)
     self.assertTrue(3 in context.exception)
コード例 #10
0
 def test_construct_4(self):
     """If invalid rates read mode is entered, raise an exception."""
     with self.assertRaises(ValueError) as context:
         CurrencyConverter(app_id, 'fXXXile', symbols_filepath,
                           rates_filepath)
     self.assertTrue(4 in context.exception)
コード例 #11
0
 def test_construct_2(self):
     """If rates file is missing, but rates_read_mode is not set to 'api', raise an exception."""
     with self.assertRaises(ValueError) as context:
         CurrencyConverter(app_id, 'file', symbols_filepath)
     self.assertTrue(2 in context.exception)
コード例 #12
0
 def test_construct_1(self):
     """If rates file is set, but does not exist, raise an exception."""
     with self.assertRaises(IOError) as context:
         CurrencyConverter(app_id, 'file', symbols_filepath, 'XYZ.XXX')
     self.assertTrue(1 in context.exception)
コード例 #13
0
 def setUp(self):
     """Create an object which will be used for testing convert() method."""
     self.c_converter = CurrencyConverter(app_id, 'file_no_update',
                                          symbols_filepath, rates_filepath)
コード例 #14
0
from src.CurrencyConverter import CurrencyConverter

# Get directory of the file.
current_dir = os.path.dirname(os.path.realpath(__file__))

# Get API key from config file.
with open(os.path.abspath(current_dir+'/config.txt')) as config_file:
    app_id = config_file.read().strip()

# Set filepaths
rates_filepath = os.path.abspath(current_dir+'/rates_files/latest.json')
symbols_filepath = os.path.abspath(current_dir+'/cur_symbols/currency_symbols.txt')

# Create the main object.
converter = CurrencyConverter(app_id, 'api', symbols_filepath, rates_filepath)


# If the file was imported (as module).
def convert_money(amount, input_cur, output_cur=None):
    return converter.convert(amount, input_cur, output_cur)

# If the file was run directly (as script).
if __name__ == '__main__':
    # Module required for command line parsing.
    import argparse

    # Save terminal encoding
    terminal_encoding = sys.stdin.encoding

    # Parse the incoming arguments
コード例 #15
0
ファイル: testing.py プロジェクト: jontesek/python-master
#import currency_converter
#print(currency_converter.convert_money(10, '£', '€'))
#exit()

# Get API key from config file
with open('config.txt') as config_file:
    app_id = config_file.read().strip()

# Filepaths
current_dir = os.path.dirname(os.path.realpath(__file__))
rates_filepath = os.path.abspath(current_dir+'/rates_files/latest.json')
symbols_filepath = os.path.abspath(current_dir+'/cur_symbols/currency_symbols.txt')

# The main object
converter = CurrencyConverter(app_id, 'file', symbols_filepath, rates_filepath)

# Codes test
#result = converter.convert(10, 'EUR', 'CZK')
#result = converter.convert(10, 'EUR', 'EFKA')
#result = converter.convert(10, 'EUR')
#result = converter.convert(100, 'CZK', 'USD')

# Symbols test
#result = converter.convert(10, '€', 'CZK')
#result = converter.convert(111.88, 'CZK', '€')
#result = converter.convert(111, 'Kč', '€')
#result = converter.convert(500.5, '¥', '$')
result = converter.convert("\t1000 ", ' ৳\t', '\tzł ')
#result = converter.convert(10.04, '€', 'SFr.')