def import_stock_records(filename): """Parses exported data from the Shinhan HTS.""" account_bank = Account.query.filter(Account.name == "신한 입출금").first() account_stock = Account.query.filter(Account.name == "신한 주식").first() with open(filename) as fin: for parsed in parse_stock_records(fin): insert_stock_record(parsed, account_stock, account_bank)
def import_stock_records(filename): """Parses exported data from the Shinhan HTS.""" app = create_app(__name__) with app.app_context(): account_bank = Account.query \ .filter(Account.name == '신한 입출금').first() account_stock = Account.query \ .filter(Account.name == '신한 주식').first() with open(filename) as fin: for parsed in parse_stock_records(fin): insert_stock_record(parsed, account_stock, account_bank)
def test_parse_stock_records(): sample_file = "tests/samples/shinhan_stock_records.csv" flag = True expected_keys = ( "date", "sequence", "category1", "category2", "code", "name", "quantity", "unit_price", "subtotal", "interest", "fees", "late_fees", "channel", "final_amount", ) expected_types = { "date": datetime, "sequence": int, "unit_price": int, "quantity": int, "subtotal": int, "interest": int, "fees": int, "late_fees": int, "final_amount": int, } with open(sample_file) as fin: for data in parse_stock_records(fin): flag = False for key in expected_keys: assert key in data for k, t in expected_types.items(): assert isinstance(data[k], t) # It should be either a six-digit code or None assert data["code"] is None or re.match(r"^\d{6}$", data["code"]) # print(data['date'], data['sequence'], data['category1'], # data['category2'], data['code'], data['name'], # data['unit_price'], data['quantity'], data['subtotal'], # data['final_amount']) if flag: pytest.fail("No data was read.")
def test_parse_stock_records(): sample_file = 'tests/samples/shinhan_stock_records.csv' flag = True expected_keys = ('date', 'sequence', 'category1', 'category2', 'code', 'name', 'quantity', 'unit_price', 'subtotal', 'interest', 'fees', 'late_fees', 'channel', 'final_amount') expected_types = { 'date': datetime, 'sequence': int, 'unit_price': int, 'quantity': int, 'subtotal': int, 'interest': int, 'fees': int, 'late_fees': int, 'final_amount': int, } with open(sample_file) as fin: for data in parse_stock_records(fin): flag = False for key in expected_keys: assert key in data for k, t in expected_types.items(): assert isinstance(data[k], t) # It should be either a six-digit code or None assert data['code'] is None or re.match(r'^\d{6}$', data['code']) # print(data['date'], data['sequence'], data['category1'], # data['category2'], data['code'], data['name'], # data['unit_price'], data['quantity'], data['subtotal'], # data['final_amount']) if flag: pytest.fail('No data was read.')