|
4 | 4 | import pandas as pd |
5 | 5 |
|
6 | 6 | from pathlib import Path |
7 | | -from main import preprocess_bib_file, preprocess_txt_file |
| 7 | +from main import preprocess_bib_file, preprocess_txt_file, preprocess_csv_file |
8 | 8 | from botocore.exceptions import ClientError |
9 | 9 | from sqlalchemy import create_engine |
10 | 10 |
|
|
14 | 14 |
|
15 | 15 | PG_USER = "postgres" |
16 | 16 | PG_PASS = "postgres" |
| 17 | +DB_SCHEMA = "" |
17 | 18 |
|
18 | 19 | INPUT_FILE_NAME = "input" |
19 | 20 | OUTPUT_FILE_NAME = "output" |
@@ -82,6 +83,7 @@ def test_full_bib(s3_minio): |
82 | 83 | # PostgreSQL output |
83 | 84 | "normalized_docs_DB_DSN": f"postgresql://{PG_USER}:{PG_PASS}@127.0.0.1:5432/postgres", |
84 | 85 | "normalized_docs_DB_TABLE": "normalized_docs_bib", |
| 86 | + "normalized_docs_DB_SCHEMA": DB_SCHEMA, |
85 | 87 | "normalized_overwritten_file_output_S3_HOST": "http://127.0.0.1", |
86 | 88 | "normalized_overwritten_file_output_S3_PORT": "9000", |
87 | 89 | "normalized_overwritten_file_output_S3_ACCESS_KEY": MINIO_USER, |
@@ -145,6 +147,7 @@ def test_full_txt(s3_minio): |
145 | 147 | # Postgres output |
146 | 148 | "normalized_docs_DB_DSN": f"postgresql://{PG_USER}:{PG_PASS}@127.0.0.1:5432/postgres", |
147 | 149 | "normalized_docs_DB_TABLE": "normalized_docs_txt", |
| 150 | + "normalized_docs_DB_SCHEMA": DB_SCHEMA, |
148 | 151 | "normalized_overwritten_file_output_S3_HOST": "http://127.0.0.1", |
149 | 152 | "normalized_overwritten_file_output_S3_PORT": "9000", |
150 | 153 | "normalized_overwritten_file_output_S3_ACCESS_KEY": MINIO_USER, |
@@ -177,3 +180,75 @@ def test_full_txt(s3_minio): |
177 | 180 | assert all(isinstance(t, str) for t in df.iloc[0]["tokens"]) |
178 | 181 |
|
179 | 182 | # TODO: Test overwritten file upload |
| 183 | + |
| 184 | + |
| 185 | +def test_full_csv(s3_minio): |
| 186 | + csv_path = Path(__file__).parent / "files" / f"{INPUT_FILE_NAME}.csv" |
| 187 | + csv_bytes = csv_path.read_bytes() |
| 188 | + |
| 189 | + # Upload input to MinIO |
| 190 | + s3_minio.put_object( |
| 191 | + Bucket=BUCKET_NAME, |
| 192 | + Key=f"{INPUT_FILE_NAME}.csv", |
| 193 | + Body=csv_bytes, |
| 194 | + ) |
| 195 | + |
| 196 | + env = { |
| 197 | + "UNIGRAM_NORMALIZER": "porter", |
| 198 | + # CSV input S3 |
| 199 | + "csv_file_S3_HOST": "http://127.0.0.1", |
| 200 | + "csv_file_S3_PORT": "9000", |
| 201 | + "csv_file_S3_ACCESS_KEY": MINIO_USER, |
| 202 | + "csv_file_S3_SECRET_KEY": MINIO_PWD, |
| 203 | + "csv_file_BUCKET_NAME": BUCKET_NAME, |
| 204 | + "csv_file_FILE_PATH": "", |
| 205 | + "csv_file_FILE_NAME": INPUT_FILE_NAME, |
| 206 | + "csv_file_SELECTED_ATTRIBUTE": "abstract", |
| 207 | + "csv_file_ID_COLUMN": "id", |
| 208 | + # PostgreSQL output |
| 209 | + "normalized_docs_DB_DSN": ( |
| 210 | + f"postgresql://{PG_USER}:{PG_PASS}@127.0.0.1:5432/postgres" |
| 211 | + ), |
| 212 | + "normalized_docs_DB_TABLE": "normalized_docs_csv", |
| 213 | + "normalized_docs_DB_SCHEMA": DB_SCHEMA, |
| 214 | + # overwritten CSV output |
| 215 | + "normalized_overwritten_file_output_S3_HOST": "http://127.0.0.1", |
| 216 | + "normalized_overwritten_file_output_S3_PORT": "9000", |
| 217 | + "normalized_overwritten_file_output_S3_ACCESS_KEY": MINIO_USER, |
| 218 | + "normalized_overwritten_file_output_S3_SECRET_KEY": MINIO_PWD, |
| 219 | + "normalized_overwritten_file_output_BUCKET_NAME": BUCKET_NAME, |
| 220 | + "normalized_overwritten_file_output_FILE_PATH": "", |
| 221 | + "normalized_overwritten_file_output_FILE_NAME": OUTPUT_FILE_NAME, |
| 222 | + } |
| 223 | + |
| 224 | + for k, v in env.items(): |
| 225 | + os.environ[k] = v |
| 226 | + |
| 227 | + # Run block |
| 228 | + preprocess_csv_file() |
| 229 | + |
| 230 | + # Query PostgreSQL |
| 231 | + engine = create_engine( |
| 232 | + f"postgresql+psycopg2://{PG_USER}:{PG_PASS}@localhost:5432/" |
| 233 | + ) |
| 234 | + |
| 235 | + df = pd.read_sql_table("normalized_docs_csv", engine) |
| 236 | + |
| 237 | + # Assertions |
| 238 | + assert len(df) > 0 |
| 239 | + assert "doc_id" in df.columns |
| 240 | + assert "tokens" in df.columns |
| 241 | + |
| 242 | + # IDs |
| 243 | + assert len(df["doc_id"]) == len(df) |
| 244 | + assert df["doc_id"].is_unique |
| 245 | + assert all(isinstance(x, str) for x in df["doc_id"]) |
| 246 | + |
| 247 | + # Convert PG arrays |
| 248 | + df["tokens"] = df["tokens"].apply(parse_pg_array) |
| 249 | + |
| 250 | + # Token structure |
| 251 | + assert isinstance(df.iloc[0]["tokens"], list) |
| 252 | + assert all(isinstance(t, str) for t in df.iloc[0]["tokens"]) |
| 253 | + |
| 254 | + # TODO: Test overwritten file upload |
0 commit comments