11# TODO - refactor to clean up and document better
2+ import csv
3+ import pandas as pd
4+ import numpy as np
5+
6+ def output_decorator (func ):
7+ def inner (* args , ** kwargs ):
8+ t = func (* args , ** kwargs )
9+ print (f'{ t .results } instances detected' )
10+ print (f'Results saved at { t .output } ' )
11+ return
12+ return inner
13+
214
315class Test_1_Procedures :
16+
417 # 3.1.1 - Test 1.1 Check for gaps in journal entry numbers
5- def check_for_gaps_in_JE_ID (GL_Detail_YYYYMMDD_YYYYMMDD ):
18+ # This method assumes JE's are already sorted in ascending order
19+
20+ @output_decorator
21+ def check_for_gaps_in_JE_ID (GL_Detail_YYYYMMDD_YYYYMMDD , Journal_ID_Column = 'Journal_ID' ):
22+
623 print ('Checking for gaps in Journal Entry IDs is started' )
7- from collections import deque
8- import csv
924 writer = csv .writer (open ("Output_Folder/Test_3_1_1_check_for_gaps_in_JE_ID.csv" , 'w' ))
10- je_nums = deque ( maxlen = 2 )
25+
1126 gaps = []
12- for item in GL_Detail_YYYYMMDD_YYYYMMDD ['Journal_ID' ]:
13- je_nums .append (item )
14- if len (je_nums ) == 1 :
27+ previous = None
28+
29+ for item in GL_Detail_YYYYMMDD_YYYYMMDD [Journal_ID_Column ]:
30+ if not previous :
31+ previous = item
1532 continue
16- if je_nums [1 ] - je_nums [0 ] > 1 :
17- writer .writerow (['Gap identified! {} is followed by {}' .format (* je_nums )])
18- gaps .append (list (je_nums ))
33+
34+ if item - previous > 1 :
35+ writer .writerow ([f'Gap identified! { previous } is followed by { item } ' ])
36+ gaps .append ([previous , item ])
37+
38+ previous = item
1939
2040 writer .writerow (['Test Results:' ])
21- writer .writerow (['Total of {} gaps found' . format ( len ( gaps )) ])
22- print ( '%d instances detected' % len ( gaps ))
23- print ( 'Results saved at Output_Folder/Test_3_1_1_check_for_gaps_in_JE_ID.csv' )
41+ writer .writerow ([f 'Total of { len ( gaps ) } gaps found' ])
42+
43+ return ({ "results" : len ( gaps ), "output" : " Output_Folder/Test_3_1_1_check_for_gaps_in_JE_ID.csv" } )
2444
2545
2646 # 3.1.2 Compare listing of journal entry numbers from system to log file
@@ -131,7 +151,6 @@ def check_for_weekend_entries(GL_Detail_YYYYMMDD_YYYYMMDD):
131151
132152 # Check if Entry Time falls on between 8pm and 6am
133153 def check_for_nights_entries (GL_Detail_YYYYMMDD_YYYYMMDD ):
134- import pandas as pd
135154 print ('Checking for Night Entries is started' )
136155 from datetime import datetime
137156 GL_Copy = GL_Detail_YYYYMMDD_YYYYMMDD [['Journal_ID' , 'Entered_Date' , 'Entered_Time' ]].copy ()
@@ -149,8 +168,7 @@ def check_for_nights_entries(GL_Detail_YYYYMMDD_YYYYMMDD):
149168
150169 #Check for individuals who posted 10 or fewer entries and identify entries made by these individuals
151170 def check_for_rare_users (GL_Detail_YYYYMMDD_YYYYMMDD ):
152- import pandas as pd
153- import numpy as np
171+
154172 print ('Checking for Rare Users is started' )
155173 GL_Pivot = GL_Detail_YYYYMMDD_YYYYMMDD .pivot_table (index = ['Entered_By' ], values = 'Journal_ID' ,
156174 aggfunc = np .count_nonzero ).fillna (0 )
@@ -164,8 +182,7 @@ def check_for_rare_users(GL_Detail_YYYYMMDD_YYYYMMDD):
164182
165183 # Check for accounts that were used 3 or fewer times and identify entries made to these accounts
166184 def check_for_rare_accounts (GL_Detail_YYYYMMDD_YYYYMMDD ):
167- import pandas as pd
168- import numpy as np
185+
169186 print ('Checking for Rare Accounts is started' )
170187 GL_Pivot = GL_Detail_YYYYMMDD_YYYYMMDD .pivot_table (index = ['GL_Account_Number' ], values = 'Journal_ID' ,
171188 aggfunc = np .count_nonzero ).fillna (0 )
@@ -175,4 +192,4 @@ def check_for_rare_accounts(GL_Detail_YYYYMMDD_YYYYMMDD):
175192 failed_test = GL_Copy .merge (Rare_Accounts , on = ['GL_Account_Number' ], how = 'right' ).fillna (0 )
176193 failed_test .to_csv ('Output_Folder/Test_3_2_6.2_check_for_rare_accounts.csv' )
177194 print ('%d instances detected' % len (failed_test ['GL_Account_Number' ]))
178- print ('Results saved at Output_Folder/Test_3_2_6.2_check_for_rare_accounts.csv' )
195+ print ('Results saved at Output_Folder/Test_3_2_6.2_check_for_rare_accounts.csv' )
0 commit comments