|
| 1 | +import pytest |
| 2 | +from sqlalchemy import Column, Integer, String |
| 3 | +from sqlalchemy.ext.declarative import declarative_base |
| 4 | + |
| 5 | +from flask_combo_jsonapi.utils import get_model_init_params_names, validate_model_init_params |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(scope='module') |
| 9 | +def base(): |
| 10 | + yield declarative_base() |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(scope='module') |
| 14 | +def model_without_init(base): |
| 15 | + class ModelWithoutInit(base): |
| 16 | + __tablename__ = 'model_without_init' |
| 17 | + |
| 18 | + id = Column(Integer, primary_key=True, index=True) |
| 19 | + key1 = Column(String) |
| 20 | + key2 = Column(String) |
| 21 | + |
| 22 | + yield ModelWithoutInit |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(scope='module') |
| 26 | +def model_with_kwargs_in_init(base): |
| 27 | + class ModelWithKwargsInit(base): |
| 28 | + __tablename__ = 'model_with_kwargs_init' |
| 29 | + |
| 30 | + id = Column(Integer, primary_key=True, index=True) |
| 31 | + key1 = Column(String) |
| 32 | + key2 = Column(String) |
| 33 | + |
| 34 | + def __init__(self, key1=0, **kwargs): |
| 35 | + pass |
| 36 | + |
| 37 | + yield ModelWithKwargsInit |
| 38 | + |
| 39 | + |
| 40 | +@pytest.fixture(scope='module') |
| 41 | +def model_with_positional_args_init(base): |
| 42 | + class ModelWithPositionalArgsInit(base): |
| 43 | + __tablename__ = 'model_with_positional_args_init' |
| 44 | + |
| 45 | + id = Column(Integer, primary_key=True, index=True) |
| 46 | + key1 = Column(String) |
| 47 | + key2 = Column(String) |
| 48 | + |
| 49 | + def __init__(self, key1, key2=0): |
| 50 | + pass |
| 51 | + |
| 52 | + yield ModelWithPositionalArgsInit |
| 53 | + |
| 54 | + |
| 55 | +def test_get_model_init_params_names(model_without_init, model_with_kwargs_in_init, |
| 56 | + model_with_positional_args_init): |
| 57 | + args, has_kwargs = get_model_init_params_names(model_without_init) |
| 58 | + assert ([], True) == (args, has_kwargs) |
| 59 | + |
| 60 | + args, has_kwargs = get_model_init_params_names(model_with_kwargs_in_init) |
| 61 | + assert (['key1'], True) == (args, has_kwargs) |
| 62 | + |
| 63 | + args, has_kwargs = get_model_init_params_names(model_with_positional_args_init) |
| 64 | + assert (['key1', 'key2'], False) == (args, has_kwargs) |
| 65 | + |
| 66 | + |
| 67 | +def test_validate_model_init_params(model_with_kwargs_in_init, model_with_positional_args_init): |
| 68 | + schema_attrs = ['id', 'key1', 'key2'] |
| 69 | + invalid_params = validate_model_init_params(model_with_kwargs_in_init, schema_attrs) |
| 70 | + assert invalid_params is None |
| 71 | + |
| 72 | + invalid_params = validate_model_init_params(model_with_positional_args_init, schema_attrs) |
| 73 | + assert invalid_params == ['id'] |
0 commit comments