DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
DropTableTests.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;
18 using Deveel.Data.Sql.Tables;
19 using Deveel.Data.Types;
20 
21 using NUnit.Framework;
22 
23 namespace Deveel.Data.Sql {
24  [TestFixture]
26  protected override ISession CreateAdminSession(IDatabase database) {
27  using (var session = base.CreateAdminSession(database)) {
28  using (var query = session.CreateQuery()) {
29  var tn1 = ObjectName.Parse("APP.test_table1");
30  var tableInfo1 = new TableInfo(tn1);
31  tableInfo1.AddColumn(new ColumnInfo("id", PrimitiveTypes.Integer()));
32  tableInfo1.AddColumn(new ColumnInfo("name", PrimitiveTypes.String()));
33  tableInfo1.AddColumn(new ColumnInfo("date", PrimitiveTypes.DateTime()));
34  query.CreateTable(tableInfo1);
35  query.AddPrimaryKey(tn1, "id");
36 
37  var tn2 = ObjectName.Parse("APP.test_table2");
38  var tableInfo2 = new TableInfo(tn2);
39  tableInfo2.AddColumn(new ColumnInfo("id", PrimitiveTypes.Integer()));
40  tableInfo2.AddColumn(new ColumnInfo("other_id", PrimitiveTypes.Integer()));
41  tableInfo2.AddColumn(new ColumnInfo("count", PrimitiveTypes.Integer()));
42  query.CreateTable(tableInfo2);
43  query.AddPrimaryKey(tn2, "id");
44  query.AddForeignKey(tn2, new[] { "other_id" }, tn1, new[] { "id" }, ForeignKeyAction.Cascade, ForeignKeyAction.Cascade, null);
45 
46  query.Commit();
47  }
48  }
49 
50  return base.CreateAdminSession(database);
51  }
52 
53  [Test]
54  public void DropNonReferencedTable() {
55  var tableName = ObjectName.Parse("APP.test_table2");
56  Assert.DoesNotThrow(() => Query.DropTable(tableName));
57 
58  bool exists = false;
59  Assert.DoesNotThrow(() => exists = Query.TableExists(tableName));
60  Assert.IsFalse(exists);
61  }
62 
63  [Test]
64  public void DropReferencedTable() {
65  var tableName = ObjectName.Parse("APP.test_table1");
66  Assert.Throws<ConstraintViolationException>(() => Query.DropTable(tableName));
67 
68  bool exists = false;
69  Assert.DoesNotThrow(() => exists = Query.TableExists(tableName));
70  Assert.IsTrue(exists);
71  }
72 
73  [Test]
74  public void DropAllTables() {
75  var tableNames = new[] {
76  ObjectName.Parse("APP.test_table1"),
77  ObjectName.Parse("APP.test_table2"),
78  };
79 
80  Assert.DoesNotThrow(() => Query.DropTables(tableNames));
81  }
82  }
83 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
Defines the metadata properties of a column within a table of a database.
Definition: ColumnInfo.cs:36
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
A database exception that represents a constraint violation.
Describes the name of an object within a database.
Definition: ObjectName.cs:44
The representation of a single database in the system.
Definition: IDatabase.cs:40
override ISession CreateAdminSession(IDatabase database)
ForeignKeyAction
Enumerates the foreign key referential trigger actions.
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
Defines the metadata properties of a table existing within a database.
Definition: TableInfo.cs:41