DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
StringTypeTests.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.Globalization;
18 using System.Text;
19 
20 using NUnit.Framework;
21 
22 namespace Deveel.Data.Types {
23  [TestFixture]
24  [Category("Data Types")]
25  public class StringTypeTests {
26  [Test]
27  public void BasicVarChar_Create() {
28  var type = PrimitiveTypes.String(SqlTypeCode.VarChar);
29  Assert.AreEqual(SqlTypeCode.VarChar, type.TypeCode);
30  Assert.AreEqual(Int16.MaxValue, type.MaxSize);
31  Assert.IsTrue(type.IsPrimitive);
32  Assert.IsTrue(type.IsIndexable);
33  Assert.IsNull(type.Locale);
34  }
35 
36  [Test]
37  public void BasicVarChar_Compare() {
38  var type1 = PrimitiveTypes.String(SqlTypeCode.VarChar);
39  var type2 = PrimitiveTypes.String(SqlTypeCode.VarChar);
40 
41  Assert.AreEqual(type1.TypeCode, type2.TypeCode);
42  Assert.IsTrue(type1.IsComparable(type2));
43  Assert.IsTrue(type1.CanCastTo(type2));
44  }
45 
46  [Test]
47  public void BasicVarChar_Parse() {
48  const string typeString = "VARCHAR";
49  SqlType sqlType = null;
50  Assert.DoesNotThrow(() => sqlType = SqlType.Parse(typeString));
51  Assert.IsNotNull(sqlType);
52  Assert.IsInstanceOf<StringType>(sqlType);
53  Assert.AreEqual(SqlTypeCode.VarChar, sqlType.TypeCode);
54 
55  var stringType = (StringType) sqlType;
56  Assert.AreEqual(Int16.MaxValue, stringType.MaxSize);
57  Assert.AreEqual(null, stringType.Locale);
58  }
59 
60  [Test]
61  public void SizedVarChar_Create() {
62  var type = PrimitiveTypes.String(SqlTypeCode.VarChar, 255);
63  Assert.AreEqual(SqlTypeCode.VarChar, type.TypeCode);
64  Assert.AreEqual(255, type.MaxSize);
65  }
66 
67  [Test]
68  public void SizedVarChar_Compare() {
69  var type1 = PrimitiveTypes.String(SqlTypeCode.VarChar, 255);
70  var type2 = PrimitiveTypes.String(SqlTypeCode.VarChar, 200);
71 
72  Assert.AreEqual(type1.TypeCode, type2.TypeCode);
73  Assert.IsFalse(type1.Equals(type2));
74  Assert.IsTrue(type1.IsComparable(type2));
75  }
76 
77  [Test]
78  public void SizedVarChar_Parse() {
79  const string typeString = "VARCHAR(255)";
80  SqlType sqlType = null;
81  Assert.DoesNotThrow(() => sqlType = SqlType.Parse(typeString));
82  Assert.IsNotNull(sqlType);
83  Assert.IsInstanceOf<StringType>(sqlType);
84  Assert.AreEqual(SqlTypeCode.VarChar, sqlType.TypeCode);
85 
86  var stringType = (StringType) sqlType;
87  Assert.AreEqual(255, stringType.MaxSize);
88  Assert.AreEqual(null, stringType.Locale);
89  }
90 
91  [Test]
92  public void LocalizedVarChar_Parse() {
93  const string typeString = "VARCHAR(255) LOCALE 'en-Us'";
94  SqlType sqlType = null;
95  Assert.DoesNotThrow(() => sqlType = SqlType.Parse(typeString));
96  Assert.IsNotNull(sqlType);
97  Assert.IsInstanceOf<StringType>(sqlType);
98  Assert.AreEqual(SqlTypeCode.VarChar, sqlType.TypeCode);
99 
100  var stringType = (StringType) sqlType;
101  Assert.AreEqual(255, stringType.MaxSize);
102  Assert.AreEqual(CultureInfo.GetCultureInfo("en-US"), stringType.Locale);
103  Assert.IsNotNull(stringType.Encoding);
104  Assert.AreEqual(Encoding.Unicode.WebName, stringType.Encoding.WebName);
105  }
106 
107  [Test]
108  [Category("Strings"), Category("SQL Parse")]
110  const string typeString = "VARCHAR(255) LOCALE 'en-Us' ENCODING 'UTF-16'";
111  SqlType sqlType = null;
112  Assert.DoesNotThrow(() => sqlType = SqlType.Parse(typeString));
113  Assert.IsNotNull(sqlType);
114  Assert.IsInstanceOf<StringType>(sqlType);
115  Assert.AreEqual(SqlTypeCode.VarChar, sqlType.TypeCode);
116 
117  var stringType = (StringType)sqlType;
118  Assert.AreEqual(255, stringType.MaxSize);
119  Assert.AreEqual(CultureInfo.GetCultureInfo("en-US"), stringType.Locale);
120  Assert.AreEqual(Encoding.Unicode.WebName, stringType.Encoding.WebName);
121  }
122 
123  [Test]
124  [Category("Strings"), Category("SQL Parse")]
125  public void SizedWithEncoding_Parse() {
126  const string typeString = "VARCHAR(255) ENCODING 'UTF-16'";
127  SqlType sqlType = null;
128  Assert.DoesNotThrow(() => sqlType = SqlType.Parse(typeString));
129  Assert.IsNotNull(sqlType);
130  Assert.IsInstanceOf<StringType>(sqlType);
131  Assert.AreEqual(SqlTypeCode.VarChar, sqlType.TypeCode);
132 
133  var stringType = (StringType)sqlType;
134  Assert.AreEqual(255, stringType.MaxSize);
135  Assert.IsNull(stringType.Locale);
136  Assert.AreEqual(Encoding.Unicode.WebName, stringType.Encoding.WebName);
137  }
138  }
139 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
static SqlType Parse(string s)
Parses a SQL formatted string that defines a data-type into a constructed SqlType object equivalent...
Definition: SqlType.cs:321