Can't get a test working for an INSERT statement

1.1k Views Asked by At

We have spent 2 frustrating weeks trying every possible permutation of functions and approaches using sqlmock but can't get a test working for an INSERT statement. This is very basic mom and pop SQL. Just a simple insert using Gorm.

Way too much code to post and way to many permutations of errors and approaches to list here but I'll post the latest.

type AnyTime struct{}

func (a AnyTime) Match(v driver.Value) bool {
    _, ok := v.(time.Time)
    return ok
}

func TestInsertStoragePool(t *testing.T) {

    cases := []struct {
        givenPool    StoragePool
        wantedError  error
    }{
        {
            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, mock, err := sqlmock.New()
        if err != nil {
            t.Fatal(err)
        }

        // Every attempt we make we try "Postgres", "mysql" and "sqlite3" here
        DB, err := gorm.Open("sqlite3", db)
        if err != nil {
            t.Fatal(err)
        }
        DB.LogMode(true)

        mock.ExpectBegin()
        mock.ExpectExec(`INSERT INTO "storage_pools"`).WithArgs(
            AnyTime{},
            AnyTime{},
            nil,
            0,
            0,
            "",
            "",
            "",
            0,
            0,
            0,
            "",
            c.givenPool.Name,
            "",
            0,
            "",
            "",
            c.givenPool.PoolId,
            "",
            "",
            "",
            c.givenPool.Type,
            "",
            "").WillReturnResult(sqlmock.NewResult(1, 1))

        mock.ExpectCommit()

        err = InsertStoragePool(DB, &c.givenPool)
        if err != nil {
            fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
        }

        if !reflect.DeepEqual(err, c.wantedError) {
            t.Errorf("expecting errror %q, got %q", c.wantedError, err)
        }

    }

}

The error from this attempt:

$go test ./...

(/database.go:231)
[2019-12-13 09:22:48]  [0.11ms]  INSERT  INTO "storage_pools" ("created_at","updated_at","deleted_at","allocatediops","capacityiops","clusterid","clustername","created","disksizeallocated","disksizetotal","disksizeused","ipaddress","name","path","physicalsize","podid","podname","poolid","provider","scope","state","type","zoneid","zonename") VALUES ('2019-12-13 09:22:48','2019-12-13 09:22:48',NULL,0,0,'','','',0,0,0,'','POOL1','',0,'','','1','','','','NetworkFilesystem','','')
[1 rows affected or returned ]

(/database.go:231)
[2019-12-13 09:22:48]  call to ExecQuery 'INSERT  INTO "volumes" ("created_at","updated_at","deleted_at","clusterid","clustername","domainid","domain","name","path","physicalsize","podid","podname","size","state","storageid","storage","type","virtualsize","virtualmachineid","vmdisplayname","vmname","vmstate","volumeid","zoneid","zonename") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)' with args [{Name: Ordinal:1 Value:2019-12-13 09:22:48.093749 -0800 PST m=+0.004941653} {Name: Ordinal:2 Value:2019-12-13 09:22:48.093749 -0800 PST m=+0.004941653} {Name: Ordinal:3 Value:<nil>} {Name: Ordinal:4 Value:} {Name: Ordinal:5 Value:} {Name: Ordinal:6 Value:} {Name: Ordinal:7 Value:} {Name: Ordinal:8 Value:VOL01} {Name: Ordinal:9 Value:} {Name: Ordinal:10 Value:0} {Name: Ordinal:11 Value:} {Name: Ordinal:12 Value:} {Name: Ordinal:13 Value:0} {Name: Ordinal:14 Value:} {Name: Ordinal:15 Value:1} {Name: Ordinal:16 Value:} {Name: Ordinal:17 Value:ROOT} {Name: Ordinal:18 Value:0} {Name: Ordinal:19 Value:} {Name: Ordinal:20 Value:} {Name: Ordinal:21 Value:} {Name: Ordinal:22 Value:} {Name: Ordinal:23 Value:1} {Name: Ordinal:24 Value:} {Name: Ordinal:25 Value:}], was not expected, next expectation is: ExpectedCommit => expecting transaction Commit
1

There are 1 best solutions below

0
On

The error message is telling you that an additional insert was being executed after the one you expected:

INSERT INTO "volumes" ( ..... ) was not expected, next expectation is: ExpectedCommit

In your code we can see that your StoragePool struct has also some Volumes inside in a slice.

So probably when you call InsertStoragePool, first a row is inserted into storage_pools, then the volumes are inserted.

You should add a new expectation for that insert before adding the expectation for the commit.