Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ else:

> Note that AsyncAdaper must be used for AynscEnforcer.

### Suppressing the Default Table Warning

By default, when using the default `CasbinRule` table, the adapter will show a warning to remind you to call `create_table()`. If you want to suppress this warning, you have two options:

**Option 1: Use the `warning` parameter**
```python
adapter = casbin_async_sqlalchemy_adapter.Adapter(
'sqlite+aiosqlite:///test.db',
warning=False # Suppress the warning
)
```

**Option 2: Explicitly pass the `db_class` parameter**
```python
from casbin_async_sqlalchemy_adapter import CasbinRule

adapter = casbin_async_sqlalchemy_adapter.Adapter(
'sqlite+aiosqlite:///test.db',
db_class=CasbinRule # Explicitly use default class
)
```

## External Session Support

The adapter supports using externally managed SQLAlchemy sessions. This feature is useful for:
Expand Down
3 changes: 2 additions & 1 deletion casbin_async_sqlalchemy_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def __init__(
if warning:
warnings.warn(
"Using default CasbinRule table, please note the use of the 'Adapter().create_table()' method"
" to create the table, and ignore this warning if you are using a custom CasbinRule table.",
" to create the table. To suppress this warning, pass 'warning=False' to the Adapter constructor,"
" or pass a custom 'db_class' parameter.",
RuntimeWarning,
)
else:
Expand Down
75 changes: 75 additions & 0 deletions tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import os
import unittest
import warnings
from unittest import IsolatedAsyncioTestCase

import casbin
Expand Down Expand Up @@ -418,5 +419,79 @@ async def test_add_policies_bulk_internal_session(self):
assert r in tuples


class TestWarningParameter(IsolatedAsyncioTestCase):
"""Test warning suppression functionality"""

async def test_default_warning_shown(self):
"""Test that warning is shown by default when using default CasbinRule"""
engine = create_async_engine("sqlite+aiosqlite://", future=True)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
adapter = Adapter(engine)

# Should have exactly one warning
self.assertEqual(len(w), 1)
self.assertTrue(issubclass(w[0].category, RuntimeWarning))
self.assertIn("Using default CasbinRule table", str(w[0].message))
self.assertIn("warning=False", str(w[0].message))

async def test_warning_suppressed_with_parameter(self):
"""Test that warning=False suppresses the warning"""
engine = create_async_engine("sqlite+aiosqlite://", future=True)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
adapter = Adapter(engine, warning=False)

# Should have no warnings
self.assertEqual(len(w), 0)

async def test_warning_suppressed_with_custom_db_class(self):
"""Test that passing db_class suppresses the warning"""
engine = create_async_engine("sqlite+aiosqlite://", future=True)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
adapter = Adapter(engine, db_class=CasbinRule)

# Should have no warnings
self.assertEqual(len(w), 0)

async def test_warning_suppressed_with_both_parameters(self):
"""Test that passing both db_class and warning=False works"""
engine = create_async_engine("sqlite+aiosqlite://", future=True)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
adapter = Adapter(engine, db_class=CasbinRule, warning=False)

# Should have no warnings
self.assertEqual(len(w), 0)

async def test_custom_db_class_no_warning(self):
"""Test that custom db_class doesn't show warning regardless of warning parameter"""
class CustomRule(Base):
__tablename__ = "custom_casbin_rule"
id = Column(Integer, primary_key=True)
ptype = Column(String(255))
v0 = Column(String(255))
v1 = Column(String(255))
v2 = Column(String(255))
v3 = Column(String(255))
v4 = Column(String(255))
v5 = Column(String(255))

engine = create_async_engine("sqlite+aiosqlite://", future=True)

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
# Even with warning=True, custom db_class should not warn
adapter = Adapter(engine, db_class=CustomRule, warning=True)

# Should have no warnings
self.assertEqual(len(w), 0)


if __name__ == "__main__":
unittest.main()