Skip to content

Commit

Permalink
Merge pull request #75 from teoti001/ci-tests
Browse files Browse the repository at this point in the history
explicitly run Pylint and Mypy in CI

This adds type hints to a few places, fixes a few pylint issues and runs it in CI tasks
  • Loading branch information
Spindel authored Jul 21, 2021
2 parents 15a8c41 + f8fba40 commit 5c8df81
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 48 deletions.
19 changes: 19 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ caramel:flake:
script:
- flake8 caramel/ tests/

caramel:mypy:
stage: test
when: always
image: ${PYTHON_IMAGE}
before_script:
- pip3 install mypy "sqlalchemy[mypy]"
script:
- mypy --install-types --non-interactive --config-file=setup.cfg caramel/ tests/

caramel:pylint:
stage: test
when: always
image: ${PYTHON_IMAGE}
before_script:
- pip3 install pylint pylint-exit
- python3 setup.py develop
script:
- pylint --rcfile=setup.cfg caramel/ tests/ || pylint-exit --error-fail $?

rebase:check:
extends: .rebase
stage: rebase
Expand Down
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ CLIENT_CERT := $(VENV)/client.crt
CARAMEL_TOOL := $(VENV)/bin/caramel_tool

# Terminal formatting
BOLD := tput bold
PASS := $(BOLD); tput setaf 2
FAIL := $(BOLD); tput setaf 1
LINE := echo "---------------------------------------"
RESET_TERM := tput op; tput sgr0
BOLD := printf "\033[1m"
PASS := $(BOLD); printf "\033[32m"
FAIL := $(BOLD); printf "\033[31m"
LINE := $(BOLD); echo "---------------------------------------"
RESET_TERM := printf "\033[0m"
BLR := $(BOLD); $(LINE); $(RESET_TERM) #Bold Line, Reset formatting

#Check for python3 install and virtual environment
Expand Down
21 changes: 9 additions & 12 deletions caramel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import sqlalchemy as _sa
from sqlalchemy.ext.declarative import (
declarative_base as _declarative_base,
declared_attr as _declared_attr,
declared_attr,
as_declarative
)
import sqlalchemy.orm as _orm
from zope.sqlalchemy import register
Expand All @@ -14,7 +14,7 @@
import datetime as _datetime
import dateutil.parser
import uuid

from typing import List

X509_V3 = 0x2 # RFC 2459, 4.1.2.1

Expand Down Expand Up @@ -88,10 +88,11 @@ def _fkcolumn(referent, *args, **kwargs):
register(DBSession)


@as_declarative()
class Base(object):
@_declared_attr
def __tablename__(cls):
return cls.__name__.lower()
@declared_attr # type: ignore
def __tablename__(cls) -> str: # pylint: disable=no-self-argument
return cls.__name__.lower() # pylint: disable=no-member

id = _sa.Column(_sa.Integer, primary_key=True)

Expand All @@ -108,10 +109,6 @@ def all(cls):
return cls.query().all()


# XXX: Newer versions of sqlalchemy have a decorator variant 'as_declarative'
Base = _declarative_base(cls=Base)


# XXX: not the best of names
def init_session(engine, create=False):
DBSession.configure(bind=engine)
Expand All @@ -135,10 +132,10 @@ class CSR(Base):
orgunit = _sa.Column(_sa.String(_UB_OU_LEN))
commonname = _sa.Column(_sa.String(_UB_CN_LEN))
rejected = _sa.Column(_sa.Boolean(create_constraint=True))
accessed = _orm.relationship(
accessed: List["AccessLog"] = _orm.relationship(
"AccessLog", backref="csr", order_by="AccessLog.when.desc()"
)
certificates = _orm.relationship(
certificates: List["Certificate"] = _orm.relationship(
"Certificate",
backref="csr",
order_by="Certificate.not_after.desc()",
Expand Down
17 changes: 17 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,20 @@ previous = true
[flake8]
max-line-length = 88
extend-ignore = E203

# mypy related settings
[mypy]
plugins = sqlalchemy.ext.mypy.plugin
[mypy-pyramid.*]
ignore_missing_imports = True
[mypy-transaction]
ignore_missing_imports = True
[mypy-zope.*]
ignore_missing_imports = True

[pylint]
generated-members=
scoped_session,
Base,
DBSession,

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

requires = [
"pyramid",
"SQLAlchemy >= 1.1",
"SQLAlchemy >= 1.4",
"transaction",
"pyramid_tm",
"zope.sqlalchemy >= 1.3",
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class CSRFixture(AttributeCollection, SimilarityComparable):
"accessed",
"certificates",
)
commonname: str
pem: str
certificates: list

@property
def _match_excluded_attrs_(self):
Expand Down Expand Up @@ -88,6 +91,11 @@ def __call__(self):


class CertificateFixture(AttributeCollection, SimilarityComparable):
not_before: datetime
not_after: datetime
common_subject: tuple
pem: str

def __call__(self, csr):
# FIXME: adjust when models.Certificate gets a proper __init__
cert = models.Certificate(csr, self.pem)
Expand All @@ -98,6 +106,10 @@ def __call__(self, csr):


class AccessLogFixture(AttributeCollection, SimilarityComparable):
when: datetime
addr: str
pem: str

def __call__(self, csr):
access = models.AccessLog(csr, self.addr)
access.when = self.when
Expand Down
30 changes: 0 additions & 30 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
HTTPNotFound,
)

import transaction

from caramel.models import (
init_session,
DBSession,
CSR,
AccessLog,
)
Expand Down Expand Up @@ -183,30 +180,3 @@ def test_not_signed(self):
csr.accessed[0].when, now, delta=datetime.timedelta(seconds=1)
)
pass


@unittest.skip("Example test case from scaffold, barely altered.")
class TestMyView(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
from sqlalchemy import create_engine

engine = create_engine("sqlite://")
init_session(engine, create=True)
from caramel.models import MyModel

with transaction.manager:
model = MyModel(name="one", value=55)
model.save()

def tearDown(self):
DBSession.remove()
testing.tearDown()

def test_it(self):
from caramel.views import my_view

request = testing.DummyRequest()
info = my_view(request)
self.assertEqual(info["one"].name, "one")
self.assertEqual(info["project"], "caramel")

0 comments on commit 5c8df81

Please sign in to comment.