Skip to content

Commit

Permalink
Added more endpoint tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverLok committed Nov 29, 2024
1 parent 9fef60a commit 34af488
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 27 deletions.
2 changes: 0 additions & 2 deletions cmd/assignment-get/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func TestAssignmentGetBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find course: 'ZZZ'.`,
ExpectEmptyStdout: true,
},
courseID: "ZZZ",
assignmentID: "hw0",
Expand All @@ -38,7 +37,6 @@ func TestAssignmentGetBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find assignment: 'zzz'.`,
ExpectEmptyStdout: true,
},
courseID: "course101",
assignmentID: "zzz",
Expand Down
1 change: 0 additions & 1 deletion cmd/assignments-list/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ func TestAssignmentListBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find course: 'ZZZ'`,
ExpectEmptyStdout: true,
},
courseID: "ZZZ",
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/call-api-endpoint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func main() {
// Split the parameter into it's key and value.
parts := strings.SplitN(arg, ":", 2)
if len(parts) != 2 {
log.Fatal("Invalid parameter format: missing a colon. Expected format is 'key:value', e.g., 'target-email:[email protected]'.", log.NewAttr("parameter", parts))
log.Fatal("Invalid parameter format: missing a colon. Expected format is 'key:value', e.g., 'id:123'.", log.NewAttr("parameter", parts))

// Return to prevent further execution after log.Fatal().
return
Expand Down
115 changes: 106 additions & 9 deletions cmd/call-api-endpoint/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ func TestCallApiEndpointBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedStderrSubstring: `Failed to find the endpoint. | {"endpoint":"ZZZ"}`,
ExpectedExitCode: 1,
ExpectEmptyStdout: true,
},
endpoint: "ZZZ",
parameters: []string{
"target-email:[email protected]",
"course-id:course101",
"assignment-id:hw0",
"target-submission:1697406256",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedStderrSubstring: `Invalid parameter format: missing a colon. Expected format is 'key:value', e.g., 'target-email:[email protected]'. | {"parameter":["course-idcourse101"]}`,
ExpectedStderrSubstring: `Invalid parameter format: missing a colon. Expected format is 'key:value', e.g., 'id:123'. | {"parameter":["course-idcourse101"]}`,
ExpectedExitCode: 1,
ExpectEmptyStdout: true,
},
endpoint: "courses/assignments/submissions/fetch/user/peek",
parameters: []string{
Expand All @@ -50,39 +42,101 @@ func TestCallApiEndpointBase(test *testing.T) {

// Base functionality for each supported endpoint.
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/get",
parameters: []string{
"course-id:course101",
"assignment-id:hw0",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/list",
parameters: []string{
"course-id:course101",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/users/drop",
parameters: []string{
"target-email:[email protected]",
"course-id:course101",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/users/get",
parameters: []string{
"target-email:[email protected]",
"course-id:course101",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/users/list",
parameters: []string{
"course-id:course101",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/submissions/fetch/course/attempts",
parameters: []string{
"course-id:course101",
"assignment-id:hw0",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/submissions/fetch/course/scores",
parameters: []string{
"course-id:course101",
"assignment-id:hw0",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/submissions/fetch/user/attempt",
parameters: []string{
"target-email:[email protected]",
"course-id:course101",
"assignment-id:hw0",
"target-submission:1697406256",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/submissions/fetch/user/history",
parameters: []string{
"target-email:[email protected]",
"course-id:course101",
"assignment-id:hw0",
"target-submission:1697406256",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/submissions/fetch/user/peek",
parameters: []string{
"target-email:[email protected]",
Expand All @@ -92,15 +146,58 @@ func TestCallApiEndpointBase(test *testing.T) {
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "courses/assignments/submissions/remove",
parameters: []string{
"target-email:[email protected]",
"course-id:course101",
"assignment-id:hw0",
"target-submission:1697406256",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "lms/user/get",
parameters: []string{
"target-email:[email protected]",
"course-id:course101",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "logs/query",
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "metadata/describe",
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "users/get",
parameters: []string{
"target-email:[email protected]",
},
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "users/list",
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
IgnoreStdout: true,
},
endpoint: "users/remove",
parameters: []string{
"target-email:[email protected]",
Expand Down
1 change: 0 additions & 1 deletion cmd/course-user-drop/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestCourseUserDropBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find course: 'ZZZ'.`,
ExpectEmptyStdout: true,
},
targetEmail: "[email protected]",
courseID: "ZZZ",
Expand Down
1 change: 0 additions & 1 deletion cmd/course-user-get/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestCourseUserGetBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find course: 'ZZZ'."`,
ExpectEmptyStdout: true,
},
targetEmail: "[email protected]",
courseID: "ZZZ",
Expand Down
2 changes: 0 additions & 2 deletions cmd/course-user-list/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,13 @@ func TestCourseUserListBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find course: 'ZZZ'.`,
ExpectEmptyStdout: true,
},
courseID: "ZZZ",
},
{
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find course: 'ZZZ'.`,
ExpectEmptyStdout: true,
},
table: true,
courseID: "ZZZ",
Expand Down
2 changes: 0 additions & 2 deletions cmd/peek-submission/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestPeekBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find course: 'ZZZ'.`,
ExpectEmptyStdout: true,
},
targetEmail: "[email protected]",
courseID: "ZZZ",
Expand All @@ -77,7 +76,6 @@ func TestPeekBase(test *testing.T) {
CommonCMDTestCase: cmd.CommonCMDTestCase{
ExpectedExitCode: 1,
ExpectedStderrSubstring: `Could not find assignment: 'zzz'.`,
ExpectEmptyStdout: true,
},
targetEmail: "[email protected]",
courseID: "course101",
Expand Down
11 changes: 3 additions & 8 deletions internal/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
type CommonCMDTestCase struct {
ExpectedExitCode int
ExpectedStdout string
ExpectEmptyStdout bool
IgnoreStdout bool
ExpectedStderrSubstring string
LogLevel log.LogLevel
}
Expand Down Expand Up @@ -180,13 +180,8 @@ func RunCommonCMDTests(test *testing.T, mainFunc func(), args []string, commonTe
return "", "", -1, false
}

if commonTestCase.ExpectEmptyStdout && len(stdout) > 0 {
test.Errorf("%sUnexpected output. Expected no output, but received '%s'.", prefix, stdout)
return "", "", -1, false
}

if !commonTestCase.ExpectEmptyStdout && len(stdout) == 0 {
test.Errorf("%sExpected output but received no output.", prefix)
if commonTestCase.ExpectedStdout != stdout && !commonTestCase.IgnoreStdout {
test.Errorf("%sUnexpected output. Expected: \n'%s', \n Actual: \n'%s'.", prefix, commonTestCase.ExpectedStdout, stdout)
return "", "", -1, false
}

Expand Down

0 comments on commit 34af488

Please sign in to comment.