DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
SequenceManagerTests.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 
19 using Deveel.Data.Sql;
20 using Deveel.Data.Sql.Objects;
21 using Deveel.Data.Sql.Sequences;
22 
23 using NUnit.Framework;
24 
25 namespace Deveel.Data {
26  [TestFixture]
27  public sealed class SequenceManagerTests : ContextBasedTest {
28  private ObjectName testSequenceName = ObjectName.Parse("APP.test_sequence");
29 
30  protected override IQuery CreateQuery(ISession session) {
31  var query = base.CreateQuery(session);
32 
33  if (TestContext.CurrentContext.Test.Name != "CreateNormalSequence") {
34  var seqInfo = new SequenceInfo(testSequenceName, new SqlNumber(0), new SqlNumber(1), new SqlNumber(0), new SqlNumber(Int64.MaxValue), 126);
35  query.CreateObject(seqInfo);
36  }
37 
38  return query;
39  }
40 
41  [Test]
42  public void CreateNormalSequence() {
43  var sequenceManager = new SequenceManager(Session.Transaction);
44 
45  var sequenceName = ObjectName.Parse("APP.test_sequence");
46  var seqInfo = new SequenceInfo(sequenceName, new SqlNumber(0), new SqlNumber(1), new SqlNumber(0), new SqlNumber(Int64.MaxValue), 126);
47 
48  ISequence sequence =null;
49  Assert.DoesNotThrow(() => sequence = sequenceManager.CreateSequence(seqInfo));
50  Assert.IsNotNull(sequence);
51  }
52 
53  [Test]
54  public void CreateNativeSequence() {
55  var sequenceManager = new SequenceManager(Session.Transaction);
56 
57  var tableName = ObjectName.Parse("APP.test_table");
58  var seqInfo = SequenceInfo.Native(tableName);
59 
60  ISequence sequence = null;
61  Assert.DoesNotThrow(() => sequence = sequenceManager.CreateSequence(seqInfo));
62  Assert.IsNotNull(sequence);
63  }
64 
65  [Test]
66  public void IncremementSequenceValue() {
67  var sequenceManager = new SequenceManager(Session.Transaction);
68 
69  ISequence sequence = null;
70  Assert.DoesNotThrow(() => sequence = sequenceManager.GetSequence(testSequenceName));
71  Assert.IsNotNull(sequence);
72 
73  SqlNumber currentValue = SqlNumber.Null;
74  Assert.DoesNotThrow(() => currentValue = sequence.NextValue());
75  Assert.IsNotNull(currentValue);
76  Assert.AreEqual(new SqlNumber(1), currentValue);
77  }
78  }
79 }
static ObjectName Parse(string s)
Parses the given string into a ObjectName object.
Definition: ObjectName.cs:139
ITransaction Transaction
Definition: Session.cs:104
Describes the name of an object within a database.
Definition: ObjectName.cs:44
This is a session that is constructed around a given user and a transaction, to the given database...
Definition: Session.cs:32
A default implementation of a sequence manager that is backed by a given transaction.
override IQuery CreateQuery(ISession session)
An isolated session to a given database for a given user, encapsulating the transaction for operation...
Definition: ISession.cs:30
Provides the meta information about a ISequence configuring its operative behavior.
Definition: SequenceInfo.cs:28
static SequenceInfo Native(ObjectName tableName)
Creates an object that describes a native sequence for the table having the specified name...
static readonly SqlNumber Null
Definition: SqlNumber.cs:31
Represents a numberic sequence in a transaction.
Definition: ISequence.cs:25