DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
NumericObjectTests.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 
18 using Deveel.Data.Sql.Objects;
19 using Deveel.Data.Types;
20 
21 using NUnit.Framework;
22 
23 namespace Deveel.Data {
24  [TestFixture]
25  [Category("Data Objects")]
26  [Category("Numbers")]
27  public class NumericObjectTests {
28  [Test]
29  public void Integer_Create() {
30  var obj = DataObject.Integer(33);
31  Assert.IsNotNull(obj);
32  Assert.IsInstanceOf<NumericType>(obj.Type);
33  Assert.AreEqual(SqlTypeCode.Integer, obj.Type.TypeCode);
34  Assert.AreEqual(33, obj);
35  }
36 
37  [Test]
38  public void BigInt_Create() {
39  var obj = DataObject.BigInt(8399902L);
40  Assert.IsNotNull(obj);
41  Assert.IsInstanceOf<NumericType>(obj.Type);
42  Assert.AreEqual(SqlTypeCode.BigInt, obj.Type.TypeCode);
43  Assert.AreEqual(8399902L, obj);
44  }
45 
46  [Test]
47  public void Integer_Compare_Equal() {
48  var obj1 = DataObject.Integer(33);
49  var obj2 = DataObject.Integer(33);
50 
51  Assert.IsNotNull(obj1);
52  Assert.IsNotNull(obj2);
53 
54  Assert.IsInstanceOf<NumericType>(obj1.Type);
55  Assert.IsInstanceOf<NumericType>(obj2.Type);
56 
57  Assert.AreEqual(SqlTypeCode.Integer, obj1.Type.TypeCode);
58  Assert.AreEqual(SqlTypeCode.Integer, obj2.Type.TypeCode);
59 
60  Assert.IsTrue(obj1.IsComparableTo(obj2));
61  Assert.AreEqual(0, obj1.CompareTo(obj2));
62  }
63 
64  [Test]
65  public void Integer_Compare_NotEqual() {
66  var obj1 = DataObject.Integer(33);
67  var obj2 = DataObject.Integer(87);
68 
69  Assert.IsNotNull(obj1);
70  Assert.IsNotNull(obj2);
71 
72  Assert.IsInstanceOf<NumericType>(obj1.Type);
73  Assert.IsInstanceOf<NumericType>(obj2.Type);
74 
75  Assert.AreEqual(SqlTypeCode.Integer, obj1.Type.TypeCode);
76  Assert.AreEqual(SqlTypeCode.Integer, obj2.Type.TypeCode);
77 
78  Assert.IsTrue(obj1.IsComparableTo(obj2));
79  Assert.AreEqual(-1, obj1.CompareTo(obj2));
80  }
81 
82  [Test]
83  public void Integer_Convert_ToDouble() {
84  var obj = DataObject.Integer(33);
85  Assert.IsNotNull(obj);
86  Assert.IsInstanceOf<NumericType>(obj.Type);
87  Assert.AreEqual(SqlTypeCode.Integer, obj.Type.TypeCode);
88  Assert.AreEqual(33, obj);
89 
90  DataObject result = null;
91  Assert.DoesNotThrow(() => result = obj.CastTo(PrimitiveTypes.Numeric(SqlTypeCode.Double)));
92  Assert.IsNotNull(result);
93  Assert.IsInstanceOf<NumericType>(result.Type);
94  Assert.AreEqual(SqlTypeCode.Double, result.Type.TypeCode);
95  }
96 
97  [Test]
98  public void Integer_Convert_ToVarChar() {
99  var obj = DataObject.Integer(33);
100  Assert.IsNotNull(obj);
101  Assert.IsInstanceOf<NumericType>(obj.Type);
102  Assert.AreEqual(SqlTypeCode.Integer, obj.Type.TypeCode);
103  Assert.AreEqual(33, obj);
104 
105  DataObject result = null;
106  Assert.DoesNotThrow(() => result = obj.AsVarChar());
107  Assert.IsNotNull(result);
108  Assert.IsInstanceOf<StringType>(result.Type);
109  Assert.AreEqual("33", ((SqlString)result.Value).Value);
110  }
111 
112  [Test]
114  var obj = DataObject.Integer(1);
115  Assert.IsNotNull(obj);
116  Assert.IsInstanceOf<NumericType>(obj.Type);
117  Assert.AreEqual(SqlTypeCode.Integer, obj.Type.TypeCode);
118  Assert.AreEqual(1, obj);
119 
120  DataObject result = null;
121  Assert.DoesNotThrow(() => result = obj.AsBoolean());
122  Assert.IsNotNull(result);
123  Assert.IsInstanceOf<BooleanType>(result.Type);
124  Assert.IsTrue((SqlBoolean)result.Value);
125  }
126  }
127 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
static DataObject Integer(int value)
Definition: DataObject.cs:576
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
DataObject AsBoolean()
Converts this object to a boolean type.
Definition: DataObject.cs:512
DataObject CastTo(SqlType destType)
Converts this object to the given SqlType.
Definition: DataObject.cs:488
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
DataObject AsVarChar()
Definition: DataObject.cs:528
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
static DataObject BigInt(long value)
Definition: DataObject.cs:580