-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_factory.py
92 lines (79 loc) · 3.17 KB
/
test_factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
""" Test factory
"""
import unittest
from sqlalchemy import (BigInteger, Column, Integer, String, DateTime, Date,
Numeric, Boolean, Float)
from flask_sqlalchemy import SQLAlchemy
from flask import Flask
from flask_restplus import Api
from flask_restplus_sqlalchemy import ApiModelFactory
class TestFactory(unittest.TestCase):
"""Test Factory Logic
"""
@classmethod
def setUpClass(cls):
""" Create some simple test cases
"""
cls.flask_app = Flask(__name__)
cls.flask_app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
cls.flask_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
cls.api: Api = Api(
version='x',
title='test_api',
default='test',
default_label='test',
description='Test')
db: SQLAlchemy = SQLAlchemy()
class Person(db.Model): # pylint: disable=unused-variable
""" Person Entity
"""
__tablename__ = "person"
__abstract__ = False
id: Column = Column(
BigInteger().with_variant(Integer, "sqlite"),
primary_key=True,
nullable=False
)
created_on: Column = Column(DateTime, nullable=False)
active: Column = Column(Boolean, nullable=False)
birth: Column = Column(Date, nullable=False)
first_name: Column = Column(String(100), nullable=False)
middle_name: Column = Column(String(100), nullable=True)
last_name: Column = Column(String(100), nullable=False)
class Fake(db.Model):
""" Abstract entity that should not be placed into API Model factory
"""
__tablename__ = "fake"
__abstract__ = True
class NotFake(Fake): # pylint: disable=unused-variable
""" Inherits abstract class so should appear
"""
__tablename__ = "not_fake"
id: Column = Column(
BigInteger().with_variant(Integer, "sqlite"),
primary_key=True,
nullable=False
)
do_care: Column = Column(Numeric, nullable=False)
do_alt: Column = Column(Float, nullable=False)
cls.db = db
# Note: How init must be called before the factory in called
cls.db.init_app(cls.flask_app)
cls.api_model_factory = ApiModelFactory(api=cls.api, db=cls.db)
def test_fake(self):
""" Verify fake throws expection as it is an abstract class
"""
self.assertRaises(Exception, self.api_model_factory.get_entity, 'fake')
def test_not_fake(self):
""" Verify fake throws expection as it is an abstract class
"""
self.assertIsNotNone(self.api_model_factory.get_entity('not_fake'))
def test_missing(self):
""" Verify object throws expection as it is not database class
"""
self.assertRaises(
Exception, self.api_model_factory.get_entity, 'object')
def test_person(self):
""" Check if person is in the Factory model
"""
self.assertIsNotNone(self.api_model_factory.get_entity('person'))