|
5 | 5 | using System.Text.Json.Serialization;
|
6 | 6 | using System.Threading.Tasks;
|
7 | 7 | using NUnit.Framework;
|
| 8 | +using static Npgsql.Tests.TestUtil; |
8 | 9 |
|
9 | 10 | // ReSharper disable MethodHasAsyncOverload
|
10 | 11 |
|
@@ -380,4 +381,72 @@ public async Task ConfigureJsonOptions_is_order_independent()
|
380 | 381 | Assert.That(reader.GetFieldValue<Test>(0).Id, Is.EqualTo(1));
|
381 | 382 | }
|
382 | 383 | }
|
| 384 | + |
| 385 | + [Test] |
| 386 | + public async Task ReloadTypes([Values] bool async) |
| 387 | + { |
| 388 | + await using var adminConnection = await OpenConnectionAsync(); |
| 389 | + var type = await GetTempTypeName(adminConnection); |
| 390 | + |
| 391 | + var dataSourceBuilder = CreateDataSourceBuilder(); |
| 392 | + dataSourceBuilder.MapEnum<Mood>(type); |
| 393 | + await using var dataSource = dataSourceBuilder.Build(); |
| 394 | + |
| 395 | + await using var connection = await dataSource.OpenConnectionAsync(); |
| 396 | + await connection.ExecuteNonQueryAsync($"CREATE TYPE {type} AS ENUM ('sad', 'ok', 'happy')"); |
| 397 | + |
| 398 | + if (async) |
| 399 | + await dataSource.ReloadTypesAsync(); |
| 400 | + else |
| 401 | + dataSource.ReloadTypes(); |
| 402 | + |
| 403 | + Assert.ThrowsAsync<InvalidCastException>(async () => await connection.ExecuteScalarAsync($"SELECT 'happy'::{type}")); |
| 404 | + |
| 405 | + // Close connection and reopen to make sure it picks up the new type and mapping from the data source |
| 406 | + await connection.CloseAsync(); |
| 407 | + await connection.OpenAsync(); |
| 408 | + |
| 409 | + Assert.DoesNotThrowAsync(async () => await connection.ExecuteScalarAsync($"SELECT 'happy'::{type}")); |
| 410 | + } |
| 411 | + |
| 412 | + [Test] |
| 413 | + public async Task ReloadTypes_across_data_sources([Values] bool async) |
| 414 | + { |
| 415 | + await using var adminConnection = await OpenConnectionAsync(); |
| 416 | + var type = await GetTempTypeName(adminConnection); |
| 417 | + |
| 418 | + var dataSourceBuilder = CreateDataSourceBuilder(); |
| 419 | + dataSourceBuilder.MapEnum<Mood>(type); |
| 420 | + await using var dataSource1 = dataSourceBuilder.Build(); |
| 421 | + await using var connection1 = await dataSource1.OpenConnectionAsync(); |
| 422 | + |
| 423 | + await using var dataSource2 = dataSourceBuilder.Build(); |
| 424 | + await using var connection2 = await dataSource2.OpenConnectionAsync(); |
| 425 | + |
| 426 | + await connection1.ExecuteNonQueryAsync($"CREATE TYPE {type} AS ENUM ('sad', 'ok', 'happy')"); |
| 427 | + |
| 428 | + if (async) |
| 429 | + await dataSource1.ReloadTypesAsync(); |
| 430 | + else |
| 431 | + dataSource1.ReloadTypes(); |
| 432 | + |
| 433 | + Assert.ThrowsAsync<InvalidCastException>(async () => await connection1.ExecuteScalarAsync($"SELECT 'happy'::{type}")); |
| 434 | + Assert.ThrowsAsync<InvalidCastException>(async () => await connection2.ExecuteScalarAsync($"SELECT 'happy'::{type}")); |
| 435 | + |
| 436 | + // Close connection and reopen to check that the new type and mapping is not available in dataSource2 |
| 437 | + await connection2.CloseAsync(); |
| 438 | + await connection2.OpenAsync(); |
| 439 | + |
| 440 | + Assert.ThrowsAsync<InvalidCastException>(async () => await connection2.ExecuteScalarAsync($"SELECT 'happy'::{type}")); |
| 441 | + |
| 442 | + await dataSource2.ReloadTypesAsync(); |
| 443 | + |
| 444 | + // Close connection2 and reopen to make sure it picks up the new type and mapping from dataSource2 |
| 445 | + await connection2.CloseAsync(); |
| 446 | + await connection2.OpenAsync(); |
| 447 | + |
| 448 | + Assert.DoesNotThrowAsync(async () => await connection2.ExecuteScalarAsync($"SELECT 'happy'::{type}")); |
| 449 | + } |
| 450 | + |
| 451 | + enum Mood { Sad, Ok, Happy } |
383 | 452 | }
|
0 commit comments