Skip to content

POC subtestWrapper #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions examples/documentation_examples/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,19 @@ func TestDocumentationExamples(t *testing.T) {

db := client.Database("documentation_examples")

documentation_examples.InsertExamples(t, db)
documentation_examples.QueryToplevelFieldsExamples(t, db)
documentation_examples.QueryEmbeddedDocumentsExamples(t, db)
documentation_examples.QueryArraysExamples(t, db)
documentation_examples.QueryArrayEmbeddedDocumentsExamples(t, db)
documentation_examples.QueryNullMissingFieldsExamples(t, db)
documentation_examples.ProjectionExamples(t, db)
documentation_examples.UpdateExamples(t, db)
documentation_examples.DeleteExamples(t, db)
documentation_examples.RunCommandExamples(t, db)
documentation_examples.IndexExamples(t, db)
documentation_examples.StableAPIExamples()
stw := newSubtestWrapper(t, db)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I do enjoy the abstraction here, is the usage of subtestWrapper strictly necessary? What would you think about just making each of these an explicit subtest? As in:

t.Run("InsertExamples", func(t *testing.T) {
   documentation_examples.InsertExamples(t, db)
}
t.Run("QueryToplevelFieldsExamples", func(t *testing.T) {
   documentation_examples.QueryToplevelFieldsExamples(t, db)
}
...

That might be more readable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only real value of the wrapper is to avoid multi-line subtests but that's just cosmetic. I'm more than happy to do it this way.

stw.run("InsertExamples", documentation_examples.InsertExamples)
stw.run("QueryToplevelFieldsExamples", documentation_examples.QueryToplevelFieldsExamples)
stw.run("QueryEmbeddedDocumentsExamples", documentation_examples.QueryEmbeddedDocumentsExamples)
stw.run("QueryArraysExamples", documentation_examples.QueryArraysExamples)
stw.run("QueryArrayEmbeddedDocumentsExamples", documentation_examples.QueryArrayEmbeddedDocumentsExamples)
stw.run("QueryNullMissingFieldssExamples", documentation_examples.QueryNullMissingFieldsExamples)
stw.run("ProjectionExamples", documentation_examples.ProjectionExamples)
stw.run("UpdateExamples", documentation_examples.UpdateExamples)
stw.run("DeleteExamples", documentation_examples.DeleteExamples)
stw.run("RunCommandExamples", documentation_examples.RunCommandExamples)
stw.run("IndexExamples", documentation_examples.IndexExamples)
stw.runEmpty("StableAPExamples", documentation_examples.StableAPIExamples)

// Because it uses RunCommand with an apiVersion, the strict count example can only be
// run on 5.0+ without auth. It also cannot be run on 6.0+ since the count command was
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could also use an mtest.Testing instance for the StableAPIStrictCountExample like mtest.Options().MinServerVersion("5.0").MaxServerVersion("6.0").Auth(true) instead of the odd testutil and os.Getenv logic I wrote below.

Expand Down Expand Up @@ -157,3 +158,29 @@ func createTopology(t *testing.T) *topology.Topology {
}
return topo
}

// subtestWrapper maintains testing state for subtest operations
type subtestWrapper struct {
t *testing.T
db *mongo.Database
}

func newSubtestWrapper(t *testing.T, db *mongo.Database) subtestWrapper {
stw := new(subtestWrapper)
stw.t = t
stw.db = db
return *stw
}

type subtest func(*testing.T, *mongo.Database)
type subtestEmpty func()

// run wraps a subtest using an `stw` object
func (stw subtestWrapper) run(name string, st subtest) {
stw.t.Run(name, func(t *testing.T) { st(stw.t, stw.db) })
}

// runEmpty wraps a subtest without functional arguments from an `stw` object.
func (stw subtestWrapper) runEmpty(name string, st subtestEmpty) {
stw.t.Run(name, func(t *testing.T) { st() })
}