@@ -529,6 +529,55 @@ func TestValuer(t *testing.T) {
529
529
})
530
530
}
531
531
532
+ type testValuerWithValidation struct {
533
+ value string
534
+ }
535
+
536
+ func (tv testValuerWithValidation ) Value () (driver.Value , error ) {
537
+ if len (tv .value ) == 0 {
538
+ return nil , fmt .Errorf ("Invalid string valuer. Value must not be empty" )
539
+ }
540
+
541
+ return tv .value , nil
542
+ }
543
+
544
+ func TestValuerWithValidation (t * testing.T ) {
545
+ runTests (t , dsn , func (dbt * DBTest ) {
546
+ in := testValuerWithValidation {"a_value" }
547
+ var out string
548
+ var rows * sql.Rows
549
+
550
+ dbt .mustExec ("CREATE TABLE testValuer (value VARCHAR(255)) CHARACTER SET utf8" )
551
+ dbt .mustExec ("INSERT INTO testValuer VALUES (?)" , in )
552
+
553
+ rows = dbt .mustQuery ("SELECT value FROM testValuer" )
554
+ defer rows .Close ()
555
+
556
+ if rows .Next () {
557
+ rows .Scan (& out )
558
+ if in .value != out {
559
+ dbt .Errorf ("Valuer: %v != %s" , in , out )
560
+ }
561
+ } else {
562
+ dbt .Errorf ("Valuer: no data" )
563
+ }
564
+
565
+ if _ , err := dbt .db .Exec ("INSERT INTO testValuer VALUES (?)" , testValuerWithValidation {"" }); err == nil {
566
+ dbt .Errorf ("Failed to check valuer error" )
567
+ }
568
+
569
+ if _ , err := dbt .db .Exec ("INSERT INTO testValuer VALUES (?)" , nil ); err != nil {
570
+ dbt .Errorf ("Failed to check nil" )
571
+ }
572
+
573
+ if _ , err := dbt .db .Exec ("INSERT INTO testValuer VALUES (?)" , map [string ]bool {}); err == nil {
574
+ dbt .Errorf ("Failed to check not valuer" )
575
+ }
576
+
577
+ dbt .mustExec ("DROP TABLE IF EXISTS testValuer" )
578
+ })
579
+ }
580
+
532
581
type timeTests struct {
533
582
dbtype string
534
583
tlayout string
0 commit comments