Esempio n. 1
0
        public void ItThrowsWhenItCannotConvertCriteriaToQueryable()
        {
            var repo = CompanyRepo.CreateRepo();

            repo.Collection.Drop();
            repo.Save(new Company
            {
                Id   = 1,
                Name = "bar"
            });
            repo.Save(new Company
            {
                Id             = 2,
                Name           = "foo",
                ChildCompanies = new [] { 1 }
            });
            repo.Save(new Company
            {
                Id             = 3,
                Name           = "bar",
                ChildCompanies = new [] { 1 }
            });

            Assert.Throws <NotSupportedException>(() => repo.Builder()
                                                  .Queryable(q => q
                                                             .Where(i => i.Name.Split(',').Count() == 0))
                                                  .Count());
        }
Esempio n. 2
0
        public void ItDoesNotCallTheWrapperOverInMemoryOperations()
        {
            var             wrapperCalls = 0;
            Action <Action> wrapper      = action =>
            {
                wrapperCalls++;
                action();
                action();
            };
            var repo = CompanyRepo.CreateRepo(new RepositoryConfiguration
            {
                CollectionName   = "companies",
                DatabaseName     = "testdata",
                ConnectionString = "mongodb://localhost",
                SafeModeSetting  = SafeMode.True,
                CustomWrapper    = wrapper
            });

            repo.Collection.Drop();
            repo.Save(new Company
            {
                Id   = 1,
                Name = "bar"
            });
            repo.Save(new Company
            {
                Id             = 2,
                Name           = "foo",
                ChildCompanies = new [] { 1 }
            });
            repo.Save(new Company
            {
                Id             = 3,
                Name           = "bar",
                ChildCompanies = new [] { 1 }
            });

            var queryableCalls  = 0;
            var enumerableCalls = 0;

            repo
            .Builder()
            .ByName("bar")
            .Queryable(c =>
            {
                queryableCalls++;
                return(c);
            })
            .Where(i => i.Id == 1)
            .Select(i =>
            {
                enumerableCalls++;
                return(i);
            })
            .ToList();
            Assert.AreEqual(1, wrapperCalls);
            Assert.AreEqual(2, queryableCalls);
            Assert.AreEqual(1, enumerableCalls);
        }
Esempio n. 3
0
        public void ItThrowsUnsafeMongoOperationException()
        {
            var repo = CompanyRepo.CreateRepo();

            repo.Collection.Drop();

            Assert.Throws <UnsafeMongoOperationException>(() => repo.Builder().GetAll());
        }
Esempio n. 4
0
        public void ItExposesMongoQueryable()
        {
            var repo = CompanyRepo.CreateRepo();

            repo.Collection.Drop();
            repo.Save(new Company
            {
                Id   = 1,
                Name = "bar"
            });
            repo.Save(new Company
            {
                Id             = 2,
                Name           = "foo",
                ChildCompanies = new [] { 1 }
            });
            repo.Save(new Company
            {
                Id             = 3,
                Name           = "bar",
                ChildCompanies = new [] { 1 }
            });

            Assert.AreEqual(2, repo
                            .Queryable(q => q
                                       .Where(i => i.Name == "bar"))
                            .Count());

            Assert.IsNotNull(repo
                             .Queryable(q => q
                                        .First(i => i.Name == "bar")));

            Assert.AreEqual(2, repo.Builder()
                            .Queryable(q => q
                                       .Where(i => i.Name == "bar"))
                            .Count());

            Assert.AreEqual(1, repo.Builder()
                            .ByName("bar")
                            .Queryable(q => q
                                       .Where(i => i.ChildCompanies.Any()))
                            .Count());

            Assert.IsNotNull(repo.Builder()
                             .ByName("bar")
                             .Queryable(q => q.First()));
        }
Esempio n. 5
0
        public void ItQueriesSuccessfullyWithTwoConventions()
        {
            var repo = CompanyRepo.CreateRepo();

            repo.Collection.Drop();
            repo.Save(new Company
            {
                Id   = 1,
                Name = "Test One"
            });
            repo.Save(new Company
            {
                Id             = 2,
                Name           = "Test Two",
                ChildCompanies = new [] { 1 }
            });

            Assert.AreEqual(1, repo.Builder()
                            .ByName("Test One")
                            .GetAll()
                            .Count);
            Assert.AreEqual(2, repo.Builder()
                            .GetAll(true)
                            .Count);
            Assert.AreEqual(1, repo.Builder()
                            .ChildCompaniesContains(1)
                            .GetAll()
                            .Count);
            Assert.AreEqual(1, repo.Builder()
                            .ByName("Test Two")
                            .ChildCompaniesContains(1)
                            .GetAll()
                            .Count);
            Assert.AreEqual(0, repo.Builder()
                            .ChildCompaniesContains(0)
                            .GetAll()
                            .Count);
            Assert.AreEqual(0, repo.Builder()
                            .ByName("Test One")
                            .ChildCompaniesContains(1)
                            .GetAll()
                            .Count);
        }
Esempio n. 6
0
        public void ItLimitsResultSetsWhenYouAskItTo()
        {
            var repo = CompanyRepo.CreateRepo();

            repo.Collection.Drop();
            repo.Save(new Company
            {
                Id   = 1,
                Name = "foo"
            });
            repo.Save(new Company
            {
                Id   = 2,
                Name = "foo",
            });

            Assert.AreEqual(2, repo.Builder()
                            .ByName("foo")
                            .GetSome(3)
                            .Count);
            Assert.AreEqual(2, repo.Builder()
                            .ByName("foo")
                            .GetSome(2)
                            .Count);
            Assert.AreEqual(1, repo.Builder()
                            .ByName("foo")
                            .GetSome(1)
                            .Count);
            Assert.AreEqual(2, repo.Builder()
                            .ByName("foo")
                            .GetSome(0)
                            .Count);
            Assert.AreEqual(2, repo.Builder()
                            .ByName("foo")
                            .GetSome(-1)
                            .Count);
        }
        public void ItQueriesAHundredTimesInLessThanASecond()
        {
            var repo = CompanyRepo.CreateRepo();

            repo.Collection.Drop();
            repo.Save(new Company
            {
                Id   = 1,
                Name = "bar"
            });
            repo.Save(new Company
            {
                Id             = 2,
                Name           = "foo",
                ChildCompanies = new [] { 1 }
            });
            repo.Save(new Company
            {
                Id             = 3,
                Name           = "bar",
                ChildCompanies = new [] { 1 }
            });

            var totalWatch = Stopwatch.StartNew();

            Enumerable.Range(0, 100).ToList().ForEach(_ =>
                                                      Assert.True(repo.Builder()
                                                                  .ByName("foo")
                                                                  .ChildCompaniesContains(1)
                                                                  .Queryable(q => q)
                                                                  .Any()));

            totalWatch.Stop();
            Assert.LessOrEqual(totalWatch.ElapsedMilliseconds, 1000,
                               "Either your machine is slow, or we need a different performance expectation.");
        }
Esempio n. 8
0
        public void ItCallsTheWrapper()
        {
            var             called  = false;
            Action <Action> wrapper = action =>
            {
                called = true;
                action();
            };
            var repo = CompanyRepo.CreateRepo(new RepositoryConfiguration
            {
                CollectionName   = "companies",
                DatabaseName     = "testdata",
                ConnectionString = "mongodb://localhost",
                SafeModeSetting  = SafeMode.True,
                CustomWrapper    = wrapper
            });

            repo.Collection.Drop();

            Assert.Throws <UnsafeMongoOperationException>(() => repo.Builder().GetAll());
            Assert.IsFalse(called);
            Assert.IsEmpty(repo.Builder().GetAll(true));
            Assert.IsTrue(called);
        }
Esempio n. 9
0
        public void ItReducesAtTheDatabaseQueryLevel()
        {
            var repo = CompanyRepo.CreateRepo();

            repo.Collection.Drop();
            repo.Save(new Company
            {
                Id   = 1,
                Name = "foo"
            });
            repo.Save(new Company
            {
                Id   = 2,
                Name = "foo",
            });

            Assert.AreEqual(2, repo.Queryable(q => q.Count()));
            Assert.IsNotNull(repo.Queryable(q => q.First()));
            Assert.AreEqual(2, repo.Builder()
                            .Queryable(q => q.Count()));
            Assert.AreEqual(2, repo.Builder()
                            .ByName("foo")
                            .Queryable(q => q.Count()));
        }