Skip to content

Commit

Permalink
Merge pull request #158 from jdkandersson/bug/constructable-xtablenam…
Browse files Browse the repository at this point in the history
…e-malformed

Bug/constructable xtablename malformed
  • Loading branch information
jdkandersson authored Aug 16, 2020
2 parents aa0d078 + d77f896 commit 4a2d634
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Release Notes

## Version _next_

- Fix bug where iterating over constructable schemas did not handle some exceptions
- Add black dependency back in

## Version 1.4.1 - 2020-08-09

- Remove black dependency
Expand Down
1 change: 1 addition & 0 deletions open_alchemy/facades/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Facades for ringfenced modules."""
# pylint: disable=useless-import-alias

from . import code_formatter as code_formatter
from . import jsonschema as jsonschema
from . import models as models
from . import sqlalchemy as sqlalchemy
22 changes: 22 additions & 0 deletions open_alchemy/facades/code_formatter/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""Apply automatic code formatting to source code."""

import black


def apply(*, source: str) -> str:
"""
Apply automatic code formatting to source code.
Args:
source: The source code.
Returns:
The formatted source code.
"""
try:
return black.format_file_contents(
src_contents=source, fast=False, mode=black.FileMode()
)
except black.NothingChanged:
return source
3 changes: 2 additions & 1 deletion open_alchemy/models_file/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ def generate_models(self) -> str:
model_sources.append(model_source)

# Generate source code for models file
return _models.generate(models=model_sources)
raw_source = _models.generate(models=model_sources)
return facades.code_formatter.apply(source=raw_source)
5 changes: 4 additions & 1 deletion open_alchemy/schemas/helpers/iterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ def constructable(
"""
for name, schema in schemas.items():
if not helpers.schema.constructable(schema=schema, schemas=schemas):
try:
if not helpers.schema.constructable(schema=schema, schemas=schemas):
continue
except (exceptions.MalformedSchemaError, exceptions.SchemaNotFoundError):
continue

yield name, schema
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ markers =
init
slow
schemas
code_formatter
python_functions = test_*
mocked-sessions = examples.app.database.db.session

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"jsonschema>=3",
"Jinja2>=2",
"sqlalchemy-stubs>=0.3",
"black",
],
include_package_data=True,
extras_require={
Expand Down
25 changes: 25 additions & 0 deletions tests/open_alchemy/facades/test_code_formatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"""Tests for code formatting."""

import pytest

from open_alchemy import facades


@pytest.mark.parametrize(
"source, expected_source",
[
pytest.param("'test'\n", '"test"\n', id="apply formatting"),
pytest.param('"test"\n', '"test"\n', id="no formatting"),
],
)
@pytest.mark.facade
@pytest.mark.code_formatter
def test_apply(source, expected_source):
"""
GIVEN source code and expected source code
WHEN apply is called with the source code
THEN the expected source code is returned.
"""
returned_source = facades.code_formatter.apply(source=source)

assert returned_source == expected_source
2 changes: 1 addition & 1 deletion tests/open_alchemy/integration/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class TTable({expected_model_base}):
query: orm.Query
# Model properties
column: 'sqlalchemy.Column[typing.Optional[int]]'
column: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, column: typing.Optional[int] = None) -> None:
"""
Expand Down
28 changes: 16 additions & 12 deletions tests/open_alchemy/models_file/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Tests for models file."""

# pylint: disable=line-too-long

import sys

import pytest
Expand Down Expand Up @@ -59,7 +57,7 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down Expand Up @@ -157,7 +155,7 @@ class TModel1({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down Expand Up @@ -239,7 +237,7 @@ class TModel2({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[str]]'
id: "sqlalchemy.Column[typing.Optional[str]]"
def __init__(self, id: typing.Optional[str] = None) -> None:
"""
Expand Down Expand Up @@ -366,7 +364,7 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down Expand Up @@ -480,10 +478,12 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
name: 'sqlalchemy.Column[typing.Optional[str]]'
id: 'sqlalchemy.Column[typing.Optional[int]]'
name: "sqlalchemy.Column[typing.Optional[str]]"
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, name: typing.Optional[str] = None, id: typing.Optional[int] = None) -> None:
def __init__(
self, name: typing.Optional[str] = None, id: typing.Optional[int] = None
) -> None:
"""
Construct.
Expand All @@ -495,7 +495,9 @@ def __init__(self, name: typing.Optional[str] = None, id: typing.Optional[int] =
...
@classmethod
def from_dict(cls, name: typing.Optional[str] = None, id: typing.Optional[int] = None) -> "TModel":
def from_dict(
cls, name: typing.Optional[str] = None, id: typing.Optional[int] = None
) -> "TModel":
"""
Construct from a dictionary (eg. a POST payload).
Expand Down Expand Up @@ -542,7 +544,9 @@ def to_str(self) -> str:
Model: typing.Type[TModel] = models.Model # type: ignore
''' # noqa: E501
'''
print(repr(source))
print(repr(expected_source))
assert source == expected_source


Expand Down Expand Up @@ -593,7 +597,7 @@ class TModel({_EXPECTED_MODEL_BASE}):
query: orm.Query
# Model properties
id: 'sqlalchemy.Column[typing.Optional[int]]'
id: "sqlalchemy.Column[typing.Optional[int]]"
def __init__(self, id: typing.Optional[int] = None) -> None:
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/open_alchemy/schemas/helpers/test_iterate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
[
pytest.param({}, [], id="empty"),
pytest.param({"Schema1": {}}, [], id="single not"),
pytest.param(
{"Schema1": {"x-tablename": True}}, [], id="single malformed tablename"
),
pytest.param(
{"Schema1": {"x-inherits": 1}}, [], id="single malformed inherits"
),
pytest.param(
{"Schema1": {"allOf": [{"$ref": "#/components/schemas/Schema2"}, {}]}},
[],
id="single missing reference",
),
pytest.param(
{"Schema1": {"x-tablename": "table 1"}},
[("Schema1", {"x-tablename": "table 1"})],
Expand Down

0 comments on commit 4a2d634

Please sign in to comment.