@@ -109,6 +109,56 @@ def postgres_cache_config() -> PostgreSQLDatabaseConfiguration:
109109 )
110110
111111
112+ @pytest .fixture (scope = "module" , name = "postgres_cache_config_fixture_wrong_namespace" )
113+ def postgres_cache_config_wrong_namespace () -> PostgreSQLDatabaseConfiguration :
114+ """Fixture with invalid namespace containing spaces for validation testing.
115+
116+ Create a PostgreSQLDatabaseConfiguration with an invalid namespace ("foo bar baz")
117+ to verify that the PostgresCache constructor properly rejects namespaces with spaces.
118+
119+ Returns:
120+ PostgreSQLDatabaseConfiguration: A configuration object with host,
121+ port, db, user, SecretStr password, and an invalid namespace containing
122+ spaces. Values are placeholders and not intended for real database
123+ connections.
124+ """
125+ # can be any configuration, becuase tests won't really try to
126+ # connect to database
127+ return PostgreSQLDatabaseConfiguration (
128+ host = "localhost" ,
129+ port = 1234 ,
130+ db = "database" ,
131+ user = "user" ,
132+ password = SecretStr ("password" ),
133+ namespace = "foo bar baz" ,
134+ )
135+
136+
137+ @pytest .fixture (scope = "module" , name = "postgres_cache_config_fixture_too_long_namespace" )
138+ def postgres_cache_config_too_long_namespace () -> PostgreSQLDatabaseConfiguration :
139+ """Fixture with namespace exceeding PostgreSQL's 63-character limit.
140+
141+ Create a PostgreSQLDatabaseConfiguration with an overly long namespace
142+ to verify that the PostgresCache constructor enforces the maximum length constraint.
143+
144+ Returns:
145+ PostgreSQLDatabaseConfiguration: A configuration object with host,
146+ port, db, user, SecretStr password, and a namespace exceeding 63
147+ characters. Values are placeholders and not intended for real database
148+ connections.
149+ """
150+ # can be any configuration, becuase tests won't really try to
151+ # connect to database
152+ return PostgreSQLDatabaseConfiguration (
153+ host = "localhost" ,
154+ port = 1234 ,
155+ db = "database" ,
156+ user = "user" ,
157+ password = SecretStr ("password" ),
158+ namespace = "too long namespace that is longer than allowed 63 characters limit" ,
159+ )
160+
161+
112162def test_cache_initialization (
113163 postgres_cache_config_fixture : PostgreSQLDatabaseConfiguration ,
114164 mocker : MockerFixture ,
@@ -220,7 +270,7 @@ def test_initialize_cache_when_connected(
220270 mocker .patch ("psycopg2.connect" )
221271 cache = PostgresCache (postgres_cache_config_fixture )
222272 # should not fail
223- cache .initialize_cache ()
273+ cache .initialize_cache ("public" )
224274
225275
226276def test_initialize_cache_when_disconnected (
@@ -234,7 +284,7 @@ def test_initialize_cache_when_disconnected(
234284 cache .connection = None
235285
236286 with pytest .raises (CacheError , match = "cache is disconnected" ):
237- cache .initialize_cache ()
287+ cache .initialize_cache ("public" )
238288
239289
240290def test_ready_method (
@@ -619,3 +669,44 @@ def test_insert_and_get_without_referenced_documents(
619669 assert len (retrieved_entries ) == 1
620670 assert retrieved_entries [0 ] == entry_without_docs
621671 assert retrieved_entries [0 ].referenced_documents is None
672+
673+
674+ def test_initialize_cache_with_custom_namespace (
675+ postgres_cache_config_fixture : PostgreSQLDatabaseConfiguration ,
676+ mocker : MockerFixture ,
677+ ) -> None :
678+ """Test the initialize_cache() with a custom namespace."""
679+ mock_connect = mocker .patch ("psycopg2.connect" )
680+ cache = PostgresCache (postgres_cache_config_fixture )
681+
682+ mock_connection = mock_connect .return_value
683+ mock_cursor = mock_connection .cursor .return_value
684+
685+ # should not fail and should execute CREATE SCHEMA
686+ cache .initialize_cache ("custom_schema" )
687+
688+ # Verify CREATE SCHEMA was called for non-public namespace
689+ create_schema_calls = [
690+ call
691+ for call in mock_cursor .execute .call_args_list
692+ if "CREATE SCHEMA" in str (call )
693+ ]
694+ assert len (create_schema_calls ) > 0
695+
696+
697+ def test_connect_to_cache_with_improper_namespace (
698+ postgres_cache_config_fixture_wrong_namespace : PostgreSQLDatabaseConfiguration ,
699+ ) -> None :
700+ """Test that PostgresCache constructor raises ValueError for invalid namespace."""
701+ # should fail due to invalid namespace containing spaces
702+ with pytest .raises (ValueError , match = "Invalid namespace: foo bar baz" ):
703+ PostgresCache (postgres_cache_config_fixture_wrong_namespace )
704+
705+
706+ def test_connect_to_cache_with_too_long_namespace (
707+ postgres_cache_config_fixture_too_long_namespace : PostgreSQLDatabaseConfiguration ,
708+ ) -> None :
709+ """Test that PostgresCache constructor raises ValueError for invalid namespace."""
710+ # should fail due to invalid namespace containing spaces
711+ with pytest .raises (ValueError , match = "Invalid namespace: too long namespace" ):
712+ PostgresCache (postgres_cache_config_fixture_too_long_namespace )
0 commit comments