Coverage for fundamentals/utKit.py : 20%

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#!/usr/bin/env python
2# encoding: utf-8
3"""
4*A unit-testing kit to simplify my unit-tests*
6:Author:
7 David Young
8"""
9from builtins import object
10import sys
11import os
12import logging
13import logging.config
14import yaml
15try:
16 yaml.warnings({'YAMLLoadWarning': False})
17except:
18 pass
21class utKit(object):
22 """
23 *Default setup for fundamentals style unit-testing workflow (all tests base on nose module)*
25 **Key Arguments**
27 - ``moduleDirectory`` -- the directory to the unit-testing test file
30 **Usage**
32 To use this kit within any of your unit-test modules add the following code before your test methods:
34 ```python
35 from fundamentals.utKit import utKit
36 # SETUP AND TEARDOWN FIXTURE FUNCTIONS FOR THE ENTIRE MODULE
37 moduleDirectory = os.path.dirname(__file__)
38 utKit = utKit(moduleDirectory)
39 log, dbConn, pathToInputDir, pathToOutputDir = utKit.setupModule()
40 utKit.tearDownModule()
41 ```
43 """
44 # Initialisation
46 def __init__(
47 self,
48 moduleDirectory
49 ):
50 self.moduleDirectory = moduleDirectory
51 # x-self-arg-tmpx
53 # SETUP PATHS TO COMMON DIRECTORIES FOR TEST DATA
54 self.pathToInputDir = moduleDirectory + "/input/"
55 self.pathToOutputDir = moduleDirectory + "/output/"
57 # SETUP LOGGING
58 self.loggerConfig = """
59 version: 1
60 formatters:
61 file_style:
62 format: '* %(asctime)s - %(name)s - %(levelname)s (%(pathname)s > %(funcName)s > %(lineno)d) - %(message)s '
63 datefmt: '%Y/%m/%d %H:%M:%S'
64 console_style:
65 format: '* %(asctime)s - %(levelname)s: %(pathname)s:%(funcName)s:%(lineno)d > %(message)s'
66 datefmt: '%H:%M:%S'
67 html_style:
68 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>'
69 datefmt: '%Y-%m-%d <span class= "time">%H:%M <span class= "seconds">%Ss</span></span>'
70 handlers:
71 console:
72 class: logging.StreamHandler
73 level: DEBUG
74 formatter: console_style
75 stream: ext://sys.stdout
76 root:
77 level: DEBUG
78 handlers: [console]"""
80 self.dbConfig = """
81 version: 1
82 db: unit_tests
83 host: localhost
84 user: utuser
85 password: utpass
86 """
88 return
90 def setupModule(
91 self):
92 """
93 *The setupModule method*
95 **Return**
97 - ``log`` -- a logger
98 - ``dbConn`` -- a database connection to a test database (details from yaml settings file)
99 - ``pathToInputDir`` -- path to modules own test input directory
100 - ``pathToOutputDir`` -- path to modules own test output directory
102 """
104 ## VARIABLES ##
105 logging.config.dictConfig(yaml.load(self.loggerConfig))
106 log = logging.getLogger(__name__)
107 if self.dbConfig:
108 import pymysql as ms
109 connDict = yaml.load(self.dbConfig)
110 dbConn = ms.connect(
111 host=connDict['host'],
112 user=connDict['user'],
113 passwd=connDict['password'],
114 db=connDict['db'],
115 use_unicode=True,
116 charset='utf8',
117 local_infile=1,
118 client_flag=ms.constants.CLIENT.MULTI_STATEMENTS,
119 connect_timeout=3600
120 )
121 dbConn.autocommit(True)
122 else:
123 dbConn = False
125 return log, dbConn, self.pathToInputDir, self.pathToOutputDir
127 def tearDownModule(
128 self):
129 """
130 *The tearDownModule method*
131 """
133 return None
135 def get_project_root(self):
136 """
137 *Get the root of the `python` package - useful for getting files in the root directory of a project*
139 **Return**
141 - ``rootPath`` -- the root path of a project
143 """
144 import os
145 rootPath = os.path.dirname(__file__)
147 return rootPath
149 def refresh_database(self):
150 """
151 *Refresh the unit test database*
152 """
153 from fundamentals.mysql import directory_script_runner
154 from fundamentals import tools
155 packageDirectory = self.get_project_root()
156 su = tools(
157 arguments={"settingsFile": packageDirectory +
158 "/test_settings.yaml"},
159 docString=__doc__,
160 logLevel="DEBUG",
161 options_first=False,
162 projectName=None,
163 defaultSettingsFile=False
164 )
165 arguments, settings, log, dbConn = su.setup()
166 directory_script_runner(
167 log=log,
168 pathToScriptDirectory=packageDirectory + "/tests/input",
169 dbConn=dbConn,
170 successRule=None,
171 failureRule=None
172 )
174if __name__ == '__main__':
175 main()