|
| 1 | +import logging |
| 2 | +from typing import Dict, Optional |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +import pytest |
| 6 | +from testcontainers.core.container import DockerContainer |
| 7 | +from testcontainers.core.waiting_utils import wait_for_logs |
| 8 | + |
| 9 | +from feast.data_source import DataSource |
| 10 | +from feast.infra.offline_stores.contrib.postgres_offline_store.postgres import ( |
| 11 | + PostgreSQLOfflineStoreConfig, |
| 12 | + PostgreSQLSource, |
| 13 | +) |
| 14 | +from feast.infra.utils.postgres.connection_utils import df_to_postgres_table |
| 15 | +from tests.integration.feature_repos.universal.data_source_creator import ( |
| 16 | + DataSourceCreator, |
| 17 | +) |
| 18 | +from tests.integration.feature_repos.universal.online_store_creator import ( |
| 19 | + OnlineStoreCreator, |
| 20 | +) |
| 21 | + |
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
| 24 | +POSTGRES_USER = "test" |
| 25 | +POSTGRES_PASSWORD = "test" |
| 26 | +POSTGRES_DB = "test" |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="session") |
| 30 | +def postgres_container(): |
| 31 | + container = ( |
| 32 | + DockerContainer("postgres:latest") |
| 33 | + .with_exposed_ports(5432) |
| 34 | + .with_env("POSTGRES_USER", POSTGRES_USER) |
| 35 | + .with_env("POSTGRES_PASSWORD", POSTGRES_PASSWORD) |
| 36 | + .with_env("POSTGRES_DB", POSTGRES_DB) |
| 37 | + ) |
| 38 | + |
| 39 | + container.start() |
| 40 | + |
| 41 | + log_string_to_wait_for = "database system is ready to accept connections" |
| 42 | + waited = wait_for_logs( |
| 43 | + container=container, predicate=log_string_to_wait_for, timeout=30, interval=10, |
| 44 | + ) |
| 45 | + logger.info("Waited for %s seconds until postgres container was up", waited) |
| 46 | + |
| 47 | + yield container |
| 48 | + container.stop() |
| 49 | + |
| 50 | + |
| 51 | +class PostgreSQLDataSourceCreator(DataSourceCreator, OnlineStoreCreator): |
| 52 | + def __init__( |
| 53 | + self, project_name: str, fixture_request: pytest.FixtureRequest, **kwargs |
| 54 | + ): |
| 55 | + super().__init__(project_name,) |
| 56 | + |
| 57 | + self.project_name = project_name |
| 58 | + self.container = fixture_request.getfixturevalue("postgres_container") |
| 59 | + if not self.container: |
| 60 | + raise RuntimeError( |
| 61 | + "In order to use this data source " |
| 62 | + "'feast.infra.offline_stores.contrib.postgres_offline_store.tests' " |
| 63 | + "must be include into pytest plugins" |
| 64 | + ) |
| 65 | + |
| 66 | + self.offline_store_config = PostgreSQLOfflineStoreConfig( |
| 67 | + type="postgres", |
| 68 | + host="localhost", |
| 69 | + port=self.container.get_exposed_port(5432), |
| 70 | + database=self.container.env["POSTGRES_DB"], |
| 71 | + db_schema="public", |
| 72 | + user=self.container.env["POSTGRES_USER"], |
| 73 | + password=self.container.env["POSTGRES_PASSWORD"], |
| 74 | + ) |
| 75 | + |
| 76 | + def create_data_source( |
| 77 | + self, |
| 78 | + df: pd.DataFrame, |
| 79 | + destination_name: str, |
| 80 | + suffix: Optional[str] = None, |
| 81 | + timestamp_field="ts", |
| 82 | + created_timestamp_column="created_ts", |
| 83 | + field_mapping: Dict[str, str] = None, |
| 84 | + ) -> DataSource: |
| 85 | + destination_name = self.get_prefixed_table_name(destination_name) |
| 86 | + |
| 87 | + if self.offline_store_config: |
| 88 | + df_to_postgres_table(self.offline_store_config, df, destination_name) |
| 89 | + |
| 90 | + return PostgreSQLSource( |
| 91 | + name=destination_name, |
| 92 | + query=f"SELECT * FROM {destination_name}", |
| 93 | + timestamp_field=timestamp_field, |
| 94 | + created_timestamp_column=created_timestamp_column, |
| 95 | + field_mapping=field_mapping or {"ts_1": "ts"}, |
| 96 | + ) |
| 97 | + |
| 98 | + def create_offline_store_config(self) -> PostgreSQLOfflineStoreConfig: |
| 99 | + assert self.offline_store_config |
| 100 | + return self.offline_store_config |
| 101 | + |
| 102 | + def get_prefixed_table_name(self, suffix: str) -> str: |
| 103 | + return f"{self.project_name}_{suffix}" |
| 104 | + |
| 105 | + def create_online_store(self) -> Dict[str, str]: |
| 106 | + assert self.container |
| 107 | + return { |
| 108 | + "type": "postgres", |
| 109 | + "host": "localhost", |
| 110 | + "port": self.container.get_exposed_port(5432), |
| 111 | + "database": POSTGRES_DB, |
| 112 | + "db_schema": "feature_store", |
| 113 | + "user": POSTGRES_USER, |
| 114 | + "password": POSTGRES_PASSWORD, |
| 115 | + } |
| 116 | + |
| 117 | + def create_saved_dataset_destination(self): |
| 118 | + # FIXME: ... |
| 119 | + return None |
| 120 | + |
| 121 | + def teardown(self): |
| 122 | + pass |
0 commit comments