DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
CreateTableTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 
6 
7 using NUnit.Framework;
8 
9 namespace Deveel.Data.Sql.Compile {
10  [TestFixture]
12  [Test]
13  public void SimpleTable() {
14  const string sql = "CREATE TABLE test (a INT NOT NULL, b VARCHAR NULL)";
15 
16  var result = Compile(sql);
17  Assert.IsNotNull(result);
18  Assert.IsFalse(result.HasErrors);
19  Assert.AreEqual(1, result.Statements.Count);
20 
21  var statement = (CreateTableStatement) result.Statements.ElementAt(0);
22 
23  Assert.IsNotNull(statement);
24  Assert.IsInstanceOf<CreateTableStatement>(statement);
25 
26  var createTable = (CreateTableStatement) statement;
27 
28  Assert.IsNotNullOrEmpty(createTable.TableName);
29  Assert.AreEqual("test", createTable.TableName);
30  Assert.IsFalse(createTable.Temporary);
31  Assert.IsFalse(createTable.IfNotExists);
32 
33  Assert.AreEqual(2, createTable.Columns.Count);
34  Assert.AreEqual("a", createTable.Columns[0].ColumnName);
35  Assert.IsTrue(createTable.Columns[0].IsNotNull);
36 
37  Assert.AreEqual("b", createTable.Columns[1].ColumnName);
38  Assert.IsFalse(createTable.Columns[1].IsNotNull);
39  }
40 
41  [Test]
42  public void WithIdentityColumn() {
43  const string sql = "CREATE TABLE test (id INT IDENTITY, name VARCHAR NOT NULL)";
44 
45  var result = Compile(sql);
46  Assert.IsNotNull(result);
47  Assert.IsFalse(result.HasErrors);
48  Assert.AreEqual(1, result.Statements.Count);
49 
50  var statement = (CreateTableStatement) result.Statements.ElementAt(0);
51 
52  Assert.IsNotNull(statement);
53  Assert.IsInstanceOf<CreateTableStatement>(statement);
54 
55  var createTable = (CreateTableStatement) statement;
56 
57  Assert.IsNotNullOrEmpty(createTable.TableName);
58  Assert.AreEqual("test", createTable.TableName);
59  Assert.IsFalse(createTable.Temporary);
60  Assert.IsFalse(createTable.IfNotExists);
61 
62  Assert.AreEqual(2, createTable.Columns.Count);
63  Assert.IsTrue(createTable.Columns[0].IsIdentity);
64  Assert.IsNotNull(createTable.Columns[0].DefaultExpression);
65  Assert.IsInstanceOf<SqlFunctionCallExpression>(createTable.Columns[0].DefaultExpression);
66  }
67 
68  [Test]
69  public void WithColumnDefault() {
70  const string sql = "CREATE TABLE test (a VARCHAR DEFAULT 'one')";
71 
72  var result = Compile(sql);
73  Assert.IsNotNull(result);
74  Assert.IsFalse(result.HasErrors);
75  Assert.AreEqual(1, result.Statements.Count);
76 
77  var statement = (CreateTableStatement) result.Statements.ElementAt(0);
78 
79  Assert.IsNotNull(statement);
80  Assert.IsInstanceOf<CreateTableStatement>(statement);
81 
82  var createTable = (CreateTableStatement) statement;
83 
84  Assert.IsNotNullOrEmpty(createTable.TableName);
85  Assert.AreEqual("test", createTable.TableName);
86 
87  Assert.AreEqual(1, createTable.Columns.Count);
88  Assert.IsNotNull(createTable.Columns[0].DefaultExpression);
89  Assert.IsInstanceOf<SqlConstantExpression>(createTable.Columns[0].DefaultExpression);
90  }
91 
92  [Test]
93  public void WithConstraints() {
94  const string sql = "CREATE TABLE test (id INT NOT NULL, UNIQUE(id))";
95 
96  var result = Compile(sql);
97  Assert.IsNotNull(result);
98  Assert.IsFalse(result.HasErrors);
99  Assert.AreEqual(2, result.Statements.Count);
100 
101  Assert.IsInstanceOf<CreateTableStatement>(result.Statements.ElementAt(0));
102  Assert.IsInstanceOf<AlterTableStatement>(result.Statements.ElementAt(1));
103 
104  var statement = (CreateTableStatement) result.Statements.ElementAt(0);
105 
106  Assert.IsNotNull(statement);
107  Assert.IsInstanceOf<CreateTableStatement>(statement);
108 
109  var createTable = (CreateTableStatement) statement;
110 
111  Assert.IsNotNullOrEmpty(createTable.TableName);
112  Assert.AreEqual("test", createTable.TableName);
113  }
114 
115  [Test]
116  public void WithColumnConstraints() {
117  const string sql = "CREATE TABLE test (id INT NOT NULL PRIMARY KEY)";
118 
119  var result = Compile(sql);
120  Assert.IsNotNull(result);
121  Assert.IsFalse(result.HasErrors);
122  Assert.AreEqual(2, result.Statements.Count);
123 
124  Assert.IsInstanceOf<CreateTableStatement>(result.Statements.ElementAt(0));
125  Assert.IsInstanceOf<AlterTableStatement>(result.Statements.ElementAt(1));
126 
127  var statement = (CreateTableStatement) result.Statements.ElementAt(0);
128 
129  Assert.IsNotNull(statement);
130  Assert.IsInstanceOf<CreateTableStatement>(statement);
131 
132  var createTable = (CreateTableStatement) statement;
133 
134  Assert.IsNotNullOrEmpty(createTable.TableName);
135  Assert.AreEqual("test", createTable.TableName);
136  }
137  }
138 }
The statement object used to create a table in a database.
An expression that holds a constant value.