| | | 1 | | using ArturRios.Data.Relational.Core.Entities; |
| | | 2 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 3 | | using ArturRios.Data.Relational.Core.Repositories; |
| | | 4 | | using ArturRios.Output; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Util.Test.Mock; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// In-memory implementation of <see cref="IRepository{T, TKey}"/> for use in tests. |
| | | 10 | | /// Entities are stored in a backing list. Outcomes match <c>EfRepository<T, TKey></c>, so code tested against the |
| | | 11 | | /// fake meets the same results in production: a lookup that finds nothing succeeds with no data; an update or delete |
| | | 12 | | /// that would affect no row — an unknown id, or an <see cref="IVersionedEntity"/> whose |
| | | 13 | | /// <see cref="IVersionedEntity.ConcurrencyStamp"/> is stale — fails with <see cref="RelationalErrors.ConcurrencyMessage |
| | | 14 | | /// and changes nothing; and an update issues a new stamp, as the real context does. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// <para>How <see cref="Create"/> and <see cref="CreateRange"/> assign identifiers depends on the key type:</para> |
| | | 18 | | /// <list type="bullet"> |
| | | 19 | | /// <item><description><see cref="long"/> and <see cref="int"/>: sequentially, starting at <c>1</c>.</description></item |
| | | 20 | | /// <item><description><see cref="Guid"/>: <see cref="Guid.NewGuid"/>.</description></item> |
| | | 21 | | /// <item><description> |
| | | 22 | | /// Any other key type (e.g. <see cref="string"/>): the caller assigns the identifier before creating the entity; |
| | | 23 | | /// creating one whose identifier is unset, or already stored, returns a failed output. |
| | | 24 | | /// </description></item> |
| | | 25 | | /// </list> |
| | | 26 | | /// <para> |
| | | 27 | | /// When a generator applies, it overwrites any identifier the caller set. Pass an id generator to the constructor |
| | | 28 | | /// to replace the default for any key type. |
| | | 29 | | /// </para> |
| | | 30 | | /// </remarks> |
| | | 31 | | /// <typeparam name="T">The entity type handled by the repository.</typeparam> |
| | | 32 | | /// <typeparam name="TKey">The entity's primary key type.</typeparam> |
| | | 33 | | public class FakeRepository<T, TKey> : IRepository<T, TKey> where T : Entity<TKey> where TKey : IEquatable<TKey> |
| | | 34 | | { |
| | | 35 | | private readonly FakeEntityStore<T, TKey> _store; |
| | | 36 | | |
| | | 37 | | /// <summary>Creates an empty repository that assigns identifiers with the default for <typeparamref name="TKey"/>.< |
| | 60 | 38 | | public FakeRepository() : this(null) { } |
| | | 39 | | |
| | | 40 | | /// <summary>Creates an empty repository that assigns identifiers with <paramref name="idGenerator"/>.</summary> |
| | | 41 | | /// <param name="idGenerator"> |
| | | 42 | | /// Called once per created entity to produce its identifier, or <c>null</c> to use the default for |
| | | 43 | | /// <typeparamref name="TKey"/>. |
| | | 44 | | /// </param> |
| | 66 | 45 | | public FakeRepository(Func<TKey>? idGenerator) => _store = new FakeEntityStore<T, TKey>(idGenerator); |
| | | 46 | | |
| | | 47 | | /// <summary>Exposes the stored entities as a queryable sequence.</summary> |
| | | 48 | | /// <returns>A queryable over every stored entity.</returns> |
| | 12 | 49 | | public IQueryable<T> Query() => _store.Items.AsQueryable(); |
| | | 50 | | |
| | | 51 | | /// <summary>Returns all stored entities.</summary> |
| | | 52 | | /// <returns>A successful output whose data is a snapshot of every stored entity, unaffected by later writes.</retur |
| | 4 | 53 | | public DataOutput<IEnumerable<T>> GetAll() => _store.GetAll(); |
| | | 54 | | |
| | | 55 | | /// <summary>Returns the entity with the given identifier.</summary> |
| | | 56 | | /// <param name="id">The identifier to look up.</param> |
| | | 57 | | /// <returns> |
| | | 58 | | /// A successful output carrying the matching entity, or a successful output with no data when no stored entity |
| | | 59 | | /// has that identifier. |
| | | 60 | | /// </returns> |
| | 16 | 61 | | public DataOutput<T?> GetById(TKey id) => _store.GetById(id); |
| | | 62 | | |
| | | 63 | | /// <summary>Adds <paramref name="entity"/> to the store, assigning its identifier as described on the class.</summa |
| | | 64 | | /// <param name="entity">The entity to store.</param> |
| | | 65 | | /// <returns> |
| | | 66 | | /// A successful output carrying the entity's identifier, or a failed output when the identifier is unset (for |
| | | 67 | | /// key types without a generator) or already stored (<see cref="RelationalErrors.UniqueViolationMessage"/>). |
| | | 68 | | /// </returns> |
| | 32 | 69 | | public DataOutput<TKey> Create(T entity) => _store.Create(entity); |
| | | 70 | | |
| | | 71 | | /// <summary>Adds every entity in <paramref name="entities"/> to the store, assigning identifiers as described on th |
| | | 72 | | /// <param name="entities">The entities to store.</param> |
| | | 73 | | /// <returns> |
| | | 74 | | /// A successful output carrying the identifiers, in insertion order, or a failed output when any identifier is unse |
| | | 75 | | /// or duplicated — in which case nothing is stored. |
| | | 76 | | /// </returns> |
| | 7 | 77 | | public DataOutput<IEnumerable<TKey>> CreateRange(IEnumerable<T> entities) => _store.CreateRange(entities); |
| | | 78 | | |
| | | 79 | | /// <summary>Copies the writable properties of <paramref name="entity"/> onto the stored entity with the same identi |
| | | 80 | | /// <param name="entity">The entity carrying the new values.</param> |
| | | 81 | | /// <returns> |
| | | 82 | | /// A successful output carrying the updated stored entity, or a failed output carrying |
| | | 83 | | /// <see cref="RelationalErrors.ConcurrencyMessage"/> when no stored entity has a matching identifier (and, for an |
| | | 84 | | /// <see cref="IVersionedEntity"/>, concurrency stamp). |
| | | 85 | | /// </returns> |
| | 6 | 86 | | public DataOutput<T> Update(T entity) => _store.Update(entity); |
| | | 87 | | |
| | | 88 | | /// <summary>Updates every entity in <paramref name="entities"/>, all or none: when any of them would update no stor |
| | | 89 | | /// (see the single-entity update), nothing is updated.</summary> |
| | | 90 | | /// <param name="entities">The entities carrying the new values.</param> |
| | | 91 | | /// <returns> |
| | | 92 | | /// A successful output carrying the updated stored entities, or a failed output carrying |
| | | 93 | | /// <see cref="RelationalErrors.ConcurrencyMessage"/>. |
| | | 94 | | /// </returns> |
| | 2 | 95 | | public DataOutput<IEnumerable<T>> UpdateRange(IEnumerable<T> entities) => _store.UpdateRange(entities); |
| | | 96 | | |
| | | 97 | | /// <summary>Removes the stored entity with the same identifier as <paramref name="entity"/>.</summary> |
| | | 98 | | /// <param name="entity">The entity to remove.</param> |
| | | 99 | | /// <returns> |
| | | 100 | | /// A successful output carrying the identifier of the removed entity, or a failed output carrying |
| | | 101 | | /// <see cref="RelationalErrors.ConcurrencyMessage"/> when no stored entity has a matching identifier (and, for an |
| | | 102 | | /// <see cref="IVersionedEntity"/>, concurrency stamp). |
| | | 103 | | /// </returns> |
| | 7 | 104 | | public DataOutput<TKey> Delete(T entity) => _store.Delete(entity); |
| | | 105 | | |
| | | 106 | | /// <summary>Removes every stored entity whose identifier is in <paramref name="ids"/>.</summary> |
| | | 107 | | /// <param name="ids">The identifiers to remove.</param> |
| | | 108 | | /// <returns>A successful output carrying the identifiers that were actually removed.</returns> |
| | 2 | 109 | | public DataOutput<IEnumerable<TKey>> DeleteRange(IEnumerable<TKey> ids) => _store.DeleteRange(ids); |
| | | 110 | | } |