|
| 1 | +import pandas |
| 2 | +import pyarrow |
| 3 | +from pyiceberg import schema, catalog |
| 4 | + |
| 5 | +from process_report.invoices.iceberg_invoice import IcebergInvoice |
| 6 | +from process_report.tests.base import BaseTestCaseWithTempDir |
| 7 | + |
| 8 | + |
| 9 | +class TestIceberg(BaseTestCaseWithTempDir): |
| 10 | + @classmethod |
| 11 | + def setUpClass(cls): |
| 12 | + super().setUpClass() |
| 13 | + # Create in-memory catalog |
| 14 | + cls.catalog_name = "catalog_foo" |
| 15 | + cls.table_path = "namespace_foo.table_foo" |
| 16 | + |
| 17 | + config_dict = { |
| 18 | + "type": "sql", |
| 19 | + "warehouse": str(cls.tempdir), |
| 20 | + "uri": f"sqlite:///{str(cls.tempdir)}/foo.db", |
| 21 | + } |
| 22 | + cls.catalog_config = config_dict |
| 23 | + |
| 24 | + # Initialize test schema that's used in setUp() |
| 25 | + cls.catalog = catalog.load_catalog(name=cls.catalog_name, **config_dict) |
| 26 | + cls.test_schema = schema.Schema( |
| 27 | + schema.NestedField(1, "Invoice Month", schema.StringType()), |
| 28 | + schema.NestedField(2, "Cost", schema.DecimalType(21, 2)), |
| 29 | + schema.NestedField(3, "PI", schema.StringType()), |
| 30 | + ) |
| 31 | + |
| 32 | + def setUp(self): |
| 33 | + self.catalog.create_namespace_if_not_exists("namespace_foo") |
| 34 | + self.catalog.create_table_if_not_exists(self.table_path, self.test_schema) |
| 35 | + |
| 36 | + def tearDown(self): |
| 37 | + self.catalog.drop_table(self.table_path) |
| 38 | + |
| 39 | + def test_upload_one_dataframe(self): |
| 40 | + # Create test dataframe matching table schema |
| 41 | + test_df = pandas.DataFrame( |
| 42 | + { |
| 43 | + "Invoice Month": ["2024-01", "2024-01"], |
| 44 | + "Cost": [100.0, 200.0], |
| 45 | + "PI": ["PI1", "PI2"], |
| 46 | + }, |
| 47 | + ).astype({"Cost": pandas.ArrowDtype(pyarrow.decimal128(21, 2))}) |
| 48 | + |
| 49 | + # Create IcebergInvoice instance |
| 50 | + inv = IcebergInvoice( |
| 51 | + invoice_month="2024-01", |
| 52 | + data=test_df, |
| 53 | + iceberg_catalog_name=self.catalog_name, |
| 54 | + iceberg_catalog_config=self.catalog_config, |
| 55 | + iceberg_table_path=self.table_path, |
| 56 | + ) |
| 57 | + inv.process() |
| 58 | + inv.export() |
| 59 | + |
| 60 | + # Verify data was uploaded, and Iceberg cost column can be casted to Decimal |
| 61 | + table = self.catalog.load_table(self.table_path) |
| 62 | + uploaded_df = table.scan().to_pandas().astype(test_df.dtypes) |
| 63 | + assert uploaded_df.equals(test_df) |
| 64 | + |
| 65 | + def test_upload_new_column(self): |
| 66 | + # Create test dataframe with an extra column |
| 67 | + test_df = pandas.DataFrame( |
| 68 | + { |
| 69 | + "Invoice Month": ["2024-02", "2024-02"], |
| 70 | + "Cost": [150.0, 250.0], |
| 71 | + "PI": ["PI3", "PI4"], |
| 72 | + "extra_column": ["extra1", "extra2"], # New column |
| 73 | + } |
| 74 | + ).astype({"Cost": pandas.ArrowDtype(pyarrow.decimal128(21, 2))}) |
| 75 | + |
| 76 | + # Create IcebergInvoice instance |
| 77 | + inv = IcebergInvoice( |
| 78 | + invoice_month="2024-02", |
| 79 | + data=test_df, |
| 80 | + iceberg_catalog_name=self.catalog_name, |
| 81 | + iceberg_catalog_config=self.catalog_config, |
| 82 | + iceberg_table_path=self.table_path, |
| 83 | + ) |
| 84 | + inv.process() |
| 85 | + inv.export() |
| 86 | + |
| 87 | + # Verify data was uploaded with new column (schema evolution) |
| 88 | + table = self.catalog.load_table(self.table_path) |
| 89 | + uploaded_df = table.scan().to_pandas().astype(test_df.dtypes) |
| 90 | + assert uploaded_df.equals(test_df) |
| 91 | + |
| 92 | + def test_schema_evolution_with_existing_data(self): |
| 93 | + # First, upload initial data without extra column |
| 94 | + first_df = pandas.DataFrame( |
| 95 | + { |
| 96 | + "Invoice Month": ["2024-01", "2024-01"], |
| 97 | + "Cost": [100.0, 200.0], |
| 98 | + "PI": ["PI1", "PI2"], |
| 99 | + } |
| 100 | + ).astype({"Cost": pandas.ArrowDtype(pyarrow.decimal128(21, 2))}) |
| 101 | + |
| 102 | + inv = IcebergInvoice( |
| 103 | + invoice_month="2024-01", |
| 104 | + data=first_df, |
| 105 | + iceberg_catalog_name=self.catalog_name, |
| 106 | + iceberg_catalog_config=self.catalog_config, |
| 107 | + iceberg_table_path=self.table_path, |
| 108 | + ) |
| 109 | + inv.process() |
| 110 | + inv.export() |
| 111 | + |
| 112 | + # Now upload data with an extra column |
| 113 | + second_df = pandas.DataFrame( |
| 114 | + { |
| 115 | + "Invoice Month": ["2024-02", "2024-02"], |
| 116 | + "Cost": [150.0, 250.0], |
| 117 | + "PI": ["PI3", "PI4"], |
| 118 | + "extra_column": ["new1", "new2"], # New column |
| 119 | + } |
| 120 | + ).astype({"Cost": pandas.ArrowDtype(pyarrow.decimal128(21, 2))}) |
| 121 | + |
| 122 | + inv2 = IcebergInvoice( |
| 123 | + invoice_month="2024-02", |
| 124 | + data=second_df, |
| 125 | + iceberg_catalog_name=self.catalog_name, |
| 126 | + iceberg_catalog_config=self.catalog_config, |
| 127 | + iceberg_table_path=self.table_path, |
| 128 | + ) |
| 129 | + inv2.process() |
| 130 | + inv2.export() |
| 131 | + |
| 132 | + table = self.catalog.load_table(self.table_path) |
| 133 | + result_df = table.scan().to_pandas().astype(second_df.dtypes) |
| 134 | + |
| 135 | + # Verify the table has schema evolved with the new column |
| 136 | + # Old rows should have None for the new column |
| 137 | + expected_df = pandas.DataFrame( |
| 138 | + { |
| 139 | + "Invoice Month": ["2024-02", "2024-02", "2024-01", "2024-01"], |
| 140 | + "Cost": [150.0, 250.0, 100.0, 200.0], |
| 141 | + "PI": ["PI3", "PI4", "PI1", "PI2"], |
| 142 | + "extra_column": ["new1", "new2", None, None], |
| 143 | + } |
| 144 | + ).astype({"Cost": pandas.ArrowDtype(pyarrow.decimal128(21, 2))}) |
| 145 | + assert result_df.equals(expected_df) |
0 commit comments