Hide keyboard shortcuts

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* 

5 

6:Author: 

7 David Young 

8 

9:Date Created: 

10 April 16, 2014 

11""" 

12################# GLOBAL IMPORTS #################### 

13from builtins import object 

14import sys 

15import os 

16import logging 

17import logging.config 

18import yaml 

19try: 

20 yaml.warnings({'YAMLLoadWarning': False}) 

21except: 

22 pass 

23 

24 

25class utKit(object): 

26 

27 """ 

28 *Default setup for fundamentals style unit-testing workflow (all tests base on nose module)* 

29 

30 **Key Arguments:** 

31 - ``moduleDirectory`` -- the directory to the unit-testing test file 

32 

33 **Usage:** 

34 To use this kit within any of your unit-test modules add the following code before your test methods: 

35 

36 .. code-block:: python  

37 

38 from fundamentals.utKit import utKit 

39 # SETUP AND TEARDOWN FIXTURE FUNCTIONS FOR THE ENTIRE MODULE 

40 moduleDirectory = os.path.dirname(__file__) 

41 utKit = utKit(moduleDirectory) 

42 log, dbConn, pathToInputDir, pathToOutputDir = utKit.setupModule() 

43 utKit.tearDownModule()  

44 """ 

45 # Initialisation 

46 

47 def __init__( 

48 self, 

49 moduleDirectory 

50 ): 

51 self.moduleDirectory = moduleDirectory 

52 # x-self-arg-tmpx 

53 

54 # SETUP PATHS TO COMMON DIRECTORIES FOR TEST DATA 

55 self.pathToInputDir = moduleDirectory + "/input/" 

56 self.pathToOutputDir = moduleDirectory + "/output/" 

57 

58 # SETUP LOGGING 

59 self.loggerConfig = """ 

60 version: 1 

61 formatters: 

62 file_style: 

63 format: '* %(asctime)s - %(name)s - %(levelname)s (%(pathname)s > %(funcName)s > %(lineno)d) - %(message)s ' 

64 datefmt: '%Y/%m/%d %H:%M:%S' 

65 console_style: 

66 format: '* %(asctime)s - %(levelname)s: %(pathname)s:%(funcName)s:%(lineno)d > %(message)s' 

67 datefmt: '%H:%M:%S' 

68 html_style: 

69 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>' 

70 datefmt: '%Y-%m-%d <span class= "time">%H:%M <span class= "seconds">%Ss</span></span>' 

71 handlers: 

72 console: 

73 class: logging.StreamHandler 

74 level: DEBUG 

75 formatter: console_style 

76 stream: ext://sys.stdout 

77 root: 

78 level: DEBUG 

79 handlers: [console]""" 

80 

81 self.dbConfig = """ 

82 version: 1 

83 db: unit_tests 

84 host: localhost 

85 user: utuser 

86 password: utpass 

87 """ 

88 

89 return 

90 

91 def setupModule( 

92 self): 

93 """ 

94 *The setupModule method* 

95 

96 **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 

101 """ 

102 import pymysql as ms 

103 ## VARIABLES ## 

104 logging.config.dictConfig(yaml.load(self.loggerConfig)) 

105 log = logging.getLogger(__name__) 

106 if self.dbConfig: 

107 connDict = yaml.load(self.dbConfig) 

108 dbConn = ms.connect( 

109 host=connDict['host'], 

110 user=connDict['user'], 

111 passwd=connDict['password'], 

112 db=connDict['db'], 

113 use_unicode=True, 

114 charset='utf8', 

115 local_infile=1, 

116 client_flag=ms.constants.CLIENT.MULTI_STATEMENTS, 

117 connect_timeout=3600 

118 ) 

119 dbConn.autocommit(True) 

120 else: 

121 dbConn = False 

122 

123 return log, dbConn, self.pathToInputDir, self.pathToOutputDir 

124 

125 def tearDownModule( 

126 self): 

127 """ 

128 *The tearDownModule method* 

129 

130 **Key Arguments:** 

131 # - 

132 

133 **Return:** 

134 - None 

135 """ 

136 

137 return None 

138 

139 

140if __name__ == '__main__': 

141 main()