I am new to Go language , can you please let me know why I am getting this error in my code
argument 1 expected [uint8 - 99] does not match actual [string - c9d778419a8ce7]
Actual code
var isRecordPresentQuery = `SELECT EXISTS
(SELECT 1
FROM public.records
WHERE "recordHash" = $1)`
var addRecordQuery = `INSERT INTO public.records
("fileId",
"recordHash"
)
VALUES(
$1, $2
)
RETURNING "recordId"`
hashKey := []string{
"c9d778419a8ce7",
"9881d3d5c5f089",
"b7c68a247ed250",
}
count := 0
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pgxMock, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer pgxMock.Close()
for _, line := range tt.input.filelines {
if line[0] == byte('6') {
recordHash := string(hashKey[count])
pgxMock.ExpectQuery(regexp.QuoteMeta(isRecordPresentQuery)).WithArgs(
recordHash,
).WillReturnRows(pgxmock.NewRows([]string{"exists"}).AddRow(false))
pgxMock.ExpectBegin()
pgxMock.ExpectQuery(regexp.QuoteMeta(addRecordQuery)).WithArgs(
1,
recordHash[count]
).WillReturnRows(pgxmock.NewRows([]string{"recordId"}).AddRow(int64(1)))
pgxMock.ExpectCommit()
count++
}
}
pgxMock.ExpectBegin()
if tt.expectedError == nil {
err = Processor(context.Background(), tt.input.filelines, tt.input.fileid)
assert.Nil(t, err)
}
})
}
I have tried hardcoding the values, I don't see any issue if I hardcode the hash values , but if i use array, then i get this error
I am new to Go language , can you please let me know why I am getting this error in my code
argument 1 expected [uint8 - 99] does not match actual [string - c9d778419a8ce7]
Actual code
var isRecordPresentQuery = `SELECT EXISTS
(SELECT 1
FROM public.records
WHERE "recordHash" = $1)`
var addRecordQuery = `INSERT INTO public.records
("fileId",
"recordHash"
)
VALUES(
$1, $2
)
RETURNING "recordId"`
hashKey := []string{
"c9d778419a8ce7",
"9881d3d5c5f089",
"b7c68a247ed250",
}
count := 0
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pgxMock, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
}
defer pgxMock.Close()
for _, line := range tt.input.filelines {
if line[0] == byte('6') {
recordHash := string(hashKey[count])
pgxMock.ExpectQuery(regexp.QuoteMeta(isRecordPresentQuery)).WithArgs(
recordHash,
).WillReturnRows(pgxmock.NewRows([]string{"exists"}).AddRow(false))
pgxMock.ExpectBegin()
pgxMock.ExpectQuery(regexp.QuoteMeta(addRecordQuery)).WithArgs(
1,
recordHash[count]
).WillReturnRows(pgxmock.NewRows([]string{"recordId"}).AddRow(int64(1)))
pgxMock.ExpectCommit()
count++
}
}
pgxMock.ExpectBegin()
if tt.expectedError == nil {
err = Processor(context.Background(), tt.input.filelines, tt.input.fileid)
assert.Nil(t, err)
}
})
}
I have tried hardcoding the values, I don't see any issue if I hardcode the hash values , but if i use array, then i get this error
Share Improve this question asked Mar 15 at 22:17 KshanStagKshanStag 171 silver badge3 bronze badges2 Answers
Reset to default 1Lets look at how you are using hashKey
. The only time you access this is:
recordHash := string(hashKey[count])
So recordHash
will contain a hash string (on the first iteration, this will be c9d778419a8ce7
). recordHash
is then used in:
pgxMock.ExpectQuery(regexp.QuoteMeta(addRecordQuery)).WithArgs(
1,
recordHash[count]
).WillReturnRows(pgxmock.NewRows([]string{"recordId"}).AddRow(int64(1)))
Sticking with the first iteration, recordHash[count]
will be the character c
(ASCII 99). Using an index expression on a string will return a byte
(an alias for a unit8). So the error is saying that you are comparing a byte
/unit8
99 (c
) against a string c9d778419a8ce7
(and this will fail).
I suspect the following will work as you expect:
pgxMock.ExpectQuery(regexp.QuoteMeta(addRecordQuery)).WithArgs(
1,
recordHash
).WillReturnRows(pgxmock.NewRows([]string{"recordId"}).AddRow(int64(1)))
hashKey := []string{
"c9d778419a8ce7",
"9881d3d5c5f089",
"b7c68a247ed250",
}
recordHash := string(hashKey[count])
pgxMock.ExpectQuery(regexp.QuoteMeta(addRecordQuery)).WithArgs(
1,
recordHash[count]
)
recordHash[count]
is equivalent to hashKey[count][count]
, which means it is a single character rather than the entire string. You should pass in recordHash
directly.