def import_fund(code, from_date, to_date): """Imports fund data from KOFIA. :param code: e.g., KR5223941018 :param from_date: e.g., 2016-01-01 :param to_date: e.g., 2016-02-28 """ provider = Kofia() app = create_app(__name__) with app.app_context(): asset = get_asset_by_fund_code(code) # FIXME: Target asset should also be determined by asset.data.code base_asset = Asset.query.filter_by(name="KRW").first() data = provider.fetch_data(code, parse_date(from_date), parse_date(to_date)) for date, unit_price, quantity in data: log.info("Import data on {}", date) unit_price /= 1000.0 try: AssetValue.create( asset=asset, base_asset=base_asset, evaluated_at=date, close=unit_price, granularity=Granularity.day, source="kofia", ) except IntegrityError: log.warn("Identical record has been found for {}. Skipping.", date) db.session.rollback()
def import_fund(code, from_date, to_date): """Imports fund data from KOFIA. :param code: e.g., KR5223941018 :param from_date: e.g., 2016-01-01 :param to_date: e.g., 2016-02-28 """ provider = Kofia() app = create_app(__name__) with app.app_context(): asset = get_asset_by_fund_code(code) # FIXME: Target asset should also be determined by asset.data.code base_asset = Asset.query.filter_by(name='KRW').first() data = provider.fetch_data( code, parse_date(from_date), parse_date(to_date)) for date, unit_price, quantity in data: log.info('Import data on {}', date) unit_price /= 1000.0 try: AssetValue.create( asset=asset, base_asset=base_asset, evaluated_at=date, close=unit_price, granularity=Granularity.day, source='kofia') except IntegrityError: log.warn('Identical record has been found for {}. Skipping.', date) db.session.rollback()
def test_kofia_get_request_body(): provider = Kofia() body = provider.get_request_body( 'KR5223941018', parse_date('2016-06-02'), parse_date('2016-06-03')) # TODO: Parse XML for assertion assert '20160602' in body assert '20160603' in body
def test_kofia_get_request_body(): provider = Kofia() body = provider.get_request_body( "KR5223941018", parse_date("2016-06-02"), parse_date("2016-06-03") ) # TODO: Parse XML for assertion assert "20160602" in body assert "20160603" in body
def test_kofia_fetch_data(): provider = Kofia() from_date, to_date = parse_date("2016-05-01"), parse_date("2016-05-30") data = provider.fetch_data("KR5223941018", from_date, to_date) for date, unit_price, quantity in data: assert isinstance(date, datetime) assert from_date <= date <= to_date assert isinstance(unit_price, float) assert isinstance(quantity, float)
def test_kofia_fetch_data(): provider = Kofia() from_date, to_date = parse_date('2016-05-01'), parse_date('2016-05-30') data = provider.fetch_data('KR5223941018', from_date, to_date) for date, unit_price, quantity in data: assert isinstance(date, datetime) assert from_date <= date <= to_date assert isinstance(unit_price, float) assert isinstance(quantity, float)
def test_kofia_request_headers(): provider = Kofia() headers = provider.request_headers assert "Origin" in headers assert "kofia.or.kr" in headers["Origin"] assert "User-Agent" in headers assert "Content-Type" in headers assert headers["Content-Type"] == "text/xml" assert "Accept" in headers assert headers["Accept"] == "text/xml" assert "Referer" in headers assert "kofia.or.kr" in headers["Referer"]
def test_kofia_request_headers(): provider = Kofia() headers = provider.request_headers assert 'Origin' in headers assert 'kofia.or.kr' in headers['Origin'] assert 'User-Agent' in headers assert 'Content-Type' in headers assert headers['Content-Type'] == 'text/xml' assert 'Accept' in headers assert headers['Accept'] == 'text/xml' assert 'Referer' in headers assert 'kofia.or.kr' in headers['Referer']
def test_kofia_request_url(): provider = Kofia() assert "kofia.or.kr" in provider.request_url