Merge pull request #1936 from esdrubal/DotNetRelativeOrAbsolute
[mono.git] / mcs / class / WebMatrix.Data / Test / WebMatrix.Data / DatabaseTests.cs
1 // 
2 // DatabaseTests.cs
3 //  
4 // Author:
5 //       Jérémie "garuma" Laval <jeremie.laval@gmail.com>
6 // 
7 // Copyright (c) 2011 Novell
8 // 
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:
15 // 
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 // 
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
25 // THE SOFTWARE.
26
27
28 using System;
29 using System.IO;
30 using System.Linq;
31 using System.Data.Common;
32 using System.Collections.Generic;
33
34 using WebMatrix.Data;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.WebMatrix.Data
39 {
40         [TestFixtureAttribute]
41         public class DatabaseTests
42         {
43                 Database database;
44                 string dbPath;
45
46                 [SetUp]
47                 public void Setup ()
48                 {
49                         dbPath = Path.Combine ("Test", "testsqlite.db");
50                         database = Database.OpenConnectionString ("Data Source="+dbPath+";Version=3;", "Mono.Data.Sqlite");
51                 }
52
53                 [Test]
54                 public void QuerySingleTest ()
55                 {
56                         var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
57
58                         Assert.IsNotNull (result);
59                         Assert.AreEqual ("Grendel", result.Text);
60                         Assert.AreEqual (5, result.Priority);
61                 }
62
63                 [Test]
64                 public void SimpleQueryTest ()
65                 {
66                         var result = database.Query ("select * from memos");
67
68                         Assert.IsNotNull (result);
69                         Assert.AreEqual (5, result.Count ());
70
71                         var col1 = new string[] { "Webmatrix", "Grendel", "Garuma", "jpobst", "Gonzalo" };
72                         var col2 = new object[] { 10, 5, -1, 6, 4 };
73                         int index = 0;
74
75                         foreach (var row in result) {
76                                 Assert.AreEqual (col1[index], row.Text);
77                                 Assert.AreEqual (col2[index], row.Priority);
78                                 index++;
79                         }
80                 }
81
82                 [Test]
83                 public void InsertTest ()
84                 {
85                         string newPath = dbPath + ".tmp";
86                         File.Copy (dbPath, newPath, true);
87
88                         database = Database.OpenConnectionString ("Data Source="+newPath+";Version=3;", "Mono.Data.Sqlite");
89                         database.Execute ("insert into memos values ('foo', @0);", 42);
90
91                         Assert.AreEqual (42, database.QueryValue ("select Priority from memos where Text='foo'"));
92                         Assert.AreEqual (6, database.GetLastInsertId ());
93
94                         File.Delete (newPath);
95                 }
96
97                 [Test]
98                 public void QueryValueTest ()
99                 {
100                         var res = database.QueryValue ("select Priority from memos where Text='Webmatrix'");
101                         Assert.AreEqual (10, res);
102                 }
103
104                 [Test]
105                 public void ConnectionOpenedTest ()
106                 {
107                         bool opened = false;
108                         Database.ConnectionOpened += (sender, e) => opened = sender == database;
109
110                         var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
111
112                         Assert.IsTrue (opened);
113                 }
114         }
115 }