Skip to content

Commit

Permalink
Improvements for SoberReport
Browse files Browse the repository at this point in the history
  • Loading branch information
otech-nl committed Oct 23, 2018
1 parent ab9af9d commit 64921ea
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
.tox
build
dist
venv
docs
8 changes: 8 additions & 0 deletions .pytest_cache/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# pytest cache directory #

This directory contains data from the pytest's cache plugin,
which provides the `--lf` and `--ff` options, as well as the `cache` fixture.

**Do not** commit this to version control.

See [the docs](https://docs.pytest.org/en/latest/cache.html) for more information.
6 changes: 6 additions & 0 deletions .pytest_cache/v/cache/nodeids
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
"tests/test_base.py::test_basemodel",
"tests/test_decorators.py::test_inheritance",
"tests/test_decorators.py::test_reference",
"tests/test_decorators.py::test_crossreference"
]
12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask-sqlangelo = {editable = true, path = ".", extras = ["test"]}

[dev-packages]

[requires]
python_version = "3.6"
185 changes: 185 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SQLAngelo
#######

SQLAlchemy with defaults and sugar.
Flask SQLAlchemy with convenience and sugar.

Documentation
===============
Expand Down
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[aliases]
test=pytest

[tool:pytest]
testpaths=tests
addopts=--doctest-modules

13 changes: 12 additions & 1 deletion sqlangelo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" Wrapper around Flask-SQLAlchemy and friends """
from flask_sqlalchemy import SQLAlchemy
from . import decorators, mixins, types

class SQLAngelo(SQLAlchemy):

Expand All @@ -20,7 +21,17 @@ def __init__(self, app, db_uri, debug=False): # noqa: C901
# connect tot database
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # suppress warning
app.config['SQLALCHEMY_DATABASE_URI'] = db_uri
super(SQLAngelo, self).__init__(app)
super(SQLAngelo, self).__init__(app, session_options={
'expire_on_commit': False # as per https://gist.github.com/krak3n/9fa1268ee0a92a67f71a
})

# create basemodel
self.BaseModel = base.get_base_model(self)
self.decorators = decorators
self.mixins = mixins
self.types = types

def init(self):
''' (re)create the database '''
self.drop_all()
self.create_all()
5 changes: 3 additions & 2 deletions sqlangelo/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .mixins import CRUDMixin, NamingMixin, inflect_engine
from .mixins import CRUD, Naming, inflect_engine


def log(msg):
Expand All @@ -8,7 +8,7 @@ def log(msg):

def get_base_model(db): # noqa: C901

class BaseModel(db.Model, CRUDMixin, NamingMixin):
class BaseModel(db.Model, CRUD, Naming):
""" used as super model for all other models
:var id: every model should have a unique id
Expand Down Expand Up @@ -131,6 +131,7 @@ def add_cross_reference(cls, peer_cls, names=None, x_names=None, x_cls=None):
setattr(cls, x_names[0],
db.relationship(
peer_cls.__name__,
lazy='dynamic',
secondary=x_cls.__tablename__,
backref=db.backref(x_names[1], lazy='dynamic'),
**kwargs))
Expand Down
10 changes: 4 additions & 6 deletions sqlangelo/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
inflect_engine = inflect.engine()


class NamingMixin(object):
class Naming(object):
""" Provide some convenient names for models. """

@classmethod
Expand All @@ -23,7 +23,7 @@ def get_plural(cls):
return inflect_engine.plural(cls.get_api())


class IntrospectionMixin(object):
class Introspection(object):
""" access to meta data about a model
Available as app.db.Introspectionmixin
Expand Down Expand Up @@ -74,7 +74,7 @@ def to_dict(self, remove=''):
return {c: getattr(self, c) for c in self.columns(remove=remove)}


class CRUDMixin(object):
class CRUD(object):
""" provide Create, Read, Update and Delete (CRUD) methods
Available as app.db.CRUDmixin.
Expand Down Expand Up @@ -136,8 +136,6 @@ def update(self, commit=True, report=True, **kwargs):
self.report('Updating %s "%s": %s' % (self.__class__.__name__,
self,
pformat(kwargs)))
log('UPDATE %s' % self)
log(' %s' % kwargs)
self.before_update(kwargs)
for attr, value in kwargs.items():
setattr(self, attr, value)
Expand Down Expand Up @@ -211,7 +209,7 @@ def bulk_insert(cls, new_records):
cls.commit()


class OperationsMixin(object):
class Operations(object):

@classmethod
def get(cls, id):
Expand Down
12 changes: 3 additions & 9 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,23 @@
########################################


class BaseModel(db.BaseModel, mixins.CRUDMixin):
__abstract__ = True

########################################


class Group(BaseModel):
class Group(db.BaseModel):
abbr = db.Column(db.Unicode(6), nullable=False)

def __str__(self):
return self.abbr


@decorators.add_cross_reference(Group)
@decorators.make_polymorphic_top(BaseModel, 'User Employee')
@decorators.make_polymorphic_top(db.BaseModel, 'User Employee')
class User(object):
email = db.Column(db.Unicode(30), nullable=False)

def __str__(self):
return '%s (%s)' % (self.email, ', '.join([str(g) for g in self.groups]))


class Company(BaseModel):
class Company(db.BaseModel):
name = db.Column(db.Unicode(30), nullable=False)

def __str__(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tests import models
from tests.models import db


def test_basemodel():
assert models.BaseModel.__abstract__
assert hasattr(models.BaseModel, 'id')
assert db.BaseModel.__abstract__
assert hasattr(db.BaseModel, 'id')
11 changes: 11 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Tox (https://tox.readthedocs.io/) is a tool for running tests
# in multiple virtualenvs. This configuration file will run the
# test suite on all supported python versions. To use it, "pip install tox"
# and then run "tox" from this directory.

[tox]
# envlist = py27, py35
envlist = py27

[testenv]
commands = {envpython} setup.py test

0 comments on commit 64921ea

Please sign in to comment.