Пример #1
0
        public async Task EnsureCreatedCreatesCollectionIfMissing()
        {
            // Arrange
            IOrderedQueryable <DocumentCollection> queryable = new EnumerableQuery <DocumentCollection>(new List <DocumentCollection>());

            _mockDocumentClient.Setup(x =>
                                      x.CreateDocumentCollectionQuery(It.IsAny <string>(), It.IsAny <FeedOptions>())).Returns(queryable);
            var collection = new DocumentCollection {
                Id = "collection"
            };
            var collectionResponse = collection.ToResourceResponse(HttpStatusCode.OK);
            var databaseUri        = UriFactory.CreateDatabaseUri("databaseName");

            _mockDocumentClient.Setup(x => x.CreateDocumentCollectionAsync(It.Is <Uri>(uri => uri == databaseUri),
                                                                           It.Is <DocumentCollection>(coll => coll.Id == collection.Id),
                                                                           It.IsAny <RequestOptions>())).ReturnsAsync(collectionResponse);

            var creator = new CosmosCollectionCreator(new CosmonautClient(_mockDocumentClient.Object));

            // Act
            var result = await creator.EnsureCreatedAsync <Dummy>("databaseName", collection.Id, 500, new JsonSerializerSettings());

            // Assert
            result.Should().BeTrue();
        }
Пример #2
0
        public static Mock <IDocumentClient> GetMockDocumentClient(string databaseName = "databaseName", string collectionName = "dummies")
        {
            var mockDocumentClient = new Mock <IDocumentClient>();
            var database           = new Database {
                Id = databaseName
            };
            var collection = new DocumentCollection
            {
                Id = collectionName
            };

            collection.SetPropertyValue("resource", "docs");
            collection.SetPropertyValue("_self", "docs");
            var mockOffer = new Mock <Offer>();

            mockOffer.Object.SetPropertyValue("resource", "docs");
            mockOffer.Object.SetResourceTimestamp(DateTime.UtcNow);
            mockDocumentClient.Setup(x => x.AuthKey).Returns(new SecureString());
            mockDocumentClient.Setup(x => x.ServiceEndpoint).Returns(new Uri("http://test.com"));
            mockDocumentClient.Setup(x => x.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(databaseName), null))
            .ReturnsAsync(database.ToResourceResponse(HttpStatusCode.OK));
            mockDocumentClient.Setup(x => x.ReadDocumentCollectionAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collectionName), null))
            .ReturnsAsync(collection.ToResourceResponse(HttpStatusCode.OK));
            mockDocumentClient.Setup(x => x.CreateDatabaseQuery(null))
            .Returns(new EnumerableQuery <Database>(new List <Database> {
                database
            }));
            mockDocumentClient.Setup(x => x.CreateDocumentCollectionQuery(It.IsAny <string>(), null))
            .Returns(new EnumerableQuery <DocumentCollection>(new List <DocumentCollection> {
                collection
            }));
            var offerV2 = new OfferV2(mockOffer.Object, 400);

            offerV2.SetResourceTimestamp(DateTime.UtcNow);
            mockDocumentClient.Setup(x => x.CreateOfferQuery(null)).Returns(
                new EnumerableQuery <OfferV2>(new List <OfferV2>
            {
                offerV2
            }));
            return(mockDocumentClient);
        }