-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
126 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
make/activate.sh | ||
/usr/local/lib/python-makelib/activate.sh |
Submodule make
deleted from
072e72
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import sqlalchemy as sa | ||
from sqlalchemy.orm import MappedColumn | ||
from sqlalchemy.orm import DeclarativeBase, mapped_column | ||
from bddrest import status, response, when, given | ||
|
||
from yhttp.core import json | ||
from yhttp.ext import sqlalchemy as saext, dbmanager | ||
from yhttp.ext.sqlalchemy import metadata as m | ||
|
||
|
||
def test_metadata(Given, freshdb, app): | ||
field_title = m.String('title', length=(1, 3)) | ||
field_alias = m.String('alias', optional=True, default='OOF') | ||
|
||
class Base(DeclarativeBase): | ||
pass | ||
|
||
class Foo(Base): | ||
__tablename__ = 'foo' | ||
|
||
id = mapped_column(sa.Integer, primary_key=True) | ||
title = field_title.column(nullable=False) | ||
alias = field_alias.column() | ||
|
||
dbmanager.install(app) | ||
saext.install(app, Base) | ||
app.ready() | ||
app.db.create_objects() | ||
|
||
@app.route() | ||
@app.bodyguard((field_title, field_alias), strict=True) | ||
@json | ||
def post(req): | ||
session = app.db.session | ||
with session.begin(): | ||
f = Foo(title=req.form['title'], alias=req.form['alias']) | ||
session.add(f) | ||
|
||
return dict(id=f.id, title=f.title, alias=f.alias) | ||
|
||
with Given(verb='post', form=dict(title='foo', alias='FOO')): | ||
assert status == 200 | ||
assert response.json == dict(id=1, title='foo', alias='FOO') | ||
|
||
when(form=given - 'title') | ||
assert status == '400 title: Required' | ||
|
||
when(form=given - 'alias') | ||
assert status == 200 | ||
assert response.json == dict(id=2, title='foo', alias='OOF') | ||
|
||
|
||
def test_metadata_args(): | ||
col = m.String('foo', length=(0, 20)).column() | ||
assert isinstance(col, MappedColumn) | ||
assert isinstance(col.column.type, sa.String) | ||
assert col.column.type.length == 20 | ||
|
||
col = m.String('foo').column() | ||
assert isinstance(col, MappedColumn) | ||
assert isinstance(col.column.type, sa.String) | ||
assert col.column.type.length is None | ||
|
||
col = m.Integer('foo').column() | ||
assert isinstance(col, MappedColumn) | ||
assert isinstance(col.column.type, sa.Integer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import abc | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy.orm import mapped_column as sa_mapped_column | ||
|
||
from yhttp.core import guard as yguard | ||
|
||
|
||
class FieldMixin(metaclass=abc.ABCMeta): | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
|
||
@property | ||
@abc.abstractmethod | ||
def _satype(self): | ||
raise NotImplementedError | ||
|
||
def column(self, *args, nullable=None, **kwargs): | ||
if nullable is not None: | ||
kwargs['nullable'] = nullable | ||
else: | ||
kwargs['nullable'] = self.optional | ||
|
||
return sa_mapped_column( | ||
self._satype, | ||
*args, | ||
**kwargs | ||
) | ||
|
||
|
||
class String(FieldMixin, yguard.String): | ||
@property | ||
def _satype(self): | ||
if self.length: | ||
return sa.String(self.length[1]) | ||
return sa.String | ||
|
||
|
||
class Integer(FieldMixin, yguard.Integer): | ||
@property | ||
def _satype(self): | ||
return sa.Integer |