MCPcopy
hub / github.com/go-sql-driver/mysql / TestCRUD

Function TestCRUD

driver_test.go:311–404  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

309}
310
311func TestCRUD(t *testing.T) {
312 runTestsParallel(t, dsn, func(dbt *DBTest, tbl string) {
313 // Create Table
314 dbt.mustExec("CREATE TABLE " + tbl + " (value BOOL)")
315
316 // Test for unexpected data
317 var out bool
318 rows := dbt.mustQuery("SELECT * FROM " + tbl)
319 if rows.Next() {
320 dbt.Error("unexpected data in empty table")
321 }
322 rows.Close()
323
324 // Create Data
325 res := dbt.mustExec("INSERT INTO " + tbl + " VALUES (1)")
326 count, err := res.RowsAffected()
327 if err != nil {
328 dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
329 }
330 if count != 1 {
331 dbt.Fatalf("expected 1 affected row, got %d", count)
332 }
333
334 id, err := res.LastInsertId()
335 if err != nil {
336 dbt.Fatalf("res.LastInsertId() returned error: %s", err.Error())
337 }
338 if id != 0 {
339 dbt.Fatalf("expected InsertId 0, got %d", id)
340 }
341
342 // Read
343 rows = dbt.mustQuery("SELECT value FROM " + tbl)
344 if rows.Next() {
345 rows.Scan(&out)
346 if true != out {
347 dbt.Errorf("true != %t", out)
348 }
349
350 if rows.Next() {
351 dbt.Error("unexpected data")
352 }
353 } else {
354 dbt.Error("no data")
355 }
356 rows.Close()
357
358 // Update
359 res = dbt.mustExec("UPDATE "+tbl+" SET value = ? WHERE value = ?", false, true)
360 count, err = res.RowsAffected()
361 if err != nil {
362 dbt.Fatalf("res.RowsAffected() returned error: %s", err.Error())
363 }
364 if count != 1 {
365 dbt.Fatalf("expected 1 affected row, got %d", count)
366 }
367
368 // Check Update

Callers

nothing calls this directly

Calls 9

runTestsParallelFunction · 0.85
mustExecMethod · 0.80
mustQueryMethod · 0.80
RowsAffectedMethod · 0.80
LastInsertIdMethod · 0.80
ScanMethod · 0.80
NextMethod · 0.45
ErrorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected