Exemplo n.º 1
0
        public async void Test_GetBooksAsync_Verify()
        {
            //Arrange
            var mockContext    = new Mock <IMongoContext>();
            var mockCollection = CreateMockCollection();
            var collection     = mockCollection.Object;

            Book document = new Book();

            document.Author   = "asdas";
            document.BookName = "sadasd";
            document.Id       = ObjectId.GenerateNewId().ToString();
            mockCollection.Object.InsertOne(document);

            BookstoreDatabaseSettings setting = new BookstoreDatabaseSettings();

            setting.BooksCollectionName = "Book";
            var mockIDBsettings = new Mock <IOptions <BookstoreDatabaseSettings> >();

            mockContext.Setup(x => x.GetCollection <Book>("Book")).Returns(mockCollection.Object);
            mockIDBsettings.Setup(x => x.Value).Returns(setting);

            //Act

            BookService service = new BookService(mockIDBsettings.Object, mockContext.Object);
            var         result  = await service.GetBooksAsync();

            //ASSERT
            mockContext.Verify(x => x.GetCollection <Book>("Book"), Times.Once);
            mockCollection.Verify(m => m.FindAsync <Book>(FilterDefinition <Book> .Empty, null, CancellationToken.None), Times.Once);
        }
Exemplo n.º 2
0
        public void Test3()
        {
            var mockClient     = new Mock <IMongoClient>();
            var mockContext    = new Mock <IMongoContext>();
            var client         = mockClient.Object;
            var mockCollection = CreateMockCollection();
            var collection     = mockCollection.Object;
            var session        = new Mock <IClientSessionHandle>().Object;

            var options           = new ChangeStreamOptions();
            var cancellationToken = new CancellationTokenSource().Token;

            Book document = new Book();

            document.Author   = "asdas";
            document.BookName = "sadasd";

            document.Id = ObjectId.GenerateNewId().ToString();

            collection.InsertOne(document);

            mockContext.Setup(x => x.GetCollection <Book>("Book")).Returns(collection);


            BookstoreDatabaseSettings setting = new BookstoreDatabaseSettings();

            setting.BooksCollectionName = "Book";
            var mockIDBsettings = new Mock <IOptions <BookstoreDatabaseSettings> >();

            mockIDBsettings.Setup(x => x.Value).Returns(setting);

            BookService service = new BookService(mockIDBsettings.Object, mockContext.Object);
            var         result  = service.Create(document);

            mockContext.Verify(x => x.GetCollection <Book>("Book"), Times.Once);

            var filterDefinition = Builders <Book> .Filter.Eq("BookName", "sadasd");

            var options1 = new FindOptions <Book>(); // no projection


            var filterExpression = (Expression <Func <Book, bool> >)(x => x.BookName == "sadasd");

            //FindFluent<Book, Book> fluent;

            //mockCollection.Verify(x=>x.Find(filterExpression, null), Times.Once);

            mockCollection.Verify(m => m.FindAsync <Book>(It.IsAny <ExpressionFilterDefinition <Book> >(), options1, cancellationToken), Times.Once);
        }
Exemplo n.º 3
0
        private void ConfigureDatabase(IServiceCollection services)
        {
            string connectionString    = Environment.GetEnvironmentVariable("CONNECTION_STRING");
            string databaseName        = Environment.GetEnvironmentVariable("BOOK_DATABASE");
            string booksCollectionName = Environment.GetEnvironmentVariable("BOOKS_COLLECTION");

            BookstoreDatabaseSettings bookstoreDatabaseSettings = new BookstoreDatabaseSettings()
            {
                BooksCollectionName = booksCollectionName,
                ConnectionString    = connectionString,
                DatabaseName        = databaseName
            };

            services.AddSingleton <IBookstoreDatabaseSettings>(bookstoreDatabaseSettings);
        }
Exemplo n.º 4
0
        public void Test2()
        {
            var mockClient     = new Mock <IMongoClient>();
            var mockContext    = new Mock <IMongoContext>();
            var client         = mockClient.Object;
            var mockCollection = CreateMockCollection();
            var collection     = mockCollection.Object;
            var session        = new Mock <IClientSessionHandle>().Object;

            var options           = new ChangeStreamOptions();
            var cancellationToken = new CancellationTokenSource().Token;

            Book document = new Book();

            document.Author   = "asdas";
            document.BookName = "sadasd";

            document.Id = ObjectId.GenerateNewId().ToString();

            collection.InsertOne(document);

            mockContext.Setup(x => x.GetCollection <Book>("Book")).Returns(collection);


            BookstoreDatabaseSettings setting = new BookstoreDatabaseSettings();

            setting.BooksCollectionName = "Book";
            var mockIDBsettings = new Mock <IOptions <BookstoreDatabaseSettings> >();

            mockIDBsettings.Setup(x => x.Value).Returns(setting);

            BookService service = new BookService(mockIDBsettings.Object, mockContext.Object);
            var         result  = service.Create(document);

            mockContext.Verify(x => x.GetCollection <Book>("Book"), Times.Once);
        }
Exemplo n.º 5
0
        public void Test_GetBooks_Verify()
        {
            //Arrange
            var  mockContext    = new Mock <IMongoContext>();
            var  mockCollection = CreateMockCollection();
            Book document       = new Book();

            document.Author   = "asdas";
            document.BookName = "sadasd";
            document.Id       = ObjectId.GenerateNewId().ToString();
            mockCollection.Object.InsertOne(document);

            BookstoreDatabaseSettings setting = new BookstoreDatabaseSettings();

            setting.BooksCollectionName = "Book";
            var mockIDBsettings = new Mock <IOptions <BookstoreDatabaseSettings> >();

            mockContext.Setup(x => x.GetCollection <Book>("Book")).Returns(mockCollection.Object);
            mockIDBsettings.Setup(x => x.Value).Returns(setting);
            var filterExpression1 = (Expression <Func <Book, bool> >)(x => x.Id == document.Id);


            //        var expectedEntities = new List<Book>();
            //        expectedEntities.Add(document);
            //        var mockCursor = new Mock<IAsyncCursor<Book>>();
            //        mockCursor.Setup(_ => _.Current).Returns(expectedEntities); //<-- Note the entities here
            //        mockCursor
            //            .SetupSequence(_ => _.MoveNext(It.IsAny<CancellationToken>()))
            //            .Returns(true)
            //            .Returns(false);
            //        mockCursor
            //            .SetupSequence(_ => _.MoveNextAsync(It.IsAny<CancellationToken>()))
            //            .Returns(Task.FromResult(true))
            //            .Returns(Task.FromResult(false));

            //        mockCollection.Setup(x => x.FindSync<Book>(
            //        filterExpression1,
            //        null,
            //        CancellationToken.None
            //    ))
            //.Returns(mockCursor.Object);

            //Act

            BookService service = new BookService(mockIDBsettings.Object, mockContext.Object);
            var         result  = service.Get(document.Id);

            //ASSERT
            mockContext.Verify(x => x.GetCollection <Book>("Book"), Times.Once);

            var bookMock = new Mock <IFindFluent <Book, Book> >();
            //mockCollection.Verify(m => m.Find<Book>(FilterDefinition<Book>.Empty, null, CancellationToken.None), Times.Once);
            var options           = new FindOptions <Book>();
            var cancellationToken = new CancellationTokenSource().Token;
            var filterDefinition  = Builders <Book> .Filter.Eq("BookName", "sadasd");

            var filters = FilterDefinition <Book> .Equals("BookName", "sadasd");

            //mockCollection.Verify(m => m.FindSync<Book>(filterDefinition, options, cancellationToken));

            var filterExpression = (Expression <Func <Book, bool> >)(x => x.Id == document.Id);



            Expression <Func <Book, bool> > predicate = x => x.BookName == "sadasd";

            // Expression<Func<Person, bool>> predicate = x => x.Name == captured[0];

            mockCollection.Verify(m => m.FindSync <Book>(predicate, null, CancellationToken.None), Times.Once);
        }
Exemplo n.º 6
0
 public BookService(BookstoreDatabaseSettings settings) : base(settings)
 {
 }
Exemplo n.º 7
0
 public BookController(IOptionsMonitor <Appsetting> _Appsetting)
 {
     bookstoreDatabaseSettings = _Appsetting?.CurrentValue?.BookstoreDatabaseSettings;
 }