Coverage for dryxPyramid/utKit.py : 57%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2*Unit testing tools*
3"""
4import os
5import shutil
6import unittest
7import yaml
8from pyramid import testing
9from pyramid.path import AssetResolver
10from pyramid.request import apply_request_extensions
11from paste.deploy.loadwsgi import appconfig
12import pymysql as ms
13from sqlalchemy import engine_from_config
14from sqlalchemy.orm import sessionmaker
15from fundamentals import utKit
17# OVERRIDES
20class utKit(utKit):
21 """
22 *Override dryx utKit*
23 """
24 # Variable Data Atrributes
26 # Override Variable Data Atrributes
27 # Initialisation
28 def __init__(
29 self,
30 moduleDirectory,
31 dbConn=False
32 ):
33 self.moduleDirectory = moduleDirectory
34 # x-self-arg-tmpx
36 # SETUP PATHS TO COMMON DIRECTORIES FOR TEST DATA
37 self.pathToInputDir = moduleDirectory + "/input/"
38 self.pathToOutputDir = moduleDirectory + "/output/"
40 # SETUP LOGGING
41 self.loggerConfig = """
42 version: 1
43 formatters:
44 file_style:
45 format: '* %(asctime)s - %(name)s - %(levelname)s (%(filename)s > %(funcName)s > %(lineno)d) - %(message)s '
46 datefmt: '%Y/%m/%d %H:%M:%S'
47 console_style:
48 format: '* %(asctime)s - %(levelname)s: %(filename)s:%(funcName)s:%(lineno)d > %(message)s'
49 datefmt: '%H:%M:%S'
50 html_style:
51 format: '<div id="row" class="%(levelname)s"><span class="date">%(asctime)s</span> <span class="label">file:</span><span class="filename">%(filename)s</span> <span class="label">method:</span><span class="funcName">%(funcName)s</span> <span class="label">line#:</span><span class="lineno">%(lineno)d</span> <span class="pathname">%(pathname)s</span> <div class="right"><span class="message">%(message)s</span><span class="levelname">%(levelname)s</span></div></div>'
52 datefmt: '%Y-%m-%d <span class= "time">%H:%M <span class= "seconds">%Ss</span></span>'
53 handlers:
54 console:
55 class: logging.StreamHandler
56 level: DEBUG
57 formatter: console_style
58 stream: ext://sys.stdout
59 root:
60 level: DEBUG
61 handlers: [console]"""
63 # Override Variable Data Atrributes
64 self.dbConfig = False
65 if dbConn:
66 self.dbConfig = """
67 version: 1
68 db: unit_tests
69 host: localhost
70 user: utuser
71 password: utpass
72 """
74 return
76 def get_project_root(self):
77 """
78 *Get the root of the `python` package - useful for getting files in the root directory of a project*
80 **Return**
82 - ``rootPath`` -- the root path of a project
84 """
85 import os
86 rootPath = os.path.dirname(__file__)
88 return rootPath
90 def refresh_database(self):
91 """
92 *Refresh the unit test database*
93 """
94 from fundamentals.mysql import directory_script_runner
95 from fundamentals import tools
96 packageDirectory = self.get_project_root()
97 su = tools(
98 arguments={"settingsFile": packageDirectory +
99 "/test_settings.yaml"},
100 docString=__doc__,
101 logLevel="DEBUG",
102 options_first=False,
103 projectName=None,
104 defaultSettingsFile=False
105 )
106 arguments, settings, log, dbConn = su.setup()
107 directory_script_runner(
108 log=log,
109 pathToScriptDirectory=packageDirectory + "/tests/input",
110 dbConn=dbConn,
111 successRule=None,
112 failureRule=None
113 )
116def db(request):
117 # database connection
118 maker = request.registry.dbmaker
119 session = maker()
121 def cleanup(request):
122 if request.exception is not None:
123 session.rollback()
124 else:
125 session.commit()
126 session.close()
127 request.add_finished_callback(cleanup)
129 return session
132class BaseTest(unittest.TestCase):
134 def __init__(self, *args, **kwargs):
135 unittest.TestCase.__init__(self, *args, **kwargs)
137 def setUp(self):
138 '''This is the default setup method for pyramid unit tests. It will instantiate a database connection to the unit_tesing database on localhost
139 '''
140 moduleDirectory = os.path.dirname(__file__)
141 app_settings = appconfig(
142 'config:' + self.testIni)
143 from webtest import TestApp
144 self.testapp = TestApp('config:' + self.testIni)
145 # test user and test pass need to be adding to the test_settings.yaml
146 # file
147 self.testapp.post(
148 '/login', params={'login': self.settings["test user"], 'password': self.settings["test pass"]})
150 def tearDown(self):
151 testing.tearDown()