Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 5 | package auditor |
| 6 | |
| 7 | import ( |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 8 | "reflect" |
| 9 | "testing" |
| 10 | "time" |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 11 | |
| 12 | sqlmock "github.com/DATA-DOG/go-sqlmock" |
| 13 | |
| 14 | _ "v.io/x/ref/runtime/factories/fake" |
| 15 | "v.io/x/ref/test" |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | func TestSQLDatabaseQuery(t *testing.T) { |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 19 | ctx, shutdown := test.V23InitWithParams(test.InitParams{}) |
| 20 | defer shutdown() |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 21 | db, err := sqlmock.New() |
| 22 | if err != nil { |
| 23 | t.Fatalf("failed to create new mock database stub: %v", err) |
| 24 | } |
| 25 | columns := []string{"Email", "Caveat", "Timestamp", "Blessings"} |
| 26 | sqlmock.ExpectExec("CREATE TABLE IF NOT EXISTS tableName (.+)"). |
| 27 | WillReturnResult(sqlmock.NewResult(0, 1)) |
Cosmos Nicolaou | 1c33b7d | 2015-06-24 15:15:54 -0700 | [diff] [blame] | 28 | d, err := newSQLDatabase(ctx, db, "tableName") |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 29 | if err != nil { |
| 30 | t.Fatalf("failed to create SQLDatabase: %v", err) |
| 31 | } |
| 32 | |
| 33 | entry := databaseEntry{ |
| 34 | email: "email", |
| 35 | caveats: []byte("caveats"), |
| 36 | timestamp: time.Now(), |
| 37 | blessings: []byte("blessings"), |
| 38 | } |
| 39 | sqlmock.ExpectExec("INSERT INTO tableName (.+) VALUES (.+)"). |
| 40 | WithArgs(entry.email, entry.caveats, entry.timestamp, entry.blessings). |
| 41 | WillReturnResult(sqlmock.NewResult(0, 1)) // no insert id, 1 affected row |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 42 | if err := d.Insert(ctx, entry); err != nil { |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 43 | t.Errorf("failed to insert into SQLDatabase: %v", err) |
| 44 | } |
| 45 | |
| 46 | // Test the querying. |
| 47 | sqlmock.ExpectQuery("SELECT Email, Caveats, Timestamp, Blessings FROM tableName"). |
| 48 | WithArgs(entry.email). |
| 49 | WillReturnRows(sqlmock.NewRows(columns).AddRow(entry.email, entry.caveats, entry.timestamp, entry.blessings)) |
Cosmos Nicolaou | d922992 | 2015-06-24 14:12:24 -0700 | [diff] [blame] | 50 | ch := d.Query(ctx, entry.email) |
Suharsh Sivakumar | 38a1a7b | 2014-11-17 15:14:38 -0800 | [diff] [blame] | 51 | if res := <-ch; !reflect.DeepEqual(res, entry) { |
| 52 | t.Errorf("got %#v, expected %#v", res, entry) |
| 53 | } |
| 54 | |
| 55 | var extra bool |
| 56 | for _ = range ch { |
| 57 | // Drain the channel to prevent the producer goroutines from being leaked. |
| 58 | extra = true |
| 59 | } |
| 60 | if extra { |
| 61 | t.Errorf("Got more entries that expected") |
| 62 | } |
| 63 | } |