Example #1
0
        public async Task LoadingDocumentShouldNotDuplicateIndex()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates",
                };

                session.Save(bill);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>(x => x.Name == "Bill").Count());

                var person = await session
                    .QueryAsync<Person, PersonByName>()
                    .Where(x => x.Name == "Bill")
                    .FirstOrDefault();

                Assert.NotNull(person);
                Assert.Equal("Bill", person.Firstname);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>(x => x.Name == "Bill").Count());

                var person = await session
                    .QueryAsync<Person, PersonByName>()
                    .Where(x => x.Name == "Bill")
                    .FirstOrDefault();

                Assert.NotNull(person);
                Assert.Equal("Bill", person.Firstname);
            }
        }
Example #2
0
        public void RemovingDocumentShouldDeleteMappedIndex()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            var bill = new Person
            {
                Firstname = "Bill",
                Lastname = "Gates"
            };

            using (var session = _store.CreateSession())
            {
                session.Save(bill);
            }

            using (var session = _store.CreateSession())
            {
                var personByName = session.QueryIndex<PersonByName>().FirstOrDefault();
                Assert.NotNull(personByName);

                var person = session.Load<Person>().FirstOrDefault();
                Assert.NotNull(person);

                session.Delete(person);
            }

            using (var session = _store.CreateSession())
            {
                var personByName = session.QueryIndex<PersonByName>().FirstOrDefault();
                Assert.Null(personByName);
            }
        }
Example #3
0
        public async Task CanCountThenListOrdered()
        {
            _store.RegisterIndexes<PersonAgeIndexProvider>();

            using (var session = _store.CreateSession())
            {
                for (int i = 0; i < 100; i++)
                {
                    var person = new Person
                    {
                        Firstname = "Bill" + i,
                        Lastname = "Gates" + i,
                        Age = i
                    };

                    session.Save(person);
                }
            }

            using (var session = _store.CreateSession())
            {
                var query = session.QueryIndexAsync<PersonByAge>().OrderBy(x => x.Age);

                Assert.Equal(100, await query.Count());
                Assert.Equal(100, (await query.List()).Count());
            }
        }
Example #4
0
        public async Task ShouldOrderJoinedMapIndexes()
        {
            _store.RegisterIndexes<PersonIndexProvider>();
            _store.RegisterIndexes<PersonAgeIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Age = 1
                };

                var steve = new Person
                {
                    Firstname = "Steve",
                    Age = 2
                };

                var paul = new Person
                {
                    Firstname = "Scott",
                    Age = 2
                };

                session.Save(bill);
                session.Save(steve);
                session.Save(paul);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(2, await session.QueryAsync().For<Person>()
                    .With<PersonByName>(x => x.Name.StartsWith("S"))
                    .With<PersonByAge>(x => x.Age == 2)
                    .Count());

                Assert.Equal("Scott", (await session.QueryAsync().For<Person>()
                    .With<PersonByName>(x => x.Name.StartsWith("S"))
                    .OrderBy(x => x.Name)
                    .With<PersonByAge>(x => x.Age == 2)
                    .FirstOrDefault())
                    .Firstname);

                Assert.Equal("Steve", (await session.QueryAsync().For<Person>()
                    .With<PersonByName>(x => x.Name.StartsWith("S"))
                    .OrderByDescending(x => x.Name)
                    .With<PersonByAge>(x => x.Age == 2)
                    .FirstOrDefault())
                    .Firstname);
            }
        }
Example #5
0
        public void ShouldSaveBigDocuments()
        {
            using(var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = new String('x', 10000),
                };

                session.Save(bill);
            }
        }
Example #6
0
        public void ShouldSaveCustomObjectAsync()
        {
            var session = _store.CreateSession();
            var bill = new Person
            {
                Firstname = "Bill",
                Lastname = "Gates"
            };

            session.Save(bill);
            var task = session.CommitAsync();

            task.Wait();
        }
Example #7
0
        public async Task ShouldKeepTrackedOnAutoFlush()
        {
            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates"
                };

                session.Save(bill);
                var newBill = await session.GetAsync<Person>(bill.Id);

                Assert.Same(newBill, bill);
            }
        }
Example #8
0
        public void ShouldPageResults()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            using (var session = _store.CreateSession())
            {
                for (int i = 0; i < 100; i++)
                {
                    var person = new Person
                    {
                        Firstname = "Bill" + i,
                        Lastname = "Gates" + i,
                    };

                    session.Save(person);
                }

            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(100, session.QueryIndex<PersonByName>().Count());
                Assert.Equal(10, session.QueryIndex<PersonByName>().OrderBy(x => x.Name).Skip(0).Take(10).ToList().Count());
                Assert.Equal(1, session.QueryIndex<PersonByName>().Count(x => x.Name == "Bill0"));

                var persons = session.Query<Person, PersonByName>().Take(10).List();

                Assert.Equal(10, persons.Count());
            }
        }
Example #9
0
        public async Task ShouldUpdateEntitiesFromSeparateSessions()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            var bill = new Person
            {
                Firstname = "Bill",
                Lastname = "Gates"
            };

            using (var session = _store.CreateSession())
            {
                session.Save(bill);
                Assert.Equal(1, await session.QueryAsync<Person, PersonByName>().Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Where(x => x.Name == "Bill").Count());
            }

            using (var session = _store.CreateSession())
            {
                bill.Firstname = "Bill2";
                session.Save(bill);

                Assert.Equal(1, await session.QueryAsync<Person, PersonByName>().Count());
                Assert.Equal(0, await session.QueryIndexAsync<PersonByName>().Where(x => x.Name == "Bill").Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Where(x => x.Name == "Bill2").Count());
            }
        }
Example #10
0
        public void ShouldAssignIdWhenSaved()
        {
            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates"
                };

                Assert.True(bill.Id == 0);
                session.Save(bill);
                Assert.True(bill.Id != 0);
            }
        }
Example #11
0
        public async Task ShouldFilterMapIndexPerCollection()
        {
            await _store.InitializeCollectionAsync("Collection1");

            using (new NamedCollection("Collection1"))
            {
                using (var session = _store.CreateSession())
                {
                    session.ExecuteMigration(schemaBuilder => schemaBuilder
                    .CreateMapIndexTable(nameof(PersonByNameCol), column => column
                        .Column<string>(nameof(PersonByNameCol.Name))
                        )
                    );
                }

                _store.RegisterIndexes<PersonIndexProviderCol>();

                using (var session = _store.CreateSession())
                {
                    var bill = new Person
                    {
                        Firstname = "Bill",
                        Lastname = "Gates",
                    };

                    var steve = new Person
                    {
                        Firstname = "Steve",
                        Lastname = "Balmer"
                    };

                    session.Save(bill);
                    session.Save(steve);
                }

                using (var session = _store.CreateSession())
                {
                    Assert.Equal(2, await session.QueryAsync<Person, PersonByNameCol>().Count());
                    Assert.Equal(1, await session.QueryAsync<Person, PersonByNameCol>(x => x.Name == "Steve").Count());
                    Assert.Equal(1, await session.QueryAsync<Person, PersonByNameCol>().Where(x => x.Name == "Steve").Count());
                }
            }

            // Store a Person in the default collection
            using (var session = _store.CreateSession())
            {
                var satya = new Person
                {
                    Firstname = "Satya",
                    Lastname = "Nadella",
                };

                session.Save(satya);
            }

            // Ensure the index hasn't been altered
            using (var session = _store.CreateSession())
            {
                Assert.Equal(1, await session.QueryAsync<Person>().Count());
                Assert.Equal(2, await session.QueryIndexAsync<PersonByNameCol>().Count());
            }
        }
Example #12
0
        public async Task ShouldReadUncommittedRecords()
        {
            /*
             * session1 created
             * session1 0 index found
             * session1 save and commit person
             * session1 1 index found (session1 statements flushed)
             * session2 created
             * session2 1 index found (session1 statements isolated)
             * session2 save and commit person
             * session2 2 index found (session2 statements flushed)
             * session2 disposed (session2 transation committed)
             * session2 2 index found
             * session1 disposed
             * session1 2 index found
             */

            _store.RegisterIndexes<PersonIndexProvider>();

            var session1IsFlushed = new ManualResetEvent(false);
            var session2IsDisposed = new ManualResetEvent(false);

            var task1 = Task.Run(async () =>
            {
                using (var session1 = _store.CreateSession(IsolationLevel.ReadUncommitted))
                {
                    Assert.Equal(0, await session1.QueryIndexAsync<PersonByName>().Count());

                    var bill = new Person
                    {
                        Firstname = "Bill",
                        Lastname = "Gates",
                    };

                    session1.Save(bill);
                    await session1.CommitAsync();

                    Assert.Equal(1, await session1.QueryIndexAsync<PersonByName>().Count());

                    session1IsFlushed.Set();
                    if (!session2IsDisposed.WaitOne(5000))
                    {
                        Assert.True(false, "session2IsDisposed timeout");
                    }
                }

                using (var session1 = _store.CreateSession(IsolationLevel.ReadUncommitted))
                {
                    Assert.Equal(2, await session1.QueryIndexAsync<PersonByName>().Count());
                }
            });

            var task2 = Task.Run(async () =>
            {
                if (!session1IsFlushed.WaitOne(5000))
                {
                    Assert.True(false, "session1IsFlushed timeout");
                }

                using (var session2 = _store.CreateSession(IsolationLevel.ReadUncommitted))
                {
                    Assert.Equal(1, await session2.QueryIndexAsync<PersonByName>().Count());

                    var steve = new Person
                    {
                        Firstname = "Steve",
                        Lastname = "Ballmer",
                    };

                    session2.Save(steve);

                    await session2.CommitAsync();

                    Assert.Equal(2, await session2.QueryIndexAsync<PersonByName>().Count());
                }

                using (var session2 = _store.CreateSession(IsolationLevel.ReadUncommitted))
                {
                    Assert.Equal(2, await session2.QueryIndexAsync<PersonByName>().Count());
                }

                session2IsDisposed.Set();

            });

            await Task.WhenAll(task1, task2);
        }
Example #13
0
        public async Task ShouldAcceptEmptyIsIn()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill"
                };

                var steve = new Person
                {
                    Firstname = "Steve"
                };

                var paul = new Person
                {
                    Firstname = "Scott"
                };

                session.Save(bill);
                session.Save(steve);
                session.Save(paul);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(2, await session.QueryAsync().For<Person>()
                    .With<PersonByName>(x => x.Name.IsIn(new[] { "Bill", "Steve" }))
                    .Count());

                Assert.Equal(0, await session.QueryAsync().For<Person>()
                    .With<PersonByName>(x => x.Name.IsIn(new string[0]))
                    .Count());
            }
        }
Example #14
0
        public void ShouldCreateIndexAndLinkToDocument()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates",
                };

                var steve = new Person
                {
                    Firstname = "Steve",
                    Lastname = "Balmer"
                };

                session.Save(bill);
                session.Save(steve);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(2, session.QueryIndex<PersonByName>().Count());
                Assert.Equal(1, session.QueryIndex<PersonByName>().Count(x => x.Name == "Bill"));
                Assert.Equal(1, session.QueryIndex<PersonByName>().Count(x => x.Name == "Steve"));
                Assert.Equal(0, session.QueryIndex<PersonByName>().Count(x => x.Name == "Joe"));

                var person =
                    session.Query<Person, PersonByName>().Where(x => x.Name == "Bill").FirstOrDefault();
                Assert.NotNull(person);
                Assert.Equal("Bill", person.Firstname);
            }
        }
Example #15
0
        public async Task ShouldUpdateAutoflushedIndex()
        {
            // When auto-flush is called on an entity
            // its indexes should be updated on the actual commit

            _store.RegisterIndexes<PersonIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates"
                };

                session.Save(bill);

                // This query triggers an auto-flush

                Assert.Equal(1, await session.QueryAsync<Person, PersonByName>().Count());

                bill.Firstname = "Bill2";
                session.Save(bill);

                Assert.Equal(1, await session.QueryAsync<Person, PersonByName>().Where(x => x.Name == "Bill2").Count());

                bill.Firstname = "Bill3";
                session.Save(bill);

                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Count());
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Where(x => x.Name == "Bill3").Count());
            }
        }
Example #16
0
        public void ShouldDeleteCustomObject()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            var bill = new Person
            {
                Firstname = "Bill",
                Lastname = "Gates"
            };

            using (var session = _store.CreateSession())
            {
                session.Save(bill);
            }

            using (var session = _store.CreateSession())
            {
                var person = session.Query<Person>().FirstOrDefault();
                Assert.NotNull(person);

                session.Delete(person);
            }

            using (var session = _store.CreateSession())
            {
                var person = session.Load<Person>().FirstOrDefault();
                Assert.Null(person);
            }
        }
Example #17
0
        public async Task ShouldAppendIndexOnUpdate()
        {
            // When an object is updated, its map indexes
            // should be created a new records (to support append only scenarios)

            _store.RegisterIndexes<PersonIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates"
                };

                session.Save(bill);

                var p1 = await session.QueryIndexAsync<PersonByName>().FirstOrDefault();

                Assert.Equal(1, p1.Id);

                bill.Firstname = "Bill2";
                session.Save(bill);

                var p2 = await session.QueryIndexAsync<PersonByName>().FirstOrDefault();

                Assert.Equal(2, p2.Id);

                bill.Firstname = "Bill3";
                session.Save(bill);

                var p3 = await session.QueryIndexAsync<PersonByName>().FirstOrDefault();

                Assert.Equal(3, p3.Id);

            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonByName>().Where(x => x.Name == "Bill3").Count());
            }
        }
Example #18
0
        public void ShouldQueryByMappedIndex()
        {
            _store.RegisterIndexes<PersonIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates",
                };

                var steve = new Person
                {
                    Firstname = "Steve",
                    Lastname = "Balmer"
                };

                session.Save(bill);
                session.Save(steve);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(2, session.Query().For<Person>().With<PersonByName>().Count());
                Assert.Equal(1, session.Query().For<Person>().With<PersonByName>(x => x.Name == "Steve").Count());
                Assert.Equal(1, session.Query().For<Person>().With<PersonByName>().Where(x => x.Name == "Steve").Count());
                Assert.Equal(1, session.Query().For<Person>().With<PersonByName>().Where(x => x.Name == "Steve").List().Count());
            }
        }
Example #19
0
        public async Task ShouldCreateSeveralMapIndexPerDocument()
        {
            // When an index returns multiple map indexes, they should all be stored
            // and queryable.

            // This test also ensure we can use a SQL keyword as the column name (Identity)

            _store.RegisterIndexes<PersonIdentitiesIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var hanselman = new Person
                {
                    Firstname = "Scott",
                    Lastname = "Hanselman"
                };

                var guthrie = new Person
                {
                    Firstname = "Scott",
                    Lastname = "Guthrie"
                };

                session.Save(hanselman);
                session.Save(guthrie);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(4, await session.QueryIndexAsync<PersonIdentity>().Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonIdentity>().Where(x => x.Identity == "Hanselman").Count());
                Assert.Equal(1, await session.QueryIndexAsync<PersonIdentity>().Where(x => x.Identity == "Guthrie").Count());
                Assert.Equal(2, await session.QueryIndexAsync<PersonIdentity>().Where(x => x.Identity == "Scott").Count());
            }
        }
Example #20
0
        public void ShouldSaveCustomObject()
        {
            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates"
                };

                session.Save(bill);
            }
        }
Example #21
0
        public async Task ShouldJoinMapIndexes()
        {
            _store.RegisterIndexes<PersonIndexProvider>();
            _store.RegisterIndexes<PersonAgeIndexProvider>();

            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Age = 1
                };

                var steve = new Person
                {
                    Firstname = "Steve",
                    Age = 2
                };

                var paul = new Person
                {
                    Firstname = "Paul",
                    Age = 2
                };

                session.Save(bill);
                session.Save(steve);
                session.Save(paul);
            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(3, await session.QueryIndexAsync<PersonByName>().Count());
                Assert.Equal(2, await session.QueryIndexAsync<PersonByAge>(x => x.Age == 2).Count());
                Assert.Equal(1, await session.QueryAsync().For<Person>()
                    .With<PersonByName>(x => x.Name == "Steve")
                    .With<PersonByAge>(x => x.Age == 2)
                    .Count());
            }
        }
Example #22
0
        public void ShouldSaveSeveralObjects()
        {
            using (var session = _store.CreateSession())
            {
                var bill = new Person
                {
                    Firstname = "Bill",
                    Lastname = "Gates"
                };

                var steve = new Person
                {
                    Firstname = "Steve",
                    Lastname = "Balmer"
                };

                session.Save(bill);
                session.Save(steve);
            }
        }
Example #23
0
        public async Task ShouldOrderOnValueType()
        {
            _store.RegisterIndexes<PersonAgeIndexProvider>();

            using (var session = _store.CreateSession())
            {
                for (int i = 0; i < 100; i++)
                {
                    var person = new Person
                    {
                        Firstname = "Bill" + i,
                        Lastname = "Gates" + i,
                        Age = i
                    };

                    session.Save(person);
                }

            }

            using (var session = _store.CreateSession())
            {
                Assert.Equal(100, await session.QueryIndexAsync<PersonByAge>().Count());
                Assert.Equal(0, (await session.QueryIndexAsync<PersonByAge>().OrderBy(x => x.Age).FirstOrDefault()).Age);
                Assert.Equal(99, (await session.QueryIndexAsync<PersonByAge>().OrderByDescending(x => x.Age).FirstOrDefault()).Age);
            }
        }