DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SqlCastExpressionTests.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 using System;
16 
17 using Deveel.Data.Sql.Objects;
18 using Deveel.Data.Types;
19 
20 using NUnit.Framework;
21 
22 namespace Deveel.Data.Sql.Expressions {
23  [TestFixture]
24  public class SqlCastExpressionTests {
25  [Test]
26  public void CastStringToInteger() {
28 
29  SqlExpression casted = null;
30  Assert.DoesNotThrow(() => casted = exp.Evaluate());
31  Assert.IsNotNull(casted);
32  Assert.IsInstanceOf<SqlConstantExpression>(casted);
33 
34  var value = ((SqlConstantExpression) casted).Value;
35  Assert.IsNotNull(value.Value);
36  Assert.IsInstanceOf<NumericType>(value.Type);
37  Assert.AreEqual(SqlTypeCode.Integer, value.Type.TypeCode);
38  Assert.AreEqual(new SqlNumber(1234), value.Value);
39  }
40 
41  [Test]
42  public void CastStringToNumber() {
44 
45  SqlExpression casted = null;
46  Assert.DoesNotThrow(() => casted = exp.Evaluate());
47  Assert.IsNotNull(casted);
48  Assert.IsInstanceOf<SqlConstantExpression>(casted);
49 
50  var value = ((SqlConstantExpression)casted).Value;
51  Assert.IsNotNull(value.Value);
52  Assert.IsInstanceOf<NumericType>(value.Type);
53  Assert.AreEqual(SqlTypeCode.Numeric, value.Type.TypeCode);
54  Assert.AreEqual(SqlNumber.Parse("12.3e4"), value.Value);
55  }
56 
57  [Test]
58  public void CastStringToDateTime() {
60 
61  SqlExpression casted = null;
62  Assert.DoesNotThrow(() => casted = exp.Evaluate());
63  Assert.IsNotNull(casted);
64  Assert.IsInstanceOf<SqlConstantExpression>(casted);
65 
66  var value = ((SqlConstantExpression)casted).Value;
67  Assert.IsNotNull(value.Value);
68  Assert.IsInstanceOf<DateType>(value.Type);
69  Assert.AreEqual(SqlTypeCode.DateTime, value.Type.TypeCode);
70  Assert.AreEqual(new SqlDateTime(2015, 09, 01), value.Value);
71  }
72 
73  [Test]
74  public void CastStringToDate() {
76 
77  SqlExpression casted = null;
78  Assert.DoesNotThrow(() => casted = exp.Evaluate());
79  Assert.IsNotNull(casted);
80  Assert.IsInstanceOf<SqlConstantExpression>(casted);
81 
82  var value = ((SqlConstantExpression)casted).Value;
83  Assert.IsNotNull(value.Value);
84  Assert.IsInstanceOf<DateType>(value.Type);
85  Assert.AreEqual(SqlTypeCode.Date, value.Type.TypeCode);
86  Assert.AreEqual(new SqlDateTime(2015, 09, 01), value.Value);
87  }
88 
89  [Test]
90  public void CastStringToTime() {
92 
93  SqlExpression casted = null;
94  Assert.DoesNotThrow(() => casted = exp.Evaluate());
95  Assert.IsNotNull(casted);
96  Assert.IsInstanceOf<SqlConstantExpression>(casted);
97 
98  var value = ((SqlConstantExpression)casted).Value;
99  Assert.IsNotNull(value.Value);
100  Assert.IsInstanceOf<DateType>(value.Type);
101  Assert.AreEqual(SqlTypeCode.Time, value.Type.TypeCode);
102 
103  // we round the expected value to the result offset because of the UTC parsing logic
104  // of the date type: all we care here is the time component
105 
106  var result = ((SqlDateTime)value.Value);
107  var expected = new SqlDateTime(1, 1, 1, 22, 13, 01, 0, result.Offset);
108  Assert.AreEqual(expected, result);
109  }
110 
111 
112  [Test]
113  public void CastStringToBooleanTrue() {
115 
116  SqlExpression casted = null;
117  Assert.DoesNotThrow(() => casted = exp.Evaluate());
118  Assert.IsNotNull(casted);
119  Assert.IsInstanceOf<SqlConstantExpression>(casted);
120 
121  var value = ((SqlConstantExpression)casted).Value;
122  Assert.IsNotNull(value.Value);
123  Assert.IsInstanceOf<BooleanType>(value.Type);
124  Assert.AreEqual(SqlTypeCode.Boolean, value.Type.TypeCode);
125  Assert.AreEqual(SqlBoolean.True, value.Value);
126  }
127 
128  [Test]
129  public void CastStringToBooleanFalse() {
131 
132  SqlExpression casted = null;
133  Assert.DoesNotThrow(() => casted = exp.Evaluate());
134  Assert.IsNotNull(casted);
135  Assert.IsInstanceOf<SqlConstantExpression>(casted);
136 
137  var value = ((SqlConstantExpression)casted).Value;
138  Assert.IsNotNull(value.Value);
139  Assert.IsInstanceOf<BooleanType>(value.Type);
140  Assert.AreEqual(SqlTypeCode.Boolean, value.Type.TypeCode);
141  Assert.AreEqual(SqlBoolean.False, value.Value);
142  }
143 
144  [Test]
145  public void CastBooleanFalseToString() {
147 
148  SqlExpression casted = null;
149  Assert.DoesNotThrow(() => casted = exp.Evaluate());
150  Assert.IsNotNull(casted);
151  Assert.IsInstanceOf<SqlConstantExpression>(casted);
152 
153  var value = ((SqlConstantExpression)casted).Value;
154  Assert.IsNotNull(value.Value);
155  Assert.IsInstanceOf<StringType>(value.Type);
156  Assert.AreEqual(SqlTypeCode.String, value.Type.TypeCode);
157  Assert.AreEqual(new SqlString("False"), value.Value);
158  }
159 
160  [Test]
161  public void CastBooleanTrueToString() {
163 
164  SqlExpression casted = null;
165  Assert.DoesNotThrow(() => casted = exp.Evaluate());
166  Assert.IsNotNull(casted);
167  Assert.IsInstanceOf<SqlConstantExpression>(casted);
168 
169  var value = ((SqlConstantExpression)casted).Value;
170  Assert.IsNotNull(value.Value);
171  Assert.IsInstanceOf<StringType>(value.Type);
172  Assert.AreEqual(SqlTypeCode.String, value.Type.TypeCode);
173  Assert.AreEqual(new SqlString("True"), value.Value);
174  }
175 
176  [Test]
177  public void CastDateToString() {
178  var date = DataObject.Date(new SqlDateTime(2015, 02, 03));
180 
181  SqlExpression casted = null;
182  Assert.DoesNotThrow(() => casted = exp.Evaluate());
183  Assert.IsNotNull(casted);
184  Assert.IsInstanceOf<SqlConstantExpression>(casted);
185 
186  var value = ((SqlConstantExpression)casted).Value;
187  Assert.IsNotNull(value.Value);
188  Assert.IsInstanceOf<StringType>(value.Type);
189  Assert.AreEqual(SqlTypeCode.String, value.Type.TypeCode);
190  Assert.AreEqual(new SqlString("2015-02-03"), value.Value);
191  }
192 
193  [Test]
194  public void CastNumberToString() {
195  Assert.Inconclusive();
196  }
197  }
198 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static DataObject Date(DateTimeOffset value)
Definition: DataObject.cs:600
static BooleanType Boolean()
static DataObject String(string s)
Definition: DataObject.cs:592
static DataObject Boolean(SqlBoolean value)
Definition: DataObject.cs:544
DataObject Value
Gets the constant value of the expression.
virtual SqlExpression Evaluate(EvaluateContext context)
When overridden by a derived class, this method evaluates the expression within the provided context...
static NumericType Numeric()
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
An expression that holds a constant value.
static SqlCastExpression Cast(SqlExpression value, SqlType destType)
Defines the base class for instances that represent SQL expression tree nodes.
static SqlConstantExpression Constant(object value)
static SqlNumber Parse(string s)
Definition: SqlNumber.cs:738
Deveel.Data.Sql.Objects.SqlString SqlString
Definition: DataObject.cs:27