using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; using Centron.BusinessLogic.Administration.Settings; using Centron.BusinessLogic.IndexSearch; using Centron.BusinessLogic.Sales.Support; using Centron.BusinessLogic.WebServices.Sales.Support; using Centron.DAO.AdoNETDataAccess; using Centron.Data.Entities.Sales.Support; using Centron.Interfaces.BL; using Centron.Tests.EndToEnd.Infrastructure; using Xunit.Abstractions; namespace Centron.Tests.EndToEnd.Tests.IndexSearch; public class IndexSearchTests : EndToEndTest { public IndexSearchTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { } public override void Execute() { // First, index everything this.WithSession(s => s.GetBL().UpdateAllIndexes(default)); this.Verifier.VerifySql("Index_Count", "SELECT Count = COUNT(*) FROM dbo.ObjectFulltextIndex"); this.Verifier.VerifySql("IndexStats_Count", "SELECT Count = COUNT(*) FROM dbo.ObjectFulltextIndexStats"); // Remove a ticket (it should get removed from the index) this.Sql("DELETE FROM dbo.hlpdsk_requests WHERE I3D = 10"); // Set one ticket to "update requested" this.UpdateTicket(); // Now, update all indexes again var sqlStatements = SqlDiagnosticObserver.Start(() => { this.WithSession(s => s.GetBL().UpdateAllIndexes(default)); }); this.Verifier.Verify("Statements", sqlStatements.Select(f => NormalizeSqlAliasNumbers(f.CommandText))); // Try querying something var result = this.WithSession(s => s.GetBL().SearchIndex("gmbh ve")); this.Verifier.Verify("Search_Result", result.Select(f => new { Kind = f.Kind, ObjectI3D = f.ObjectI3D, }).Distinct().OrderBy(f => f.ObjectI3D)); // Query tickets this.QueryTickets(); } private void UpdateTicket() { var ticket = this.WithSession(s => s.GetBL().GetHelpdeskByI3D(11, this.GetLoggedInUser())).ThrowIfError(); this.WithSession(s => s.GetBL().SaveHelpdesk(ticket, this.GetLoggedInUser())); } private static string NormalizeSqlAliasNumbers(string sqlStatement) { return Regex.Replace(sqlStatement, @"\b([A-Za-z][A-Za-z0-9]*\d+)_\d+_", "$1_alias_"); } private void QueryTickets() { var filter = new HelpdeskFilter { SearchText = "Dell telefoniert", }; for (int i = 1; i <= 100; i++) { // Enable fulltext-search var settings2 = this.WithSession(s => s.GetBL().GetFulltextIndexServiceSettings()); settings2.IsFulltextIndexServiceActive = true; this.WithSession(s => s.GetBL().UpdateFulltextIndexServiceSettings(settings2)); var indexSearchWatch = Stopwatch.StartNew(); var indexSearchTickets = this.WithSession(s => s.GetBL().GetHelpdesksThroughPaging(filter, HelpdeskSort.Number, true, 1, int.MaxValue, this.GetLoggedInUser())); indexSearchWatch.Stop(); // Disable fulltext-search var settings1 = this.WithSession(s => s.GetBL().GetFulltextIndexServiceSettings()); settings1.IsFulltextIndexServiceActive = false; this.WithSession(s => s.GetBL().UpdateFulltextIndexServiceSettings(settings1)); var oldSearchWatch = Stopwatch.StartNew(); var oldSearchTickets = this.WithSession(s => s.GetBL().GetHelpdesksThroughPaging(filter, HelpdeskSort.Number, true, 1, int.MaxValue, this.GetLoggedInUser())); oldSearchWatch.Stop(); this.TestOutputHelper.WriteLine($"Run {i}: {(oldSearchWatch.Elapsed < indexSearchWatch.Elapsed ? "old" : "new")}"); } } }