|
| 1 | +using Microsoft.Data.Sqlite; |
| 2 | +using System.ComponentModel.DataAnnotations.Schema; |
| 3 | + |
| 4 | +namespace Open.Database.Extensions.Generator.Tests; |
| 5 | + |
| 6 | +// The Map<T>() the query types declare themselves (DbQuery, DbQuery<TParams>, DbQueryUsing, DbQueryCall): intercepted as the extensions are, |
| 7 | +// and equal to the same type's Map(RowMap<T>.Default). |
| 8 | + |
| 9 | +/// <summary>No annotations: matched by name.</summary> |
| 10 | +public sealed class QueryRow |
| 11 | +{ |
| 12 | + public int Id { get; set; } |
| 13 | + public string Name { get; set; } = ""; |
| 14 | + public int? Rank { get; set; } |
| 15 | +} |
| 16 | + |
| 17 | +/// <summary>Every column's ordinal declared: read by position.</summary> |
| 18 | +public sealed class OrderedQueryRow |
| 19 | +{ |
| 20 | + [Column(Order = 0)] |
| 21 | + public int Id { get; set; } |
| 22 | + |
| 23 | + [Column(Order = 1)] |
| 24 | + public string Name { get; set; } = ""; |
| 25 | + |
| 26 | + [Column(Order = 2)] |
| 27 | + public int? Rank { get; set; } |
| 28 | +} |
| 29 | + |
| 30 | +public partial class ParityTests |
| 31 | +{ |
| 32 | + const string QueryRows = "select 1 as Id, 'a' as Name, 7 as Rank union all select 2, 'b', null order by Id"; |
| 33 | + const string OneQueryRow = "select @id as Id, 'a' as Name, null as Rank"; |
| 34 | + |
| 35 | + // A parameter type only this class can name, as a repository declares one. |
| 36 | + readonly record struct ById(int Id); |
| 37 | + |
| 38 | + static SqliteConnection Memory() |
| 39 | + { |
| 40 | + var connection = new SqliteConnection("Data Source=:memory:"); |
| 41 | + connection.Open(); |
| 42 | + return connection; |
| 43 | + } |
| 44 | + |
| 45 | + /// <summary>One intercepted call, one result set served by generated code, the rows of the runtime map.</summary> |
| 46 | + static void QueryParity<T>(Func<List<T>> runtime, Func<List<T>> generated, int rows) |
| 47 | + { |
| 48 | + List<T> expected = runtime(); |
| 49 | + var start = Counts.Now(); |
| 50 | + List<T> actual = generated(); |
| 51 | + Served(Counts.Now().Since(start)); |
| 52 | + Parity.SameRows(expected, actual); |
| 53 | + Assert.Equal(rows, actual.Count); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void DbQuery_definition_is_served() |
| 58 | + { |
| 59 | + using SqliteConnection connection = Memory(); |
| 60 | + DbQuery query = DbQuery.Text(QueryRows); |
| 61 | + |
| 62 | + QueryParity( |
| 63 | + () => query.Map(RowMap<QueryRow>.Default).Using(connection).ToList(), |
| 64 | + () => query.Map<QueryRow>().Using(connection).ToList(), |
| 65 | + 2); |
| 66 | + |
| 67 | + // On the value a factory returns, with no variable, and a model read by position. |
| 68 | + QueryParity( |
| 69 | + () => query.Map(RowMap<OrderedQueryRow>.Default).Using(connection).ToList(), |
| 70 | + () => DbQuery.Text(QueryRows).Map<OrderedQueryRow>().Using(connection).ToList(), |
| 71 | + 2); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void A_definition_mapped_once_serves_every_call() |
| 76 | + { |
| 77 | + using SqliteConnection connection = Memory(); |
| 78 | + var start = Counts.Now(); |
| 79 | + MappedDbQuery<QueryRow> mapped = DbQuery.Text(QueryRows).Map<QueryRow>(); |
| 80 | + Assert.Equal(2, mapped.Using(connection).ToList().Count); |
| 81 | + Assert.Equal(2, mapped.Using(connection).ToList().Count); |
| 82 | + Counts c = Counts.Now().Since(start); |
| 83 | + Assert.True(c.Intercepted == 1 && c.Served == 2 && c.Declined == 0, c.ToString()); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void Typed_definition_with_a_private_parameter_type_is_served() |
| 88 | + { |
| 89 | + using SqliteConnection connection = Memory(); |
| 90 | + DbQuery<ById> query = DbQuery.Text(OneQueryRow).Params<ById>(); |
| 91 | + |
| 92 | + QueryParity( |
| 93 | + () => query.Map(RowMap<QueryRow>.Default).Using(connection).AddParams(new(3)).ToList(), |
| 94 | + () => query.Map<QueryRow>().Using(connection).AddParams(new(3)).ToList(), |
| 95 | + 1); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void Typed_definition_inside_a_generic_method_is_served() |
| 100 | + { |
| 101 | + using SqliteConnection connection = Memory(); |
| 102 | + |
| 103 | + QueryParity( |
| 104 | + () => DbQuery.Text(OneQueryRow).Params<ById>().Map(RowMap<QueryRow>.Default).Using(connection).AddParams(new(4)).ToList(), |
| 105 | + () => MapTyped(DbQuery.Text(OneQueryRow).Params<ById>()).Using(connection).AddParams(new(4)).ToList(), |
| 106 | + 1); |
| 107 | + |
| 108 | + static MappedDbQuery<TValues, QueryRow> MapTyped<TValues>(DbQuery<TValues> query) |
| 109 | + where TValues : struct, IEquatable<TValues> |
| 110 | + => query.Map<QueryRow>(); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void DbQueryUsing_is_served_from_a_value_a_variable_and_an_in_parameter() |
| 115 | + { |
| 116 | + using SqliteConnection connection = Memory(); |
| 117 | + DbQuery query = DbQuery.Text(QueryRows); |
| 118 | + DbQueryUsing bound = query.Using(connection); |
| 119 | + List<QueryRow> Runtime() => query.Using(connection).Map(RowMap<QueryRow>.Default).ToList(); |
| 120 | + |
| 121 | + QueryParity(Runtime, () => query.Using(connection).Map<QueryRow>().ToList(), 2); |
| 122 | + QueryParity(Runtime, () => bound.Map<QueryRow>().ToList(), 2); |
| 123 | + QueryParity(Runtime, () => FromIn(bound), 2); |
| 124 | + |
| 125 | + static List<QueryRow> FromIn(in DbQueryUsing source) => source.Map<QueryRow>().ToList(); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void DbQueryCall_is_served_from_a_value_and_a_variable() |
| 130 | + { |
| 131 | + using SqliteConnection connection = Memory(); |
| 132 | + DbQuery query = DbQuery.Text(OneQueryRow); |
| 133 | + DbQueryCall call = query.Using(connection).AddParam("id", 5); |
| 134 | + List<QueryRow> Runtime() => query.Using(connection).AddParam("id", 5).Map(RowMap<QueryRow>.Default).ToList(); |
| 135 | + |
| 136 | + QueryParity(Runtime, () => query.Using(connection).AddParam("id", 5).Map<QueryRow>().ToList(), 1); |
| 137 | + QueryParity(Runtime, () => call.Map<QueryRow>().ToList(), 1); |
| 138 | + } |
| 139 | + |
| 140 | + [Fact] |
| 141 | + public void A_default_bound_query_throws_as_the_runtime_map_does() |
| 142 | + { |
| 143 | + Parity.SameError( |
| 144 | + Catch(() => default(DbQueryUsing).Map(RowMap<QueryRow>.Default)), |
| 145 | + Catch(() => default(DbQueryUsing).Map<QueryRow>())); |
| 146 | + Parity.SameError( |
| 147 | + Catch(() => default(DbQueryCall).Map(RowMap<QueryRow>.Default)), |
| 148 | + Catch(() => default(DbQueryCall).Map<QueryRow>())); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public void A_single_value_on_a_query_is_not_intercepted() |
| 153 | + { |
| 154 | + using SqliteConnection connection = Memory(); |
| 155 | + var start = Counts.Now(); |
| 156 | + Assert.Equal([1, 2], DbQuery.Text(QueryRows).Map<int>().Using(connection).ToList()); |
| 157 | + Assert.Equal([1, 2], DbQuery.Text(QueryRows).Using(connection).Map<int>().ToList()); |
| 158 | + Assert.Equal(0, Counts.Now().Since(start).Intercepted); |
| 159 | + } |
| 160 | +} |
0 commit comments