feat: restructured test to be more idiomatic

This commit is contained in:
2026-07-29 00:51:30 +05:30
parent 9eeac4e7df
commit c296945f0e
+31 -45
View File
@@ -66,28 +66,42 @@ func runner(commands []string, t *testing.T) []string {
return results return results
} }
func TestMain(t *testing.T) { func TestE2E(t *testing.T) {
commands := []string{ tests := []struct {
"insert 1 user1 person1@example.com", name string
"select", commands []string
".exit", expected []string
}{
{
name: "Test Main Loop",
commands: []string{"insert 1 user1 person1@example.com", "select", ".exit"},
expected: []string{"godb > Executed.", "godb > {1 user1 person1@example.com}", "Executed.", "godb > bye!"},
},
{
name: "Test Maximum Length For Values",
commands: []string{"insert 0 " + strings.Repeat("n", 32) + " " + strings.Repeat("n", 255), ".exit"},
expected: []string{"godb > Executed.", "godb > bye!"},
},
{
name: "Test Over Maximum Length For Values",
commands: []string{"insert 0 " + strings.Repeat("n", 42) + " " + strings.Repeat("n", 275), ".exit"},
expected: []string{"godb > Values for either username or email are too long", "godb > bye!"},
},
} }
expected := []string{ for _, test := range tests {
"godb > Executed.", t.Run(test.name, func(t *testing.T) {
"godb > {1 user1 person1@example.com}", results := runner(test.commands, t)
"Executed.", for i := range results {
"godb > bye!", results[i] = strings.ReplaceAll(results[i], "\n", "")
} }
results := runner(commands, t)
for i := range results { if !slices.Equal(test.expected, results) {
results[i] = strings.ReplaceAll(results[i], "\n", "") t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", test.expected, results)
}
})
} }
if !slices.Equal(expected, results) {
t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", expected, results)
}
} }
func TestTableFull(t *testing.T) { func TestTableFull(t *testing.T) {
@@ -106,31 +120,3 @@ func TestTableFull(t *testing.T) {
t.Errorf("Our Tables max capacity is 1300 rows, but we're able to insert %v\n", len(results)) t.Errorf("Our Tables max capacity is 1300 rows, but we're able to insert %v\n", len(results))
} }
} }
func TestMaxLengthForValues(t *testing.T) {
commands := []string{"insert 0 " + strings.Repeat("n", 32) + " " + strings.Repeat("n", 255), ".exit"}
expected := []string{"godb > Executed.", "godb > bye!"}
results := runner(commands, t)
for i := range results {
results[i] = strings.ReplaceAll(results[i], "\n", "")
}
if !slices.Equal(expected, results) {
t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", expected, results)
}
}
func TestOverMaxLengthValues(t *testing.T) {
commands := []string{"insert 0 " + strings.Repeat("n", 42) + " " + strings.Repeat("n", 275), ".exit"}
expected := []string{"godb > Values for either username or email are too long", "godb > bye!"}
results := runner(commands, t)
for i := range results {
results[i] = strings.ReplaceAll(results[i], "\n", "")
}
if !slices.Equal(expected, results) {
t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", expected, results)
}
}