|
| 1 | +import pytest |
| 2 | +from pocketbase import Client |
| 3 | +from pocketbase.models.record import Record |
| 4 | +from pocketbase.utils import camel_to_snake |
| 5 | + |
| 6 | +class DummyClient: |
| 7 | + def __init__(self, auto_snake_case=True): |
| 8 | + self.auto_snake_case = auto_snake_case |
| 9 | + |
| 10 | +class DummyRecord(Record): |
| 11 | + def __init__(self, data, client=None): |
| 12 | + self.client = client |
| 13 | + self.expand = {} |
| 14 | + self.load(data) |
| 15 | + |
| 16 | +def test_camel_to_snake_enabled(): |
| 17 | + assert camel_to_snake("TestCase") == "test_case" |
| 18 | + assert camel_to_snake("testCase") == "test_case" |
| 19 | + assert camel_to_snake("test_case") == "test_case" |
| 20 | + assert camel_to_snake("TestBS123") == "test_bs123" |
| 21 | + assert camel_to_snake("TestCase", enabled=False) == "TestCase" |
| 22 | + |
| 23 | +def test_record_snake_case_enabled(): |
| 24 | + client = DummyClient(auto_snake_case=True) |
| 25 | + data = {"myFieldName": "value", "anotherField": 42} |
| 26 | + record = DummyRecord(data, client=client) |
| 27 | + assert hasattr(record, "my_field_name") |
| 28 | + assert hasattr(record, "another_field") |
| 29 | + assert record.my_field_name == "value" |
| 30 | + assert record.another_field == 42 |
| 31 | + |
| 32 | +def test_record_snake_case_disabled(): |
| 33 | + client = DummyClient(auto_snake_case=False) |
| 34 | + data = {"myFieldName": "value", "anotherField": 42} |
| 35 | + record = DummyRecord(data, client=client) |
| 36 | + assert hasattr(record, "myFieldName") |
| 37 | + assert hasattr(record, "anotherField") |
| 38 | + assert not hasattr(record, "my_field_name") |
| 39 | + assert not hasattr(record, "another_field") |
| 40 | + assert record.myFieldName == "value" |
| 41 | + assert record.anotherField == 42 |
0 commit comments