summary refs log tree commit diff stats
path: root/registry/query_test.go
blob: 7eed2cd6d287d66d3a6d52522721df62c97d7520 (plain) (blame)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
Copyright (c) 2019 Ben Morrison (gbmor)

This file is part of Registry.

Registry is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Registry is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Registry.  If not, see <https://www.gnu.org/licenses/>.
*/

package registry

import (
	"bufio"
	"os"
	"strings"
	"testing"
	"time"
)

var queryUserCases = []struct {
	name    string
	term    string
	wantErr bool
}{
	{
		name:    "Valid User",
		term:    "foo",
		wantErr: false,
	},
	{
		name:    "Empty Query",
		term:    "",
		wantErr: false,
	},
	{
		name:    "Nonexistent User",
		term:    "doesntexist",
		wantErr: true,
	},
	{
		name:    "Garbage Data",
		term:    "will be replaced with garbage data",
		wantErr: true,
	},
}

// Checks if Registry.QueryUser() returns users that
// match the provided substring.
func Test_Registry_QueryUser(t *testing.T) {
	registry := initTestEnv()
	var buf = make([]byte, 256)
	// read random data into case 8
	rando, _ := os.Open("/dev/random")
	reader := bufio.NewReader(rando)
	n, err := reader.Read(buf)
	if err != nil || n == 0 {
		t.Errorf("Couldn't set up test: %v\n", err)
	}
	queryUserCases[3].term = string(buf)

	for n, tt := range queryUserCases {

		t.Run(tt.name, func(t *testing.T) {
			out, err := registry.QueryUser(tt.term)

			if out == nil && err != nil && !tt.wantErr {
				t.Errorf("Received nil output or an error when unexpected. Case %v, %v, %v\n", n, tt.term, err)
			}

			if out != nil && tt.wantErr {
				t.Errorf("Received unexpected nil output when an error was expected. Case %v, %v\n", n, tt.term)
			}

			for _, e := range out {
				one := strings.Split(e, "\t")

				if !strings.Contains(one[0], tt.term) && !strings.Contains(one[1], tt.term) {
					t.Errorf("Received incorrect output: %v != %v\n", tt.term, e)
				}
			}
		})
	}
}
func Benchmark_Registry_QueryUser(b *testing.B) {
	registry := initTestEnv()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		for _, tt := range queryUserCases {
			_, err := registry.QueryUser(tt.term)
			if err != nil {
				b.Errorf("%v\n", err)
			}
		}
	}
}

var queryInStatusCases = []struct {
	name    string
	substr  string
	wantNil bool
	wantErr bool
}{
	{
		name:    "Tag in Status",
		substr:  "twtxt",
		wantNil: false,
		wantErr: false,
	},
	{
		name:    "Valid URL",
		substr:  "https://example.com/twtxt.txt",
		wantNil: false,
		wantErr: false,
	},
	{
		name:    "Multiple Words in Status",
		substr:  "next programming",
		wantNil: false,
		wantErr: false,
	},
	{
		name:    "Multiple Words, Not in Status",
		substr:  "explosive bananas from antarctica",
		wantNil: true,
		wantErr: false,
	},
	{
		name:    "Empty Query",
		substr:  "",
		wantNil: true,
		wantErr: true,
	},
	{
		name:    "Nonsense",
		substr:  "ahfiurrenkhfkajdhfao",
		wantNil: true,
		wantErr: false,
	},
	{
		name:    "Invalid URL",
		substr:  "https://doesnt.exist/twtxt.txt",
		wantNil: true,
		wantErr: false,
	},
	{
		name:    "Garbage Data",
		substr:  "will be replaced with garbage data",
		wantNil: true,
		wantErr: false,
	},
}

// This tests whether we can find a substring in all of
// the known status messages, disregarding the metadata
// stored with each status.
func Test_Registry_QueryInStatus(t *testing.T) {
	registry := initTestEnv()
	var buf = make([]byte, 256)
	// read random data into case 8
	rando, _ := os.Open("/dev/random")
	reader := bufio.NewReader(rando)
	n, err := reader.Read(buf)
	if err != nil || n == 0 {
		t.Errorf("Couldn't set up test: %v\n", err)
	}
	queryInStatusCases[7].substr = string(buf)

	for _, tt := range queryInStatusCases {

		t.Run(tt.name, func(t *testing.T) {

			out, err := registry.QueryInStatus(tt.substr)
			if err != nil && !tt.wantErr {
				t.Errorf("Caught unexpected error: %v\n", err)
			}

			if !tt.wantErr && out == nil && !tt.wantNil {
				t.Errorf("Got nil when expecting output\n")
			}

			if err == nil && tt.wantErr {
				t.Errorf("Expecting error, got nil.\n")
			}

			for _, e := range out {
				split := strings.Split(strings.ToLower(e), "\t")

				if e != "" {
					if !strings.Contains(split[3], strings.ToLower(tt.substr)) {
						t.Errorf("Status without substring returned\n")
					}
				}
			}
		})
	}

}
func Benchmark_Registry_QueryInStatus(b *testing.B) {
	registry := initTestEnv()
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		for _, tt := range queryInStatusCases {
			_, err := registry.QueryInStatus(tt.substr)
			if err != nil {
				continue
			}
		}
	}
}

// Tests whether we can retrieve the 20 most
// recent statuses in the registry
func Test_QueryAllStatuses(t *testing.T) {
	registry := initTestEnv()
	t.Run("Latest Statuses", func(t *testing.T) {
		out, err := registry.QueryAllStatuses()
		if out == nil || err != nil {
			t.Errorf("Got no statuses, or more than 20: %v, %v\n", len(out), err)
		}
	})
}
func Benchmark_QueryAllStatuses(b *testing.B) {
	registry := initTestEnv()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, err := registry.QueryAllStatuses()
		if err != nil {
			continue
		}
	}
}

var get20cases = []struct {
	name    string
	page    int
	wantErr bool
}{
	{
		name:    "First Page",
		page:    1,
		wantErr: false,
	},
	{
		name:    "High Page Number",
		page:    256,
		wantErr: false,
	},
	{
		name:    "Illegal Page Number",
		page:    -23,
		wantErr: false,
	},
}

func Test_ReduceToPage(t *testing.T) {
	registry := initTestEnv()
	for _, tt := range get20cases {
		t.Run(tt.name, func(t *testing.T) {
			out, err := registry.QueryAllStatuses()
			if err != nil && !tt.wantErr {
				t.Errorf("%v\n", err.Error())
			}
			out = ReduceToPage(tt.page, out)
			if len(out) > 20 || len(out) == 0 {
				t.Errorf("Page-Reduce Malfunction: length of data %v\n", len(out))
			}
		})
	}
}

func Benchmark_ReduceToPage(b *testing.B) {
	registry := initTestEnv()
	out, _ := registry.QueryAllStatuses()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		for _, tt := range get20cases {
			ReduceToPage(tt.page, out)
		}
	}
}

// This tests whether we can find a substring in the
// given user's status messages, disregarding the metadata
// stored with each status.
func Test_User_FindInStatus(t *testing.T) {
	registry := initTestEnv()
	var buf = make([]byte, 256)
	// read random data into case 8
	rando, _ := os.Open("/dev/random")
	reader := bufio.NewReader(rando)
	n, err := reader.Read(buf)
	if err != nil || n == 0 {
		t.Errorf("Couldn't set up test: %v\n", err)
	}
	queryInStatusCases[7].substr = string(buf)

	data := make([]*User, 0)

	for _, v := range registry.Users {
		data = append(data, v)
	}

	for _, tt := range queryInStatusCases {
		t.Run(tt.name, func(t *testing.T) {
			for _, e := range data {

				tag := e.FindInStatus(tt.substr)
				if tag == nil && !tt.wantNil {
					t.Errorf("Got nil tag\n")
				}
			}
		})
	}

}
func Benchmark_User_FindInStatus(b *testing.B) {
	registry := initTestEnv()
	data := make([]*User, 0)

	for _, v := range registry.Users {
		data = append(data, v)
	}
	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		for _, tt := range data {
			for _, v := range queryInStatusCases {
				tt.FindInStatus(v.substr)
			}
		}
	}
}

func Test_SortByTime_Slice(t *testing.T) {
	registry := initTestEnv()

	statusmap, err := registry.GetStatuses()
	if err != nil {
		t.Errorf("Failed to finish test initialization: %v\n", err)
	}

	t.Run("Sort By Time ([]TimeMap)", func(t *testing.T) {
		sorted, err := SortByTime(statusmap)
		if err != nil {
			t.Errorf("%v\n", err)
		}
		split := strings.Split(sorted[0], "\t")
		firsttime, _ := time.Parse("RFC3339", split[0])

		for i := range sorted {
			if i < len(sorted)-1 {

				nextsplit := strings.Split(sorted[i+1], "\t")
				nexttime, _ := time.Parse("RFC3339", nextsplit[0])

				if firsttime.Before(nexttime) {
					t.Errorf("Timestamps out of order: %v\n", sorted)
				}

				firsttime = nexttime
			}
		}
	})
}

// Benchmarking a sort of 1000000 statuses by timestamp.
// Right now it's at roughly 2000ns per 2 statuses.
// Set sortMultiplier to be the number of desired
// statuses divided by four.
func Benchmark_SortByTime_Slice(b *testing.B) {
	// I set this to 250,000,000 and it hard-locked
	// my laptop. Oops.
	sortMultiplier := 250
	b.Logf("Benchmarking SortByTime with a constructed slice of %v statuses ...\n", sortMultiplier*4)
	registry := initTestEnv()

	statusmap, err := registry.GetStatuses()
	if err != nil {
		b.Errorf("Failed to finish benchmark initialization: %v\n", err)
	}

	// Constructed registry has four statuses. This
	// makes a TimeMapSlice of 1000000 statuses.
	statusmaps := make([]TimeMap, sortMultiplier*4)
	for i := 0; i < sortMultiplier; i++ {
		statusmaps = append(statusmaps, statusmap)
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_, err := SortByTime(statusmaps...)
		if err != nil {
			b.Errorf("%v\n", err)
		}
	}
}

func Test_SortByTime_Single(t *testing.T) {
	registry := initTestEnv()

	statusmap, err := registry.GetStatuses()
	if err != nil {
		t.Errorf("Failed to finish test initialization: %v\n", err)
	}

	t.Run("Sort By Time (TimeMap)", func(t *testing.T) {
		sorted, err := SortByTime(statusmap)
		if err != nil {
			t.Errorf("%v\n", err)
		}
		split := strings.Split(sorted[0], "\t")
		firsttime, _ := time.Parse("RFC3339", split[0])

		for i := range sorted {
			if i < len(sorted)-1 {

				nextsplit := strings.Split(sorted[i+1], "\t")
				nexttime, _ := time.Parse("RFC3339", nextsplit[0])

				if firsttime.Before(nexttime) {
					t.Errorf("Timestamps out of order: %v\n", sorted)
				}

				firsttime = nexttime
			}
		}
	})
}

func Benchmark_SortByTime_Single(b *testing.B) {
	registry := initTestEnv()

	statusmap, err := registry.GetStatuses()
	if err != nil {
		b.Errorf("Failed to finish benchmark initialization: %v\n", err)
	}

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		_, err := SortByTime(statusmap)
		if err != nil {
			b.Errorf("%v\n", err)
		}
	}
}