def setUp(self): # Open non-persisted db self.__repository = Rates(':memory:', True) # Pre-register some exchange rates self.__knownRates = [('PSD', 2.0), ('USD', 1.6), ('RUB', 1.9), ('TRY', 0.7)] for (currency, rate) in self.__knownRates: self.__repository.setRate(currency, rate)
def setUp(self): server.server.config['TESTING'] = True self.app = server.server.test_client() rates = Rates(':memory:', True) server.converter = Converter(rates) rates.setRate('USD', 1.45) rates.setRate('TYR', 0.723) rates.setRate('KD', 1.45) rates.setRate('RTATATTATATA', 1.45)
def setUp(self): # Open non-persisted db repository = Rates(':memory:', True) # Pre-register some exchange rates self.__knownRates = [('PSD', 2.0), ('USD', 1.6), ('RUB', 1.9), ('TRY', 0.725)] for (currency, rate) in self.__knownRates: repository.setRate(currency, rate) self.__converter = Converter(repository)
from rates.rates import Rates from rates import downloader import sys if __name__ == "__main__": # Parse arguments db_file = sys.argv[1] if len(sys.argv) > 1 else "rates.pdl" # Download and store rates rates = Rates(db_file, True) for (currency, rate) in downloader.getExchangeRates(): print("Set exchange rate %s for 'EUR' -> '%s'" % (rate, currency)) rates.setRate(currency, rate) print("%s exchange rates have been retrieved and saved to %s" % (rates.getRatesCount(), db_file))
if not query: return make_error_response() value, origin_currency, target_currency = query try: converted_value = converter.convert_and_round(origin_currency, target_currency, value) except: return make_error_response() result = '%s %s = %s %s' % (value, origin_currency, converted_value, target_currency) return jsonify(answer = result) def make_error_response(): return jsonify(answer = "I' sorry Dave. I'm afraid. I can't do that"), '500 UnknownError' if __name__ == "__main__": # Parse arguments db_file = sys.argv[1] if len(sys.argv) > 1 else "rates.pdl" debug = '--debug' in sys.argv # Load data from db rates = Rates(db_file, False) print("%s exchange rates loaded from %s" % (rates.getRatesCount(), db_file)) # Create converter service converter = Converter(rates) # Start server server.run(debug=True)
class RatesTests(unittest.TestCase): def setUp(self): # Open non-persisted db self.__repository = Rates(':memory:', True) # Pre-register some exchange rates self.__knownRates = [('PSD', 2.0), ('USD', 1.6), ('RUB', 1.9), ('TRY', 0.7)] for (currency, rate) in self.__knownRates: self.__repository.setRate(currency, rate) def test_setNewRate(self): self.assertEqual(self.__repository.getRatesCount(), 4) self.__repository.setRate('RRR', 0.5) self.assertEqual(self.__repository.getRatesCount(), 5) def test_setInvalidRate(self): self.assertRaises(AssertionError, self.__repository.setRate, 'RRR', -0.5) self.assertRaises(AssertionError, self.__repository.setRate, 'RRR', 0.0) self.assertRaises(AssertionError, self.__repository.setRate, 'RRR', -0.0) def test_setExistingRate(self): self.assertEqual(self.__repository.getRatesCount(), 4) self.assertEqual(self.__repository.getRate('PSD'), 2.0) self.__repository.setRate('PSD', 0.1) self.assertEqual(self.__repository.getRatesCount(), 4) self.assertEqual(self.__repository.getRate('PSD'), 0.1) def test_getExistingRate(self): self.assertEqual(self.__repository.getRate('USD'), 1.6) def test_getUnknownRate(self): self.assertEqual(self.__repository.getRate('FYL'), None) def test_getAllRates(self): self.assertEqual(self.__repository.getAllRates(), self.__knownRates)