public void Site_should_create_new_record() { var sites = new List <Site>() { new Site(new TenantId("abc"), "abc", "abc", true), new Site(new TenantId("abc"), "def", "def", true), }.AsQueryable(); var expected = new Collection <Site> { new Site(new TenantId("abc"), "abc", "abc", true), new Site(new TenantId("abc"), "def", "def", true), }; var actual = new Collection <Site>(); var mockSet = new Mock <DbSet <Site> >(); mockSet.Setup(r => r.Add(It.IsAny <Site>())) // 在Callback函数里把SavePatient的入参添加到actual列表中 .Callback <Site>(p => actual.Add(p)); mockSet.As <IQueryable <Site> >() .Setup(m => m.Provider) .Returns(sites.Provider); mockSet.As <IQueryable <Site> >().Setup(m => m.Expression).Returns(sites.Expression); mockSet.As <IQueryable <Site> >().Setup(m => m.ElementType).Returns(sites.ElementType); mockSet.As <IQueryable <Site> >().Setup(m => m.GetEnumerator()).Returns(() => sites.GetEnumerator()); var mockContext = new Mock <BusinessDbContext>(); mockContext.Setup(c => c.Set <Site>()).Returns(mockSet.Object); var entityRepository = new DomainRepository <Site>(mockContext.Object); var result = entityRepository.Find(Guid.NewGuid()); Assert.Null(result); entityRepository.Add(new Site(new TenantId("abc"), "abc", "abc", true)); entityRepository.Add(new Site(new TenantId("abc"), "def", "def", true)); entityRepository.SaveChanges(); // Assert Assert.Equal(expected.Count(), actual.Count()); }
public void WhenEntityDeleted_ThenPersists() { DatabaseTestUtility.DropCreateFirebrickDatabase(); // Arrange var repository = new DomainRepository(new DatabaseFactory()); Domain entity = DefaultModelHelper.DummyPopulatedDomain(); // Act repository.Add(entity); repository.Commit(); IEnumerable<Domain> returnedEntities = repository.GetAll(); // Assert Assert.Equal(1, returnedEntities.Count()); // Act repository.Delete(entity); repository.Commit(); // Arrange // Use a new context and repository to verify the vehicle was deleted var repositoryTwo = new DomainRepository(new DatabaseFactory()); IEnumerable<Domain> verifyEntities = repositoryTwo.GetAll(); // Assert Assert.NotNull(verifyEntities); Assert.Equal(0, verifyEntities.Count()); }
public void WhenAddingEntity_ThenGetAndUpdate() { DatabaseTestUtility.DropCreateFirebrickDatabase(); // Arrange var repository = new DomainRepository(new DatabaseFactory()); Domain newEntity = DefaultModelHelper.DummyPopulatedDomain(); // Act repository.Add(newEntity); repository.Commit(); // Use a new context and repository to verify the vehicle was added var repositoryTwo = new DomainRepository(new DatabaseFactory()); Domain loadedEntity = repositoryTwo.GetById(1); loadedEntity.HostHeader = "NewHostHeader"; repositoryTwo.Update(loadedEntity); repositoryTwo.Commit(); // Use a new context and repository to verify the vehicle was added var repositoryThree = new DomainRepository(new DatabaseFactory()); newEntity = repositoryThree.GetById(1); Assert.NotNull(newEntity); Assert.Equal("NewHostHeader", newEntity.HostHeader); }
public void Site_should_save_new_record() { var entityRepository = new DomainRepository <Site>(dbContext); var site = new Site(new TenantId("abc"), "def", "def", true); entityRepository.Add(site); dbContext.Entry(site).State = EntityState.Added; entityRepository.SaveChanges(); var result = entityRepository.Find(site.Id); Assert.NotNull(result); Assert.Equal("def", result.Name); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); ISiteRepository repository; BusinessDbContext dbContext; var optionsBuilder = new DbContextOptionsBuilder <BusinessDbContext>(); optionsBuilder.UseMySql("Server=localhost;database=book2site;uid=root;pwd=P@ssword;charset=utf8;port=3306;SslMode=None"); dbContext = new BusinessDbContext(optionsBuilder.Options); repository = new SiteRepository(dbContext); var entityRepository = new DomainRepository <Site>(dbContext); var site = new Site(new TenantId("abc"), "def", "def", true); entityRepository.Add(site); entityRepository.SaveChanges(); var result = entityRepository.Find(site.Id); }
public async Task <ViewModels.DomainViewModel> QueryForDomainDataAsync(string domainName) { if (!Umbler.Infra.CrossCutting.StringHelper.IsValidDomain(domainName)) { return(new ViewModels.DomainViewModel() { Name = "Dominío Invalído" }); } var domain = await _domainRepository.GetByDomainNameAsync(domainName); if (domain == null) { domain = await FillDomainDataAsync(domainName); _domainRepository.Add(domain); } if (DateTime.Now.Subtract(domain.UpdatedAt).TotalMinutes > domain.Ttl) { var refreshedDomain = await FillDomainDataAsync(domainName); domain.HostedAt = refreshedDomain.HostedAt; domain.Name = refreshedDomain.Name; domain.Ip = refreshedDomain.Ip; domain.WhoIs = refreshedDomain.WhoIs; domain.UpdatedAt = refreshedDomain.UpdatedAt; domain.Ttl = refreshedDomain.Ttl; _domainRepository.Update(); } return(Adapters.DomainEntityToDomainViewModel.GetViewModel(domain)); }