5 // Jérémie "garuma" Laval <jeremie.laval@gmail.com>
7 // Copyright (c) 2011 Novell
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31 using System.Data.Common;
32 using System.Collections.Generic;
36 using NUnit.Framework;
38 namespace MonoTests.WebMatrix.Data
40 [TestFixtureAttribute]
41 public class DatabaseTests
49 dbPath = Path.Combine ("Test", "testsqlite.db");
50 database = Database.OpenConnectionString ("Data Source="+dbPath+";Version=3;", "Mono.Data.Sqlite");
54 public void QuerySingleTest ()
56 var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
58 Assert.IsNotNull (result);
59 Assert.AreEqual ("Grendel", result.Text);
60 Assert.AreEqual (5, result.Priority);
64 public void SimpleQueryTest ()
66 var result = database.Query ("select * from memos");
68 Assert.IsNotNull (result);
69 Assert.AreEqual (5, result.Count ());
71 var col1 = new string[] { "Webmatrix", "Grendel", "Garuma", "jpobst", "Gonzalo" };
72 var col2 = new object[] { 10, 5, -1, 6, 4 };
75 foreach (var row in result) {
76 Assert.AreEqual (col1[index], row.Text);
77 Assert.AreEqual (col2[index], row.Priority);
83 public void InsertTest ()
85 string newPath = dbPath + ".tmp";
86 File.Copy (dbPath, newPath, true);
88 database = Database.OpenConnectionString ("Data Source="+newPath+";Version=3;", "Mono.Data.Sqlite");
89 database.Execute ("insert into memos values ('foo', @0);", 42);
91 Assert.AreEqual (42, database.QueryValue ("select Priority from memos where Text='foo'"));
92 Assert.AreEqual (6, database.GetLastInsertId ());
94 File.Delete (newPath);
98 public void QueryValueTest ()
100 var res = database.QueryValue ("select Priority from memos where Text='Webmatrix'");
101 Assert.AreEqual (10, res);
105 public void ConnectionOpenedTest ()
108 Database.ConnectionOpened += (sender, e) => opened = sender == database;
110 var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
112 Assert.IsTrue (opened);