/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * * Permission is hereby granted, free of charge, to any person obtaining a copy of this * software and associated documentation files (the "Software"), to deal in the Software * without restriction, including without limitation the rights to use, copy, modify, * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System; using System.Collections; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Linq.Expressions; namespace UnicornShopLegacy.Tests { internal class FakeDbSet : DbSet, IDbSet where T : class { private readonly List data; public FakeDbSet() { this.data = new List(); } Expression IQueryable.Expression { get { return this.data.AsQueryable().Expression; } } IQueryProvider IQueryable.Provider { get { return this.data.AsQueryable().Provider; } } public List Local { get { return this.data; } } Type IQueryable.ElementType { get { return this.data.AsQueryable().ElementType; } } public override T Find(params object[] keyValues) { throw new NotImplementedException("Derive from FakeDbSet and override Find"); } public override T Add(T item) { this.data.Add(item); return item; } public override T Remove(T item) { this.data.Remove(item); return item; } public override T Attach(T item) { return null; } public T Detach(T item) { this.data.Remove(item); return item; } public override T Create() { return Activator.CreateInstance(); } public TDerivedEntity Create() where TDerivedEntity : class, T { return Activator.CreateInstance(); } public override IEnumerable AddRange(IEnumerable entities) { this.data.AddRange(entities); return this.data; } public override IEnumerable RemoveRange(IEnumerable entities) { for (int i = entities.Count() - 1; i >= 0; i--) { T entity = entities.ElementAt(i); if (this.data.Contains(entity)) { this.Remove(entity); } } return this; } IEnumerator IEnumerable.GetEnumerator() { return this.data.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return this.data.GetEnumerator(); } } }