-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstatic.go
More file actions
147 lines (130 loc) · 5.21 KB
/
static.go
File metadata and controls
147 lines (130 loc) · 5.21 KB
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
package dsunit
import (
"path"
"runtime"
"strings"
"testing"
"github.com/viant/toolbox"
)
var dsUnitService Service
var baseDirectory string
//SetService sets global dsunit service.
func SetService(service Service) {
dsUnitService = service
}
//GetService returns dsunit service.
func GetService() Service {
if dsUnitService == nil {
file, _, _ := getCallerInfo(4)
baseDirectory = path.Dir(file) + "/"
dsUnitService = NewServiceLocal(baseDirectory)
}
return dsUnitService
}
//UseRemoteTestServer this method changes service to run all operation remotely using passed in URL.
func UseRemoteTestServer(serverURL string) {
file, _, _ := getCallerInfo(3)
baseDirectory := path.Dir(file) + "/"
SetService(NewServiceClient(baseDirectory, serverURL))
}
func getCallerFileAndMethod() (string, string) {
file, method, _ := getCallerInfo(3)
return file, method
}
func handleResponse(t *testing.T, response *Response) {
if response.hasError() {
file, method, line := getCallerInfo(4)
_, file = path.Split(file)
t.Errorf("\n%v.%v:%v %v", file, method, line, response.Message)
t.FailNow()
} else {
t.Logf(response.Message)
}
}
func getCallerInfo(callerIndex int) (string, string, int) {
var callerPointer = make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(callerIndex, callerPointer)
callerInfo := runtime.FuncForPC(callerPointer[0])
file, line := callerInfo.FileLine(callerPointer[0])
callerName := callerInfo.Name()
dotPosition := strings.LastIndex(callerName, ".")
return file, callerName[dotPosition+1:], line
}
//PrepareDatastore matches all dataset files that are in the same location as a test file, with the same test file prefix, followed by lowe camel case test name.
func PrepareDatastore(t *testing.T, datastore string) {
testFile, method, _ := getCallerInfo(3)
pathPrefix := removeFileExtension(testFile)
service := GetService()
response := service.PrepareDatastoreFor(datastore, pathPrefix+"_", method)
handleResponse(t, response)
}
//PrepareDatastoreFor matches all dataset files that are located in baseDirectory with method name and
// populate datastore with all listed dataset
// Note the matchable dataset files in the base directory have the following naming:
//
// <lower_underscore method name>_populate_<table>.[json|csv]
// To prepare dataset to populate datastore table: 'users' and 'permissions' for test method ReadAll you would
// have you create the following files in the baseDirectory
//
// read_all_prepare_travelers2.json
// read_all_populate_permissions.json
//
func PrepareDatastoreFor(t *testing.T, datastore string, baseDirectory string, method string) {
service := GetService()
response := service.PrepareDatastoreFor(datastore, baseDirectory, method)
handleResponse(t, response)
}
//ExpectDatasets matches all dataset files that are located in the same directory as the test file with method name and
//verifies that all listed dataset values are present in datastore
func ExpectDatasets(t *testing.T, datastore string, checkPolicy int) {
file, method, _ := getCallerInfo(3)
pathPrefix := removeFileExtension(file)
service := GetService()
response := service.ExpectDatasetsFor(datastore, pathPrefix+"_", method, checkPolicy)
handleResponse(t, response.Response)
}
//ExpectDatasetFor matches all dataset files that are located in baseDirectory with method name and
// verifies that all listed dataset values are present in datastore
// Note the matchable dataset files in the base directory have the following naming:
//
// <lower_underscore method name>_expect_<table>.[json|csv]
// To prepare expected dataset table: 'users' and 'permissions' for test method ReadAll you would
// have you create the following files in the baseDirectory
//
// read_all_expect_users.json
// read_all_expect_permissions.json
//
func ExpectDatasetFor(t *testing.T, datastore string, checkPolicy int, baseDirectory string, method string) {
service := GetService()
response := service.ExpectDatasetsFor(datastore, baseDirectory, method, checkPolicy)
handleResponse(t, response.Response)
}
//InitDatastoreFromURL initialises datastore from URL, URL needs to point to InitDatastoreRequest JSON
//it register datastore, table descriptor, data mapping, and optionally recreate datastore
func InitDatastoreFromURL(t *testing.T, url string) {
service := GetService()
response := service.InitFromURL(url)
handleResponse(t, response)
}
//ExecuteScriptFromURL executes script from URL, URL should point to ExecuteScriptRequest JSON.
func ExecuteScriptFromURL(t *testing.T, url string) {
service := GetService()
response := service.ExecuteScriptsFromURL(url)
handleResponse(t, response)
}
//ExpandTestProtocolAsUrlIfNeeded extends input if it start with test:// fragment to currently test file directory as file protocol
func ExpandTestProtocolAsURLIfNeeded(input string) string {
GetService()
if strings.HasPrefix(input, TestSchema) {
return toolbox.FileSchema + baseDirectory + input[len(TestSchema):]
}
return input
}
//ExpandTestProtocolAsPathIfNeeded extends input if it start with test:// fragment to currently test file directory
func ExpandTestProtocolAsPathIfNeeded(input string) string {
GetService()
if strings.HasPrefix(input, TestSchema) {
return baseDirectory + input[len(TestSchema):]
}
return input
}