def initialize_db(path): """Initialize the app wide currency db if not already initialized.""" ratesdb = RatesDB(str(path)) ratesdb.register_rate_provider(default_currency_rate_provider) Currency.set_rates_db(ratesdb)
def set_ratedb_for_tests(async=False, slow_down_provider=False, provider=None): log = [] # Returns a RatesDB that isn't async and that uses a fake provider def fake_provider(currency, start_date, end_date): log.append((start_date, end_date, currency)) number_of_days = (end_date - start_date).days + 1 return [(start_date + timedelta(i), 1.42 + (.01 * i)) for i in range(number_of_days)] db = RatesDB(':memory:', async=async) if provider is None: provider = fake_provider if slow_down_provider: provider = slow_down(provider) db.register_rate_provider(provider) Currency.set_rates_db(db) return db, log def test_unknown_currency(): # Only known currencies are accepted. with raises(ValueError): Currency('FOO') def test_async_and_repeat(): # If you make an ensure_rates() call and then the same call right after (before the first one # is finished, the server will not be hit twice. db, log = set_ratedb_for_tests(async=True, slow_down_provider=True) db.ensure_rates(date(2008, 5, 20), ['USD']) db.ensure_rates(date(2008, 5, 20), ['USD']) jointhreads() eq_(len(log), 1)