-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Enabled defining tavern tests using YAML * Added a few test cases * added mutation tests * added simple query tests * Updated docs
- Loading branch information
Showing
29 changed files
with
865 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package graphql_test | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/kcarretto/realm/tavern/auth" | ||
"github.com/kcarretto/realm/tavern/ent/enttest" | ||
"github.com/kcarretto/realm/tavern/graphql" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/99designs/gqlgen/client" | ||
"github.com/99designs/gqlgen/graphql/handler" | ||
_ "github.com/mattn/go-sqlite3" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type testCase struct { | ||
State string `yaml:"state"` | ||
Requestor struct { | ||
SessionToken string `yaml:"session_token"` | ||
} `yaml:"requestor"` | ||
Query string `yaml:"query"` | ||
Variables map[string]any `yaml:"variables"` | ||
Expected map[string]any `yaml:"expected"` | ||
ExpectedError string `yaml:"expected_error"` | ||
} | ||
|
||
func runTestCase(t *testing.T, path string) { | ||
// TestDB Config | ||
var ( | ||
driverName = "sqlite3" | ||
dataSourceName = "file:ent?mode=memory&cache=shared&_fk=1" | ||
) | ||
|
||
// Read Test Case | ||
tcBytes, err := os.ReadFile(path) | ||
require.NoError(t, err, "failed to read test case %q", path) | ||
|
||
// Parse Test Case | ||
var tc testCase | ||
yamlErr := yaml.Unmarshal(tcBytes, &tc) | ||
require.NoError(t, yamlErr, "failed to parse test case %q", path) | ||
|
||
// Marshal expected result to JSON | ||
expectedJSON, err := json.Marshal(tc.Expected) | ||
require.NoError(t, err) | ||
|
||
// Ent Client | ||
graph := enttest.Open(t, driverName, dataSourceName, enttest.WithOptions()) | ||
defer graph.Close() | ||
|
||
// Initial DB State | ||
db, err := sql.Open(driverName, dataSourceName) | ||
require.NoError(t, err, "failed to open test db") | ||
defer db.Close() | ||
_, dbErr := db.Exec(tc.State) | ||
require.NoError(t, dbErr, "failed to setup test db state") | ||
|
||
// Server | ||
srv := auth.Middleware(handler.NewDefaultServer(graphql.NewSchema(graph)), graph) | ||
gqlClient := client.New(srv) | ||
|
||
var opts []client.Option | ||
|
||
// Variables | ||
for key, val := range tc.Variables { | ||
opts = append(opts, client.Var(key, val)) | ||
} | ||
|
||
// Requestor | ||
if tc.Requestor.SessionToken != "" { | ||
opts = append(opts, client.AddCookie(&http.Cookie{ | ||
Name: auth.SessionCookieName, | ||
Value: tc.Requestor.SessionToken, | ||
Expires: time.Now().Add(24 * time.Hour), | ||
})) | ||
} | ||
|
||
// Make Request | ||
resp := new(map[string]any) | ||
queryErr := gqlClient.Post(tc.Query, resp, opts...) | ||
|
||
// Handle Expected Errors | ||
if tc.ExpectedError != "" { | ||
assert.ErrorContains(t, queryErr, tc.ExpectedError) | ||
return | ||
} | ||
require.NoError(t, queryErr, "query failed with error") | ||
|
||
// Marshal response to JSON | ||
respJSON, err := json.Marshal(resp) | ||
require.NoError(t, err, "failed to marshal response to JSON") | ||
|
||
// Assert the result is as expected | ||
assert.Equal(t, string(expectedJSON), string(respJSON), "response does not match expected result") | ||
} | ||
|
||
// TestAPI finds and runs all test cases defined in the testdata directory. | ||
func TestAPI(t *testing.T) { | ||
runTestsInDir(t, "testdata") | ||
} | ||
|
||
func runTestsInDir(t *testing.T, root string) { | ||
files, err := os.ReadDir(root) | ||
require.NoError(t, err) | ||
|
||
for _, f := range files { | ||
// Derive relative path | ||
path := filepath.Join(root, f.Name()) | ||
|
||
// Recurse in subdirectories by grouping them into sub-tests | ||
if f.IsDir() { | ||
t.Run(filepath.Base(f.Name()), func(t *testing.T) { | ||
runTestsInDir(t, path) | ||
}) | ||
continue | ||
} | ||
|
||
// Skip files that are not test case files | ||
if filepath.Ext(path) != ".yml" { | ||
continue | ||
} | ||
|
||
// Run test case | ||
testName := filepath.Base(strings.TrimSuffix(path, ".yml")) | ||
t.Run(testName, func(t *testing.T) { | ||
runTestCase(t, path) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Example Test Case | ||
|
||
# Create a user in the DB before the test case runs | ||
state: | | ||
INSERT INTO `users` (id,oauth_id,photo_url,name,session_token,is_activated,is_admin) | ||
VALUES (5,"test_oauth_id","https://photos.com","test","secretToken",true,true); | ||
# Authenticate as that user (via session_token) | ||
requestor: | ||
session_token: secretToken | ||
|
||
# Query all existing users | ||
query: | | ||
query Users { | ||
users { | ||
id | ||
} | ||
} | ||
# Ensure the GraphQL query returns exactly one user, the initial one we created | ||
expected: | ||
users: | ||
- id: "5" |
20 changes: 20 additions & 0 deletions
20
tavern/graphql/testdata/mutations/claimTasks/ExistingSession.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
state: | | ||
INSERT INTO `sessions` (id, name, identifier) | ||
VALUES (1337,"delightful-lich","EXISTING-SESSION"); | ||
query: | | ||
mutation ExistingSession($input: ClaimTasksInput!) { | ||
claimTasks(input: $input) { | ||
id | ||
} | ||
} | ||
variables: | ||
input: | ||
principal: root | ||
hostname: some-machine | ||
hostPlatform: Linux | ||
sessionIdentifier: EXISTING-SESSION | ||
hostIdentifier: MY-HOST | ||
agentIdentifier: COOL-TEST | ||
|
||
expected: | ||
claimTasks: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
state: | | ||
INSERT INTO `sessions` (id, name, identifier) | ||
VALUES (1337,"delightful-lich","EXISTING-SESSION"); | ||
INSERT INTO `sessions` (id, name, identifier) | ||
VALUES (1338,"bad-boi","BAD-SESSION"); | ||
INSERT INTO `tomes` (id, name, description, eldritch, hash, created_at, last_modified_at) | ||
VALUES (2000,"Test Tome","Used in a unit test :D","print('Hello World!')","abcdefg","2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `jobs` (id, job_tome, name, created_at, last_modified_at) | ||
VALUES (7000,2000,"Test Job","2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `tasks` (id, task_session, job_tasks, created_at, last_modified_at) | ||
VALUES (8000,1337,7000,"2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `tasks` (id, task_session, job_tasks, created_at, last_modified_at) | ||
VALUES (8001,1337,7000,"2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `tasks` (id, task_session, job_tasks, created_at, last_modified_at) | ||
VALUES (8002,1338,7000,"2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `tasks` (id, task_session, job_tasks, created_at, last_modified_at, claimed_at) | ||
VALUES (8003,1337,7000,"2023-03-04 14:51:13","2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
query: | | ||
mutation Filters($input: ClaimTasksInput!) { | ||
claimTasks(input: $input) { | ||
id | ||
} | ||
} | ||
variables: | ||
input: | ||
principal: root | ||
hostname: some-machine | ||
hostPlatform: Linux | ||
sessionIdentifier: EXISTING-SESSION | ||
hostIdentifier: MY-HOST | ||
agentIdentifier: COOL-TEST | ||
|
||
expected: | ||
claimTasks: | ||
- id: "8000" | ||
- id: "8001" |
30 changes: 30 additions & 0 deletions
30
tavern/graphql/testdata/mutations/claimTasks/MultiTask.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
state: | | ||
INSERT INTO `sessions` (id, name, identifier) | ||
VALUES (1337,"delightful-lich","EXISTING-SESSION"); | ||
INSERT INTO `tomes` (id, name, description, eldritch, hash, created_at, last_modified_at) | ||
VALUES (2000,"Test Tome","Used in a unit test :D","print('Hello World!')","abcdefg","2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `jobs` (id, job_tome, name, created_at, last_modified_at) | ||
VALUES (7000,2000,"Test Job","2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `tasks` (id, task_session, job_tasks, created_at, last_modified_at) | ||
VALUES (8000,1337,7000,"2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `tasks` (id, task_session, job_tasks, created_at, last_modified_at) | ||
VALUES (8001,1337,7000,"2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
query: | | ||
mutation MutiTask($input: ClaimTasksInput!) { | ||
claimTasks(input: $input) { | ||
id | ||
} | ||
} | ||
variables: | ||
input: | ||
principal: root | ||
hostname: some-machine | ||
hostPlatform: Linux | ||
sessionIdentifier: EXISTING-SESSION | ||
hostIdentifier: MY-HOST | ||
agentIdentifier: COOL-TEST | ||
|
||
expected: | ||
claimTasks: | ||
- id: "8000" | ||
- id: "8001" |
17 changes: 17 additions & 0 deletions
17
tavern/graphql/testdata/mutations/claimTasks/NewSession.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
query: | | ||
mutation NewSession($input: ClaimTasksInput!) { | ||
claimTasks(input: $input) { | ||
id | ||
} | ||
} | ||
variables: | ||
input: | ||
principal: root | ||
hostname: some-machine | ||
hostPlatform: Linux | ||
sessionIdentifier: SESSION-IDENTIFIER | ||
hostIdentifier: MY-HOST | ||
agentIdentifier: COOL-TEST | ||
|
||
expected: | ||
claimTasks: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
state: | | ||
INSERT INTO `sessions` (id, name, identifier) | ||
VALUES (1337,"delightful-lich","EXISTING-SESSION"); | ||
INSERT INTO `tomes` (id, name, description, eldritch, hash, created_at, last_modified_at) | ||
VALUES (2000,"Test Tome","Used in a unit test :D","print('Hello World!')","abcdefg","2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `jobs` (id, job_tome, name, created_at, last_modified_at) | ||
VALUES (7000,2000,"Test Job","2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
INSERT INTO `tasks` (id, task_session, job_tasks, created_at, last_modified_at) | ||
VALUES (8000,1337,7000,"2023-03-04 14:51:13","2023-03-04 14:51:13"); | ||
query: | | ||
mutation OneTask($input: ClaimTasksInput!) { | ||
claimTasks(input: $input) { | ||
id | ||
} | ||
} | ||
variables: | ||
input: | ||
principal: root | ||
hostname: some-machine | ||
hostPlatform: Linux | ||
sessionIdentifier: EXISTING-SESSION | ||
hostIdentifier: MY-HOST | ||
agentIdentifier: COOL-TEST | ||
|
||
expected: | ||
claimTasks: | ||
- id: "8000" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
state: | | ||
INSERT INTO `users` (id,oauth_id,photo_url,name,session_token,is_activated,is_admin) | ||
VALUES (5,"test_oauth_id","https://photos.com","test","secretToken",true,true); | ||
INSERT INTO `sessions` (id, name, identifier) | ||
VALUES (1337,"delightful-lich","ABCDEFG-123456"); | ||
INSERT INTO `tomes` (id, name, description, eldritch, hash, created_at, last_modified_at) | ||
VALUES (2000,"Test Tome","Used in a unit test :D", "print('Hello World!')", "abcdefg", "2023-03-04 14:51:13", "2023-03-04 14:51:13"); | ||
requestor: | ||
session_token: secretToken | ||
query: | | ||
mutation CreateJobWithNoFiles($sessionIDs: [ID!]!, $input: CreateJobInput!) { | ||
createJob(sessionIDs:$sessionIDs, input:$input) { | ||
name | ||
tome { | ||
id | ||
name | ||
} | ||
tasks { | ||
session { | ||
id | ||
} | ||
job { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
variables: | ||
sessionIDs: | ||
- 1337 | ||
input: | ||
name: "WonderfulJob" | ||
tomeID: "2000" | ||
|
||
expected: | ||
createJob: | ||
name: "WonderfulJob" | ||
tome: | ||
id: "2000" | ||
name: "Test Tome" | ||
tasks: | ||
- session: | ||
id: "1337" | ||
job: | ||
name: "WonderfulJob" |
Oops, something went wrong.