Ejemplo n.º 1
0
        public void BasicLoadDocumentsWithEnumerable()
        {
            using (var store = GetDocumentStore())
            {
                new Companies_ByEmployeeLastName().Execute(store);

                using (var session = store.OpenSession())
                {
                    var employee1 = new Employee {
                        LastName = "Doe"
                    };
                    var employee2 = new Employee {
                        LastName = "Gates"
                    };

                    session.Store(employee1);
                    session.Store(employee2);

                    var company = new Company
                    {
                        Name         = "HR",
                        EmployeesIds = new List <string>
                        {
                            employee1.Id,
                            employee2.Id
                        }
                    };

                    session.Store(company);

                    session.SaveChanges();
                }

                WaitForIndexing(store);

                using (var session = store.OpenSession())
                {
                    var companies = session.Query <Companies_ByEmployeeLastName.Result, Companies_ByEmployeeLastName>()
                                    .Where(x => x.LastName == "Gates")
                                    .OfType <Company>()
                                    .ToList();

                    Assert.Equal(1, companies.Count);
                    Assert.Equal("HR", companies[0].Name);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task HandleReferenceAndMapping_ShouldNotMissChangedReference()
        {
            // https://issues.hibernatingrhinos.com/issue/RavenDB-14506
            const int batchSize = 128;

            using (var store = GetDocumentStore(new Options
            {
                ModifyDatabaseRecord = doc =>
                {
                    doc.Settings[RavenConfiguration.GetKey(x => x.Indexing.MapBatchSize)] = batchSize.ToString();
                },
                ModifyDocumentStore = localStore =>
                {
                    localStore.Conventions = new Raven.Client.Documents.Conventions.DocumentConventions {
                        MaxNumberOfRequestsPerSession = int.MaxValue
                    };
                }
            }))
            {
                var index = new Companies_ByEmployeeLastName();

                using (var session = store.OpenAsyncSession())
                {
                    var list = Enumerable.Range(0, batchSize + 1)
                               .Select(i =>
                    {
                        var employeeId = $"Employees/{i}";
                        return(
                            new Company {
                            Id = $"Companies/{i}", EmployeesIds = new List <string> {
                                employeeId
                            }
                        },
                            new Employee {
                            Id = employeeId
                        }
                            );
                    })
                               .ToArray();

                    var(firstCompany, _) = list[0];
                    foreach (var(company, _) in list)
                    {
                        await session.StoreAsync(company);
                    }
                    await session.SaveChangesAsync();

                    var newEmployee = new Employee {
                        Id = $"Employees/{batchSize + 1}"
                    };
                    firstCompany.EmployeesIds.Add(newEmployee.Id);
                    await session.StoreAsync(firstCompany);

                    await session.SaveChangesAsync();

                    index.Execute(store);
                    Indexes.WaitForIndexing(store);

                    //Index disable
                    store.Maintenance.Send(new DisableIndexOperation(index.IndexName));
                    foreach (var(_, employee) in list)
                    {
                        await session.StoreAsync(employee);
                    }
                    await session.SaveChangesAsync();

                    await session.StoreAsync(newEmployee);

                    await session.SaveChangesAsync();

                    await session.StoreAsync(newEmployee);

                    await session.SaveChangesAsync();

                    await session.StoreAsync(new Company { EmployeesIds = new List <string> {
                                                               newEmployee.Id
                                                           } });

                    await session.SaveChangesAsync();

                    //Index enable
                    store.Maintenance.Send(new EnableIndexOperation(index.IndexName));
                    Indexes.WaitForIndexing(store);

                    //Assert
                    var queryResult = await session
                                      .Query <Companies_ByEmployeeLastName.Result, Companies_ByEmployeeLastName>()
                                      .OfType <Company>()
                                      .ToArrayAsync();

                    Assert.Contains(queryResult, e => e.Id == $"Companies/{batchSize}");
                }
            }
        }