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
# 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
    # example 1: ./currency_converter.py --amount 100.0 --input_currency EUR --output_currency CZK
    # example 2: ./currency_converter.py --amount 0.9 --input_currency ¥ --output_currency AUD
    parser = argparse.ArgumentParser(description='Currency Converter in Python')
    parser.add_argument('--amount', action='store', help='Amount of money to convert',
                        metavar='<float>', required=True)
    parser.add_argument('--input_currency', action='store', help='From currency',
                        metavar='<3 letter currency code or currency symbol>', required=True)
    parser.add_argument('--output_currency', action='store',
                        help='To currency | If missing, to all currencies.',
                        metavar='<3 letter currency code or currency symbol>')
    args = parser.parse_args()

    # Calculate the result.
    try:
        result = converter.convert(args.amount, args.input_currency, args.output_currency, terminal_encoding)
    except Exception, e:
        raise SystemExit(e)

    # Show JSON string
    print(result)
예제 #3
0
#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.')

# JSON
print(result)