Commit 9520dd48 authored by Patrick van der Leer's avatar Patrick van der Leer
Browse files

Made the unittest my bitch, coverage of 77%!

parent a265db59
......@@ -36,12 +36,15 @@ class PrettyPrint:
fields = {}
skip = ["metadata", "query", "query_class"]
for field in [x for x in dir(obj) if not x.startswith('_') and x not in skip]:
# temp fix
if not hasattr(obj, field):
continue
# temp fix
if isfunction(obj.__getattribute__(field)) or ismethod(obj.__getattribute__(field)):
continue
from flask_sqlalchemy import BaseQuery
from sqlalchemy.orm.dynamic import AppenderQuery
if isinstance(obj.__getattribute__(field), dt):
fields[field] = str(obj.__getattribute__(field))
elif field == "versions" and versions:
......
from .core import AuthorFactory, BookFactory, BookEditionFactory, BookSerieFactory, GenreFactory, PublisherFactory
from .user import UserFactory
__all__ = (
AuthorFactory,
......@@ -6,5 +7,6 @@ __all__ = (
BookEditionFactory,
BookSerieFactory,
GenreFactory,
PublisherFactory
PublisherFactory,
UserFactory
)
from faker.providers import BaseProvider
from eBookHub.utils import languages
from pycountry import languages as pycountry_languages
class LanguageProvider(BaseProvider):
def iso639_2(self):
return self.random_element(languages)
return self.random_element([language.alpha_2 for language in pycountry_languages if hasattr(language, 'alpha_2')])
import factory
from passlib.apps import custom_app_context as pwd_context
from eBookHub import db
from eBookHub.models import User
class UserFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = User
sqlalchemy_session = db.session
id = factory.Sequence(lambda n: n)
name = factory.Faker('name')
email = factory.Faker('email')
@factory.post_generation
def password_hash(self, create, extracted, **kwargs):
return pwd_context.hash("{}:{}:{}".format(self.id, self.name, self.email))
__all__ = (
UserFactory
)
......@@ -3,7 +3,7 @@
from eBookHub import init
from eBookHub.extensions import db as _db
from eBookHub.config import TestConfig
from eBookHub.database import SoftDeleteMixin
from eBookHub.database import SoftDeleteMixin, PrettyPrint
class MyTestCase(object):
......@@ -27,7 +27,8 @@ class MyTestCase(object):
class ModelTestCase(MyTestCase):
app = None
model = None
factory = None
def test_factory(self):
"""Test factory instance"""
......@@ -50,3 +51,8 @@ class ModelTestCase(MyTestCase):
assert self.model.get(pk).deleted
inst.delete(hard_delete=True)
assert self.model.get(pk) is None
def test_pretty_print(self):
inst = self.factory()
if isinstance(inst, PrettyPrint):
inst.serialize()
# -*- coding: utf-8 -*-
from flask_testing import TestCase
from eBookHub.models import Book, BookEdition, BookSerie
from eBookHub.models.factory import BookFactory, BookEditionFactory, BookSerieFactory
from tests.base import ModelTestCase
class BookModelTestCase(ModelTestCase, TestCase):
model = Book
factory = BookFactory
class BookEditionModelTestCase(ModelTestCase, TestCase):
model = BookEdition
factory = BookEditionFactory
class BookSerieModelTestCase(ModelTestCase, TestCase):
model = BookSerie
factory = BookSerieFactory
# -*- coding: utf-8 -*-
from flask_testing import TestCase
from eBookHub.models import Publisher
from eBookHub.models.factory import PublisherFactory
from tests.base import ModelTestCase
class PublisherModelTestCase(ModelTestCase, TestCase):
model = Publisher
factory = PublisherFactory
# -*- coding: utf-8 -*-
from flask_testing import TestCase
from eBookHub.models import User
from eBookHub.models.factory import UserFactory
from tests.base import ModelTestCase
class UserModelTestCase(ModelTestCase, TestCase):
model = User
factory = UserFactory
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment