DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlStringTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 using System;
17 using System.Text;
18 
19 using NUnit.Framework;
20 
21 namespace Deveel.Data.Sql.Objects {
22  [TestFixture]
23  [Category("SQL Objects")]
24  public class SqlStringTests {
25  [Test]
26  [Category("Strings")]
27  public void String_Create() {
28  const string s = "Test string UTF-16 LE";
29  var sqlString = new SqlString(s);
30  Assert.IsNotNull(sqlString);
31  Assert.AreEqual(s.Length, sqlString.Length);
32  Assert.AreEqual(s, sqlString);
33  }
34 
35  [Test]
36  [Category("Strings")]
37  public void String_Compare_Equal() {
38  const string s = "Test string in UTF-16 LE";
39  var sqlString1 = new SqlString(s);
40  var sqlString2 = new SqlString(s);
41  Assert.AreEqual(0, sqlString1.CompareTo(sqlString2));
42  }
43 
44  [Test]
45  [Category("Strings")]
46  public void String_Equals() {
47  const string s = "Test string in UTF-16 LE";
48  var sqlString1 = new SqlString(s);
49  var sqlString2 = new SqlString(s);
50  Assert.IsTrue(sqlString1.Equals(sqlString2));
51  }
52 
53  [Test]
54  [Category("Strings")]
55  public void String_Concat() {
56  const string s1 = "First string comes before the ";
57  const string s2 = "Second string that comes after";
58  var sqlString1 = new SqlString(s1);
59  var sqlString2 = new SqlString(s2);
60 
61  var sqlString3 = new SqlString();
62  Assert.DoesNotThrow(() => sqlString3 = sqlString1.Concat(sqlString2));
63  Assert.AreEqual("First string comes before the Second string that comes after", sqlString3.ToString(Encoding.UTF8));
64  }
65 
66  [Test]
67  public void String_EqualsToNull() {
68  var s = SqlString.Null;
69  Assert.IsTrue(s.IsNull);
70  Assert.AreEqual(SqlNull.Value, s);
71  Assert.AreEqual(s, SqlNull.Value);
72  }
73 
74  [Test]
75  [Category("Conversion")]
76  [Category("Date Time")]
77  public void String_Convert_TimeStamp() {
78  const string s = "2011-01-23T23:44:21.525 +01:00";
79  var sqlString = new SqlString(s);
80 
81  var timeStamp = new SqlDateTime();
82  Assert.DoesNotThrow(() => timeStamp = (SqlDateTime) Convert.ChangeType(sqlString, typeof(SqlDateTime)));
83  Assert.IsFalse(timeStamp.IsNull);
84  Assert.AreEqual(2011, timeStamp.Year);
85  Assert.AreEqual(01, timeStamp.Month);
86  Assert.AreEqual(23, timeStamp.Day);
87  Assert.AreEqual(23, timeStamp.Hour);
88  Assert.AreEqual(44, timeStamp.Minute);
89  Assert.AreEqual(525, timeStamp.Millisecond);
90  Assert.AreEqual(1, timeStamp.Offset.Hours);
91  Assert.AreEqual(0, timeStamp.Offset.Minutes);
92  }
93 
94  [Test]
95  [Category("Conversion")]
96  [Category("Date Time")]
97  public void String_Convert_Time() {
98  const string s = "23:44:21.525";
99  var sqlString = new SqlString(s);
100 
101  var time = new SqlDateTime();
102  Assert.DoesNotThrow(() => time = (SqlDateTime) Convert.ChangeType(sqlString, typeof(SqlDateTime)));
103  Assert.IsFalse(time.IsNull);
104  Assert.AreEqual(1, time.Year);
105  Assert.AreEqual(1, time.Month);
106  Assert.AreEqual(1, time.Day);
107  Assert.AreEqual(23, time.Hour);
108  Assert.AreEqual(44, time.Minute);
109  Assert.AreEqual(21, time.Second);
110  Assert.AreEqual(525, time.Millisecond);
111  }
112 
113  [Test]
114  [Category("Conversion")]
115  [Category("Date Time")]
116  public void String_Convert_Date() {
117  const string s = "2011-01-23";
118  var sqlString = new SqlString(s);
119 
120  var date = new SqlDateTime();
121  Assert.DoesNotThrow(() => date = (SqlDateTime) Convert.ChangeType(sqlString, typeof(SqlDateTime)));
122  Assert.IsFalse(date.IsNull);
123  Assert.AreEqual(2011, date.Year);
124  Assert.AreEqual(01, date.Month);
125  Assert.AreEqual(23, date.Day);
126  Assert.AreEqual(0, date.Hour);
127  Assert.AreEqual(0, date.Minute);
128  Assert.AreEqual(0, date.Millisecond);
129  Assert.AreEqual(0, date.Offset.Hours);
130  Assert.AreEqual(0, date.Offset.Minutes);
131  }
132 
133  [Test]
134  [Category("Conversion")]
135  [Category("Numbers")]
136  public void String_Convert_BigNumber() {
137  const string s = "7689994.0000033992988477226661525553666370058812345883288477383";
138  var sqlString = new SqlString(s);
139 
140  var number = new SqlNumber();
141  Assert.DoesNotThrow(() => number = (SqlNumber)Convert.ChangeType(sqlString, typeof(SqlNumber)));
142  Assert.IsFalse(number.IsNull);
143  Assert.IsFalse(number.CanBeInt32);
144  Assert.IsFalse(number.CanBeInt64);
145  Assert.AreEqual(NumericState.None, number.State);
146  }
147 
148  [Test]
149  [Category("Conversion")]
150  [Category("Booleans")]
152  const string s = "true";
153  var sqlString = new SqlString(s);
154 
155  var b = new SqlBoolean();
156  Assert.DoesNotThrow(() => b = (SqlBoolean)Convert.ChangeType(sqlString, typeof(SqlBoolean)));
157  Assert.IsFalse(b.IsNull);
158  Assert.AreEqual(SqlBoolean.True, b);
159  }
160 
161  [Test]
162  [Category("Conversion")]
163  [Category("Booleans")]
165  const string s = "false";
166  var sqlString = new SqlString(s);
167 
168  var b = new SqlBoolean();
169  Assert.DoesNotThrow(() => b = (SqlBoolean)Convert.ChangeType(sqlString, typeof(SqlBoolean)));
170  Assert.IsFalse(b.IsNull);
171  Assert.AreEqual(SqlBoolean.False, b);
172  }
173 
174  [Test]
175  [Category("Conversion")]
176  [Category("Booleans")]
178  const string s = "";
179  var sqlString = new SqlString(s);
180 
181  var b = new SqlBoolean();
182  Assert.DoesNotThrow(() => b = (SqlBoolean)Convert.ChangeType(sqlString, typeof(SqlBoolean)));
183  Assert.IsTrue(b.IsNull);
184  Assert.AreEqual(SqlBoolean.Null, b);
185  }
186  }
187 }
NumericState
Lists all the possible special states of a number.
Definition: NumericState.cs:21
static readonly SqlNull Value
Definition: SqlNull.cs:24
Deveel.Data.Sql.Objects.SqlBoolean SqlBoolean
Definition: DataObject.cs:26
Deveel.Data.Sql.Objects.SqlString SqlString
Definition: DataObject.cs:27