|
|
@@ -6,6 +6,7 @@ import (
|
|
|
"fmt"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type position struct {
|
|
|
@@ -252,3 +253,91 @@ func TestUDT_NullObject(t *testing.T) {
|
|
|
t.Errorf("expected empty string to be returned for null udt: got %q", readCol.Owner)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func TestMapScanUDT(t *testing.T) {
|
|
|
+ if *flagProto < protoVersion3 {
|
|
|
+ t.Skip("UDT are only available on protocol >= 3")
|
|
|
+ }
|
|
|
+
|
|
|
+ session := createSession(t)
|
|
|
+ defer session.Close()
|
|
|
+
|
|
|
+ err := createTable(session, `CREATE TYPE log_entry (
|
|
|
+ created_timestamp timestamp,
|
|
|
+ message text
|
|
|
+ );`)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ err = createTable(session, `CREATE TABLE requests_by_id (
|
|
|
+ id uuid PRIMARY KEY,
|
|
|
+ type int,
|
|
|
+ log_entries list<frozen <log_entry>>
|
|
|
+ );`)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ entry := []struct {
|
|
|
+ CreatedTimestamp time.Time `cql:"created_timestamp"`
|
|
|
+ Message string `cql:"message"`
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ CreatedTimestamp: time.Now().Truncate(time.Millisecond),
|
|
|
+ Message: "test time now",
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ id, _ := RandomUUID()
|
|
|
+ const typ = 1
|
|
|
+
|
|
|
+ err = session.Query("INSERT INTO requests_by_id(id, type, log_entries) VALUES (?, ?, ?)", id, typ, entry).Exec()
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ rawResult := map[string]interface{}{}
|
|
|
+ err = session.Query(`SELECT * FROM requests_by_id WHERE id = ?`, id).MapScan(rawResult)
|
|
|
+ if err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ logEntries, ok := rawResult["log_entries"].([]map[string]interface{})
|
|
|
+ if !ok {
|
|
|
+ t.Fatal("log_entries not in scanned map")
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(logEntries) != 1 {
|
|
|
+ t.Fatalf("expected to get 1 log_entry got %d", len(logEntries))
|
|
|
+ }
|
|
|
+
|
|
|
+ logEntry := logEntries[0]
|
|
|
+
|
|
|
+ timestamp, ok := logEntry["created_timestamp"]
|
|
|
+ if !ok {
|
|
|
+ t.Error("created_timestamp not unmarshalled into map")
|
|
|
+ } else {
|
|
|
+ if ts, ok := timestamp.(time.Time); ok {
|
|
|
+ if !ts.In(time.UTC).Equal(entry[0].CreatedTimestamp.In(time.UTC)) {
|
|
|
+ t.Errorf("created_timestamp not equal to stored: got %v expected %v", ts.In(time.UTC), entry[0].CreatedTimestamp.In(time.UTC))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ t.Errorf("created_timestamp was not time.Time got: %T", timestamp)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ message, ok := logEntry["message"]
|
|
|
+ if !ok {
|
|
|
+ t.Error("message not unmarshalled into map")
|
|
|
+ } else {
|
|
|
+ if ts, ok := message.(string); ok {
|
|
|
+ if ts != message {
|
|
|
+ t.Errorf("message not equal to stored: got %v expected %v", ts, entry[0].Message)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ t.Errorf("message was not string got: %T", message)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|