2
Copyright 2013-2014 Canonical Ltd.
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.
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.
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/>.
20
_ "code.google.com/p/gosqlite/sqlite3"
22
. "launchpad.net/gocheck"
25
type sqlmSuite struct{ lmSuite }
27
var _ = Suite(&sqlmSuite{})
29
func (s *sqlmSuite) SetUpSuite(c *C) {
30
s.constructor = func() (LevelMap, error) { return NewSqliteLevelMap(":memory:") }
33
func (s *sqlmSuite) TestNewCanFail(c *C) {
34
m, err := NewSqliteLevelMap("/does/not/exist")
39
func (s *sqlmSuite) TestSetCanFail(c *C) {
41
filename := dir + "test.db"
42
db, err := sql.Open("sqlite3", filename)
44
// create the wrong kind of table
45
_, err = db.Exec("CREATE TABLE level_map (foo)")
48
m, err := NewSqliteLevelMap(filename)
51
err = m.Set("foo", 42)
52
c.Check(err, ErrorMatches, "cannot set .*")
55
func (s *sqlmSuite) TestGetAllCanFail(c *C) {
57
filename := dir + "test.db"
58
db, err := sql.Open("sqlite3", filename)
60
// create the wrong kind of table
61
_, err = db.Exec("CREATE TABLE level_map AS SELECT 'what'")
64
m, err := NewSqliteLevelMap(filename)
67
all, err := m.GetAll()
69
c.Check(err, ErrorMatches, "cannot read level .*")
72
func (s *sqlmSuite) TestGetAllCanFailDifferently(c *C) {
74
filename := dir + "test.db"
75
db, err := sql.Open("sqlite3", filename)
77
// create a view with the name the table will have
78
_, err = db.Exec("CREATE TABLE foo (foo)")
80
_, err = db.Exec("CREATE VIEW level_map AS SELECT * FROM foo")
83
_, err = db.Exec("DROP TABLE foo")
86
m, err := NewSqliteLevelMap(filename)
89
all, err := m.GetAll()
91
c.Check(err, ErrorMatches, "cannot retrieve levels .*")