From c6591cdabb31a6f15b83f3496fa0235695892ec1 Mon Sep 17 00:00:00 2001 From: Saxon Chuang Date: Tue, 7 Jun 2022 13:03:27 +0800 Subject: [PATCH] [test] add unit test of #178 --- mssql_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/mssql_test.go b/mssql_test.go index 4a9c14b..182dd7d 100644 --- a/mssql_test.go +++ b/mssql_test.go @@ -1917,3 +1917,69 @@ IF @@ROWCOUNT = 0 t.Fatal(err) } } + +// https://github.com/alexbrainman/odbc/issues/178 +func TestMSSQLIssue178(t *testing.T) { + db, _, err := mssqlConnect() + if err != nil { + t.Fatal(err) + } + defer func() { + // not checking resources usage here, because these are + // unpredictable due to use of goroutines. + err := db.Close() + if err != nil { + t.Fatalf("error closing DB: %v", err) + } + }() + + var langNames = map[string]string{ + "zh_tw": "張三", + "zh_cn": "张三", + } + + db.Exec("drop table dbo.temp") + exec(t, db, "create table dbo.temp (lang char(5), name varchar(4))") + + func() { + // insert people names + s, err := db.Prepare("insert into dbo.temp (lang, name) values (?, ?)") + if err != nil { + t.Fatal(err) + } + defer s.Close() + defer exec(t, db, "drop table dbo.temp") + + for lang, name := range langNames { + _, err := s.Exec(lang, name) + if err != nil { + t.Fatal(err) + } + } + + // query inserted data and compare with langPeople + rows, err := db.Query("select lang, name from dbo.temp") + if err != nil { + t.Fatalf("Query: %v", err) + } + defer rows.Close() + + for rows.Next() { + var rowLang, rowName string + if err = rows.Scan(&rowLang, &rowName); err != nil { + t.Fatalf("Scan: %v", err) + } + name, ok := langNames[rowLang] + if !ok { + t.Fatalf("can't find people name by lang=: %v", rowLang) + } + if name != rowName { + t.Fatalf("people name missmatch, %v != %v", rowName, name) + } + } + err = rows.Err() + if err != nil { + t.Fatal(err) + } + }() +}