33from datetime import timedelta
44from random import random
55from time import sleep
6+ import sys
7+ import os
68
79import pytest
810
911from cachier import cachier
1012from cachier .cores .sql import _SQLCore
1113
12- SQLITE_MEMORY = " sqlite:///:memory:"
14+ SQL_CONN_STR = os . environ . get ( "SQLALCHEMY_DATABASE_URL" , " sqlite:///:memory:")
1315
1416
1517@pytest .mark .sql
1618def test_sql_core_basic ():
17- @cachier (backend = "sql" , sql_engine = SQLITE_MEMORY )
19+ @cachier (backend = "sql" , sql_engine = SQL_CONN_STR )
1820 def f (x , y ):
1921 return random () + x + y
2022
@@ -34,7 +36,7 @@ def f(x, y):
3436
3537@pytest .mark .sql
3638def test_sql_core_keywords ():
37- @cachier (backend = "sql" , sql_engine = SQLITE_MEMORY )
39+ @cachier (backend = "sql" , sql_engine = SQL_CONN_STR )
3840 def f (x , y ):
3941 return random () + x + y
4042
@@ -56,7 +58,7 @@ def f(x, y):
5658def test_sql_stale_after ():
5759 @cachier (
5860 backend = "sql" ,
59- sql_engine = SQLITE_MEMORY ,
61+ sql_engine = SQL_CONN_STR ,
6062 stale_after = timedelta (seconds = 2 ),
6163 next_time = False ,
6264 )
@@ -74,7 +76,7 @@ def f(x, y):
7476
7577@pytest .mark .sql
7678def test_sql_overwrite_and_skip_cache ():
77- @cachier (backend = "sql" , sql_engine = SQLITE_MEMORY )
79+ @cachier (backend = "sql" , sql_engine = SQL_CONN_STR )
7880 def f (x ):
7981 return random () + x
8082
@@ -92,7 +94,7 @@ def f(x):
9294
9395@pytest .mark .sql
9496def test_sql_concurrency ():
95- @cachier (backend = "sql" , sql_engine = SQLITE_MEMORY )
97+ @cachier (backend = "sql" , sql_engine = SQL_CONN_STR )
9698 def slow_func (x ):
9799 sleep (1 )
98100 return random () + x
@@ -119,7 +121,7 @@ def call():
119121
120122@pytest .mark .sql
121123def test_sql_clear_being_calculated ():
122- @cachier (backend = "sql" , sql_engine = SQLITE_MEMORY )
124+ @cachier (backend = "sql" , sql_engine = SQL_CONN_STR )
123125 def slow_func (x ):
124126 sleep (1 )
125127 return random () + x
@@ -133,7 +135,7 @@ def slow_func(x):
133135
134136@pytest .mark .sql
135137def test_sql_missing_entry ():
136- @cachier (backend = "sql" , sql_engine = SQLITE_MEMORY )
138+ @cachier (backend = "sql" , sql_engine = SQL_CONN_STR )
137139 def f (x ):
138140 return x
139141
@@ -144,7 +146,7 @@ def f(x):
144146
145147@pytest .mark .sql
146148def test_sql_failed_write (monkeypatch ):
147- @cachier (backend = "sql" , sql_engine = SQLITE_MEMORY )
149+ @cachier (backend = "sql" , sql_engine = SQL_CONN_STR )
148150 def f (x ):
149151 return x
150152
@@ -159,3 +161,43 @@ def fail_set_entry(self, key, func_res):
159161 with pytest .raises (Exception ):
160162 f (1 )
161163 monkeypatch .setattr (_SQLCore , "set_entry" , orig )
164+
165+
166+ @pytest .mark .sql
167+ def test_import_cachier_without_sqlalchemy (monkeypatch ):
168+ """Test that importing cachier works when SQLAlchemy is missing.
169+
170+ This should work unless SQL core is used."""
171+ # Simulate SQLAlchemy not installed
172+ modules_backup = sys .modules .copy ()
173+ sys .modules ["sqlalchemy" ] = None
174+ sys .modules ["sqlalchemy.orm" ] = None
175+ sys .modules ["sqlalchemy.engine" ] = None
176+ try :
177+ import importlib # noqa: F401
178+ import cachier # noqa: F401
179+
180+ # Should import fine
181+ finally :
182+ sys .modules .clear ()
183+ sys .modules .update (modules_backup )
184+
185+
186+ @pytest .mark .sql
187+ def test_sqlcore_importerror_without_sqlalchemy (monkeypatch ):
188+ """Test that using SQL core without SQLAlchemy raises an ImportError."""
189+ # Simulate SQLAlchemy not installed
190+ modules_backup = sys .modules .copy ()
191+ sys .modules ["sqlalchemy" ] = None
192+ sys .modules ["sqlalchemy.orm" ] = None
193+ sys .modules ["sqlalchemy.engine" ] = None
194+ try :
195+ import importlib
196+
197+ sql_mod = importlib .import_module ("cachier.cores.sql" )
198+ with pytest .raises (ImportError ) as excinfo :
199+ sql_mod ._SQLCore (hash_func = None , sql_engine = "sqlite:///:memory:" )
200+ assert "SQLAlchemy is required" in str (excinfo .value )
201+ finally :
202+ sys .modules .clear ()
203+ sys .modules .update (modules_backup )
0 commit comments