Exemplo n.º 1
0
        public async Task Find_Objects_in_Collection(string collectionName, string findExpression, 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 findResult = await controller.FindObjects(new DatabaseRouteParameters()
            {
                DatabaseName = DATABASE_NAME, CollectionName = collectionName
            }, new FindQueryParameters()
            {
                Start = start, Limit = limit
            }, findExpression);

            var findMvcResult = ((OkObjectResult)findResult);

            Assert.Equal(200, findMvcResult.StatusCode);

            var array = JArray.Parse(findMvcResult.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);
        }
Exemplo n.º 2
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);
        }