def test_load_parameters_pass_to_executemany(monkeypatch, pgtestdb_conn, test_table_data): # Arrange # Patch 'iter_rows' function within etlhelper.etl module mock_executemany = Mock() monkeypatch.setattr(etlhelper_etl, 'executemany', mock_executemany) # Sentinel items are unique so confirm object that was passed through table = sentinel.table commit_chunks = sentinel.commit_chunks chunk_size = sentinel.chunk_size # Act load(table, pgtestdb_conn, test_table_data, commit_chunks=commit_chunks, chunk_size=chunk_size) # Assert # load() function writes SQL query sql = """ INSERT INTO sentinel.table (id, value, simple_text, utf8_text, day, date_time) VALUES (%s, %s, %s, %s, %s, %s)""".strip() sql = re.sub(r"\s\s+", " ", sql) # replace newlines and whitespace mock_executemany.assert_called_once_with( sql, pgtestdb_conn, ANY, on_error=None, commit_chunks=sentinel.commit_chunks, chunk_size=sentinel.chunk_size)
def test_load_named_tuples(testdb_conn, test_tables, test_table_data): # Act load('dest', testdb_conn, test_table_data) # Assert sql = "SELECT * FROM dest" result = get_rows(sql, testdb_conn) assert result == test_table_data
def test_load_named_tuples_chunk_size(pgtestdb_conn, pgtestdb_test_tables, test_table_data, chunk_size): # Act load('dest', pgtestdb_conn, test_table_data, chunk_size=chunk_size) # Assert sql = "SELECT * FROM dest" result = get_rows(sql, pgtestdb_conn) assert result == test_table_data
def test_load_dicts(pgtestdb_conn, pgtestdb_test_tables, test_table_data): # Arrange data_as_dicts = [row._asdict() for row in test_table_data] # Act load('dest', pgtestdb_conn, data_as_dicts) # Assert sql = "SELECT * FROM dest" result = get_rows(sql, pgtestdb_conn) assert result == test_table_data
def test_load_dicts(testdb_conn, test_tables, test_table_data): # Arrange data_as_dicts = [row._asdict() for row in test_table_data] expected_message = ( "Database connection (<class 'pyodbc.Connection'>) doesn't support named parameters. " "Pass data as namedtuples instead.") # Act and assert # pyodbc doesn't support named parameters. with pytest.raises(ETLHelperInsertError) as exc_info: load('dest', testdb_conn, data_as_dicts) assert str(exc_info.value) == expected_message
def test_load_named_tuples(testdb_conn, test_tables, test_table_data): # Arrange # Convert to plain tuples as ORACLE makes column names upper case expected = [tuple(row) for row in test_table_data] # Act load('dest', testdb_conn, test_table_data) # Assert sql = "SELECT * FROM dest" result = get_rows(sql, testdb_conn) # Fix result date and datetime strings to native classes fixed_dates = [] for row in result: fixed_dates.append((*row[:4], row.DAY.date(), row.DATE_TIME)) assert fixed_dates == expected