summaryrefslogtreecommitdiff
path: root/flow-cleaner_test.go
blob: 93d7d76ac1c29cf87394dbc706d3a170bd139074 (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main

import (
	"bufio"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"strings"
	"testing"
	"time"

	"database/sql"
	_ "github.com/Go-SQL-Driver/MySQL"
)

func init() {
	VERBOSE = true
}

func TestCleaningFromDB(t *testing.T) {
	cfg := &Config{
		Limit:      0,
		Interval:   "5min",
		Epsilon:    0,
		DataSource: "mysql",

		DBConn:     "",
		DBName:     "test",
		RawTable:   "test_raw",
		CleanTable: "test_clean",
		DBUser:     "flowcleaner",
		DBPass:     "nil",
	}

	fmt.Println("== Testing to process from DB ==")
	prepareDB(t, cfg)
	processFromDB(cfg)
	controlCleanDB(t, cfg)
	controlRawDBMySQL(t, cfg)
	fmt.Println("== Finished testing to process from DB ==")
}

func TestCleaningFromJSON(t *testing.T) {
	cfg := &Config{
		Limit:      0,
		Interval:   "5min",
		Epsilon:    0,
		DataSource: "json",

		DBConn:     "",
		DBName:     "test",
		RawTable:   "test_raw",
		CleanTable: "test_clean",
		DBUser:     "flowcleaner",
		DBPass:     "nil",
	}

	fmt.Println("== Testing to process from stdin ==")
	prepareDB(t, cfg)
	testProcessFromStdin(t, cfg)
	time.Sleep(15 * time.Second)
	controlCleanDB(t, cfg)
	controlRawDBStdin(t, cfg)
	fmt.Println("== Finished testing to process from stdin ==")
}

func prepareDB(t *testing.T, cfg *Config) {
	db, err := sql.Open("mysql", cfg.DBUser+":"+cfg.DBPass+"@"+cfg.DBConn+"/"+cfg.DBName)
	if err != nil {
		t.Fatal("Failed to connect to db:", err)
	}
	defer db.Close()

	file, err := ioutil.ReadFile("testdata/dbTestSetup.mysql")
	if err != nil {
		t.Fatal(err)
	}

	for _, query := range strings.Split(string(file), ";") {
		query = strings.TrimSpace(query)
		if len(query) > 0 {
			_, err = db.Exec(query + ";")
			if err != nil {
				fmt.Println("QUERY:", query)
				t.Fatal(err)
			}
		}
	}
}

func testProcessFromStdin(t *testing.T, cfg *Config) {
	stdin := make(chan []byte)

	go func() {
		min := time.Now().Minute()
		binStart := ((min + 5) / 5) * 5 // Set it to the next interval
		wait := (binStart - min) % 5
		dur, err := time.ParseDuration(fmt.Sprintf("%dm", wait))
		if err != nil {
			t.Fatal(err)
		}
		fmt.Println("== Waiting for alignment of timebin before testing ==")
		fmt.Println(fmt.Sprintf("== Will start in %d minute(s) ==", wait))
		time.Sleep(dur)
		fmt.Println("== Now starting to test ==")

		for i := 0; i < 3; i++ {
			fakeStdin(t, stdin)
			if i < 1 {
				fmt.Println("== Now waiting for next time bin ==")
				time.Sleep(5 * time.Minute)
				fmt.Println("== Now continuing testing ==")
			}
		}
		close(stdin)
	}()

	rDatChan := parseRawData(stdin, cfg)
	err := cleanFromStdin(rDatChan, cfg)
	if err != nil {
		t.Error(err)
	}
}

func fakeStdin(t *testing.T, wr chan<- []byte) {
	file, err := os.Open("testdata/jsoninput")
	if err != nil {
		t.Fatal(err)
	}
	defer file.Close()

	reader := bufio.NewReader(file)

	for {
		line, err := reader.ReadBytes('\n')
		if err == io.EOF {
			break
		} else if err != nil {
			t.Fatal(err)
		} else {
			wr <- line
		}
	}
}

func controlRawDBStdin(t *testing.T, cfg *Config) {
	db, err := sql.Open("mysql", cfg.DBUser+":"+cfg.DBPass+"@"+cfg.DBConn+"/"+cfg.DBName)
	if err != nil {
		t.Fatal("Failed to connect to db:", err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT * FROM " + cfg.RawTable)
	if err != nil {
		t.Fatal("Failed to select cleaned data:", err)
	}
	defer rows.Close()

	numRows := 0
	for rows.Next() {
		numRows++
	}
	if numRows != 33 {
		t.Error("Wrong number of rows found in db, should be 33 found:", numRows)
	}
}

func controlRawDBMySQL(t *testing.T, cfg *Config) {
	db, err := sql.Open("mysql", cfg.DBUser+":"+cfg.DBPass+"@"+cfg.DBConn+"/"+cfg.DBName)
	if err != nil {
		t.Fatal("Failed to connect to db:", err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT * FROM " + cfg.RawTable)
	if err != nil {
		t.Fatal("Failed to select cleaned data:", err)
	}
	defer rows.Close()

	numRows := 0
	for rows.Next() {
		numRows++
	}
	if numRows != 3 {
		t.Error("Wrong number of rows found in db, should be 3 found:", numRows)
	}
}

func controlCleanDB(t *testing.T, cfg *Config) {
	db, err := sql.Open("mysql", cfg.DBUser+":"+cfg.DBPass+"@"+cfg.DBConn+"/"+cfg.DBName)
	if err != nil {
		t.Fatal("Failed to connect to db:", err)
	}
	defer db.Close()

	rows, err := db.Query("SELECT * FROM " + cfg.CleanTable)
	if err != nil {
		t.Fatal("Failed to select cleaned data:", err)
	}
	defer rows.Close()

	numRows := 0
	for rows.Next() {
		numRows++
	}
	if numRows != 14 {
		t.Error("Wrong number of rows found in db, should be 14 found:", numRows)
	}

	check1, err := db.Query("SELECT occurences, volume FROM " + cfg.CleanTable + " ORDER BY occurences DESC")
	if err != nil {
		t.Fatal("Failed to select occurences and volume:", err)
	}
	defer check1.Close()
	var occurences int
	var vol string

	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 36 && vol == "0-199" {
		t.Error("Failed occurences 36, vol 0-199. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 20 && vol == "200-299" {
		t.Error("Failed occurences 20, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 18 && vol == "200-299" {
		t.Error("Failed occurences 18, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 14 && vol == "200-299" {
		t.Error("Failed occurences 14, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 12 && vol == "200-299" {
		t.Error("Failed occurences 12, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 10 && vol == "200-299" {
		t.Error("Failed occurences 10, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 8 && vol == "200-299" {
		t.Error("Failed occurences 8, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 7 && vol == "200-299" {
		t.Error("Failed occurences 7, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 6 && vol == "200-299" {
		t.Error("Failed occurences 6, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 4 && vol == "200-299" {
		t.Error("Failed occurences 4, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 2 && vol == "200-299" {
		t.Error("Failed occurences 2, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 1 && vol == "200-299" {
		t.Error("Failed occurences 1, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 0 && vol == "200-299" {
		t.Error("Failed occurences 0, vol 200-299. Actually was:", occurences, vol)
	}
	check1.Next()
	check1.Scan(&occurences, &vol)
	if occurences != 0 && vol == "200-299" {
		t.Error("Failed occurences 0, vol 200-299. Actually was:", occurences, vol)
	}
}