-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
212 lines (189 loc) · 7.66 KB
/
util_test.go
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2019-2022 YottaDB LLC and/or its subsidiaries. //
// All rights reserved. //
// //
// This source code contains the intellectual property //
// of its copyright holder(s), and is made available //
// under a license. If you do not know the terms of //
// the license, please stop and do not read further. //
// //
//////////////////////////////////////////////////////////////////
package yottadb_test
import (
"fmt"
"github.com/stretchr/testify/assert"
"lang.yottadb.com/go/yottadb"
"os"
"strings"
"testing"
)
// Note we do not test MessageT() here as it gets quite a workout in the other tests that run since it is used in
// several tests which intentionally raise errors.
func TestReleaseT(t *testing.T) {
relText, err := yottadb.ReleaseT(yottadb.NOTTP, nil)
assert.Nil(t, err)
fields := strings.Fields(relText)
assert.Equal(t, len(fields), 6) // Verify we have 6 words in the response and that the constants are correct
assert.Equal(t, fields[0], "gowr")
assert.Equal(t, fields[2], "YottaDB")
// Make sure first char of YottaDB release number begins with 'r'.
ydbRel := []byte(fields[3])
assert.Equal(t, string(ydbRel[0]), "r")
}
func TestCallMTNoArgs(t *testing.T) {
envvarSave := make(map[string]string)
saveEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
err := os.Setenv("ydb_ci", "calltab.ci")
assert.Nil(t, err)
// Set up ydb_routines if doesn't already have an m_routines component
includeInEnvvar(t, "ydb_routines", "./m_routines")
retval, err := yottadb.CallMT(yottadb.NOTTP, nil, 64, "HelloWorld1")
restoreEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
assert.Nil(t, err)
assert.Equal(t, "entry called", retval)
}
func TestCallMTWithArgs(t *testing.T) {
envvarSave := make(map[string]string)
saveEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
err := os.Setenv("ydb_ci", "calltab.ci")
assert.Nil(t, err)
// Set up ydb_routines if doesn't already have an m_routines component
includeInEnvvar(t, "ydb_routines", "./m_routines")
retval, err := yottadb.CallMT(yottadb.NOTTP, nil, 64, "HelloWorld2", "parm1", "parm2", "parm3")
restoreEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
assert.Nil(t, err)
assert.Equal(t, "parm3parm2parm1", retval)
}
func TestCallMDescTNoArgs(t *testing.T) {
var mrtn yottadb.CallMDesc
envvarSave := make(map[string]string)
saveEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
err := os.Setenv("ydb_ci", "calltab.ci")
assert.Nil(t, err)
// Set up ydb_routines if doesn't already have an m_routines component
includeInEnvvar(t, "ydb_routines", "./m_routines")
mrtn.SetRtnName("HelloWorld1")
retval, err := mrtn.CallMDescT(yottadb.NOTTP, nil, 64)
restoreEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
assert.Nil(t, err)
assert.Equal(t, "entry called", retval)
}
func TestCallMDescTWithArgs(t *testing.T) {
var mrtn yottadb.CallMDesc
envvarSave := make(map[string]string)
saveEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
err := os.Setenv("ydb_ci", "calltab.ci")
assert.Nil(t, err)
// Set up ydb_routines if doesn't already have an m_routines component
includeInEnvvar(t, "ydb_routines", "./m_routines")
mrtn.SetRtnName("HelloWorld2")
retval, err := mrtn.CallMDescT(yottadb.NOTTP, nil, 64, "parm1", "parm2", "parm3")
restoreEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
assert.Nil(t, err)
assert.Equal(t, "parm3parm2parm1", retval)
}
func TestCallMT(t *testing.T) {
envvarSave := make(map[string]string)
saveEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
err := os.Setenv("ydb_ci", "calltab.ci")
includeInEnvvar(t, "ydb_routines", "./m_routines")
defer restoreEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
var errstr yottadb.BufferT
errstr.Alloc(2048)
defer errstr.Free()
/* M callin that returns 20 characters, using a 20 character buffer */
retval, err := yottadb.CallMT(yottadb.NOTTP, &errstr, 20, "CallMTStrTest")
if nil != err {
panic(err)
}
if "a0a1a2a3a4a5a6a7a8a9" != retval {
panic(fmt.Sprintf("CallMT() did not return the correct string. Got: %s; Expected: a0a1a2a3a4a5a6a7a8a9", retval))
}
if 20 != len(retval) {
panic(fmt.Sprintf("CallMT() return is not the correct length. Got: %d; Expected: 20", len(retval)))
}
/* M callin that returns 20 characters, using a 15 character buffer; should return truncated string */
retval, err = yottadb.CallMT(yottadb.NOTTP, &errstr, 15, "CallMTStrTest")
if nil != err {
panic(err)
}
if "a0a1a2a3a4a5a6a" != retval {
panic(fmt.Sprintf("CallMT() did not return the correct string. Got: %s; Expected: a0a1a2a3a4a5a6a", retval))
}
if 15 != len(retval) {
panic(fmt.Sprintf("CallMT() return is not the correct length. Got: %d; Expected: 15", len(retval)))
}
}
func TestCallMDescT(t *testing.T) {
// env setup
envvarSave := make(map[string]string)
saveEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
err := os.Setenv("ydb_ci", "calltab.ci")
includeInEnvvar(t, "ydb_routines", "./m_routines")
defer restoreEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
var errstr yottadb.BufferT
errstr.Alloc(2048)
defer errstr.Free()
var callin yottadb.CallMDesc
callin.SetRtnName("CallMTStrTest")
/* M callin that returns 20 characters, using a 20 character buffer */
retval, err := callin.CallMDescT(yottadb.NOTTP, &errstr, 20)
if nil != err {
panic(err)
}
if "a0a1a2a3a4a5a6a7a8a9" != retval {
panic(fmt.Sprintf("CallMT() did not return the correct string. Got: %s; Expected: a0a1a2a3a4a5a6a7a8a9", retval))
}
if 20 != len(retval) {
panic(fmt.Sprintf("CallMT() return is not the correct length. Got: %d; Expected: 20", len(retval)))
}
/* M callin that returns 20 characters, using a 15 character buffer; should return INVSTRLEN */
retval, err = callin.CallMDescT(yottadb.NOTTP, &errstr, 15)
if nil != err {
panic(err)
}
if "a0a1a2a3a4a5a6a" != retval {
panic(fmt.Sprintf("CallMT() did not return the correct string. Got: %s; Expected: a0a1a2a3a4a5a6a", retval))
}
if 15 != len(retval) {
panic(fmt.Sprintf("CallMT() return is not the correct length. Got: %d; Expected: 15", len(retval)))
}
}
func TestCallMTab(t *testing.T) {
var errstr yottadb.BufferT
var err error
// Environment setup
envvarSave := make(map[string]string)
saveEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
//err = os.Setenv("ydb_ci", "calltab.ci")
includeInEnvvar(t, "ydb_routines", "./m_routines")
defer restoreEnvvars(t, &envvarSave, "ydb_ci", "ydb_routines")
// Note, we did not set ydb_ci - open first calltable directly
calltabTable, err := yottadb.CallMTableOpenT(yottadb.NOTTP, &errstr, "calltab.ci")
assert.Nil(t, err)
_, err = calltabTable.CallMTableSwitchT(yottadb.NOTTP, &errstr)
assert.Nil(t, err)
retval, err := yottadb.CallMT(yottadb.NOTTP, nil, 64, "HelloWorld1")
assert.Nil(t, err)
assert.Equal(t, "entry called", retval)
// Try to invoke our test routine but expect error since it does not exist in this calltab
retval, err = yottadb.CallMT(yottadb.NOTTP, nil, 64, "HelloWorld99")
assert.NotNil(t, err)
assert.Equal(t, yottadb.YDB_ERR_CINOENTRY, yottadb.ErrorCode(err))
// Now open the new package and make it current
newtabTable, err := yottadb.CallMTableOpenT(yottadb.NOTTP, &errstr, "testcalltab.ci")
assert.Nil(t, err)
savedTable, err := newtabTable.CallMTableSwitchT(yottadb.NOTTP, &errstr)
assert.Nil(t, err)
// And try running our program again
retval, err = yottadb.CallMT(yottadb.NOTTP, nil, 64, "HelloWorld99")
assert.Nil(t, err)
assert.Equal(t, "entry was called", retval)
// Validate we can switch back and re-run HelloWorld1
_, err = savedTable.CallMTableSwitchT(yottadb.NOTTP, &errstr)
assert.Nil(t, err)
retval, err = yottadb.CallMT(yottadb.NOTTP, nil, 64, "HelloWorld1")
assert.Nil(t, err)
assert.Equal(t, "entry called", retval)
}