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
}
func TestMain(t *testing.T) {
commands := []string{
"insert 1 user1 person1@example.com",
"select",
".exit",
func TestE2E(t *testing.T) {
tests := []struct {
name string
commands []string
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{
"godb > Executed.",
"godb > {1 user1 person1@example.com}",
"Executed.",
"godb > bye!",
}
results := runner(commands, t)
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
results := runner(test.commands, t)
for i := range results {
results[i] = strings.ReplaceAll(results[i], "\n", "")
}
for i := range results {
results[i] = strings.ReplaceAll(results[i], "\n", "")
if !slices.Equal(test.expected, results) {
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) {
@@ -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))
}
}
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)
}
}