Esempio n. 1
0
        public async Task ApplyCappedCollectionModelWithIndex()
        {
            const int maxDocuments   = 100;
            const int maxSizeInBytes = maxDocuments * 1000;

            var model = new CappedCollectionModel
            {
                CollectionName       = _collectionName,
                MaxSizeInBytes       = maxSizeInBytes,
                MaxNumberOfDocuments = maxDocuments,
                Indexes = new List <CollectionIndex>
                {
                    new CollectionIndex
                    {
                        Name   = "TestIndex_1",
                        Unique = true,
                        Keys   = new List <IndexKey>
                        {
                            new IndexKey {
                                FieldName = "Field1", Descending = false
                            },
                        }
                    }
                }
            };

            var package = new CollectionModelPackage(_documentDatabase, model, new CollectionModelSettings {
                ReCreate = true
            });

            bool result = await package.Apply(_workContext);

            result.Should().BeTrue();

            (await _documentDatabase.CollectionExist(_workContext, _collectionName)).Should().BeTrue();
            CollectionDetailV1 detail = await _documentDatabase.GetCollectionDetail(_workContext, _collectionName);

            detail.Should().NotBeNull();
            detail.Name.Should().Be(_collectionName);
            detail.Type.Should().Be("collection");
            detail.Readonly.Should().BeFalse();
            detail.Capped.Should().BeTrue();
            detail.MaxDocuments.Should().HaveValue();
            detail.MaxDocuments.Should().Be(maxDocuments);
            detail.MaxSizeInBytes.Should().HaveValue();
            detail.MaxSizeInBytes.Should().BeGreaterOrEqualTo(maxSizeInBytes);

            IDocumentCollection <TestDocument> collection = _documentDatabase.GetCollection <TestDocument>(_collectionName);
            IndexDetailV1 indexDetail = await collection.Index.GetIndexDetail(_workContext, model.Indexes[0].Name);

            indexDetail.Should().NotBeNull();
            indexDetail.Keys.Count.Should().Be(1);

            model.Indexes[0].IsEquals(indexDetail).Should().BeTrue();
        }
        public async Task CreateReadTest()
        {
            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            TestDocument test = CreateTestDocument(0);

            await collection.Insert(_workContext, test);

            IEnumerable <TestDocument> results = await collection.Find(_workContext, new BsonDocument());

            results.Should().NotBeNull();
            results.Count().Should().Be(1);

            TestDocument result = results.First();

            result.Should().NotBeNull();
            result.Index.Should().Be(test.Index);
            result.FirstName.Should().Be(test.FirstName);
            result.LastName.Should().Be(test.LastName);
            result.Birthdate.ToString("s").Should().Be(test.Birthdate.ToString("s"));
            result.Address1.Should().Be(test.Address1);
            result.Address2.Should().Be(test.Address2);
            result.City.Should().Be(test.City);
            result.State.Should().Be(test.State);
            result.ZipCode.Should().Be(test.ZipCode);
        }
Esempio n. 3
0
        private async Task <bool> VerifyCollectionModel(IDocumentDatabase db, CollectionModel model)
        {
            (await db.CollectionExist(_workContext, _collectionName)).Should().BeTrue();
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            foreach (var indexDetail in model.Indexes)
            {
                IndexDetailV1 detail = await collection.Index.GetIndexDetail(_workContext, indexDetail.Name);

                detail.Should().NotBeNull();

                if (!indexDetail.IsEquals(detail))
                {
                    return(false);
                }
            }

            IEnumerable <IndexDetailV1> dbIndex = await collection.Index.ListIndexes(_workContext);

            dbIndex.Should().NotBeNull();
            dbIndex.Count().Should().Be(model.Indexes.Count + 1);

            return(dbIndex
                   .Where(x => x.Name != "_id_")
                   .OrderBy(x => x.Name)
                   .Zip(model.Indexes.OrderBy(x => x.Name), (d, m) => new { Db = d, Model = m })
                   .All(x => x.Model.IsEquals(x.Db)));
        }
Esempio n. 4
0
        public async Task CreateSingleIndexTest()
        {
            await _documentServer.DropDatabase(_workContext, _dbName);

            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);
            await db.CreateCollection(_workContext, _collectionName);

            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            IEnumerable <IndexDetailV1> list = await collection.Index.ListIndexes(_workContext);

            list.Should().NotBeNull();
            list.Count().Should().Be(1);

            list.First().Name.Should().Be("_id_");

            var model = new CollectionIndex
            {
                Name   = "TestIndex_1",
                Unique = true,
                Sparse = false,
                Keys   = new List <IndexKey>
                {
                    new IndexKey {
                        FieldName = "FirstName"
                    },
                    new IndexKey {
                        FieldName = "Last Name"
                    },
                }
            };

            await collection.Index.CreateIndex(_workContext, model);

            list = await collection.Index.ListIndexes(_workContext);

            list.Should().NotBeNull();
            list.Count().Should().Be(2);

            list.First().Name.Should().Be("_id_");

            IndexDetailV1 detail = list.Skip(1).First();

            detail.Name.Should().Be(model.Name);
            detail.Unique.Should().Be(model.Unique);
            detail.Sparse.Should().Be(model.Sparse);
            detail.Keys.Count.Should().Be(model.Keys.Count);
            detail.Keys[0].FieldName.Should().Be(model.Keys[0].FieldName);
            detail.Keys[0].Descending.Should().Be(model.Keys[0].Descending);
            detail.Keys[1].FieldName.Should().Be(model.Keys[1].FieldName);
            detail.Keys[1].Descending.Should().Be(model.Keys[1].Descending);

            await db.DropCollection(_workContext, _collectionName);

            await _documentServer.DropDatabase(_workContext, _dbName);
        }
Esempio n. 5
0
        public DocumentSearchTests()
        {
            _documentServer = new DocumentServer(Constants.ConnectionString);

            _testDocuments = Enumerable.Range(0, 10)
                             .Select(x => CreateTestDocument(x))
                             .OrderBy(x => x._id)
                             .ToList();

            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);

            _collection = db.GetCollection <TestDocument>(_collectionName);

            ResetDatabase()
            .GetAwaiter()
            .GetResult();
        }
Esempio n. 6
0
        public async Task ApplyCollectionModelFull()
        {
            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);

            var model = new CollectionModel
            {
                CollectionName = _collectionName,
                Indexes        = new List <CollectionIndex>
                {
                    new CollectionIndex
                    {
                        Name   = "TestIndex_1",
                        Unique = true,
                        Keys   = new List <IndexKey>
                        {
                            new IndexKey {
                                FieldName = "Field1", Descending = false
                            },
                        }
                    }
                }
            };

            var package = new CollectionModelPackage(db, model, new CollectionModelSettings {
                ReCreate = true
            });

            bool result = await package.Apply(_workContext);

            result.Should().BeTrue();

            (await db.CollectionExist(_workContext, _collectionName)).Should().BeTrue();
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            IndexDetailV1 detail = await collection.Index.GetIndexDetail(_workContext, model.Indexes[0].Name);

            detail.Should().NotBeNull();
            detail.Keys.Count.Should().Be(1);

            model.Indexes[0].IsEquals(detail).Should().BeTrue();

            await db.DropCollection(_workContext, _collectionName);

            await _documentServer.DropDatabase(_workContext, _dbName);
        }
        public async Task CreateMultipleReadDeleteTest()
        {
            const int count = 10;

            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            var documentList = new List <TestDocument>();

            foreach (var index in Enumerable.Range(0, count))
            {
                TestDocument test = CreateTestDocument(index);
                await collection.Insert(_workContext, test);

                documentList.Add(test);
            }

            IEnumerable <TestDocument> results = await collection.Find(_workContext, new BsonDocument());

            results.Should().NotBeNull();
            results.Count().Should().Be(count);

            // Create filter to delete odd records
            var builder = Builders <TestDocument> .Filter;
            var filter  = builder.In("Index", new int[] { 1, 3, 5, 7, 9 });

            await collection.Delete(_workContext, filter);

            results = await collection.Find(_workContext, new BsonDocument());

            results.Should().NotBeNull();
            results.Count().Should().Be(count - 5);

            filter = builder.Gte("Index", 0);
            await collection.Delete(_workContext, filter);

            results = await collection.Find(_workContext, new BsonDocument());

            results.Should().NotBeNull();
            results.Count().Should().Be(0);
        }
        public async Task CreateReadDeleteTest()
        {
            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            TestDocument test = CreateTestDocument(0);

            await collection.Insert(_workContext, test);

            IEnumerable <TestDocument> results = await collection.Find(_workContext, new BsonDocument());

            results.Should().NotBeNull();
            results.Count().Should().Be(1);

            TestDocument result = results.First();

            result.Should().NotBeNull();
            result.Index.Should().Be(test.Index);
            result.FirstName.Should().Be(test.FirstName);
            result.LastName.Should().Be(test.LastName);
            result.Birthdate.ToString("s").Should().Be(test.Birthdate.ToString("s"));
            result.Address1.Should().Be(test.Address1);
            result.Address2.Should().Be(test.Address2);
            result.City.Should().Be(test.City);
            result.State.Should().Be(test.State);
            result.ZipCode.Should().Be(test.ZipCode);

            var builder = Builders <TestDocument> .Filter;
            var filter  = builder.Eq("_id", result._id);

            await collection.Delete(_workContext, filter);

            List <TestDocument> currentList = collection.MongoCollection
                                              .AsQueryable()
                                              .Where(x => x._id == result._id)
                                              .ToList();

            currentList.Count.Should().Be(0);
        }
        public async Task CreateMultipleReadTest()
        {
            const int count = 10;

            IDocumentDatabase db = _documentServer.GetDatabase(_workContext, _dbName);
            IDocumentCollection <TestDocument> collection = db.GetCollection <TestDocument>(_collectionName);

            var documentList = new List <TestDocument>();

            foreach (var index in Enumerable.Range(0, count))
            {
                TestDocument test = CreateTestDocument(index);
                await collection.Insert(_workContext, test);

                documentList.Add(test);
            }

            IEnumerable <TestDocument> results = await collection.Find(_workContext, new BsonDocument());

            results.Should().NotBeNull();
            results.Count().Should().Be(count);

            int docIndex = 0;

            foreach (var result in results.OrderBy(x => x.Index))
            {
                var test = documentList[docIndex++];

                result.FirstName.Should().Be(test.FirstName);
                result.LastName.Should().Be(test.LastName);
                result.Birthdate.ToString("s").Should().Be(test.Birthdate.ToString("s"));
                result.Address1.Should().Be(test.Address1);
                result.Address2.Should().Be(test.Address2);
                result.City.Should().Be(test.City);
                result.State.Should().Be(test.State);
                result.ZipCode.Should().Be(test.ZipCode);
            }
        }