Exemple #1
0
        public async Task AddAsync()
        {
            var employee = await _employeeRepository.AddAsync(EmployeeGenerator.Generate(name: "  BLAKE  "));

            Assert.NotNull(employee?.Id);

            var result = await _employeeRepository.GetByIdAsync(employee.Id);

            Assert.Equal("blake", result.Name);
        }
Exemple #2
0
        public async Task JsonPatch()
        {
            var employee = await _employeeRepository.AddAsync(EmployeeGenerator.Default);

            var patch = new PatchDocument(new ReplaceOperation {
                Path = "name", Value = "Patched"
            });
            await _employeeRepository.PatchAsync(employee.Id, patch);

            employee = await _employeeRepository.GetByIdAsync(employee.Id);

            Assert.Equal("Patched", employee.Name);
            Assert.Equal(2, employee.Version);
        }
        public async Task AddAsync()
        {
            var employee = EmployeeGenerator.Default;

            Assert.Equal(0, employee.Version);

            employee = await _employeeRepository.AddAsync(employee);

            Assert.NotNull(employee?.Id);
            Assert.Equal(1, employee.Version);

            var employee2 = await _employeeRepository.GetByIdAsync(employee.Id);

            Assert.Equal(employee, employee2);
        }
        public async Task SearchShouldNotReturnDeletedDocuments()
        {
            var employee = EmployeeGenerator.Generate(age: 20, name: "Deleted");

            employee = await _employeeRepository.AddAsync(employee);

            await _client.RefreshAsync();

            employee.IsDeleted = true;
            await _employeeRepository.SaveAsync(employee);

            var employees = await _employeeRepository.GetAllByAgeAsync(20);

            var employeeById = await _employeeRepository.GetByIdAsync(employee.Id);

            Assert.NotNull(employeeById);
            Assert.True(employeeById.IsDeleted);

            Assert.Equal(0, employees.Total);
        }
Exemple #5
0
        public async Task SearchShouldNotReturnDeletedDocumentsAsync()
        {
            var employee = EmployeeGenerator.Generate(age: 20, name: "Deleted");

            employee = await _employeeRepository.AddAsync(employee, o => o.ImmediateConsistency());

            var employees = await _employeeRepository.GetAllByAgeAsync(20);

            Assert.Equal(1, employees.Total);

            employee.IsDeleted = true;
            await _employeeRepository.SaveAsync(employee, o => o.Consistency(Consistency.Eventual));

            employees = await _employeeRepository.GetAllByAgeAsync(20);

            Assert.Equal(0, employees.Total);

            var employeeById = await _employeeRepository.GetByIdAsync(employee.Id);

            Assert.NotNull(employeeById);
            Assert.True(employeeById.IsDeleted);
        }
        public async Task CanGetByIds()
        {
            var employee = await _repository.AddAsync(EmployeeGenerator.Generate());

            Assert.NotNull(employee.Id);

            var result = await _repository.GetByIdAsync(employee.Id);

            Assert.NotNull(result);
            Assert.Equal(employee.Id, result.Id);

            var employee2 = await _repository.AddAsync(EmployeeGenerator.Generate());

            Assert.NotNull(employee2.Id);

            var results = await _repository.GetByIdsAsync(new [] { employee.Id, employee2.Id });

            Assert.NotNull(results);
            Assert.Equal(2, results.Total);
        }
        public async Task CanReindexVersionedIndexWithDeletedDocsAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);

                IEmployeeRepository repository = new EmployeeRepository(_configuration);
                var employee = await repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.True((await _client.Indices.ExistsAsync(version2Index.VersionedName)).Exists);
                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                    // alias should still point to the old version until reindex
                    var aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    _logger.LogRequest(aliasResponse);
                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);

                    var countdown   = new AsyncCountdownEvent(1);
                    var reindexTask = version2Index.ReindexAsync(async(progress, message) => {
                        _logger.LogInformation($"Reindex Progress {progress}%: {message}");
                        if (progress == 91)
                        {
                            countdown.Signal();
                            await Task.Delay(1000);
                        }
                    });

                    // Wait until the first reindex pass is done.
                    await countdown.WaitAsync();

                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                    await repository.RemoveAllAsync(o => o.ImmediateConsistency());

                    // Resume after everythings been indexed.
                    await reindexTask;
                    aliasResponse = await _client.Indices.GetAliasAsync(version2Index.Name);

                    _logger.LogRequest(aliasResponse);
                    Assert.True(aliasResponse.IsValid, aliasResponse.GetErrorMessage());
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key);

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    var countResponse = await _client.CountAsync <Employee>(d => d.Index(version1Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.ApiCall.HttpStatusCode == 404, countResponse.GetErrorMessage());
                    Assert.Equal(0, countResponse.Count);

                    countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogRequest(countResponse);
                    Assert.True(countResponse.IsValid, countResponse.GetErrorMessage());
                    Assert.Equal(1, countResponse.Count);

                    Assert.Equal(employee, await repository.GetByIdAsync(employee.Id));
                    Assert.False((await _client.Indices.ExistsAsync(version1Index.VersionedName)).Exists);
                }
            }
        }
        public async Task CanReindexVersionedIndexWithReindexScriptAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version20Index = new VersionedEmployeeIndex(_configuration, 20)
            {
                DiscardIndexesOnReindex = false
            };
            await version20Index.DeleteAsync();

            var version21Index = new VersionedEmployeeIndex(_configuration, 21)
            {
                DiscardIndexesOnReindex = false
            };
            await version21Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                IEmployeeRepository version1Repository = new EmployeeRepository(version1Index);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version20Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version20Index.ConfigureAsync();

                    await version20Index.ReindexAsync();

                    IEmployeeRepository version20Repository = new EmployeeRepository(version20Index);
                    var result = await version20Repository.GetByIdAsync(employee.Id);

                    Assert.Equal("scripted", result.CompanyName);

                    using (new DisposableAction(() => version21Index.DeleteAsync().GetAwaiter().GetResult())) {
                        await version21Index.ConfigureAsync();

                        await version21Index.ReindexAsync();

                        IEmployeeRepository version21Repository = new EmployeeRepository(version21Index);
                        result = await version21Repository.GetByIdAsync(employee.Id);

                        Assert.Equal("typed script", result.CompanyName);
                    }
                }
            }

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                IEmployeeRepository version1Repository = new EmployeeRepository(version1Index);

                var utcNow   = SystemClock.UtcNow;
                var employee = await version1Repository.AddAsync(EmployeeGenerator.Generate(createdUtc: utcNow), o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version21Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version21Index.ConfigureAsync();

                    await version21Index.ReindexAsync();

                    IEmployeeRepository version21Repository = new EmployeeRepository(version21Index);
                    var result = await version21Repository.GetByIdAsync(employee.Id);

                    Assert.Equal("typed script", result.CompanyName);
                }
            }
        }
Exemple #9
0
        public async Task CanReindexVersionedIndexWithUpdatedDocsAsync()
        {
            var version1Index = new VersionedEmployeeIndex(_configuration, 1);
            await version1Index.DeleteAsync();

            var version2Index = new VersionedEmployeeIndex(_configuration, 2);
            await version2Index.DeleteAsync();

            using (new DisposableAction(() => version1Index.DeleteAsync().GetAwaiter().GetResult())) {
                await version1Index.ConfigureAsync();

                Assert.True(_client.IndexExists(version1Index.VersionedName).Exists);

                var repository = new EmployeeRepository(version1Index.Employee);
                var employee   = await repository.AddAsync(EmployeeGenerator.Default, o => o.ImmediateConsistency());

                Assert.NotNull(employee?.Id);

                using (new DisposableAction(() => version2Index.DeleteAsync().GetAwaiter().GetResult())) {
                    await version2Index.ConfigureAsync();

                    Assert.True(_client.IndexExists(version2Index.VersionedName).Exists);
                    Assert.Equal(1, await version2Index.GetCurrentVersionAsync());

                    // alias should still point to the old version until reindex
                    var aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name));

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version1Index.VersionedName, aliasResponse.Indices.First().Key);

                    var countdown   = new AsyncCountdownEvent(1);
                    var reindexTask = version2Index.ReindexAsync((progress, message) => {
                        _logger.LogInformation($"Reindex Progress {progress}%: {message}");
                        if (progress == 91)
                        {
                            countdown.Signal();
                            SystemClock.Sleep(1000);
                        }

                        return(Task.CompletedTask);
                    });

                    // Wait until the first reindex pass is done.
                    await countdown.WaitAsync();

                    Assert.Equal(1, await version1Index.GetCurrentVersionAsync());
                    await repository.AddAsync(EmployeeGenerator.Generate(createdUtc: SystemClock.UtcNow));

                    employee.Name = "Updated";
                    await repository.SaveAsync(employee);

                    // Resume after everythings been indexed.
                    await reindexTask;
                    aliasResponse = await _client.GetAliasAsync(descriptor => descriptor.Name(version2Index.Name));

                    Assert.True(aliasResponse.IsValid);
                    Assert.Equal(1, aliasResponse.Indices.Count);
                    Assert.Equal(version2Index.VersionedName, aliasResponse.Indices.First().Key);

                    Assert.Equal(2, await version1Index.GetCurrentVersionAsync());
                    Assert.Equal(2, await version2Index.GetCurrentVersionAsync());

                    await _client.RefreshAsync(Indices.All);

                    var countResponse = await _client.CountAsync <Employee>(d => d.Index(version2Index.VersionedName));

                    _logger.LogTrace(countResponse.GetRequest());
                    Assert.True(countResponse.IsValid);
                    Assert.Equal(2, countResponse.Count);

                    var result = await repository.GetByIdAsync(employee.Id);

                    Assert.Equal(ToJson(employee), ToJson(result));
                    Assert.False((await _client.IndexExistsAsync(version1Index.VersionedName)).Exists);
                }
            }
        }