go-mocket icon indicating copy to clipboard operation
go-mocket copied to clipboard

What is this testing? I can't get this library to actual fail a test.

Open rhugga opened this issue 5 years ago • 1 comments

func SetupTests(t *testing.T) *gorm.DB {
	mocket.Catcher.Register()
	mocket.Catcher.Logging = true
	db, err := gorm.Open(mocket.DriverName, "connection_string")
	if err != nil {
		t.Fatalf(err.Error())
	}
	return db
}

func TestInsertStoragePool(t *testing.T) {

	cases := []struct {
		givenPool    StoragePool
                wantedError error
	}{
		{ // happy path
			givenPool: StoragePool{
				Name:   "POOL1",
				PoolId: "1",
				Type:   "NetworkFilesystem",
				Volumes: []Volume{
					{
						Name:          "VOL01",
						StorageId:     "1",
						Type:          "ROOT",
						VolumeId:      "1",
					},

				},
			},
			wantedError: nil,
		},
	}

	for _, c := range cases {
		db := SetupTests(t)
		db.LogMode(true)
		reply := []map[string]interface{}{{"id": 5}}
		mocket.Catcher.Reset().NewMock().WithQuery("INSERT INTO \"storage_pools\"").WithID(int64(5)).WithArgs(c.givenPool).WithReply(reply)
		err := InsertStoragePool(db, &c.givenPool)
		if err != nil {
			t.Error(err)
		}
		fmt.Fprintf(os.Stderr, "\nPOOLID=%d\n", c.givenPool.ID)
	}
}

What is this testing?

  1. The primary key is getting set to 5577006791947779410, not 5.
  2. The WithReply is apparently not doing anything because this is not failing.
  3. My Type column above is an enumerated type. When I set the Type to an invalid value, this test still succeeds in flying colors.

According to go test, this test succeeds with flying colors.

rhugga avatar Dec 13 '19 15:12 rhugga

I found something similar, for testing set the expected query to just UPDATE while the underlying query is an INSERT statement. the test still passes.

tokamak-hf avatar Jun 15 '21 16:06 tokamak-hf