OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package crimsondb | 5 package crimsondb |
6 | 6 |
7 import ( | 7 import ( |
8 "database/sql/driver" | 8 "database/sql/driver" |
9 "testing" | 9 "testing" |
10 | 10 |
(...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
385 []driver.Value{ | 385 []driver.Value{ |
386 "site0", | 386 "site0", |
387 "0x01020314", | 387 "0x01020314", |
388 "0x01020304"}) | 388 "0x01020304"}) |
389 | 389 |
390 query, err = conn.PopOldestQuery() | 390 query, err = conn.PopOldestQuery() |
391 So(err, ShouldNotBeNil) | 391 So(err, ShouldNotBeNil) |
392 }) | 392 }) |
393 }) | 393 }) |
394 } | 394 } |
| 395 |
| 396 func TestInsertHost(t *testing.T) { |
| 397 t.Parallel() |
| 398 Convey("InsertHost", t, func() { |
| 399 ctx := context.Background() |
| 400 db, conn := sqlmock.NewMockDB() |
| 401 ctx = UseDB(ctx, db) |
| 402 |
| 403 Convey("with one host, generates the correct queries.", func() { |
| 404 err := InsertHost(ctx, |
| 405 &crimson.HostList{ |
| 406 Hosts: []*crimson.Host{{ |
| 407 Site: "site0", |
| 408 Hostname: "hostname0", |
| 409 MacAddr: "01:23:45:67:89:ab", |
| 410 Ip: "1.2.3.20", |
| 411 BootClass: "linux"}}}) |
| 412 |
| 413 query, err := conn.PopOldestQuery() |
| 414 So(err, ShouldBeNil) |
| 415 expected := ("INSERT INTO host (site, hostname, mac_addr
, ip, boot_class) " + |
| 416 "VALUES (?, ?, ?, ?, ?)") |
| 417 So(query.Query, ShouldEqual, expected) |
| 418 So(query.Args, ShouldResemble, |
| 419 []driver.Value{ |
| 420 "site0", |
| 421 "hostname0", |
| 422 "0x0123456789ab", |
| 423 "0x01020314", |
| 424 "linux"}) |
| 425 }) |
| 426 }) |
| 427 } |
| 428 |
| 429 func TestSelectHost(t *testing.T) { |
| 430 t.Parallel() |
| 431 Convey("SelectHost", t, func() { |
| 432 ctx := context.Background() |
| 433 db, conn := sqlmock.NewMockDB() |
| 434 ctx = UseDB(ctx, db) |
| 435 |
| 436 Convey("given a site generates a correct query", func() { |
| 437 _, err := SelectHost(ctx, |
| 438 &crimson.HostQuery{ |
| 439 Site: "site0", |
| 440 }) |
| 441 |
| 442 query, err := conn.PopOldestQuery() |
| 443 So(err, ShouldBeNil) |
| 444 expected := ("SELECT site, hostname, mac_addr, ip, boot_
class " + |
| 445 "FROM host\nWHERE site=?") |
| 446 So(query.Query, ShouldEqual, expected) |
| 447 So(query.Args, ShouldResemble, []driver.Value{"site0"}) |
| 448 }) |
| 449 |
| 450 Convey("given a site and a boot class generates a correct query"
, func() { |
| 451 _, err := SelectHost(ctx, |
| 452 &crimson.HostQuery{ |
| 453 Site: "site0", |
| 454 BootClass: "cls", |
| 455 }) |
| 456 |
| 457 query, err := conn.PopOldestQuery() |
| 458 So(err, ShouldBeNil) |
| 459 expected := ("SELECT site, hostname, mac_addr, ip, boot_
class " + |
| 460 "FROM host\nWHERE site=?\nAND boot_class=?") |
| 461 So(query.Query, ShouldEqual, expected) |
| 462 So(query.Args, ShouldResemble, []driver.Value{"site0", "
cls"}) |
| 463 }) |
| 464 |
| 465 Convey("with all filters generates a correct query", func() { |
| 466 _, err := SelectHost(ctx, |
| 467 &crimson.HostQuery{ |
| 468 Site: "site0", |
| 469 Hostname: "hostname0", |
| 470 MacAddr: "01:23:45:67:89:ab", |
| 471 Ip: "1.23.45.67", |
| 472 BootClass: "cls", |
| 473 }) |
| 474 |
| 475 query, err := conn.PopOldestQuery() |
| 476 So(err, ShouldBeNil) |
| 477 expected := ("SELECT site, hostname, mac_addr, ip, boot_
class " + |
| 478 "FROM host\nWHERE site=?\nAND hostname=?\nAND ma
c_addr=?\nAND ip=?" + |
| 479 "\nAND boot_class=?") |
| 480 So(query.Query, ShouldEqual, expected) |
| 481 So(query.Args, ShouldResemble, []driver.Value{"site0", "
hostname0", |
| 482 "0x0123456789ab", "0x01172d43", "cls"}) |
| 483 }) |
| 484 |
| 485 Convey("given a boot class and a limit generates a correct query
", func() { |
| 486 _, err := SelectHost(ctx, |
| 487 &crimson.HostQuery{ |
| 488 Limit: 10, |
| 489 BootClass: "cls", |
| 490 }) |
| 491 |
| 492 query, err := conn.PopOldestQuery() |
| 493 So(err, ShouldBeNil) |
| 494 expected := ("SELECT site, hostname, mac_addr, ip, boot_
class " + |
| 495 "FROM host\nWHERE boot_class=?\nLIMIT ?") |
| 496 So(query.Query, ShouldEqual, expected) |
| 497 So(query.Args, ShouldResemble, []driver.Value{"cls", int
64(10)}) |
| 498 }) |
| 499 |
| 500 Convey("given a limit generates a correct query", func() { |
| 501 _, err := SelectHost(ctx, |
| 502 &crimson.HostQuery{ |
| 503 Limit: 10}) |
| 504 |
| 505 query, err := conn.PopOldestQuery() |
| 506 So(err, ShouldBeNil) |
| 507 expected := ("SELECT site, hostname, mac_addr, ip, boot_
class " + |
| 508 "FROM host\nLIMIT ?") |
| 509 So(query.Query, ShouldEqual, expected) |
| 510 So(query.Args, ShouldResemble, []driver.Value{int64(10)}
) |
| 511 }) |
| 512 |
| 513 Convey("parses resulting row properly", func() { |
| 514 So(conn.PushRows([][]driver.Value{ |
| 515 {"site0", "hostname0", "0x0123456789ab", "0x0123
4567", "linux"}}), |
| 516 ShouldBeNil) |
| 517 hostList, err := SelectHost(ctx, |
| 518 &crimson.HostQuery{ |
| 519 Limit: 10}) |
| 520 |
| 521 query, err := conn.PopOldestQuery() |
| 522 So(err, ShouldBeNil) |
| 523 expected := ("SELECT site, hostname, mac_addr, ip, boot_
class " + |
| 524 "FROM host\nLIMIT ?") |
| 525 So(query.Query, ShouldEqual, expected) |
| 526 So(query.Args, ShouldResemble, []driver.Value{int64(10)}
) |
| 527 |
| 528 So(hostList.Hosts[0], ShouldResemble, |
| 529 &crimson.Host{ |
| 530 Site: "site0", |
| 531 Hostname: "hostname0", |
| 532 MacAddr: "01:23:45:67:89:ab", |
| 533 Ip: "1.35.69.103", |
| 534 BootClass: "linux"}) |
| 535 }) |
| 536 Convey("parses resulting row with NULL properly", func() { |
| 537 So(conn.PushRows([][]driver.Value{ |
| 538 {"site0", "hostname0", "0x0123456789ab", "0x0123
4567", nil}}), |
| 539 ShouldBeNil) |
| 540 hostList, err := SelectHost(ctx, |
| 541 &crimson.HostQuery{ |
| 542 Limit: 10}) |
| 543 |
| 544 query, err := conn.PopOldestQuery() |
| 545 So(err, ShouldBeNil) |
| 546 expected := ("SELECT site, hostname, mac_addr, ip, boot_
class " + |
| 547 "FROM host\nLIMIT ?") |
| 548 So(query.Query, ShouldEqual, expected) |
| 549 So(query.Args, ShouldResemble, []driver.Value{int64(10)}
) |
| 550 |
| 551 So(hostList.Hosts[0], ShouldResemble, |
| 552 &crimson.Host{ |
| 553 Site: "site0", |
| 554 Hostname: "hostname0", |
| 555 MacAddr: "01:23:45:67:89:ab", |
| 556 Ip: "1.35.69.103", |
| 557 BootClass: ""}) |
| 558 }) |
| 559 }) |
| 560 } |
OLD | NEW |