|
| 1 | +namespace ServiceControl.Persistence.Tests; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using EFCore.PostgreSql; |
| 6 | +using Npgsql; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +class FullTextSearchTests : PersistenceTestBase |
| 10 | +{ |
| 11 | + [Test] |
| 12 | + public async Task Can_create_and_query_full_text_index() |
| 13 | + { |
| 14 | + var postgreSqlSettings = PersistenceSettings as PostgreSqlPersisterSettings; |
| 15 | + Assert.That(postgreSqlSettings, Is.Not.Null); |
| 16 | + |
| 17 | + var tableName = $"fts_{Guid.NewGuid():N}"; |
| 18 | + var commandTimeoutSeconds = 30; |
| 19 | + |
| 20 | + await using var connection = new NpgsqlConnection(postgreSqlSettings.ConnectionString); |
| 21 | + await connection.OpenAsync(); |
| 22 | + |
| 23 | + try |
| 24 | + { |
| 25 | + await ExecuteNonQuery(connection, $""" |
| 26 | + CREATE TABLE public."{tableName}" ( |
| 27 | + id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY, |
| 28 | + body TEXT NOT NULL, |
| 29 | + search_vector tsvector GENERATED ALWAYS AS (to_tsvector('english', body)) STORED |
| 30 | + ) |
| 31 | + """, commandTimeoutSeconds); |
| 32 | + |
| 33 | + await ExecuteNonQuery(connection, $"""CREATE INDEX "{tableName}_search_vector_idx" ON public."{tableName}" USING GIN (search_vector)""", commandTimeoutSeconds); |
| 34 | + |
| 35 | + await ExecuteNonQuery(connection, $""" |
| 36 | + INSERT INTO public."{tableName}"(body) VALUES |
| 37 | + ('quick brown fox jumps'), |
| 38 | + ('azure service bus transport') |
| 39 | + """, commandTimeoutSeconds); |
| 40 | + |
| 41 | + var matchCount = await ExecuteScalarInt(connection, $""" |
| 42 | + SELECT COUNT(1) |
| 43 | + FROM public."{tableName}" |
| 44 | + WHERE search_vector @@ plainto_tsquery('english', 'quick') |
| 45 | + """, commandTimeoutSeconds); |
| 46 | + |
| 47 | + Assert.That(matchCount, Is.EqualTo(1)); |
| 48 | + } |
| 49 | + finally |
| 50 | + { |
| 51 | + await ExecuteNonQuery(connection, $"""DROP TABLE IF EXISTS public."{tableName}" CASCADE""", commandTimeoutSeconds, ignoreErrors: true); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + static async Task<int> ExecuteScalarInt(NpgsqlConnection connection, string sql, int commandTimeoutSeconds) |
| 56 | + { |
| 57 | + await using var command = connection.CreateCommand(); |
| 58 | + command.CommandTimeout = commandTimeoutSeconds; |
| 59 | + command.CommandText = sql; |
| 60 | + var scalar = await command.ExecuteScalarAsync(); |
| 61 | + Assert.That(scalar, Is.Not.Null); |
| 62 | + return Convert.ToInt32(scalar); |
| 63 | + } |
| 64 | + |
| 65 | + static async Task ExecuteNonQuery(NpgsqlConnection connection, string sql, int commandTimeoutSeconds, bool ignoreErrors = false) |
| 66 | + { |
| 67 | + try |
| 68 | + { |
| 69 | + await using var command = connection.CreateCommand(); |
| 70 | + command.CommandTimeout = commandTimeoutSeconds; |
| 71 | + command.CommandText = sql; |
| 72 | + await command.ExecuteNonQueryAsync(); |
| 73 | + } |
| 74 | + catch when (ignoreErrors) |
| 75 | + { |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments