-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathFbTransactionTests.cs
More file actions
160 lines (136 loc) · 4.62 KB
/
FbTransactionTests.cs
File metadata and controls
160 lines (136 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Carlos Guzman Alvarez, Jiri Cincura (jiri@cincura.net)
using System;
using System.Threading.Tasks;
using FirebirdSql.Data.TestsBase;
using NUnit.Framework;
namespace FirebirdSql.Data.FirebirdClient.Tests;
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Default))]
[TestFixtureSource(typeof(FbServerTypeTestFixtureSource), nameof(FbServerTypeTestFixtureSource.Embedded))]
public class FbTransactionTests : FbTestsBase
{
public FbTransactionTests(FbServerType serverType, bool compression, FbWireCrypt wireCrypt)
: base(serverType, compression, wireCrypt)
{ }
[Test]
public async Task CommitTest()
{
await using (var transaction = await Connection.BeginTransactionAsync())
{
await transaction.CommitAsync();
}
}
[Test]
public async Task RollbackTest()
{
await using (var transaction = await Connection.BeginTransactionAsync())
{
await transaction.RollbackAsync();
}
}
[Test]
public async Task SavePointTest()
{
await using (var command = new FbCommand())
{
await using (var transaction = await Connection.BeginTransactionAsync("InitialSavePoint"))
{
command.Connection = Connection;
command.Transaction = transaction;
command.CommandText = "insert into TEST (INT_FIELD) values (200)";
await command.ExecuteNonQueryAsync();
await transaction.SaveAsync("FirstSavePoint");
command.CommandText = "insert into TEST (INT_FIELD) values (201)";
await command.ExecuteNonQueryAsync();
await transaction.SaveAsync("SecondSavePoint");
command.CommandText = "insert into TEST (INT_FIELD) values (202)";
await command.ExecuteNonQueryAsync();
await transaction.RollbackAsync("InitialSavePoint");
await transaction.CommitAsync();
}
}
}
[Test]
public async Task AbortTransaction()
{
FbTransaction transaction = null;
FbCommand command = null;
try
{
transaction = await Connection.BeginTransactionAsync();
command = new FbCommand("ALTER TABLE \"TEST\" drop \"INT_FIELD\"", Connection, transaction);
await command.ExecuteNonQueryAsync();
await transaction.CommitAsync();
}
catch (Exception)
{
await transaction.RollbackAsync();
}
finally
{
if (transaction != null)
{
await command.DisposeAsync();
}
if (transaction != null)
{
await command.DisposeAsync();
}
}
}
[Test]
public async Task ReadCommittedReadConsistency()
{
if (!EnsureServerVersionAtLeast(new Version(4, 0, 0, 0)))
return;
await using (var transaction = await Connection.BeginTransactionAsync(new FbTransactionOptions() { TransactionBehavior = FbTransactionBehavior.ReadConsistency }))
{ }
}
[Test]
public async Task SnapshotAtNumber()
{
if (!EnsureServerVersionAtLeast(new Version(4, 0, 0, 0)))
return;
await using (var transaction1 = await Connection.BeginTransactionAsync(new FbTransactionOptions() { TransactionBehavior = FbTransactionBehavior.Concurrency }))
{
var number1 = await new FbTransactionInfo(transaction1).GetTransactionSnapshotNumberAsync();
Assert.NotZero(number1);
await using (var conn = new FbConnection(BuildConnectionString(ServerType, Compression, WireCrypt)))
{
await conn.OpenAsync();
await using (var transaction2 = await conn.BeginTransactionAsync(new FbTransactionOptions() { TransactionBehavior = FbTransactionBehavior.Concurrency, SnapshotAtNumber = number1 }))
{
var number2 = await new FbTransactionInfo(transaction2).GetTransactionSnapshotNumberAsync();
Assert.AreEqual(number1, number2);
}
}
}
}
[Test]
public async Task CanGetTransactionId()
{
if (!EnsureServerVersionAtLeast(new Version(2, 5, 0, 0)))
return;
await using (var transaction1 = await Connection.BeginTransactionAsync())
{
var idFromInfo = await new FbTransactionInfo(transaction1).GetTransactionIdAsync();
Assert.NotZero(idFromInfo);
var command = new FbCommand("SELECT current_transaction FROM rdb$database", Connection, transaction1);
var idFromSql = await command.ExecuteScalarAsync();
Assert.AreEqual(idFromInfo, idFromSql);
}
}
}