-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataops_test.go
199 lines (163 loc) · 4.58 KB
/
dataops_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
/*
Data Operations Tests
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// TestTlocalDirs ::: Create a local dirset, remove when done.
func TestTlocalDirs(t *testing.T) {
fmt.Printf("\n\t::: Test Target localDirs() :::\n")
// testing directory
testDir := "./TTlocal_" + fmt.Sprint(time.Now().Unix())
// configured directories
locals := []string{"TTstore, maTTest"}
err := os.Mkdir(testDir, 0755)
if err != nil {
t.Error(err)
}
defer os.RemoveAll(testDir)
cderr := os.Chdir(testDir)
if cderr != nil {
t.Error(err)
}
// Create and then check the existence of each configured directory.
localDirs(locals)
for _, dir := range locals {
if _, lderr := os.Stat(dir); lderr != nil {
t.Error()
}
}
cberr := os.Chdir("../")
if cberr != nil {
t.Error(cberr)
}
}
// TestTextent ::: Stat a known file, stat an unknown file.
func TestTextent(t *testing.T) {
fmt.Printf("\n\t::: Test Target extent() :::\n")
knownFile := "/etc/passwd"
unknownFile := "/tmp/nofile" + fmt.Sprint(time.Now().Unix())
if !extent(knownFile) {
t.Error()
}
if extent(unknownFile) {
t.Error()
}
}
// TestTdirents ::: Create a tmp dir and file in it, get the dir list, match the file in the list.
func TestTdirents(t *testing.T) {
fmt.Printf("\n\t::: Test Target dirents() :::\n")
// Set up tmp
TTdir, err := ioutil.TempDir(".", "TT")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(TTdir)
TTfile, err := ioutil.TempFile(TTdir, "")
if err != nil {
t.Error(err)
}
defer os.Remove(TTfile.Name())
// Call dirents()
for _, entry := range dirents(TTdir) {
pathENT := filepath.Join(TTdir, entry.Name())
pathTT := TTfile.Name()
if pathENT != pathTT {
t.Errorf("Local file '%s' does NOT match created file '%s'.\n", pathENT, pathTT)
}
}
}
// TestTichingMeso ::: When working, this will test the presence of a random filename. ???
func TestTichingMeso(t *testing.T) {
fmt.Printf("\n\t::: Test Target ichingMeso() :::\n")
// Set up tmp
TTdir, err := ioutil.TempDir(".", "TT")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(TTdir)
TTfile, err := ioutil.TempFile(TTdir, "")
if err != nil {
t.Error(err)
}
defer os.Remove(TTfile.Name())
// Call ichingMeso()
}
// TestTfileTmp ::: Special Mesostic TMP file creation.
// Given a set of strings, match the created filename and verify its presence on disk,
// perhaps match the content itself, then delete.
func TestTfileTmp(t *testing.T) {
fmt.Printf("\n\t::: Test Target fileTmp() :::\n")
// Set up tmp
TTdir, err := ioutil.TempDir(".", "txrx")
if err != nil {
t.Error(err)
}
defer os.RemoveAll(TTdir)
spine := "cra"
source := "que"
fileName := fileTmp(&spine, &source)
if !strings.Contains(fileName, spine) {
t.Errorf("Local filename '%s' does not contain '%s'.\n", fileName, spine)
}
}
// TestTenvVar ::: Process environment variables correctly with a given fallback option.
func TestTenvVar(t *testing.T) {
fmt.Printf("\n\t::: Test Target envVar() :::\n")
var getvar string
var testvar string
var testval string
var fallval string
// testvar does not exist, fallback provided (good config)
// expected return: the fallback value
fallval = "fallback_NoVAR"
getvar = envVar(testvar, fallval)
if getvar != fallval {
t.Errorf("%s, %s, %s", testvar, testval, fallval)
}
t.Logf("fallback received: %s", getvar)
// testvar exists, but is unset, no fallback (error condition)
// correct return: empty
testvar = "TTVAR"
fallval = ""
getvar = envVar(testvar, fallval)
if getvar != "" {
t.Errorf("%s, %s, %s", testvar, testval, fallval)
}
t.Logf("empty received: %s", getvar)
// testvar exists, but is unset, fallback provided (good config)
// correct return: value for fallval
fallval = "fallback_NoValue"
getvar = envVar(testvar, fallval)
if getvar != fallval {
t.Errorf("%s, %s, %s", testvar, testval, fallval)
}
t.Logf("fallback received: %s", getvar)
// Finally testvar is set, fallback provided (good config)
// correct return: value for testval
testval = "TestTenvVar"
fallval = "fallback_NoValue"
os.Setenv(testvar, testval) // testvar := testval
getvar = envVar(testvar, fallval)
if getvar != testval {
t.Errorf("%s, %s, %s", testvar, testval, fallval)
}
t.Logf("set value received: %s", getvar)
}
// TestTrndDate ::: Test the creation of a random date
func TestTrndDate(t *testing.T) {
fmt.Printf("\n\t::: Test Target rndDate() :::\n")
salt := time.Now().Unix()
randomdate := rndDate(salt)
// split the string by "-"
// now test by checking its values mathematically.
// see if the random values fall within the ranges given?
fmt.Println(randomdate)
}