Exemple #1
0
        public async Task Delete_Collection(string collectionName, string id, string json)
        {
            IObjectService service = new MongoService(_fixture.MongoClient, _fixture.Logger);

            // Try getting items in the collection. Collection doesn't exist yet, should get a 404
            var getFirstCollectionResult = await service.GetAllAsync("bookstore", collectionName);

            Assert.Equal(404, getFirstCollectionResult.Status);

            // Add an item to collection; Mongo auto-creates the collection
            var insertResult = await service.InsertAsync("bookstore", collectionName, id, json);

            // // Try getting items in collection a 2nd time. Now it should return a 200.
            var getSecondCollectionResult = await service.GetAllAsync("bookstore", collectionName);

            Assert.Equal(200, getSecondCollectionResult.Status);

            // Delete the collection
            var deleteCollectionResult = await service.DeleteCollectionAsync("bookstore", collectionName);

            Assert.Equal(200, deleteCollectionResult.Status);

            // Try getting items in collection a 3rd time. It was just deleted so we should get a 404.
            var getThirdCollectionResult = await service.GetAllAsync("bookstore", collectionName);

            Assert.Equal(404, getThirdCollectionResult.Status);
        }
Exemple #2
0
        public async Task Get_All_Objects_Success(int start, int size, int expectedCount)
        {
            MongoService repo = new MongoService(_mongoFixture.MongoClient, _mongoFixture.Logger, new Dictionary <string, HashSet <string> >());

            var collection = "users" + System.Guid.NewGuid().ToString().Replace("-", string.Empty);

            var items = new List <string>
            {
                "{ \"Name\" : \"Mary\" }",
                "{ \"Name\" : \"John\" }",
                "{ \"Name\" : \"Kate\" }",
                "{ \"Name\" : \"Mark\" }",
                "{ \"Name\" : \"Sara\" }"
            };

            // insert all five items
            foreach (var item in items)
            {
                var insertResult = await repo.InsertAsync("bookstore", collection, null, item);
            }

            // get all the items just inserted
            var getAllResult = await repo.GetAllAsync("bookstore", collection, start, size);

            var o = JArray.Parse(getAllResult);

            var jArray = JArray.Parse(getAllResult);
            var users  = jArray.Select(jt => jt.ToObject <User>()).ToList();

            Assert.Equal(expectedCount, users.Count);
        }
Exemple #3
0
        public async Task Insert_Multiple_Objects_Fail_Malformed_Json()
        {
            var collectionName = "orders4";
            var repo           = new MongoService(_fixture.MongoClient, _fixture.Logger);

            var items = new List <string>()
            {
                "{ \"title\": \"The Red Badge of Courage\" }",
                "{ \"title\": \"Don Quixote\" }",
                "{ \"title\": \"The Grapes of Wrath\" }",
                "{ \"title\": \"The Catcher in the Rye\" }",
                " \"title\": \"Slaughterhouse-Five\" }", // bad!
                "{ \"title\": \"Of Mice and Men\" }",
                "{ \"title\": \"Gone with the Wind\" }",
                "{ \"title\": \"Fahrenheit 451\" }",
                "{ \"title\": \"The Old Man and the Sea\" }",
                "{ \"title\": \"The Great Gatsby\" }"
            };

            try
            {
                var insertManyResult = await repo.InsertManyAsync("bookstore", collectionName, items);

                throw new InvalidOperationException();
            }
            catch (Exception ex)
            {
                Assert.IsType <System.FormatException>(ex);
            }

            // Try getting items in collection, ensure nothing was inserted
            var getCollectionResult = await repo.GetAllAsync("bookstore", collectionName);

            Assert.Equal(404, getCollectionResult.Status);
        }
Exemple #4
0
        public async Task Get_Collection()
        {
            var            collectionName = "orders2";
            IObjectService service        = new MongoService(_fixture.MongoClient, _fixture.Logger);

            var items = new List <string>()
            {
                "{ \"title\": \"The Red Badge of Courage\" }",
                "{ \"title\": \"Don Quixote\" }",
                "{ \"title\": \"The Grapes of Wrath\" }",
                "{ \"title\": \"The Catcher in the Rye\" }",
                "{ \"title\": \"Slaughterhouse-Five\" }",
                "{ \"title\": \"Of Mice and Men\" }",
                "{ \"title\": \"Gone with the Wind\" }",
                "{ \"title\": \"Fahrenheit 451\" }",
                "{ \"title\": \"The Old Man and the Sea\" }",
                "{ \"title\": \"The Great Gatsby\" }"
            };

            var insertedTitles     = new Dictionary <string, string>();
            int insertedItemsCount = 0;

            foreach (var item in items)
            {
                var insertResult = await service.InsertAsync("bookstore", collectionName, item);

                if (insertResult.Status == 201)
                {
                    insertedItemsCount++;
                    JObject obj   = JObject.Parse(insertResult.Value);
                    var     id    = obj["_id"].ToString();
                    var     title = obj["title"].ToString();
                    insertedTitles.Add(id, title);
                }
                else
                {
                    Assert.True(false); // should not happen!
                }
            }

            Assert.Equal(items.Count, insertedItemsCount); // test that all inserts worked as expected

            // Try getting items in collection
            var getCollectionResult = await service.GetAllAsync("bookstore", collectionName);

            Assert.Equal(200, getCollectionResult.Status);

            Assert.Equal(items.Count, getCollectionResult.Value.Count());

            foreach (var item in getCollectionResult.Value)
            {
                JObject obj   = JObject.Parse(item);
                var     title = obj["title"].ToString();
                var     id    = obj["_id"].ToString();
                Assert.NotNull(title);
                Assert.Equal(insertedTitles[id], title);
            }
        }
Exemple #5
0
        public async Task Insert_Multiple_Objects()
        {
            var            collectionName = "orders3";
            IObjectService service        = new MongoService(_fixture.MongoClient, _fixture.Logger);

            var items = new List <string>()
            {
                "{ \"title\": \"The Red Badge of Courage\" }",
                "{ \"title\": \"Don Quixote\" }",
                "{ \"title\": \"The Grapes of Wrath\" }",
                "{ \"title\": \"The Catcher in the Rye\" }",
                "{ \"title\": \"Slaughterhouse-Five\" }",
                "{ \"title\": \"Of Mice and Men\" }",
                "{ \"title\": \"Gone with the Wind\" }",
                "{ \"title\": \"Fahrenheit 451\" }",
                "{ \"title\": \"The Old Man and the Sea\" }",
                "{ \"title\": \"The Great Gatsby\" }"
            };

            var insertManyResult = await service.InsertManyAsync("bookstore", collectionName, items);

            Assert.Equal(201, insertManyResult.Status);
            Assert.Equal(10, insertManyResult.Value.Count());

            var ids = new HashSet <string>();

            foreach (var id in insertManyResult.Value)
            {
                ids.Add(id);
            }

            // Try getting items in collection
            var getCollectionResult = await service.GetAllAsync("bookstore", collectionName);

            Assert.Equal(200, getCollectionResult.Status);
            Assert.Equal(10, getCollectionResult.Value.Count());

            foreach (var iItem in items)
            {
                JObject iObject = JObject.Parse(iItem);
                var     iTitle  = iObject["title"].ToString();

                bool found = false;
                foreach (var jItem in getCollectionResult.Value)
                {
                    JObject jObject = JObject.Parse(jItem);
                    var     jTitle  = jObject["title"].ToString();
                    var     jId     = jObject["_id"]["$oid"].ToString();

                    Assert.NotNull(jTitle);

                    if (iTitle == jTitle && ids.Contains(jId))
                    {
                        found = true;
                        break;
                    }
                }

                Assert.True(found);
            }
        }