[System] UriKind.RelativeOrAbsolute workaround.
[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 #if NET_4_0
28
29 using System;
30 using System.IO;
31 using System.Linq;
32 using System.Data.Common;
33 using System.Collections.Generic;
34
35 using WebMatrix.Data;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.WebMatrix.Data
40 {
41         [TestFixtureAttribute]
42         public class DatabaseTests
43         {
44                 Database database;
45                 string dbPath;
46
47                 [SetUp]
48                 public void Setup ()
49                 {
50                         dbPath = Path.Combine ("Test", "testsqlite.db");
51                         database = Database.OpenConnectionString ("Data Source="+dbPath+";Version=3;", "Mono.Data.Sqlite");
52                 }
53
54                 [Test]
55                 public void QuerySingleTest ()
56                 {
57                         var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
58
59                         Assert.IsNotNull (result);
60                         Assert.AreEqual ("Grendel", result.Text);
61                         Assert.AreEqual (5, result.Priority);
62                 }
63
64                 [Test]
65                 public void SimpleQueryTest ()
66                 {
67                         var result = database.Query ("select * from memos");
68
69                         Assert.IsNotNull (result);
70                         Assert.AreEqual (5, result.Count ());
71
72                         var col1 = new string[] { "Webmatrix", "Grendel", "Garuma", "jpobst", "Gonzalo" };
73                         var col2 = new object[] { 10, 5, -1, 6, 4 };
74                         int index = 0;
75
76                         foreach (var row in result) {
77                                 Assert.AreEqual (col1[index], row.Text);
78                                 Assert.AreEqual (col2[index], row.Priority);
79                                 index++;
80                         }
81                 }
82
83                 [Test]
84                 public void InsertTest ()
85                 {
86                         string newPath = dbPath + ".tmp";
87                         File.Copy (dbPath, newPath, true);
88
89                         database = Database.OpenConnectionString ("Data Source="+newPath+";Version=3;", "Mono.Data.Sqlite");
90                         database.Execute ("insert into memos values ('foo', @0);", 42);
91
92                         Assert.AreEqual (42, database.QueryValue ("select Priority from memos where Text='foo'"));
93                         Assert.AreEqual (6, database.GetLastInsertId ());
94
95                         File.Delete (newPath);
96                 }
97
98                 [Test]
99                 public void QueryValueTest ()
100                 {
101                         var res = database.QueryValue ("select Priority from memos where Text='Webmatrix'");
102                         Assert.AreEqual (10, res);
103                 }
104
105                 [Test]
106                 public void ConnectionOpenedTest ()
107                 {
108                         bool opened = false;
109                         Database.ConnectionOpened += (sender, e) => opened = sender == database;
110
111                         var result = database.QuerySingle ("select * from memos where Text=@0 limit 1", "Grendel");
112
113                         Assert.IsTrue (opened);
114                 }
115         }
116 }
117 #endif