github.com/acoshift/pgsql@v0.15.3/time_test.go (about)

     1  package pgsql_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/acoshift/pgsql"
     8  )
     9  
    10  func TestTime(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	db := open(t)
    14  	defer db.Close()
    15  
    16  	_, err := db.Exec(`
    17  		drop table if exists test_pgsql_time;
    18  		create table test_pgsql_time (
    19  			id int primary key,
    20  			value timestamp
    21  		);
    22  		insert into test_pgsql_time (
    23  			id, value
    24  		) values
    25  			(0, now()),
    26  			(1, null);
    27  	`)
    28  	if err != nil {
    29  		t.Fatalf("prepare table error; %v", err)
    30  	}
    31  	defer db.Exec(`drop table test_pgsql_time`)
    32  
    33  	var n, k time.Time
    34  	var p pgsql.Time
    35  	err = db.QueryRow(`select value from test_pgsql_time where id = 0`).Scan(&p)
    36  	if err != nil {
    37  		t.Fatalf("scan time error; %v", err)
    38  	}
    39  	err = db.QueryRow(`select value from test_pgsql_time where id = 0`).Scan(&n)
    40  	if err != nil {
    41  		t.Fatalf("scan native time error; %v", err)
    42  	}
    43  	if !p.Equal(n) {
    44  		t.Fatalf("scan time not equal when insert; expected %v; got %v", n, p)
    45  	}
    46  	err = db.QueryRow(`select value from test_pgsql_time where id = 0`).Scan(pgsql.NullTime(&k))
    47  	if err != nil {
    48  		t.Fatalf("scan null time error; %v", err)
    49  	}
    50  	if !k.Equal(n) {
    51  		t.Fatalf("scan time not equal when insert; expected %v; got %v", n, p)
    52  	}
    53  
    54  	err = db.QueryRow(`select value from test_pgsql_time where id = 1`).Scan(&p)
    55  	if err != nil {
    56  		t.Fatalf("scan time error; %v", err)
    57  	}
    58  	if !p.IsZero() {
    59  		t.Fatalf("invalid time; expected empty got %v", p)
    60  	}
    61  
    62  	n = time.Now()
    63  	p.Time = n
    64  	var ok bool
    65  	err = db.QueryRow(`select $1 = $2`, p, n).Scan(&ok)
    66  	if err != nil {
    67  		t.Fatalf("sql error; %v", err)
    68  	}
    69  	if !ok {
    70  		t.Fatalf("invalid time")
    71  	}
    72  
    73  	err = db.QueryRow(`select $1 = $2`, pgsql.NullTime(&n), n).Scan(&ok)
    74  	if err != nil {
    75  		t.Fatalf("sql error; %v", err)
    76  	}
    77  	if !ok {
    78  		t.Fatalf("invalid time")
    79  	}
    80  
    81  	p.Time = time.Time{}
    82  	err = db.QueryRow(`insert into test_pgsql_time (id, value) values (2, $1) returning value is null`, p).Scan(&ok)
    83  	if err != nil {
    84  		t.Fatalf("sql error; %v", err)
    85  	}
    86  	if !ok {
    87  		t.Fatalf("invalid time")
    88  	}
    89  
    90  	err = db.QueryRow(`insert into test_pgsql_time (id, value) values (3, $1) returning value is null`, pgsql.NullTime(new(time.Time))).Scan(&ok)
    91  	if err != nil {
    92  		t.Fatalf("sql error; %v", err)
    93  	}
    94  	if !ok {
    95  		t.Fatalf("invalid time")
    96  	}
    97  }