Skip to content

Switch the easymongo test suite to easymongotest #8

Description

@TopherGopher

Part of the easymongo refactor (see PLAN.md on the refactor branch). Agreed with the maintainer: keep one container per test function; do not consolidate into a shared container.

Working context (read this first)

Scope

  • common_test.go: replace mongotest.NewTestConnection(true) / conn.KillMongoContainer() with easymongotest.Run(t); conn becomes *easymongotest.TestConnection so the embedded *easymongo.Connection methods (Database, D, Ping, MongoURI, DatabaseNames) keep working for the ten test functions (aggregate_test.go, connect_test.go, database_test.go, delete_query_test.go, find_and_query_test.go, find_distinct_test.go, find_query_test.go, index_test.go, insert_query_test.go, update_query_test.go).
  • setup(t) / teardown(t) can shrink to conn = easymongotest.Run(t) since Run registers cleanup; keep the helper names so the ten call sites do not change.
  • connect_test.go currently imports logrus for a logger fixture; replace with a small test logger implementing easymongo.Logger (also unblocks dropping logrus).
  • Remove the direct github.com/tophergopher/mongotest (v1 path) requirement from go.mod.

Implementation details

Current common_test.go (for reference; rewrite it):

var conn *mongotest.TestConnection
func setup(t *testing.T) { conn, err = mongotest.NewTestConnection(true); ...; t.Cleanup(func() { teardown(t) }) }
func teardown(t *testing.T) { conn.KillMongoContainer() }

New version:

var conn *easymongotest.TestConnection
func setup(t *testing.T) { t.Helper(); conn = easymongotest.Run(t) }
func teardown(t *testing.T) { t.Helper() } // kept so call sites compile; Run already registers cleanup

createBatmanArchive(t) and the enemy fixture stay as they are (only the primitive import changes later in #4). connect_test.go builds a logrus logger for ConnectWith(...).Logger(...); replace it with:

type testLogger struct{ t *testing.T }
func (l testLogger) Debugf(f string, a ...interface{}) { l.t.Logf("DEBUG "+f, a...) }
func (l testLogger) Errorf(f string, a ...interface{}) { l.t.Logf("ERROR "+f, a...) }

After the change run go test -race -count=1 ./... && docker ps -a --filter label=mongotest=regression -q | wc -l and confirm the count is 0.

Tests first

This issue is itself the test change. Acceptance is the full suite:

  • go test -race ./... passes against Docker with the same ten containers started and removed (verify no mongotest=regression containers remain after the run).

Acceptance

  • No *_test.go file imports github.com/tophergopher/mongotest directly; they go through easymongotest.

Depends on: #7.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions