|
| 1 | +import pytest |
| 2 | +from aiopynamodb.models import Model |
| 3 | + |
| 4 | +from fastapi_users_db_dynamodb import config |
| 5 | +from fastapi_users_db_dynamodb.attributes import GUID |
| 6 | +from fastapi_users_db_dynamodb.tables import ensure_tables_exist |
| 7 | + |
| 8 | + |
| 9 | +class NotAModel: |
| 10 | + pass |
| 11 | + |
| 12 | + |
| 13 | +class IncompleteModel(Model): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.asyncio |
| 18 | +async def test_tables_invalid_models(monkeypatch): |
| 19 | + with pytest.raises(TypeError, match="must be a subclass of Model"): |
| 20 | + await ensure_tables_exist(NotAModel) # type: ignore |
| 21 | + |
| 22 | + with pytest.raises(AttributeError, match="PynamoDB Models require a"): |
| 23 | + await ensure_tables_exist(IncompleteModel) |
| 24 | + |
| 25 | + monkeypatch.delattr(Model, "exists", raising=True) |
| 26 | + with pytest.raises(TypeError): |
| 27 | + await ensure_tables_exist(IncompleteModel) |
| 28 | + |
| 29 | + |
| 30 | +def test_config(monkeypatch): |
| 31 | + billing_mode = config.BillingMode.PAY_PER_REQUEST |
| 32 | + assert billing_mode.value == str(billing_mode) |
| 33 | + |
| 34 | + local_get, local_set = config.__create_config() |
| 35 | + monkeypatch.setattr(config, "get", local_get) |
| 36 | + monkeypatch.setattr(config, "set", local_set) |
| 37 | + |
| 38 | + with pytest.raises(KeyError, match="Unknown config key"): |
| 39 | + config.set("non_existent_key", "some_value") |
| 40 | + |
| 41 | + with pytest.raises(TypeError, match="Invalid type for"): |
| 42 | + config.set("DATABASE_BILLING_MODE", 1001) |
| 43 | + |
| 44 | + region = "us-east-1" |
| 45 | + config.set("DATABASE_REGION", region) |
| 46 | + assert config.get("DATABASE_REGION") == region |
| 47 | + |
| 48 | + |
| 49 | +def test_attributes(user_id): |
| 50 | + id = GUID() |
| 51 | + assert id.serialize(None) is None |
| 52 | + |
| 53 | + user_id_str = str(user_id) |
| 54 | + assert user_id_str == id.serialize(user_id_str) |
| 55 | + |
| 56 | + assert id.deserialize(None) is None |
| 57 | + assert user_id == id.deserialize(user_id) |
0 commit comments