DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
StringObjectTests.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 {
23  [TestFixture]
24  [Category("Data Objects")]
25  [Category("Strings")]
26  public class StringObjectTests {
27  [Test]
28  public void BasicVarChar_Create() {
29  const string s = "Test string";
30  var sObj = DataObject.VarChar(s);
31  Assert.IsNotNull(sObj);
32  Assert.IsInstanceOf<DataObject>(sObj);
33  Assert.AreEqual(SqlTypeCode.VarChar, sObj.Type.TypeCode);
34  Assert.IsInstanceOf<SqlString>(sObj.Value);
35  Assert.AreEqual(s, sObj.Value);
36  }
37 
38  [Test]
39  public void BasicVarChar_Compare() {
40  const string s = "Test string";
41  var sObj1 = DataObject.VarChar(s);
42  var sObj2 = DataObject.VarChar(s);
43 
44  Assert.IsNotNull(sObj1);
45  Assert.IsNotNull(sObj2);
46 
47  Assert.IsTrue(sObj1.IsComparableTo(sObj2));
48  Assert.AreEqual(0, sObj1.CompareTo(sObj2));
49  }
50 
51  [Test]
52  public void BasicVarChar_Add() {
53  const string s1 = "First test string that comes ";
54  const string s2 = "before the second test string";
55  var sObj1 = DataObject.VarChar(s1);
56  var sObj2 = DataObject.VarChar(s2);
57 
58  Assert.IsNotNull(sObj1);
59  Assert.IsNotNull(sObj2);
60 
61  Assert.IsTrue(sObj1.IsComparableTo(sObj2));
62 
63  DataObject result = null;
64  Assert.DoesNotThrow(() => result = sObj1.Add(sObj2));
65  Assert.IsNotNull(result);
66  Assert.AreEqual("First test string that comes before the second test string", (string)result);
67  }
68 
69  [Test]
71  const string s = "78998";
72  var obj = DataObject.VarChar(s);
73 
74  Assert.IsNotNull(obj);
75  Assert.IsInstanceOf<StringType>(obj.Type);
76  Assert.AreEqual(SqlTypeCode.VarChar, obj.Type.TypeCode);
77 
78  DataObject result = null;
79  Assert.DoesNotThrow(() => result = obj.CastTo(PrimitiveTypes.Numeric(SqlTypeCode.Integer)));
80  Assert.IsNotNull(result);
81  Assert.IsInstanceOf<NumericType>(result.Type);
82  Assert.AreEqual(SqlTypeCode.Integer, result.Type.TypeCode);
83  Assert.AreEqual(78998, result);
84  }
85 
86  [Test]
87  [Category("Numbers")]
89  const string s = "fail";
90  var obj = DataObject.VarChar(s);
91 
92  Assert.IsNotNull(obj);
93  Assert.IsInstanceOf<StringType>(obj.Type);
94  Assert.AreEqual(SqlTypeCode.VarChar, obj.Type.TypeCode);
95 
96  DataObject result = null;
97  Assert.DoesNotThrow(() => result = obj.CastTo(PrimitiveTypes.Numeric(SqlTypeCode.Integer)));
98  Assert.IsNotNull(result);
99  Assert.IsInstanceOf<NumericType>(result.Type);
100  Assert.IsTrue(result.IsNull);
101  }
102 
103  [Test]
104  [Category("Booleans")]
106  const string s = "true";
107  var obj = DataObject.VarChar(s);
108 
109  Assert.IsNotNull(obj);
110  Assert.IsInstanceOf<StringType>(obj.Type);
111  Assert.AreEqual(SqlTypeCode.VarChar, obj.Type.TypeCode);
112 
113  DataObject result = null;
114  Assert.DoesNotThrow(() => result = obj.CastTo(PrimitiveTypes.Boolean()));
115  Assert.IsNotNull(result);
116  Assert.IsInstanceOf<BooleanType>(result.Type);
117  Assert.IsFalse(result.IsNull);
118  Assert.AreEqual(true, (bool)result);
119  }
120  }
121 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
bool IsNull
Gets a value that indicates if this object is materialized as null.
Definition: DataObject.cs:91
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
DataObject CastTo(SqlType destType)
Converts this object to the given SqlType.
Definition: DataObject.cs:488
static BooleanType Boolean()
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
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 VarChar(string s)
Definition: DataObject.cs:622
DataObject Add(DataObject other)
Adds the given value to this object value.
Definition: DataObject.cs:383