aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/datamaps/reader_test.go
blob: e214930fd570e4df713d4b16c6695f9675afab27 (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
package datamaps

import (
	"os"
	"testing"
)

func TestReadDML(t *testing.T) {
	d, _ := ReadDML("testdata/datamap.csv")
	cases := []struct {
		idx int
		val string
	}{
		{0, "Project/Programme Name"},
		{1, "Department"},
		{2, "Delivery Body"},
	}

	for _, c := range cases {
		if got := d[c.idx].Key; got != c.val {
			t.Errorf("The test expected %s, got %s.", c.val, d[c.idx].Key)
		}
	}
}

func TestNoFileReturnsError(t *testing.T) {
	// this file does not exist
	_, err := ReadDML("/home/bobbins.csv")
	// if we get no error, something has gone wrong

	if err == nil {
		t.Errorf("Should have thrown error %s", err)
	}
}

func TestBadDMLLine(t *testing.T) {
	_, err := ReadDML("/home/lemon/code/python/bcompiler-engine/tests/resources/datamap_empty_cols.csv")

	if err == nil {
		t.Errorf("No error so test failed.")
	}
}

func TestGetSheetsFromDM(t *testing.T) {
	slice, _ := ReadDML("testdata/datamap.csv")
	sheetNames := getSheetNames(slice)

	if len(sheetNames) != 15 {
		t.Errorf("The test expected 14 sheets in slice, got %d.",
			len(sheetNames))
	}
}

func TestReadXLSX(t *testing.T) {
	d := ReadXLSX("testdata/test_template.xlsx")
	cases := []struct {
		sheet, cellref, val string
	}{
		{"Summary", "A2", "Date:"},
		{"Summary", "IG10", "botticelli"},
		{"Another Sheet", "F5", "4.2"},
		{"Another Sheet", "J22", "18"},
	}

	for _, c := range cases {
		got := d[c.sheet][c.cellref].Value
		if got != c.val {
			t.Errorf("The test expected %s in %s sheet to be %s "+
				" - instead it is %s.", c.cellref, c.sheet, c.val, d[c.sheet][c.cellref].Value)
		}
	}
}

// func TestExtractWithDBDatamap(t *testing.T) {
// 	// setup - we need the datamap in the test database
// 	db, err := setupDB("./testdata/test.db")
// defer func() {
// 	db.Close()
// 	os.Remove("./testdata/test.db")
// }()

// 	if err != nil {
// 		t.Fatal("Expected to be able to set up the database.")
// 	}

// 	opts := Options{
// 		DBPath: "./testdata/test.db",
// 		DMName: "First Datamap",
// 		DMPath: "./testdata/datamap.csv",
// 	}

// 	if err := DatamapToDB(&opts); err != nil {
// 		t.Errorf("Unable to write datamap to database file because %v.", err)
// 	}

// 	d := extractDBDatamap("First Datamap", "testdata/test_template.xlsx")
// }

func TestDMLSliceFromDatabase(t *testing.T) {
	// setup - we need the datamap in the test database
	db, err := setupDB("./testdata/test.db")
	defer func() {
		db.Close()
		os.Remove("./testdata/test.db")
	}()

	if err != nil {
		t.Fatal("Expected to be able to set up the database.")
	}

	opts := Options{
		DBPath: "./testdata/test.db",
		DMName: "First Datamap",
		DMPath: "./testdata/datamap.csv",
	}

	if err := DatamapToDB(&opts); err != nil {
		t.Errorf("Unable to write datamap to database file because %v.", err)
	}

	cases := []struct {
		index int
		key   string
	}{
		{0, "Project/Programme Name"},
		{1, "Department"},
		{2, "Delivery Body"},
		{3, "Stoogge value"},
		{4, "DRRDD - IPA ID Number"},
		{5, "Controls Project ID number"},
		{6, "Jokey Entry"},
		{7, "Parrots Name"},
	}

	data, _ := DatamapFromDB("First Datamap", db)

	for _, c := range cases {
		got := data[c.index].Key
		if got != c.key {
			t.Errorf("The test expected %s but got %s\n", c.key, data[c.index].Key)
		}
	}

	if data[0].Key != "Project/Programme Name" {
		t.Errorf("expected to see Project/Programme Name and got %q\n", data[0])
	}
}

func TestExtractUsingDBDM(t *testing.T) {
	// setup - we need the datamap in the test database
	db, err := setupDB("./testdata/test.db")
	defer func() {
		db.Close()
		os.Remove("./testdata/test.db")
	}()

	if err != nil {
		t.Fatal("Expected to be able to set up the database.")
	}

	opts := Options{
		DBPath: "./testdata/test.db",
		DMName: "First Datamap",
		DMPath: "./testdata/datamap_matches_test_template.csv",
	}

	if err := DatamapToDB(&opts); err != nil {
		t.Errorf("Unable to write datamap to database file because %v.", err)
	}

	d, _ := ExtractDBDatamap("First Datamap", "testdata/test_template.xlsx", db)
	cases := []struct {
		sheet, cellref, val string
	}{
		{"Introduction", "C9", "Test Department"},
		{"Introduction", "J9", "Greedy Parrots"},
		{"Introduction", "A1", "10"},
		{"Introduction", "C22", "VUNT"},
	}

	for _, c := range cases {
		got := d[c.sheet][c.cellref].Value
		if got != c.val {
			t.Errorf("test_template.xlsx: The test expected %s in %s sheet to be %s "+
				"- instead it is %s.", c.sheet, c.cellref, c.val,
				d[c.sheet][c.cellref].Value)
		}
	}
}

func TestExtract(t *testing.T) {
	d := extract("testdata/datamap.csv", "testdata/test_template.xlsx")
	cases := []struct {
		sheet, cellref, val string
	}{
		{"Introduction", "C9", "Test Department"},
		{"Introduction", "J9", "Greedy Parrots"},
		{"Introduction", "A1", "10"},
		{"Introduction", "C22", "VUNT"},
	}

	for _, c := range cases {
		got := d[c.sheet][c.cellref].Value
		if got != c.val {
			t.Errorf("The test expected %s in %s sheet to be %s "+
				"- instead it is %s.", c.sheet, c.cellref, c.val,
				d[c.sheet][c.cellref].Value)
		}
	}

	if d["Another Sheet"]["E26"].Value != "Integer:" {
		t.Errorf("Expected E26 in Another Sheet sheet to be Integer: - instead it is %s", d["Another Sheet"]["E26"].Value)
	}
}

// func TestGetTargetFiles(t *testing.T) {
// 	// This is not a suitable test for parameterisation, but doing it this way anyway.
// 	type args struct {
// 		path string
// 	}
// 	tests := []struct {
// 		name    string
// 		args    args
// 		want    []string
// 		wantErr bool
// 	}{
// 		{"Get files in testdata",
// 			args{"./testdata/"},
// 			[]string{
// 				"testdata/test_template.xlsm",
// 				"testdata/test_template.xlsx",
// 				"testdata/test_template2.xlsx",
// 				"testdata/test_template3.xlsx",
// 			},
// 			false,
// 		},
// 	}
// 	for _, tt := range tests {
// 		t.Log("Running the test")
// 		t.Run(tt.name, func(t *testing.T) {
// 			got, err := getTargetFiles(tt.args.path)
// 			if (err != nil) != tt.wantErr {
// 				t.Errorf("getTargetFiles() error = %v, wantErr %v", err, tt.wantErr)
// 				return
// 			}
// 			if !reflect.DeepEqual(got, tt.want) {
// 				t.Errorf("getTargetFiles() = %v, want %v", got, tt.want)
// 			}
// 		})
// 	}
// }