public void ThrowArgumentException_IfEfRepositoyryIsNull() { EfGenericRepository <User> carRepo = null; Assert.Throws <ArgumentException>(() => new UsersService(carRepo)); Assert.IsNull(carRepo); }
public void RepositoryGetById() { //arrange var connection = DbConnectionFactory.CreateTransient(); var stubDB = new GroceryStoreContext(connection); var userRepo = new EfGenericRepository <User>(stubDB); var userToAdd = new User { FirstName = "Pesho", LastName = "Pesho", Username = "******", Password = "******", PhoneNumber = "Pesho" }; userRepo.Add(userToAdd); stubDB.SaveChanges(); //act var actual = userRepo.GetById(1); //assert Assert.AreEqual(userToAdd.Username, actual.Username); }
public void ThrowArgumentNullExceptionWhenPassedEntityIsNull() { //Arrange var pages = new List <Page> { new Page() { Id = Guid.NewGuid(), Title = "page", Content = "cntnt" }, new Page() { Id = Guid.NewGuid(), Title = "page", Content = "cntnt" } }; var mockedDbContext = new Mock <ISotnWikiDbContext>(); var mockedPageSet = QueryableDbSetMock.GetQueryableMockDbSet <Page>(pages); mockedDbContext.Setup(c => c.Set <Page>()).Returns(mockedPageSet); mockedDbContext.Setup(c => c.Pages).Returns(mockedPageSet); string expectedExceptionMessage = "entity"; var repositoryUnderTest = new EfGenericRepository <Page>(mockedDbContext.Object); //Act & Assert var exc = Assert.Throws <ArgumentNullException>(() => repositoryUnderTest.Add(null)); //Assert StringAssert.Contains(expectedExceptionMessage, exc.Message); }
public ArtistController() { var db = new ArtistsDbContext(); this.artistData = new EfGenericRepository <Artist>(db); this.countryData = new EfGenericRepository <Country>(db); }
public void RepositoryUpdate() { //arrange var connection = DbConnectionFactory.CreateTransient(); var stubDB = new GroceryStoreContext(connection); var userRepo = new EfGenericRepository <User>(stubDB); var user = new User { FirstName = "Pesho", LastName = "Pesho", Username = "******", Password = "******", PhoneNumber = "Pesho" }; userRepo.Add(user); stubDB.SaveChanges(); //act user.Password = "******"; userRepo.Update(user); string actual = userRepo.GetById(1).Password; //assert Assert.AreEqual("newPassword", actual); }
public void Delete_ShouldDeleteItemFromCollection() { // Arrange var item1 = new Item(); var item2 = new Item(); var item3 = new Item(); var itemsCollection = new List <Item>(); this.itemsDbSet.Setup(i => i.Add(It.IsAny <Item>())) .Callback <Item>(i => itemsCollection.Add(i)); this.itemsDbSet.Setup(i => i.Remove(It.IsAny <Item>())) .Callback <Item>(i => itemsCollection.Remove(i)); this.context.Setup(c => c.Set <Item>()).Returns(this.itemsDbSet.Object); this.repo = new EfGenericRepository <Item>(this.context.Object); // Act this.repo.Add(item1); this.repo.Add(item2); this.repo.Add(item3); this.repo.Delete(item2); // Assert Assert.AreEqual(item1, itemsCollection[0]); Assert.AreEqual(item3, itemsCollection[1]); }
public static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<ErisSystemContext, Data.Migrations.EfConfiguration>()); Importer.ImportCountries(); var db = new ErisSystemContext(); var date = new DateTime(1991, 01, 01); var country = db.Countries.Find(3); var hitman = new User(); hitman.AboutMe = "Thug life"; hitman.UserName = "******"; hitman.PasswordHash = "ASFADSFDEFE@#@$@$@ASDFAS"; hitman.DateOfBirth = date; hitman.CountriesOfOperation.Add(country); db.Users.AddOrUpdate(hitman); db.SaveChanges(); var repositoryTest = new EfGenericRepository<User>(db); var hitmen = repositoryTest.All(); foreach (var x in hitmen) { Console.WriteLine(x.UserName); Console.WriteLine(x.Gender); Console.WriteLine(x.AboutMe); Console.WriteLine(x.DateOfBirth); } }
public void Add_ShouldAddItemToCollection_CaseManyObjects() { // Arrange var item1 = new Item(); var item2 = new Item(); var item3 = new Item(); var itemsCollection = new List <Item>(); this.itemsDbSet.Setup(i => i.Add(It.IsAny <Item>())) .Callback <Item>(i => itemsCollection.Add(i)); this.context.Setup(c => c.Set <Item>()).Returns(this.itemsDbSet.Object); this.repo = new EfGenericRepository <Item>(this.context.Object); // Act this.repo.Add(item1); this.repo.Add(item2); this.repo.Add(item3); // Assert Assert.AreEqual(item1, itemsCollection[0]); Assert.AreEqual(item2, itemsCollection[1]); Assert.AreEqual(item3, itemsCollection[2]); }
public static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion <ErisSystemContext, Data.Migrations.EfConfiguration>()); Importer.ImportCountries(); var db = new ErisSystemContext(); var date = new DateTime(1991, 01, 01); var country = db.Countries.Find(3); var hitman = new User(); hitman.AboutMe = "Thug life"; hitman.UserName = "******"; hitman.PasswordHash = "ASFADSFDEFE@#@$@$@ASDFAS"; hitman.DateOfBirth = date; hitman.CountriesOfOperation.Add(country); db.Users.AddOrUpdate(hitman); db.SaveChanges(); var repositoryTest = new EfGenericRepository <User>(db); var hitmen = repositoryTest.All(); foreach (var x in hitmen) { Console.WriteLine(x.UserName); Console.WriteLine(x.Gender); Console.WriteLine(x.AboutMe); Console.WriteLine(x.DateOfBirth); } }
public void EfGenericRepository_ShouldCreateUserIRepository_WhenThePassedParametersAreValid() { var context = new Mock <ICarsSystemDbContext>(); var repository = new EfGenericRepository <User>(context.Object); Assert.IsInstanceOf <IRepository <User> >(repository); }
public CommentsService() { var data = new SimpleBlogSystemDbContext(); this.posts = new EfGenericRepository<Post>(data); this.comments = new EfGenericRepository<Comment>(data); this.users = new EfGenericRepository<User>(data); }
public PostsService() { var data = new SimpleBlogSystemDbContext(); this.posts = new EfGenericRepository<Post>(data); this.categories = new EfGenericRepository<Category>(data); this.users = new EfGenericRepository<User>(data); }
public void CreateCorrectlyUsersService_IfEfRepositoyryIsNotNull() { CarsSystemDbContext context = new CarsSystemDbContext(); EfGenericRepository <User> carRepo = new EfGenericRepository <User>(context); Assert.DoesNotThrow(() => new UsersService(carRepo)); Assert.IsNotNull(carRepo); }
public void CreateInstance_OfTypeIUsersService() { CarsSystemDbContext context = new CarsSystemDbContext(); EfGenericRepository <User> carRepo = new EfGenericRepository <User>(context); var usersService = new UsersService(carRepo); Assert.IsInstanceOf <IUsersService>(usersService); }
public void ThrowArgumentException_IfEfRepositoryIsNull() { // Arrange EfGenericRepository <Car> carRepo = null; // Act & Assert Assert.Throws <ArgumentException>(() => new CarsService(carRepo)); Assert.IsNull(carRepo); }
public void CreateCorrectlyCarsService_IfEfRepositoryIsNotNull() { // Arrange CarsSystemDbContext context = new CarsSystemDbContext(); EfGenericRepository <Car> carRepo = new EfGenericRepository <Car>(context); // Act & Assert Assert.DoesNotThrow(() => new CarsService(carRepo)); Assert.IsNotNull(carRepo); }
public void CreateCarIRepository_WhenThePassedParametersAreValid() { // Arrange var context = new Mock <ICarsSystemDbContext>(); // Act var repository = new EfGenericRepository <Car>(context.Object); // Assert Assert.IsInstanceOf <IEfGenericRepository <Car> >(repository); }
public void GetAll_OrderedByPrice_ShouldReturnCorrectResult() { // Arrange var item1 = new Item() { Id = 1, MainMaterial = MainMaterialType.Alloy, MainColour = MainColourType.Red, Price = 10 }; var item2 = new Item() { Id = 2, MainMaterial = MainMaterialType.Swarovski, MainColour = MainColourType.Blue, Price = 14 }; var item3 = new Item() { Id = 3, MainMaterial = MainMaterialType.Swarovski, MainColour = MainColourType.Golden, Price = 15 }; var item4 = new Item() { Id = 4, MainMaterial = MainMaterialType.Alloy, MainColour = MainColourType.Red, Price = 1000 }; var item5 = new Item() { Id = 5, MainMaterial = MainMaterialType.PinkGold, MainColour = MainColourType.Blue, Price = 250 }; var item6 = new Item() { Id = 6, MainMaterial = MainMaterialType.Alloy, MainColour = MainColourType.Red, Price = 5 }; var itemsCollection = new List <Item>() { item1, item2, item3, item4, item5, item6 }; var queryData = itemsCollection.AsQueryable(); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.Provider).Returns(queryData.Provider); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.Expression).Returns(queryData.Expression); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.ElementType).Returns(queryData.ElementType); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.GetEnumerator()).Returns(queryData.GetEnumerator()); this.context.Setup(c => c.Set <Item>()).Returns(this.itemsDbSet.Object); this.repo = new EfGenericRepository <Item>(this.context.Object); // Act var actualFiltered = this.repo.GetAll(null, i => i.Price).ToList(); var expectedFiltered = new List <Item>() { item6, item1, item2, item3, item5, item4 }; // Assert CollectionAssert.AreEqual(actualFiltered, expectedFiltered); }
public void ConstructorForOrderRepository_ShouldSetDbSet_WhenContextIsNotNull() { // Arange var context = new Mock <IFancyDbContext>(); // Act var efGenericRepository = new EfGenericRepository <Item>(context.Object); // Assert context.Verify(c => c.Set <Item>(), Times.Once); }
public void ThrowArgumentNullExceptionWhitCorrectMessage_WhenEntityParameterIsNull() { // Arrange var dbContext = new Mock <ICarAdvertsSystemDbContext>(); var efGenericRepository = new EfGenericRepository <MockDbModel>(dbContext.Object); MockDbModel entity = null; // Act and Assert Assert.That( () => efGenericRepository.Add(entity), Throws.InstanceOf <ArgumentNullException>().With.Message.Contains(nameof(entity))); }
public void CreateInstance_OfTypeICarsService() { // Arrange CarsSystemDbContext context = new CarsSystemDbContext(); EfGenericRepository <Car> carRepo = new EfGenericRepository <Car>(context); // Act var carsService = new CarsService(carRepo); // Assert Assert.IsInstanceOf <ICarsService>(carsService); }
public IRepository <T> Get <T>() where T : class { var key = typeof(T).FullName; if (!this.repos.ContainsKey(key)) { var repo = new EfGenericRepository <T>(this.DbContext); this.repos.Add(key, repo); } return((IRepository <T>) this.repos[key]); }
public IRepository <TEntity> GetRepository <TEntity>() where TEntity : class { if (repositories.Keys.Contains(typeof(TEntity))) { return(repositories[typeof(TEntity)] as IRepository <TEntity>); } var repository = new EfGenericRepository <TEntity>(ctx); repositories.Add(typeof(TEntity), repository); return(repository); }
public void ConstructorShould_ReturnNewInstanceOfGenericRepo_IfParamsAreValid() { // Arrange var mockedContext = new Mock <ICarAdvertsSystemDbContext>(); var mockedModel = new Mock <DbSet <IAdvert> >(); mockedContext.Setup(x => x.Set <IAdvert>()).Returns(mockedModel.Object); // Act var repository = new EfGenericRepository <IAdvert>(mockedContext.Object); // Assert Assert.That(repository, Is.Not.Null); }
public void GetFirstOrDefault_ShouldReturnFirstResult_IfManyItemsArePresentInDatabase() { // Arrange var id1 = 1; var id2 = 2; var id3 = 3; var item1 = new Item() { Id = id1 }; var item2 = new Item() { Id = id2 }; var item3 = new Item() { Id = id3 }; var item4 = new Item() { Id = id2 }; var item5 = new Item() { Id = id2 }; var itemsCollection = new List <Item>() { item1, item2, item3, item4, item5 }; var queryData = itemsCollection.AsQueryable(); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.Provider).Returns(queryData.Provider); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.Expression).Returns(queryData.Expression); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.ElementType).Returns(queryData.ElementType); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.GetEnumerator()).Returns(queryData.GetEnumerator()); this.context.Setup(c => c.Set <Item>()).Returns(this.itemsDbSet.Object); this.repo = new EfGenericRepository <Item>(this.context.Object); // Act var item = this.repo.GetFirstOrDefault(i => i.Id == id2); // Assert Assert.AreEqual(item, item2); }
public void InvockeDbSet_MethodFindOnce(int?id) { // Arrange var mockedDbSet = new Mock <DbSet <IAdvert> >(); var mockedDbContext = new Mock <ICarAdvertsSystemDbContext>(); mockedDbContext.Setup(mock => mock.Set <IAdvert>()).Returns(mockedDbSet.Object); var repository = new EfGenericRepository <IAdvert>(mockedDbContext.Object); repository.GetById(id); // Assert mockedDbSet.Verify(x => x.Find(It.IsAny <int?>()), Times.Once); }
public void Add_ShouldThrowArgumentNullException_WhenAddedEntityIsNull() { // Arrange Item item1 = null; var itemsCollection = new List <Item>(); this.itemsDbSet.Setup(i => i.Add(It.IsAny <Item>())) .Callback <Item>(i => itemsCollection.Add(i)); this.context.Setup(c => c.Set <Item>()).Returns(this.itemsDbSet.Object); this.repo = new EfGenericRepository <Item>(this.context.Object); this.repo.Add(item1); }
public void ThrowNullReferenceExceptionWhithCorrectMessage_WhenPassedArgumentIsNull() { // Arrange var mockedDbContext = new Mock <ICarAdvertsSystemDbContext>(); var mockedSet = new Mock <DbSet <IAdvert> >(); mockedDbContext.Setup(set => set.Set <IAdvert>()).Returns(mockedSet.Object); var repository = new EfGenericRepository <IAdvert>(mockedDbContext.Object); IAdvert entity = null; // Act and Assert Assert.That(() => repository.Delete(entity), Throws.ArgumentNullException.With.Message.Contains (nameof(entity))); }
public void ConstructorShould_ReturnCorrectDbSet_IfValidParamsPassed() { // Arrange var mockedContext = new Mock <ICarAdvertsSystemDbContext>(); var mockedModel = new Mock <DbSet <IAdvert> >(); mockedContext.Setup(x => x.Set <IAdvert>()).Returns(mockedModel.Object); // Act var repository = new EfGenericRepository <IAdvert>(mockedContext.Object); // Assert Assert.That(repository.DbSet, Is.Not.Null); Assert.That(repository.DbSet, Is.EqualTo(mockedModel.Object)); }
public void ReturnNull_WhenRequestedIdIsNull() { // Arrange var dbContext = new Mock <ICarAdvertsSystemDbContext>(); var entities = new Mock <DbSet <MockDbModel> >(); dbContext.Setup(c => c.Set <MockDbModel>()).Returns(entities.Object); var efGenericRepository = new EfGenericRepository <MockDbModel>(dbContext.Object); var fakeData = new HashSet <MockDbModel>() { new MockDbModel() { Id = 1 }, new MockDbModel() { Id = 2 }, new MockDbModel() { Id = 3 }, new MockDbModel() { Id = 4 }, new MockDbModel() { Id = 5 } }; var fakeDataQueryable = fakeData.AsQueryable(); entities.As <IQueryable>().Setup(e => e.GetEnumerator()).Returns(fakeDataQueryable.GetEnumerator()); entities.As <IQueryable>().Setup(e => e.ElementType).Returns(fakeDataQueryable.ElementType); entities.As <IQueryable>().Setup(e => e.Expression).Returns(fakeDataQueryable.Expression); entities.As <IQueryable>().Setup(e => e.Provider).Returns(fakeDataQueryable.Provider); // Act var actualReturnedTEntity = efGenericRepository.GetById(null); // Assert Assert.That(actualReturnedTEntity, Is.Null); }
public void CallMethod_GetByIdOnce() { // Arrange var mockedDbContext = new Mock <ICarAdvertsSystemDbContext>(); var mockedSet = new Mock <DbSet <IAdvert> >(); mockedDbContext.Setup(set => set.Set <IAdvert>()).Returns(mockedSet.Object); var repository = new EfGenericRepository <IAdvert>(mockedDbContext.Object); int id = 1; // Act repository.Delete(id); // Assert //Assert.That(() => repository.GetById(1), Times.Once()); }
public static ICollection <Match> GetMatchesInTwentyFourHours() { IRepository <Match> matchesContext = new EfGenericRepository <Match>(new BetsDbContext()); var startPeriod = DateTime.Now; var endPeriod = startPeriod.AddHours(24); var matches = matchesContext .All() .Where(m => DateTime.Compare(m.StartDate, startPeriod) > 0 && DateTime.Compare(m.StartDate, endPeriod) < 0) .ToList(); return(matches); }
public void GetSingle_ShouldThrowInvalidOperationException_IfNoItemsAreFound() { // Arrange var id1 = 1; var id2 = 2; var id3 = 3; var invalidId = 4; var item1 = new Item() { Id = id1 }; var item2 = new Item() { Id = id2 }; var item3 = new Item() { Id = id3 }; var itemsCollection = new List <Item>() { item1, item2, item3 }; itemsCollection.Add(item1); var queryData = itemsCollection.AsQueryable(); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.Provider).Returns(queryData.Provider); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.Expression).Returns(queryData.Expression); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.ElementType).Returns(queryData.ElementType); this.itemsDbSet.As <IQueryable <Item> >().Setup(m => m.GetEnumerator()).Returns(queryData.GetEnumerator()); this.context.Setup(c => c.Set <Item>()).Returns(this.itemsDbSet.Object); this.repo = new EfGenericRepository <Item>(this.context.Object); // Act var item = this.repo.GetSingle(i => i.Id == invalidId); // Assert Assert.AreEqual(item, item1); }
public ArtistController() { var db = new ArtistsDbContext(); this.artistData = new EfGenericRepository<Artist>(db); this.countryData = new EfGenericRepository<Country>(db); }
public CategoriesService() { this.categories = new EfGenericRepository<Category>(new SimpleBlogSystemDbContext()); }
public ReminderAppUnitOfWork() { _personRepo = new EfGenericRepository<Person>(People); _messageRepo = new EfGenericRepository<Message>(Messages); base.Configuration.ProxyCreationEnabled = false; }