|
12 | 12 | using System.Text.Json; |
13 | 13 | using System.Threading.Tasks; |
14 | 14 | using Microsoft.Data.Sqlite; |
| 15 | +using RepoDb.Attributes; |
15 | 16 | using RepoDb.Schema; |
16 | 17 | using RepoDb.Schema.Enumerations; |
| 18 | +using RepoDb.Schema.Models; |
17 | 19 | using RepoDb.Schema.Sqlite.Microsoft; |
18 | 20 |
|
19 | 21 | namespace RepoDb.NativeAotTests |
20 | 22 | { |
21 | 23 | public static partial class Program |
22 | 24 | { |
| 25 | + [Map("Invoice")] |
| 26 | + private sealed class SchemaInvoice |
| 27 | + { |
| 28 | + public long Id { get; set; } |
| 29 | + } |
| 30 | + |
23 | 31 | /// <summary> |
24 | 32 | /// Exercises the async exporters with real catalog reads and reflection-based JSON serialization disabled. |
25 | 33 | /// </summary> |
26 | 34 | private static async Task TestSchemaDefinitionsAsync() |
27 | 35 | { |
28 | | - await RunAsync("Async schema definitions (JSON, XML, YAML; every relationship behavior)", async () => |
| 36 | + await RunAsync("Schema save/read/copy extensions (sync/async; every relationship behavior)", async () => |
29 | 37 | { |
30 | 38 | if (JsonSerializer.IsReflectionEnabledByDefault) throw new Exception("Reflection-based JSON serialization must be disabled."); |
31 | 39 | using var connection = new SqliteConnection("Data Source=:memory:"); |
@@ -58,7 +66,48 @@ Amount decimal(18,4) NOT NULL DEFAULT 0, Name nvarchar(100), |
58 | 66 | else await connection.SaveDefinitionAsYamlAsync("Invoice", filename, behavior); |
59 | 67 | if (!expected.SequenceEqual(File.ReadAllBytes(filename))) |
60 | 68 | throw new Exception("Async schema definition differs from synchronous output: " + format + "/" + behavior); |
| 69 | + SchemaDefinitionResult synchronous; |
| 70 | + SchemaDefinitionResult asynchronous; |
| 71 | + if (format == "json") |
| 72 | + { |
| 73 | + synchronous = connection.ReadDefinitionFromJson(filename); |
| 74 | + asynchronous = await connection.ReadDefinitionFromJsonAsync(filename); |
| 75 | + } |
| 76 | + else if (format == "xml") |
| 77 | + { |
| 78 | + synchronous = connection.ReadDefinitionFromXml(filename); |
| 79 | + asynchronous = await connection.ReadDefinitionFromXmlAsync(filename); |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + synchronous = connection.ReadDefinitionFromYaml(filename); |
| 84 | + asynchronous = await connection.ReadDefinitionFromYamlAsync(filename); |
| 85 | + } |
| 86 | + var count = behavior == RelatedTableBehavior.TableOnly ? 1 |
| 87 | + : behavior == RelatedTableBehavior.ParentsAndChildren ? 3 : 2; |
| 88 | + if (synchronous.Tables.Count != count || asynchronous.Tables.Count != count || |
| 89 | + !System.Text.Json.Nodes.JsonNode.DeepEquals(synchronous.Definition, asynchronous.Definition)) |
| 90 | + throw new Exception("Schema readers did not preserve the saved definition."); |
| 91 | + var invoice = asynchronous.Tables.Single(table => table.Table.Name == "Invoice"); |
| 92 | + if (invoice.Columns.Count != 4 || invoice.PrimaryKey == null || invoice.Indexes.Count != 1 || |
| 93 | + invoice.CheckConstraints.Count != 1 || invoice.ForeignKeys.Count != (behavior == RelatedTableBehavior.TableOnly ? 0 : 1)) |
| 94 | + throw new Exception("Schema readers did not reconstruct the table metadata."); |
61 | 95 | } |
| 96 | + |
| 97 | + SchemaComposerMapper.Add<SqliteConnection>(new SqliteSchemaComposer(), true); |
| 98 | + using var target = new SqliteConnection("Data Source=:memory:"); |
| 99 | + await target.OpenAsync(); |
| 100 | + var copy = connection.CopyTableSchemaTo<SchemaInvoice>(target, relationshipBehavior: behavior); |
| 101 | + var copyAsync = await connection.CopyTableSchemaToAsync<SchemaInvoice>(target, |
| 102 | + tableExistenceBehavior: TableExistsBehavior.Drop, relationshipBehavior: behavior); |
| 103 | + if (copy.Errors.Count != 0 || copyAsync.Errors.Count != 0 || copy.Items.Count != copyAsync.Items.Count) |
| 104 | + throw new Exception("Generic schema copy failed."); |
| 105 | + var multiple = connection.CopyTableSchemaTo(new[] { "Invoice" }, target, |
| 106 | + tableExistenceBehavior: TableExistsBehavior.Skip, relationshipBehavior: behavior); |
| 107 | + var multipleAsync = await connection.CopyTableSchemaToAsync(new[] { "Invoice" }, target, |
| 108 | + tableExistenceBehavior: TableExistsBehavior.Skip, relationshipBehavior: behavior); |
| 109 | + if (multiple.Errors.Count != 0 || multipleAsync.Errors.Count != 0) |
| 110 | + throw new Exception("Multiple-table schema copy failed."); |
62 | 111 | } |
63 | 112 | } |
64 | 113 | finally |
|
0 commit comments