~ci-train-bot/ubuntu-push/ubuntu-push-ubuntu-yakkety-2021

« back to all changes in this revision

Viewing changes to client/session/levelmap/sqlevelmap_test.go

  • Committer: john.lenton at canonical
  • Date: 2014-03-20 12:15:47 UTC
  • mfrom: (81 ubuntu-push)
  • mto: This revision was merged to the branch mainline in revision 85.
  • Revision ID: john.lenton@canonical.com-20140320121547-i544is8492ulfykx
merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright 2013-2014 Canonical Ltd.
 
3
 
 
4
 This program is free software: you can redistribute it and/or modify it
 
5
 under the terms of the GNU General Public License version 3, as published
 
6
 by the Free Software Foundation.
 
7
 
 
8
 This program is distributed in the hope that it will be useful, but
 
9
 WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 PURPOSE.  See the GNU General Public License for more details.
 
12
 
 
13
 You should have received a copy of the GNU General Public License along
 
14
 with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
*/
 
16
 
 
17
package levelmap
 
18
 
 
19
import (
 
20
        _ "code.google.com/p/gosqlite/sqlite3"
 
21
        "database/sql"
 
22
        . "launchpad.net/gocheck"
 
23
)
 
24
 
 
25
type sqlmSuite struct{ lmSuite }
 
26
 
 
27
var _ = Suite(&sqlmSuite{})
 
28
 
 
29
func (s *sqlmSuite) SetUpSuite(c *C) {
 
30
        s.constructor = func() (LevelMap, error) { return NewSqliteLevelMap(":memory:") }
 
31
}
 
32
 
 
33
func (s *sqlmSuite) TestNewCanFail(c *C) {
 
34
        m, err := NewSqliteLevelMap("/does/not/exist")
 
35
        c.Assert(m, IsNil)
 
36
        c.Check(err, NotNil)
 
37
}
 
38
 
 
39
func (s *sqlmSuite) TestSetCanFail(c *C) {
 
40
        dir := c.MkDir()
 
41
        filename := dir + "test.db"
 
42
        db, err := sql.Open("sqlite3", filename)
 
43
        c.Assert(err, IsNil)
 
44
        // create the wrong kind of table
 
45
        _, err = db.Exec("CREATE TABLE level_map (foo)")
 
46
        c.Assert(err, IsNil)
 
47
        // <evil laughter>
 
48
        m, err := NewSqliteLevelMap(filename)
 
49
        c.Check(err, IsNil)
 
50
        c.Assert(m, NotNil)
 
51
        err = m.Set("foo", 42)
 
52
        c.Check(err, ErrorMatches, "cannot set .*")
 
53
}
 
54
 
 
55
func (s *sqlmSuite) TestGetAllCanFail(c *C) {
 
56
        dir := c.MkDir()
 
57
        filename := dir + "test.db"
 
58
        db, err := sql.Open("sqlite3", filename)
 
59
        c.Assert(err, IsNil)
 
60
        // create the wrong kind of table
 
61
        _, err = db.Exec("CREATE TABLE level_map AS SELECT 'what'")
 
62
        c.Assert(err, IsNil)
 
63
        // <evil laughter>
 
64
        m, err := NewSqliteLevelMap(filename)
 
65
        c.Check(err, IsNil)
 
66
        c.Assert(m, NotNil)
 
67
        all, err := m.GetAll()
 
68
        c.Check(all, IsNil)
 
69
        c.Check(err, ErrorMatches, "cannot read level .*")
 
70
}
 
71
 
 
72
func (s *sqlmSuite) TestGetAllCanFailDifferently(c *C) {
 
73
        dir := c.MkDir()
 
74
        filename := dir + "test.db"
 
75
        db, err := sql.Open("sqlite3", filename)
 
76
        c.Assert(err, IsNil)
 
77
        // create a view with the name the table will have
 
78
        _, err = db.Exec("CREATE TABLE foo (foo)")
 
79
        c.Assert(err, IsNil)
 
80
        _, err = db.Exec("CREATE VIEW level_map AS SELECT * FROM foo")
 
81
        c.Assert(err, IsNil)
 
82
        // break the view
 
83
        _, err = db.Exec("DROP TABLE foo")
 
84
        c.Assert(err, IsNil)
 
85
        // <evil laughter>
 
86
        m, err := NewSqliteLevelMap(filename)
 
87
        c.Check(err, IsNil)
 
88
        c.Assert(m, NotNil)
 
89
        all, err := m.GetAll()
 
90
        c.Check(all, IsNil)
 
91
        c.Check(err, ErrorMatches, "cannot retrieve levels .*")
 
92
}