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
32 using System.Data.Common;
33 using System.Collections.Generic;
37 using NUnit.Framework;
39 namespace MonoTests.WebMatrix.Data
41 [TestFixtureAttribute]
42 public class DatabaseTests
50 dbPath = Path.Combine ("Test", "testsqlite.db");
51 database = Database.OpenConnectionString ("Data Source="+dbPath+";Version=3;", "Mono.Data.Sqlite");
55 public void QuerySingleTest ()
57 var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
59 Assert.IsNotNull (result);
60 Assert.AreEqual ("Grendel", result.Text);
61 Assert.AreEqual (5, result.Priority);
65 public void SimpleQueryTest ()
67 var result = database.Query ("select * from memos");
69 Assert.IsNotNull (result);
70 Assert.AreEqual (5, result.Count ());
72 var col1 = new string[] { "Webmatrix", "Grendel", "Garuma", "jpobst", "Gonzalo" };
73 var col2 = new object[] { 10, 5, -1, 6, 4 };
76 foreach (var row in result) {
77 Assert.AreEqual (col1[index], row.Text);
78 Assert.AreEqual (col2[index], row.Priority);
84 public void InsertTest ()
86 string newPath = dbPath + ".tmp";
87 File.Copy (dbPath, newPath, true);
89 database = Database.OpenConnectionString ("Data Source="+newPath+";Version=3;", "Mono.Data.Sqlite");
90 database.Execute ("insert into memos values ('foo', @0);", 42);
92 Assert.AreEqual (42, database.QueryValue ("select Priority from memos where Text='foo'"));
93 Assert.AreEqual (6, database.GetLastInsertId ());
95 File.Delete (newPath);
99 public void QueryValueTest ()
101 var res = database.QueryValue ("select Priority from memos where Text='Webmatrix'");
102 Assert.AreEqual (10, res);
106 public void ConnectionOpenedTest ()
109 Database.ConnectionOpened += (sender, e) => opened = sender == database;
111 var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
113 Assert.IsTrue (opened);