Skip to content

Commit 93adc35

Browse files
authored
Merge pull request #430 from DataObjects-NET/7.1-small-tests-improvement
Small tests' improvements
2 parents f6a7388 + 28d427a commit 93adc35

File tree

5 files changed

+59
-81
lines changed

5 files changed

+59
-81
lines changed

Orm/Xtensive.Orm.Tests.Framework/StorageTestHelper.cs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
using System;
88
using System.Collections.Generic;
9+
using System.Data.Common;
910
using System.Linq;
11+
using System.Threading;
1012
using Xtensive.Orm.Providers;
1113
using Xtensive.Sql;
1214
using Xtensive.Sql.Model;
@@ -17,15 +19,13 @@ public static class StorageTestHelper
1719
{
1820
public static bool IsFetched(Session session, Key key)
1921
{
20-
EntityState dummy;
21-
return session.EntityStateCache.TryGetItem(key, false, out dummy);
22+
return session.EntityStateCache.TryGetItem(key, false, out var _);
2223
}
2324

24-
public static object GetNativeTransaction()
25+
public static object GetNativeTransaction(Session session)
2526
{
26-
var handler = Session.Demand().Handler;
27-
var sqlHandler = handler as SqlSessionHandler;
28-
if (sqlHandler!=null)
27+
var handler = session.Handler;
28+
if (handler is SqlSessionHandler sqlHandler)
2929
return sqlHandler.Connection.ActiveTransaction;
3030
throw new NotSupportedException();
3131
}
@@ -66,6 +66,39 @@ public static void DemandSchemas(ConnectionInfo connectionInfo, params string[]
6666
}
6767
}
6868

69+
/// <summary>
70+
/// Waits for full-text indexes of MS SQL to be populated.
71+
/// Every now and then it gets state of them from database or waits timeout to be reached.
72+
/// </summary>
73+
/// <param name="domain"></param>
74+
public static void WaitFullTextIndexesPopulated(Domain domain, TimeSpan timeout)
75+
{
76+
if (StorageProviderInfo.Instance.Provider == StorageProvider.SqlServer) {
77+
var driver = TestSqlDriver.Create(domain.Configuration.ConnectionInfo);
78+
using (var connection = driver.CreateConnection()) {
79+
80+
var date = DateTime.UtcNow.Add(timeout);
81+
while (!CheckFtIndexesPopulated(connection) && DateTime.UtcNow < date) {
82+
Thread.Sleep(TimeSpan.FromSeconds(2));
83+
}
84+
}
85+
}
86+
87+
static bool CheckFtIndexesPopulated(SqlConnection connection)
88+
{
89+
connection.Open();
90+
try {
91+
using (var command = connection.CreateCommand()) {
92+
command.CommandText = $"SELECT COUNT(*) FROM sys.fulltext_indexes WHERE has_crawl_completed = 0";
93+
return ((int) command.ExecuteScalar()) == 0;
94+
}
95+
}
96+
finally {
97+
connection.Close();
98+
}
99+
}
100+
}
101+
69102
private static void CreateUsers(SqlConnection connection, IEnumerable<string> schemasToCreate)
70103
{
71104
var translator = connection.Driver.Translator;
@@ -86,14 +119,14 @@ private static void CreateSchemas(SqlConnection connection, Catalog catalog, IEn
86119

87120
private static void ExecuteQuery(SqlConnection connection, ISqlCompileUnit query)
88121
{
89-
using (var command = connection.CreateCommand(query))
90-
command.ExecuteNonQuery();
122+
using var command = connection.CreateCommand(query);
123+
_ = command.ExecuteNonQuery();
91124
}
92125

93126
private static void ExecuteQuery(SqlConnection connection, string query)
94127
{
95-
using (var command = connection.CreateCommand(query))
96-
command.ExecuteNonQuery();
128+
using var command = connection.CreateCommand(query);
129+
_ = command.ExecuteNonQuery();
97130
}
98131
}
99132
}

Orm/Xtensive.Orm.Tests/Linq/ContainsTableTest.cs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
// Created: 2016.12.09
66

77
using System;
8-
using System.Data.Common;
98
using System.Linq;
10-
using System.Threading;
119
using NUnit.Framework;
1210
using Xtensive.Core;
1311
using Xtensive.Orm.Providers;
14-
using Xtensive.Orm.Services;
1512
using Xtensive.Orm.Tests.ObjectModel;
1613
using Xtensive.Orm.Tests.ObjectModel.ChinookDO;
1714

@@ -27,52 +24,29 @@ protected override void CheckRequirements()
2724
protected override Domain BuildDomain(Xtensive.Orm.Configuration.DomainConfiguration configuration)
2825
{
2926
var domain = base.BuildDomain(configuration);
30-
if (StorageProviderInfo.Instance.Provider == StorageProvider.SqlServer) {
31-
using (var session = domain.OpenSession())
32-
using (var tx = session.OpenTransaction()) {
33-
var sqlAccessor = session.Services.Get<DirectSqlAccessor>();
34-
var timeout = DateTime.UtcNow.AddSeconds(20);
35-
while (!CheckFtIndexesPopulated(sqlAccessor.CreateCommand()) && DateTime.UtcNow < timeout) {
36-
Console.WriteLine("There are unpopulated FT indexes. Waiting");
37-
Thread.Sleep(TimeSpan.FromSeconds(2));
38-
}
39-
return domain;
40-
}
41-
}
42-
else {
43-
Thread.Sleep(TimeSpan.FromSeconds(6));
44-
}
27+
StorageTestHelper.WaitFullTextIndexesPopulated(domain, TimeSpan.FromSeconds(20));
4528
return domain;
46-
47-
48-
static bool CheckFtIndexesPopulated(DbCommand sqlCommand)
49-
{
50-
using (sqlCommand) {
51-
sqlCommand.CommandText = $"SELECT COUNT(*) FROM [{WellKnownDatabases.MultiDatabase.MainDb}].sys.fulltext_indexes WHERE has_crawl_completed=0";
52-
return ((int) sqlCommand.ExecuteScalar()) == 0;
53-
}
54-
}
5529
}
5630

5731
[Test]
5832
public void SearchByTypeWithoutFulltextIndexTest()
5933
{
60-
Assert.Throws<QueryTranslationException>(() => Session.Query.ContainsTable<Artist>(e => e.SimpleTerm("some text")).Run());
61-
Assert.Throws<QueryTranslationException>(() => Query.ContainsTable<Artist> (e => e.SimpleTerm("some text")).Run());
34+
_ = Assert.Throws<QueryTranslationException>(() => Session.Query.ContainsTable<Artist>(e => e.SimpleTerm("some text")).Run());
35+
_ = Assert.Throws<QueryTranslationException>(() => Query.ContainsTable<Artist> (e => e.SimpleTerm("some text")).Run());
6236
}
6337

6438
[Test]
6539
public void NullSearchConditionBuilderTest()
6640
{
67-
Assert.Throws<ArgumentNullException>(() => Session.Query.ContainsTable<Artist>(null).Run());
68-
Assert.Throws<ArgumentNullException>(() => Query.ContainsTable<Artist>(null).Run());
41+
_ = Assert.Throws<ArgumentNullException>(() => Session.Query.ContainsTable<Artist>(null).Run());
42+
_ = Assert.Throws<ArgumentNullException>(() => Query.ContainsTable<Artist>(null).Run());
6943
}
7044

7145
[Test]
7246
public void NegativeRank()
7347
{
74-
Assert.Throws<ArgumentOutOfRangeException>(() => Session.Query.ContainsTable<Artist>(e => e.SimpleTerm("some text"), -1).Run());
75-
Assert.Throws<ArgumentOutOfRangeException>(() => Query.ContainsTable<Artist>(e => e.SimpleTerm("some text"), -1).Run());
48+
_ = Assert.Throws<ArgumentOutOfRangeException>(() => Session.Query.ContainsTable<Artist>(e => e.SimpleTerm("some text"), -1).Run());
49+
_ = Assert.Throws<ArgumentOutOfRangeException>(() => Query.ContainsTable<Artist>(e => e.SimpleTerm("some text"), -1).Run());
7650
}
7751

7852
[Test]

Orm/Xtensive.Orm.Tests/Linq/FreeTextTest.cs

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@
55
// Created: 2009.12.14
66

77
using System;
8-
using System.Data.Common;
98
using System.Collections.Generic;
109
using System.Linq;
11-
using System.Threading;
1210
using NUnit.Framework;
1311
using Xtensive.Orm.Providers;
14-
using Xtensive.Orm.Services;
1512
using Xtensive.Orm.Tests.ObjectModel;
1613
using Xtensive.Orm.Tests.ObjectModel.ChinookDO;
1714

@@ -27,36 +24,14 @@ protected override void CheckRequirements()
2724
protected override Domain BuildDomain(Xtensive.Orm.Configuration.DomainConfiguration configuration)
2825
{
2926
var domain = base.BuildDomain(configuration);
30-
if (StorageProviderInfo.Instance.Provider == StorageProvider.SqlServer) {
31-
using (var session = domain.OpenSession())
32-
using (var tx = session.OpenTransaction()) {
33-
var sqlAccessor = session.Services.Get<DirectSqlAccessor>();
34-
var timeout = DateTime.UtcNow.AddSeconds(20);
35-
while (!CheckFtIndexesPopulated(sqlAccessor.CreateCommand()) && DateTime.UtcNow < timeout) {
36-
Thread.Sleep(TimeSpan.FromSeconds(2));
37-
}
38-
return domain;
39-
}
40-
}
41-
else {
42-
Thread.Sleep(TimeSpan.FromSeconds(6));
43-
}
27+
StorageTestHelper.WaitFullTextIndexesPopulated(domain, TimeSpan.FromSeconds(20));
4428
return domain;
45-
46-
47-
static bool CheckFtIndexesPopulated(DbCommand sqlCommand)
48-
{
49-
using (sqlCommand) {
50-
sqlCommand.CommandText = $"SELECT COUNT(*) FROM [{WellKnownDatabases.MultiDatabase.MainDb}].sys.fulltext_indexes WHERE has_crawl_completed=0";
51-
return ((int) sqlCommand.ExecuteScalar()) == 0;
52-
}
53-
}
5429
}
5530

5631
[Test]
5732
public void ReuseFreeText1Test()
5833
{
59-
Assert.Throws<QueryTranslationException>(
34+
_ = Assert.Throws<QueryTranslationException>(
6035
() => {
6136
var result1 = TakeMatchesIncorrect("black babbath back").Count();
6237
Assert.AreEqual(3, result1);
@@ -148,13 +123,13 @@ public void TopNByrankEmptyResultTest()
148123
[Test]
149124
public void NegativeTopNTest()
150125
{
151-
Assert.Throws<ArgumentOutOfRangeException>(() => Session.Query.FreeText<Album>("sfdgfdhghgf", -1));
126+
_ = Assert.Throws<ArgumentOutOfRangeException>(() => Session.Query.FreeText<Album>("sfdgfdhghgf", -1));
152127
}
153128

154129
[Test]
155130
public void ZeroTopNTest()
156131
{
157-
Assert.Throws<ArgumentOutOfRangeException>(() => Session.Query.FreeText<Album>("sfdgfhgfhhj", 0));
132+
_ = Assert.Throws<ArgumentOutOfRangeException>(() => Session.Query.FreeText<Album>("sfdgfhgfhhj", 0));
158133
}
159134

160135
[Test]

Orm/Xtensive.Orm.Tests/Storage/NestedTransactionsTest.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ public void WrongNestedTransactionUsageTest()
126126
outerScope.Complete();
127127
AssertEx.ThrowsInvalidOperationException(outerScope.Dispose);
128128
}
129-
Assert.IsNull(Session.Current.Transaction);
130-
Assert.IsNull(StorageTestHelper.GetNativeTransaction());
129+
var currentSession = Session.Current;
130+
Assert.IsNull(currentSession.Transaction);
131+
Assert.IsNull(StorageTestHelper.GetNativeTransaction(currentSession));
131132
}
132133

133134
[Test]

Orm/Xtensive.Orm.Tests/Storage/TransactionModesTest.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,10 @@ public void DefaultTransactionsTest()
4141
track.Milliseconds++;
4242
session.SaveChanges();
4343
Assert.IsTrue(session.Handler.TransactionIsStarted);
44-
object dbTransaction;
45-
using (session.Activate()) {
46-
dbTransaction = StorageTestHelper.GetNativeTransaction();
47-
}
44+
var dbTransaction = StorageTestHelper.GetNativeTransaction(session);
4845
track.Milliseconds++;
4946
session.SaveChanges();
50-
using (session.Activate()) {
51-
Assert.AreSame(dbTransaction, StorageTestHelper.GetNativeTransaction());
52-
}
47+
Assert.AreSame(dbTransaction, StorageTestHelper.GetNativeTransaction(session));
5348
track.Milliseconds++;
5449
milliseconds = track.Milliseconds;
5550
trackKey = track.Key;

0 commit comments

Comments
 (0)