public void Setup() { // Arrange // Register Mapping profile for AutoMapper var mappingConfig = new MapperConfiguration(mc => { mc.AddProfile(new MappingProfile()); }); mapper = mappingConfig.CreateMapper(); fakeUserRepository = A.Fake <ISearchUserRepository>(); fakeSearchDetail = A.Fake <ISearchDetail>(); fakeLogger = A.Fake <Logger <SearchUserController> >(); fakeMemoryCache = A.Fake <MemoryCache>(); userList = new List <User>(); stateList = new List <State>(); stateList.Add(new State() { Code = "NY", Name = "New York" }); stateList.Add(new State() { Code = "NJ", Name = "New Jersey" });; }
public void CleanUp() { context.Database.EnsureDeleted(); context = null; searchSearchUserRepository = null; options = null; searchDetail = null; }
/// <summary> /// Default Constructor /// </summary> /// <param name="searchUserRepository">IUserRepository</param> /// <param name="mapper">IMapper</param> public SearchUserController(ISearchUserRepository searchUserRepository, IMapper mapper, ISearchDetail searchDetail, IMemoryCache memoryCache, ILogger <SearchUserController> log) { this._searchUserRepository = searchUserRepository; this._mapper = mapper; this._memoryCache = memoryCache; this._searchDetail = searchDetail; this._log = log; }
public void CleanUp() { mapper = null; userList = null; fakeUserRepository = null; fakeSearchDetail = null; searchUserController = null; actionResult = null; okResult = null; fakeLogger = null; fakeMemoryCache = null; stateList = null; }
public void SetUp() { // Configure in memory database options = new DbContextOptionsBuilder <UserDbContext>() .UseInMemoryDatabase(databaseName: "UserDB") .Options; searchDetail = A.Fake <ISearchDetail>(); // Insert seed data into the database using one instance of the context context = new UserDbContext(options); context.User.Add(new User() { Age = 27, Email = "*****@*****.**", FirstName = "John", LastName = "B", ID = 1, StateCode = "NJ" }); context.User.Add(new User() { Age = 37, Email = "*****@*****.**", FirstName = "A", LastName = "B", ID = 2, StateCode = "NJ" }); context.User.Add(new User() { Age = 45, Email = "*****@*****.**", FirstName = "BCD", LastName = "", ID = 3, StateCode = "NY" }); context.User.Add(new User() { Age = 20, Email = "*****@*****.**", FirstName = "Amy", LastName = "K", ID = 4, StateCode = "NJ" }); context.User.Add(new User() { Age = 72, Email = "*****@*****.**", FirstName = "Mac", LastName = "B", ID = 5, StateCode = "NJ" }); context.User.Add(new User() { Age = 97, Email = "*****@*****.**", FirstName = "Ory", LastName = "A", ID = 6, StateCode = "NY" }); context.User.Add(new User() { Age = 25, Email = "*****@*****.**", FirstName = "Jim", LastName = "K", ID = 7, StateCode = "CO" }); context.SaveChanges(); searchSearchUserRepository = new SearchUserRepository(context); fakeMemoryCache = A.Fake <MemoryCache>(); }
/// <summary> /// Gets filtered users based on StateCode or Age Range with conditions such as And and OR /// </summary> /// <param name="ISearchDetail">SearchDetail</param> /// <returns>Returns filtered users matching criteria such as State Code , Age Range</returns> public async Task <List <User> > GetFilteredUsers(ISearchDetail searchDetail) { List <User> usersList = null; switch (searchDetail.SearchCriteriaEnum) { case SearchCriteria.State: { if (string.IsNullOrEmpty(searchDetail.StateCode)) { throw new APIException("Search condition is invalid:: State"); } usersList = await _context.User.AsNoTracking().Where(x => x.StateCode.ToUpper() == searchDetail.StateCode.ToUpper()).ToListAsync(); break; } case SearchCriteria.AgeRange: { if (searchDetail.FromAge < 0 || searchDetail.ToAge < 0 || searchDetail.ToAge < searchDetail.FromAge) { throw new APIException("Search condition is invalid:: AgeRange"); } else { usersList = await _context.User.AsNoTracking().Where(x => (x.Age >= searchDetail.FromAge && x.Age <= searchDetail.ToAge)).ToListAsync(); } break; } case SearchCriteria.StateAndAgeRange: { if ((string.IsNullOrEmpty(searchDetail.StateCode))) { throw new APIException("Search condition is invalid:: State"); } else if (searchDetail.FromAge < 0 || searchDetail.ToAge < 0 || searchDetail.ToAge < searchDetail.FromAge) { throw new APIException("Search condition is invalid:: AgeRange"); } usersList = await _context.User.AsNoTracking().Where(x => (x.Age >= searchDetail.FromAge && x.Age <= searchDetail.ToAge) && (x.StateCode.ToUpper() == searchDetail.StateCode.ToUpper())).ToListAsync(); break; } case SearchCriteria.StateOrAgeRange: { if ((string.IsNullOrEmpty(searchDetail.StateCode))) { throw new APIException("Search condition is invalid:: State"); } else if (searchDetail.FromAge < 0 || searchDetail.ToAge < 0 || searchDetail.ToAge < searchDetail.FromAge) { throw new APIException("Search condition is invalid:: AgeRange"); } usersList = await _context.User.AsNoTracking().Where(x => (x.Age >= searchDetail.FromAge && x.Age <= searchDetail.ToAge) || (x.StateCode.ToUpper() == searchDetail.StateCode.ToUpper())).ToListAsync(); break; } } return(usersList); }