DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
AlterTableCompileTests.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 
5 using Deveel.Data.Types;
6 
7 using NUnit.Framework;
8 
9 namespace Deveel.Data.Sql.Compile {
10  [TestFixture]
12  [Test]
13  public void AddColumn() {
14  const string sql = "ALTER TABLE test ADD COLUMN b INT NOT NULL";
15 
16  var result = Compile(sql);
17  Assert.IsNotNull(result);
18  Assert.IsFalse(result.HasErrors);
19  Assert.IsNotEmpty(result.Statements);
20  Assert.AreEqual(1, result.Statements.Count);
21 
22  var statement = result.Statements.First();
23 
24  Assert.IsNotNull(statement);
25  Assert.IsInstanceOf<AlterTableStatement>(statement);
26 
27  var alterStatement = (AlterTableStatement) statement;
28 
29  Assert.AreEqual("test", alterStatement.TableName.FullName);
30  Assert.IsInstanceOf<AddColumnAction>(alterStatement.Action);
31 
32  var alterAction = (AddColumnAction) alterStatement.Action;
33  Assert.AreEqual("b", alterAction.Column.ColumnName);
34  Assert.IsInstanceOf<NumericType>(alterAction.Column.ColumnType);
35  Assert.IsTrue(alterAction.Column.IsNotNull);
36  }
37 
38 
39  [Test]
40  public void AddMultipleColumns() {
41  const string sql = "ALTER TABLE test ADD COLUMN b INT NOT NULL ADD c VARCHAR DEFAULT 'test'";
42 
43  var result = Compile(sql);
44  Assert.IsNotNull(result);
45  Assert.IsFalse(result.HasErrors);
46  Assert.IsNotEmpty(result.Statements);
47  Assert.AreEqual(2, result.Statements.Count);
48  }
49 
50  [Test]
52  const string sql = "ALTER TABLE test ADD COLUMN b INT NOT NULL ADD CONSTRAINT c UNIQUE(a, b)";
53 
54  var result = Compile(sql);
55  Assert.IsNotNull(result);
56  Assert.IsFalse(result.HasErrors);
57  Assert.IsNotEmpty(result.Statements);
58  Assert.AreEqual(2, result.Statements.Count);
59 
60  var firstStatement = result.Statements.ElementAt(0);
61  var secondStatement = result.Statements.ElementAt(1);
62 
63  Assert.IsNotNull(firstStatement);
64  Assert.IsNotNull(secondStatement);
65 
66  Assert.IsInstanceOf<AlterTableStatement>(firstStatement);
67  Assert.IsInstanceOf<AlterTableStatement>(secondStatement);
68 
69  var alter1 = (AlterTableStatement) firstStatement;
70  var alter2 = (AlterTableStatement) secondStatement;
71 
72  Assert.IsInstanceOf<AddColumnAction>(alter1.Action);
73  Assert.IsInstanceOf<AddConstraintAction>(alter2.Action);
74  }
75 
76  [Test]
77  public void AddPrimaryKeyContraint() {
78  const string sql = "ALTER TABLE test ADD CONSTRAINT c PRIMARY KEY(id)";
79 
80  var result = Compile(sql);
81  Assert.IsNotNull(result);
82  Assert.IsFalse(result.HasErrors);
83  Assert.IsNotEmpty(result.Statements);
84  Assert.AreEqual(1, result.Statements.Count);
85 
86  var statement = result.Statements.First();
87 
88  Assert.IsNotNull(statement);
89  Assert.IsInstanceOf<AlterTableStatement>(statement);
90 
91  var alterStatement = (AlterTableStatement) statement;
92 
93  Assert.AreEqual("test", alterStatement.TableName.FullName);
94  Assert.IsInstanceOf<AddConstraintAction>(alterStatement.Action);
95  }
96 
97  [Test]
98  public void DropColumn() {
99  const string sql = "ALTER TABLE test DROP COLUMN b";
100 
101  var result = Compile(sql);
102  Assert.IsNotNull(result);
103  Assert.IsFalse(result.HasErrors);
104  Assert.IsNotEmpty(result.Statements);
105  Assert.AreEqual(1, result.Statements.Count);
106 
107  var statement = result.Statements.ElementAt(0);
108 
109  Assert.IsInstanceOf<AlterTableStatement>(statement);
110 
111  var alter = (AlterTableStatement) statement;
112 
113  Assert.IsInstanceOf<DropColumnAction>(alter.Action);
114  }
115  }
116 }