Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.

Commit 9c23a8b

Browse files
committed
Added DurationToDuration and InterfaceToDuration
1 parent 0a95840 commit 9c23a8b

File tree

3 files changed

+157
-5
lines changed

3 files changed

+157
-5
lines changed

cqlSql_test.go

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"database/sql"
66
"testing"
7+
// "time"
78
)
89

910
func TestSqlOpen(t *testing.T) {
@@ -75,7 +76,8 @@ func TestSqlCreate(t *testing.T) {
7576

7677
// create table
7778
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
78-
result, err = db.ExecContext(ctx, "create table "+KeyspaceName+"."+TableName+" (text_data text PRIMARY KEY, int_data int)")
79+
// removed duration_data duration
80+
result, err = db.ExecContext(ctx, "create table "+KeyspaceName+"."+TableName+" (text_data text PRIMARY KEY, int_data int, timestamp_data timestamp)")
7981
cancel()
8082
if err != nil {
8183
t.Fatal("ExecContext error: ", err)
@@ -108,9 +110,19 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
108110
t.Fatal("db is nil")
109111
}
110112

111-
// insert one
113+
// truncate table
112114
ctx, cancel := context.WithTimeout(context.Background(), TimeoutValid)
113-
result, err := db.ExecContext(ctx, "insert into "+KeyspaceName+"."+TableName+" (text_data, int_data) values (?, ?)", "one", 1)
115+
result, err := db.ExecContext(ctx, "truncate table "+KeyspaceName+"."+TableName)
116+
if err != nil {
117+
t.Fatal("ExecContext error: ", err)
118+
}
119+
if result == nil {
120+
t.Fatal("result is nil")
121+
}
122+
123+
// insert one
124+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
125+
result, err = db.ExecContext(ctx, "insert into "+KeyspaceName+"."+TableName+" (text_data, int_data) values (?, ?)", "one", 1)
114126
if err != nil {
115127
t.Fatal("ExecContext error: ", err)
116128
}
@@ -311,6 +323,105 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) {
311323
t.Fatal("result is nil")
312324
}
313325

326+
/*
327+
// insert three timestamp
328+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
329+
result, err = db.ExecContext(ctx, "insert into "+KeyspaceName+"."+TableName+" (text_data, timestamp_data) values (?, ?)", "three", TestTimeNow)
330+
cancel()
331+
if err != nil {
332+
t.Fatal("ExecContext error: ", err)
333+
}
334+
if result == nil {
335+
t.Fatal("result is nil")
336+
}
337+
338+
// select three timestamp
339+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
340+
rows, err = db.QueryContext(ctx, "select text_data, timestamp_data from "+KeyspaceName+"."+TableName+" where text_data = ?", "three")
341+
if err != nil {
342+
t.Fatal("QueryContext error: ", err)
343+
}
344+
if rows == nil {
345+
t.Fatal("rows is nil")
346+
}
347+
if !rows.Next() {
348+
t.Fatal("no Next rows")
349+
}
350+
err = rows.Scan(destPointer...)
351+
if err != nil {
352+
t.Fatal("Scan error: ", err)
353+
}
354+
if dest[0] != "three" {
355+
t.Fatalf("text_data - received: %v - expected: %v", dest[0], "three")
356+
}
357+
aTime, ok := dest[1].(time.Time)
358+
if !ok {
359+
t.Fatalf("timestamp_data not time, type: %T", dest[1])
360+
}
361+
if !aTime.Equal(TestTimeNow) {
362+
t.Fatalf("timestamp_data - received: %v - expected: %v", aTime, TestTimeNow)
363+
}
364+
if rows.Next() {
365+
t.Fatal("has Next rows")
366+
}
367+
err = rows.Close()
368+
if err != nil {
369+
t.Fatal("Close error: ", err)
370+
}
371+
cancel()
372+
err = rows.Err()
373+
if err != nil {
374+
t.Fatal("Err error: ", err)
375+
}
376+
377+
// insert four duration
378+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
379+
result, err = db.ExecContext(ctx, "insert into "+KeyspaceName+"."+TableName+" (text_data, duration_data) values (?, ?)", "four", time.Hour+time.Minute+time.Second+time.Millisecond+time.Microsecond+time.Nanosecond)
380+
cancel()
381+
if err != nil {
382+
t.Fatal("ExecContext error: ", err)
383+
}
384+
if result == nil {
385+
t.Fatal("result is nil")
386+
}
387+
388+
// select four duration
389+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
390+
rows, err = db.QueryContext(ctx, "select text_data, duration_data from "+KeyspaceName+"."+TableName+" where text_data = ?", "four")
391+
if err != nil {
392+
t.Fatal("QueryContext error: ", err)
393+
}
394+
if rows == nil {
395+
t.Fatal("rows is nil")
396+
}
397+
if !rows.Next() {
398+
t.Fatal("no Next rows")
399+
}
400+
err = rows.Scan(destPointer...)
401+
if err != nil {
402+
t.Fatal("Scan error: ", err)
403+
}
404+
if dest[0] != "four" {
405+
t.Fatalf("text_data - received: %v - expected: %v", dest[0], "four")
406+
}
407+
duration := InterfaceToDuration(dest[1])
408+
if duration != time.Hour+time.Minute+time.Second+time.Millisecond+time.Microsecond+time.Nanosecond {
409+
t.Fatalf("duration_data - received: %v - expected: %v", duration, time.Hour+time.Minute+time.Second+time.Millisecond+time.Microsecond+time.Nanosecond)
410+
}
411+
if rows.Next() {
412+
t.Fatal("has Next rows")
413+
}
414+
err = rows.Close()
415+
if err != nil {
416+
t.Fatal("Close error: ", err)
417+
}
418+
cancel()
419+
err = rows.Err()
420+
if err != nil {
421+
t.Fatal("Err error: ", err)
422+
}
423+
*/
424+
314425
// select errors
315426
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
316427
rows, err = db.QueryContext(ctx, "select int_data from "+KeyspaceName+"."+TableName+" group by int_data")
@@ -364,9 +475,30 @@ func TestSqlSelectLoop(t *testing.T) {
364475
t.Fatal("db is nil")
365476
}
366477

478+
// truncate table
479+
ctx, cancel := context.WithTimeout(context.Background(), TimeoutValid)
480+
result, err := db.ExecContext(ctx, "truncate table "+KeyspaceName+"."+TableName)
481+
if err != nil {
482+
t.Fatal("ExecContext error: ", err)
483+
}
484+
if result == nil {
485+
t.Fatal("result is nil")
486+
}
487+
488+
// insert one
489+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
490+
result, err = db.ExecContext(ctx, "insert into "+KeyspaceName+"."+TableName+" (text_data, int_data) values (?, ?)", "one", 1)
491+
if err != nil {
492+
t.Fatal("ExecContext error: ", err)
493+
}
494+
if result == nil {
495+
t.Fatal("result is nil")
496+
}
497+
cancel()
498+
367499
for i := 0; i < 100; i++ {
368500
// select all
369-
ctx, cancel := context.WithTimeout(context.Background(), TimeoutValid)
501+
ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid)
370502
rows, err := db.QueryContext(ctx, "select text_data, int_data from "+KeyspaceName+"."+TableName+"")
371503
if err != nil {
372504
t.Fatal("QueryContext error: ", err)

cql_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
EnableAuthentication bool
2727
Username string
2828
Password string
29+
TestTimeNow time.Time
2930
)
3031

3132
func TestMain(m *testing.M) {
@@ -66,7 +67,8 @@ func setupForTesting() int {
6667
return 6
6768
}
6869

69-
TableName += time.Now().UTC().Format("20060102150405")
70+
TestTimeNow = time.Now().UTC().Truncate(time.Millisecond)
71+
TableName += TestTimeNow.Format("20060102150405")
7072

7173
return 0
7274
}

utility.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql/driver"
55
"fmt"
66
"reflect"
7+
"time"
78

89
"github.com/gocql/gocql"
910
)
@@ -49,3 +50,20 @@ func interfaceToValue(sourceInterface interface{}) (driver.Value, error) {
4950
}
5051
return driver.Value(source.Elem().Interface()), nil
5152
}
53+
54+
// DurationToDuration converts gocql.Duration type to time.Duration.
55+
// Does not check for overflow
56+
func DurationToDuration(cqlDuration gocql.Duration) time.Duration {
57+
return (2629800000000000 * time.Duration(cqlDuration.Months)) + (86400000000000 * time.Duration(cqlDuration.Days)) + time.Duration(cqlDuration.Nanoseconds)
58+
}
59+
60+
// InterfaceToDuration converts an interface of gocql.Duration type to time.Duration.
61+
// Does not check for overflow.
62+
// Returns 0 if interface is not gocql.Duration
63+
func InterfaceToDuration(aInterface interface{}) time.Duration {
64+
cqlDuration, ok := aInterface.(gocql.Duration)
65+
if !ok {
66+
return time.Duration(0)
67+
}
68+
return (2629800000000000 * time.Duration(cqlDuration.Months)) + (86400000000000 * time.Duration(cqlDuration.Days)) + time.Duration(cqlDuration.Nanoseconds)
69+
}

0 commit comments

Comments
 (0)