示例#1
0
        public async Task Delete_Collection(string id, string json)
        {
            // Arrange
            var controller     = new ObjectController(_fixture.MongoRepository);
            var collectionName = "orders1";

            // Try getting items in the collection. Collection doesn't exist yet, should get a 404
            var getFirstCollectionResult = await controller.GetAllObjectsInCollection(
                new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName },
                new PaginationQueryParameters { Start = 0, Limit = Int32.MaxValue });

            ObjectResult getFirstCollectionMvcResult = ((ObjectResult)getFirstCollectionResult);

            Assert.Equal(404, getFirstCollectionMvcResult.StatusCode);

            // Add an item to collection; Mongo auto-creates the collection
            var insertResult = await controller.InsertObjectWithId(new ItemRouteParameters()
            {
                DatabaseName = DATABASE_NAME, CollectionName = collectionName, Id = id
            }, json, ResponseFormat.OnlyId);

            // // Try getting items in collection a 2nd time. Now it should return a 200.
            var getSecondCollectionResult = await controller.GetAllObjectsInCollection(
                new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName },
                new PaginationQueryParameters { Start = 0, Limit = Int32.MaxValue });

            OkObjectResult getSecondCollectionMvcResult = ((OkObjectResult)getSecondCollectionResult);

            Assert.Equal(200, getSecondCollectionMvcResult.StatusCode);

            // Delete the collection
            var deleteCollectionResult = await controller.DeleteCollection(new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName });

            var deleteCollectionMvcResult = ((OkResult)deleteCollectionResult);

            Assert.Equal(200, deleteCollectionMvcResult.StatusCode);

            // Try getting items in collection a 3rd time. It was just deleted so we should get a 404.
            var getThirdCollectionResult = await controller.GetAllObjectsInCollection(
                new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName },
                new PaginationQueryParameters { Start = 0, Limit = Int32.MaxValue });

            ObjectResult getThirdCollectionMvcResult = ((ObjectResult)getThirdCollectionResult);

            Assert.Equal(404, getThirdCollectionMvcResult.StatusCode);
        }
示例#2
0
        public async Task Insert_Multiple_Objects()
        {
            // Arrange
            var controller     = new ObjectController(_fixture.MongoRepository);
            var collectionName = "orders3";

            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 payload = "[" + string.Join(',', items) + "]";

            var insertManyResult = await controller.MultiInsert(new ItemRouteParameters()
            {
                DatabaseName = DATABASE_NAME, CollectionName = collectionName
            }, payload);

            var insertManyMvcResult = ((OkObjectResult)insertManyResult);

            Assert.Equal(200, insertManyMvcResult.StatusCode);

            // Try getting items in collection
            var getCollectionResult = await controller.GetAllObjectsInCollection(
                new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName },
                new PaginationQueryParameters { Start = 0, Limit = Int32.MaxValue });

            var getCollectionMvcResult = ((OkObjectResult)getCollectionResult);

            Assert.Equal(200, getCollectionMvcResult.StatusCode);

            var array = JArray.Parse(getCollectionMvcResult.Value.ToString());

            Assert.Equal(items.Count, array.Count);

            int i = 0;

            foreach (var item in array.Children())
            {
                Assert.NotNull(item["title"]);
                Assert.NotNull(item["_id"]);
                Assert.True(items[i].Contains(item["title"].ToString()));
                Assert.Null(item["name"]);
                i++;
            }
        }
示例#3
0
        public async Task Get_All_in_Collection_Pagination(string collectionName, int start, int limit, int expectedCount)
        {
            var controller = new ObjectController(_fixture.MongoRepository);

            var items = new List <string>()
            {
                "{ 'title': 'The Red Badge of Courage', 'author': 'Stephen Crane', 'pages': 112, 'isbn': { 'isbn-10' : '0486264653', 'isbn-13' : '978-0486264653' } }",
                "{ 'title': 'Don Quixote', 'author': 'Miguel De Cervantes', 'pages': 992, 'isbn': { 'isbn-10' : '0060934344', 'isbn-13' : '978-0060934347' } }",
                "{ 'title': 'The Grapes of Wrath', 'author': 'John Steinbeck', 'pages': 464, 'isbn': { 'isbn-10' : '0143039431', 'isbn-13' : '978-0143039433' } }",
                "{ 'title': 'The Catcher in the Rye', 'author': 'J. D. Salinger', 'pages': 288, 'isbn': { 'isbn-10' : '9780316769174', 'isbn-13' : '978-0316769174' } }",
                "{ 'title': 'Slaughterhouse-Five', 'author': 'Kurt Vonnegut', 'pages': 288, 'isbn': { 'isbn-10' : '0812988523', 'isbn-13' : '978-0812988529' } }",
                "{ 'title': 'Of Mice and Men', 'author': 'John Steinbeck', 'pages': 112, 'isbn': { 'isbn-10' : '0140177396', 'isbn-13' : '978-0140177398' } }",
                "{ 'title': 'Gone with the Wind', 'author': 'Margaret Mitchell', 'pages': 960, 'isbn': { 'isbn-10' : '1451635621', 'isbn-13' : '978-1451635621' } }",
                "{ 'title': 'Fahrenheit 451', 'author': 'Ray Bradbury', 'pages': 249, 'isbn': { 'isbn-10' : '9781451673319', 'isbn-13' : '978-1451673319' } }",
                "{ 'title': 'The Old Man and the Sea', 'author': 'Ernest Hemingway', 'pages': 128, 'isbn': { 'isbn-10' : '0684801221', 'isbn-13' : '978-0684801223' } }",
                "{ 'title': 'The Great Gatsby', 'author': 'F. Scott Fitzgerald', 'pages': 180, 'isbn': { 'isbn-10' : '9780743273565', 'isbn-13' : '978-0743273565' } }",
            };

            var payload          = "[" + string.Join(',', items) + "]";
            var insertManyResult = await controller.MultiInsert(new DatabaseRouteParameters()
            {
                DatabaseName = DATABASE_NAME, CollectionName = collectionName
            }, payload);

            var insertManyMvcResult = ((OkObjectResult)insertManyResult);

            Assert.Equal(200, insertManyMvcResult.StatusCode);

            var getAllResult = await controller.GetAllObjectsInCollection(new DatabaseRouteParameters()
            {
                DatabaseName = DATABASE_NAME, CollectionName = collectionName
            }, new PaginationQueryParameters()
            {
                Start = start, Limit = limit
            });

            var getAllMvcResult = ((OkObjectResult)getAllResult);

            Assert.Equal(200, getAllMvcResult.StatusCode);

            var array = JArray.Parse(getAllMvcResult.Value.ToString());

            Assert.Equal(expectedCount, array.Count);

            // Delete the collection
            var deleteCollectionResult = await controller.DeleteCollection(new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName });

            var deleteCollectionMvcResult = ((OkResult)deleteCollectionResult);

            Assert.Equal(200, deleteCollectionMvcResult.StatusCode);
        }
示例#4
0
        public async Task Insert_Multiple_Objects_Fail_Malformed_Json()
        {
            // Arrange
            var controller     = new ObjectController(_fixture.MongoRepository);
            var collectionName = "orders4";

            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 payload = "[" + string.Join(',', items); // missing end bracket!

            try
            {
                var insertManyResult = await controller.MultiInsert(new ItemRouteParameters()
                {
                    DatabaseName = DATABASE_NAME, CollectionName = collectionName
                }, payload);

                throw new InvalidOperationException();
            }
            catch (Exception ex)
            {
                Assert.IsType <Newtonsoft.Json.JsonReaderException>(ex);
            }

            // Try getting items in collection
            var getCollectionResult = await controller.GetAllObjectsInCollection(
                new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName },
                new PaginationQueryParameters { Start = 0, Limit = Int32.MaxValue });

            var getCollectionMvcResult = ((ObjectResult)getCollectionResult);

            Assert.Equal(404, getCollectionMvcResult.StatusCode);
        }
示例#5
0
        public async Task Get_Collection()
        {
            // Arrange
            var controller     = new ObjectController(_fixture.MongoRepository);
            var collectionName = "orders2";

            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\" }"
            };

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

            foreach (var item in items)
            {
                var insertResult = await controller.InsertObjectWithNoId(new ItemRouteParameters()
                {
                    DatabaseName = DATABASE_NAME, CollectionName = collectionName
                }, item, ResponseFormat.EntireObject);

                var createdResult = ((CreatedAtActionResult)insertResult);
                if (createdResult.StatusCode == 201)
                {
                    insertedItemsCount++;
                    JObject obj   = JObject.Parse(createdResult.Value.ToString());
                    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 controller.GetAllObjectsInCollection(
                new DatabaseRouteParameters { DatabaseName = DATABASE_NAME, CollectionName = collectionName },
                new PaginationQueryParameters { Start = 0, Limit = Int32.MaxValue });

            var getCollectionMvcResult = ((OkObjectResult)getCollectionResult);

            Assert.Equal(200, getCollectionMvcResult.StatusCode);

            var array = JArray.Parse(getCollectionMvcResult.Value.ToString());

            Assert.Equal(items.Count, array.Count);

            int i = 0;

            foreach (var item in array.Children())
            {
                var title = item["title"].ToString();
                var id    = item["_id"].ToString();

                Assert.NotNull(title);
                Assert.NotNull(id);
                Assert.True(items[i].Contains(title));
                Assert.Null(item["name"]);
                Assert.Equal(insertedTitles[id], title);
                i++;
            }
        }