-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.bat
153 lines (136 loc) · 4.03 KB
/
run.bat
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@ECHO OFF
SET USERNAME=testuser
SET DBNAME=testdb
SET PORT=8899
SET PASSWORD=1234
SET CONTAINERNAME=pg-unittest-projectname
SET CREATE_SQL=.\db\create.sql
SET FUNCTIONS_SQL=.\db\functions.sql
SET IMPORT_SQL=.\db\import.sql
SET TEMP_SQL=temp.sql
SET errorMSG=
REM ==================================
REM MAIN
REM checking command line input
if "%1"=="" goto help
if "%1"=="init" goto runInit
if "%1"=="connect" goto runConnect
if "%1"=="test" goto runTests
SET errorMsg=ERROR: unknown command "%1"
goto help
REM ==================================
REM HELP
:help
ECHO === Postgres Database Unit Test Tool ===
ECHO.
ECHO RUN
ECHO run.bat COMMAND
ECHO.
ECHO DESCRIPTION
ECHO this script simplifies postgres database
ECHO unit tests preparation and execution
ECHO.
ECHO the script expects the following sql files
ECHO to exist:
ECHO - '%CREATE_SQL%'
ECHO creates tables and constraints
ECHO - '%FUNCTIONS_SQL%'
ECHO creates functions, prerequisists (like enums)
ECHO - '%IMPORT_SQL%'
ECHO import test data into the tables
ECHO.
ECHO the script expects test scripts to reside in
ECHO ./tests/
ECHO.
ECHO COMMANDS:
ECHO init
ECHO Hint: just database creation without tests
ECHO 1) removes container, if already existing
ECHO 2) creates the postgres docker container
ECHO 3) creates the database from all above files in order
ECHO 1. '%CREATE_SQL%'
ECHO 2. '%FUNCTIONS_SQL%'
ECHO 3. '%IMPORT_SQL%'
ECHO 4) finally stops the container
ECHO connect
ECHO Hint: either run init or test before this!
ECHO 1) starts the currently existing docker container
ECHO 2) connects to the database in order to debug
ECHO - when done, close connection with '\q'
ECHO 3) after closing connection, stops container
ECHO test
ECHO 1) runs init
ECHO 2) runs pgTap on the database (perform unit tests)
ECHO 3) finally stops the container
ECHO.
ECHO REMARKS:
ECHO script automatically fixes possible issues with encoding
ECHO on window consoles for german UTF-8 characters for all
ECHO psql calls
ECHO.
goto commonExit
REM ----------------------------------
REM INIT DATABASE CONTAINER
:runInit
CALL :createDatabase
docker stop %CONTAINERNAME%
ECHO.
ECHO == done
goto commonExit
REM ----------------------------------
REM CONNECT to the DATABASE
:runConnect
docker start %CONTAINERNAME%
SLEEP 2
set "PGPASSWORD=%PASSWORD%" && psql --username=%USERNAME% --dbname=%DBNAME% --host=localhost --port=%PORT%
docker stop %CONTAINERNAME%
ECHO.
ECHO == done
goto commonExit
REM ----------------------------------
REM UNIT TESTS execution
:runTests
docker start %CONTAINERNAME%
CALL :createDatabase
CALL :runUnitTests
docker stop %CONTAINERNAME%
ECHO.
ECHO == done
goto commonExit
REM ==================================
REM SUBROUTINES
REM ----------------------------------
REM create DATABASE
:createDatabase
ECHO.
ECHO == create fresh container
docker stop %CONTAINERNAME%
docker rm %CONTAINERNAME%
docker run --name %CONTAINERNAME% -e POSTGRES_USER=%USERNAME% -e POSTGRES_PASSWORD=%PASSWORD% -e POSTGRES_DB=%DBNAME% -p %PORT%:5432 -d postgres
SLEEP 2
CALL :insertDatabaseContent
goto :eof
REM ----------------------------------
REM run unit tests
:insertDatabaseContent
ECHO.
ECHO == add tables, functions, data, etc.
TYPE %CREATE_SQL% > %TEMP_SQL%
TYPE %FUNCTIONS_SQL% >> %TEMP_SQL%
TYPE %IMPORT_SQL% >> %TEMP_SQL%
SLEEP 2
set "PGPASSWORD=%PASSWORD%" && psql --username=%USERNAME% --dbname=%DBNAME% --host=localhost --port=%PORT% --file=%TEMP_SQL% --quiet
SLEEP 2
DEL %TEMP_SQL%
goto :eof
REM ----------------------------------
REM run unit tests
:runUnitTests
ECHO.
ECHO == run unit tests
docker run -it --rm --name pgtap --link %CONTAINERNAME%:db -e USER="%USERNAME%" -e DATABASE="%DBNAME%" -e PASSWORD="%PASSWORD%" -v %cd%\tests:/test hbpmip/pgtap:1.0.0-3
goto :eof
REM ==================================
REM EXIT
:commonExit
if NOT "%errorMsg%"=="" ECHO %errorMsg%