summaryrefslogtreecommitdiffstats
path: root/db/db_test.go
blob: e2308c1da252285f51fffcd71a411967cab2326d (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
// SPDX-License-Identifier: ISC
// Copyright © 2021 siddharth <s@ricketyspace.net>

package db

import (
	"os"
	"path"
	"testing"
)

func stringsContain(haystack []string, needle string) bool {
	for _, s := range haystack {
		if s == needle {
			return true
		}
	}
	return false
}

func TestOpenPathNotSet(t *testing.T) {
	// Set custom path for db.
	dbPath = ""
	defer os.Remove(dbPath)

	_, err := Open()
	if err == nil {
		t.Errorf("Error: db.Open did not fail when dbPath is empty\n")
		return
	}
	if err.Error() != "FernDB path not set" {
		t.Errorf("Error: db.Open wrong error message when dbPath is empty\n")
		return
	}
}

func TestOpenNewDB(t *testing.T) {
	// Set custom path for db.
	dbPath = path.Join(os.TempDir(), "fern-db.json")
	defer os.Remove(dbPath)

	// Open empty db.
	db, err := Open()
	if err != nil {
		t.Errorf("db.Open failed: %v", err.Error())
		return
	}

	// Verify that 'mutex' is initialized.
	if db.mutex == nil {
		t.Errorf("db.mutex is nil")
		return
	}
	db.mutex.Lock()
	db.mutex.Unlock()

	// Verify that 'downloaded' is initialized
	if db.downloaded == nil {
		t.Errorf("db.downloaded is nil")
		return
	}
}

func TestOpenExistingDB(t *testing.T) {
	// Set custom path for db.
	dbPath = path.Join(os.TempDir(), "fern-db.json")
	defer os.Remove(dbPath)

	// Write a sample test db to fern-db.json
	testDBJSON := []byte(`{"mkbhd":["rivian","v-raptor","m1-imac"],"npr":["william-prince","joy-oladokun","lucy-ducas"],"simone":["weightless","ugly-desks","safety-hat"]}`)
	dbFile, err := os.Create(dbPath)
	if err != nil {
		t.Errorf("Unable to create fern-db.json: %v", err.Error())
	}
	n, err := dbFile.Write(testDBJSON)
	if len(testDBJSON) != n {
		t.Errorf("Write to fern-db.json failed: %v", err.Error())
	}
	dbFile.Close()

	// Open the db.
	db, err := Open()
	if err != nil {
		t.Errorf("db.Open failed: %v", err.Error())
		return
	}

	// Verify that 'mutex' is initialized.
	if db.mutex == nil {
		t.Errorf("db.mutex is nil")
		return
	}
	db.mutex.Lock()
	db.mutex.Unlock()

	// Validate db.downloaded.
	var entries, expectedEntries []string
	var ok bool
	if len(db.downloaded) != 3 {
		t.Errorf("db.downloaded does not contain 3 feeds")
		return
	}
	// mkbhd
	if entries, ok = db.downloaded["mkbhd"]; !ok {
		t.Errorf("db.downloaded does not contain mkbhd")
		return
	}
	expectedEntries = []string{"rivian", "v-raptor", "m1-imac"}
	for _, entry := range entries {
		if !stringsContain(expectedEntries, entry) {
			t.Errorf("%v does not exist in db.downloaded[mkbhd]", entry)
			return
		}
	}
	// simone
	if entries, ok = db.downloaded["simone"]; !ok {
		t.Errorf("db.downloaded does not contain simone")
		return
	}
	expectedEntries = []string{"weightless", "ugly-desks", "safety-hat"}
	for _, entry := range entries {
		if !stringsContain(expectedEntries, entry) {
			t.Errorf("%v does not exist in db.downloaded[simone]", entry)
			return
		}
	}
	// npr
	if entries, ok = db.downloaded["npr"]; !ok {
		t.Errorf("db.downloaded does not contain npr")
		return
	}
	expectedEntries = []string{"william-prince", "lucy-ducas", "joy-oladokun"}
	for _, entry := range entries {
		if !stringsContain(expectedEntries, entry) {
			t.Errorf("%v does not exist in db.downloaded[npr]", entry)
			return
		}
	}
}