|
4 | 4 | "context" |
5 | 5 | "database/sql" |
6 | 6 | "testing" |
| 7 | + // "time" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | func TestSqlOpen(t *testing.T) { |
@@ -75,7 +76,8 @@ func TestSqlCreate(t *testing.T) { |
75 | 76 |
|
76 | 77 | // create table |
77 | 78 | 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)") |
79 | 81 | cancel() |
80 | 82 | if err != nil { |
81 | 83 | t.Fatal("ExecContext error: ", err) |
@@ -108,9 +110,19 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) { |
108 | 110 | t.Fatal("db is nil") |
109 | 111 | } |
110 | 112 |
|
111 | | - // insert one |
| 113 | + // truncate table |
112 | 114 | 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) |
114 | 126 | if err != nil { |
115 | 127 | t.Fatal("ExecContext error: ", err) |
116 | 128 | } |
@@ -311,6 +323,105 @@ func TestSqlInsertUpdateSelectDelete(t *testing.T) { |
311 | 323 | t.Fatal("result is nil") |
312 | 324 | } |
313 | 325 |
|
| 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 | + |
314 | 425 | // select errors |
315 | 426 | ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid) |
316 | 427 | rows, err = db.QueryContext(ctx, "select int_data from "+KeyspaceName+"."+TableName+" group by int_data") |
@@ -364,9 +475,30 @@ func TestSqlSelectLoop(t *testing.T) { |
364 | 475 | t.Fatal("db is nil") |
365 | 476 | } |
366 | 477 |
|
| 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 | + |
367 | 499 | for i := 0; i < 100; i++ { |
368 | 500 | // select all |
369 | | - ctx, cancel := context.WithTimeout(context.Background(), TimeoutValid) |
| 501 | + ctx, cancel = context.WithTimeout(context.Background(), TimeoutValid) |
370 | 502 | rows, err := db.QueryContext(ctx, "select text_data, int_data from "+KeyspaceName+"."+TableName+"") |
371 | 503 | if err != nil { |
372 | 504 | t.Fatal("QueryContext error: ", err) |
|
0 commit comments