public void ResourceResponseStreamingTest() { using (DocumentClient client = TestCommon.CreateClient(true)) { Database db = client.CreateDatabaseAsync(new Database() { Id = Guid.NewGuid().ToString() }).Result.Resource; DocumentCollection coll = TestCommon.CreateCollectionAsync(client, db, new DocumentCollection() { Id = Guid.NewGuid().ToString() }).Result; ResourceResponse <Document> doc = client.CreateDocumentAsync(coll.SelfLink, new Document() { Id = Guid.NewGuid().ToString() }).Result; Assert.AreEqual(doc.ResponseStream.Position, 0); StreamReader streamReader = new StreamReader(doc.ResponseStream); string text = streamReader.ReadToEnd(); Assert.AreEqual(doc.ResponseStream.Position, doc.ResponseStream.Length); try { doc.Resource.ToString(); Assert.Fail("Deserializing Resource here should throw exception since the stream was already read"); } catch (JsonReaderException ex) { Console.WriteLine("Expected exception while deserializing Resource: " + ex.Message); } } }
internal async Task TestEtagOnUpsertOperation(bool useGateway, Protocol protocol = Protocol.Tcp) { using (DocumentClient client = TestCommon.CreateClient(false, Protocol.Tcp)) { Database db = (await client.CreateDatabaseAsync(new Database() { Id = Guid.NewGuid().ToString() })).Resource; DocumentCollection coll = await TestCommon.CreateCollectionAsync(client, db, new DocumentCollection() { Id = Guid.NewGuid().ToString(), PartitionKey = new PartitionKeyDefinition() { Paths = new System.Collections.ObjectModel.Collection <string>() { "/id" } } }); LinqGeneralBaselineTests.Book myBook = new LinqGeneralBaselineTests.Book(); myBook.Id = Guid.NewGuid().ToString(); myBook.Title = "Azure DocumentDB 101"; Document doc = (await client.CreateDocumentAsync(coll.SelfLink, myBook)).Resource; myBook.Title = "Azure DocumentDB 201"; await client.ReplaceDocumentAsync(doc.SelfLink, myBook); AccessCondition condition = new AccessCondition(); condition.Type = AccessConditionType.IfMatch; condition.Condition = doc.ETag; RequestOptions requestOptions = new RequestOptions(); requestOptions.AccessCondition = condition; myBook.Title = "Azure DocumentDB 301"; try { await client.UpsertDocumentAsync(coll.SelfLink, myBook, requestOptions); Assert.Fail("Upsert Document should fail since the Etag is not matching."); } catch (Exception ex) { DocumentClientException innerException = ex as DocumentClientException; Assert.AreEqual(HttpStatusCode.PreconditionFailed, innerException.StatusCode, "Invalid status code"); } } }
public async Task ResourceResponseStreamingTest() { using (DocumentClient client = TestCommon.CreateClient(true)) { Database db = (await client.CreateDatabaseAsync(new Database() { Id = Guid.NewGuid().ToString() })).Resource; DocumentCollection coll = await TestCommon.CreateCollectionAsync(client, db, new DocumentCollection() { Id = Guid.NewGuid().ToString(), PartitionKey = new PartitionKeyDefinition() { Paths = new System.Collections.ObjectModel.Collection <string>() { "/id" } } }); ResourceResponse <Document> doc = await client.CreateDocumentAsync(coll.SelfLink, new Document() { Id = Guid.NewGuid().ToString() }); Assert.AreEqual(doc.ResponseStream.Position, 0); StreamReader streamReader = new StreamReader(doc.ResponseStream); string text = streamReader.ReadToEnd(); Assert.AreEqual(doc.ResponseStream.Position, doc.ResponseStream.Length); try { doc.Resource.ToString(); Assert.Fail("Deserializing Resource here should throw exception since the stream was already read"); } catch (JsonReaderException ex) { Console.WriteLine("Expected exception while deserializing Resource: " + ex.Message); } } }
internal void TestEtagOnUpsertOperation(DocumentClient client) { Database db = client.CreateDatabaseAsync(new Database() { Id = Guid.NewGuid().ToString() }).Result.Resource; DocumentCollection coll = TestCommon.CreateCollectionAsync(client, db, new DocumentCollection() { Id = Guid.NewGuid().ToString() }).Result; LinqGeneralBaselineTests.Book myBook = new LinqGeneralBaselineTests.Book(); myBook.Id = Guid.NewGuid().ToString(); myBook.Title = "Azure DocumentDB 101"; Document doc = client.CreateDocumentAsync(coll.SelfLink, myBook).Result.Resource; myBook.Title = "Azure DocumentDB 201"; client.ReplaceDocumentAsync(doc.SelfLink, myBook).Wait(); AccessCondition condition = new AccessCondition(); condition.Type = AccessConditionType.IfMatch; condition.Condition = doc.ETag; RequestOptions requestOptions = new RequestOptions(); requestOptions.AccessCondition = condition; myBook.Title = "Azure DocumentDB 301"; try { client.UpsertDocumentAsync(coll.SelfLink, myBook, requestOptions).Wait(); Assert.Fail("Upsert Document should fail since the Etag is not matching."); } catch (Exception ex) { var innerException = ex.InnerException as DocumentClientException; Assert.IsTrue(innerException.StatusCode == HttpStatusCode.PreconditionFailed, "Invalid status code"); } }
internal static async Task PartitionedCollectionSmokeTest(DocumentClient client, bool sharedOffer = false, bool sharedThroughputCollections = false, int numberOfCollections = 1) { if (!sharedOffer && sharedThroughputCollections) { throw new ArgumentException("Shared throughput collections are not supported without shared offer"); } string uniqDatabaseName = string.Format("SmokeTest_{0}", Guid.NewGuid().ToString("N")); RequestOptions options = new RequestOptions { OfferThroughput = 50000 }; Database database = sharedOffer ? await client.CreateDatabaseAsync(new Database { Id = uniqDatabaseName }, options) : await client.CreateDatabaseAsync(new Database { Id = uniqDatabaseName }); Assert.AreEqual(database.AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName)); Database readbackdatabase = await client.ReadDatabaseAsync(database.SelfLink); List <dynamic> results = await ClientTestsUtils.SqlQueryDatabases(client, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDatabaseName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(database.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.IsTrue((await ClientTestsUtils.ReadFeedDatabases(client)).Any((db) => db.Id == uniqDatabaseName)); results = await ClientTestsUtils.SqlQueryDatabases(client, string.Format(@"select r._rid, r.id from root r where r.id = ""{0}""", uniqDatabaseName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 database"); Assert.AreEqual(database.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(database.ResourceId, (await client.ReadDatabaseAsync(database.SelfLink)).Resource.ResourceId); Assert.AreEqual(((Database)results[0]).AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName)); ArrayList testCollections = new ArrayList(); for (int i = 0; i < numberOfCollections; i++) { string uniqCollectionName = "SmokeTestCollection" + Guid.NewGuid().ToString("N"); var partitionKeyDefinition = new PartitionKeyDefinition { Paths = new System.Collections.ObjectModel.Collection <string> { "/id" }, Kind = PartitionKind.Hash }; DocumentCollection collection; if (sharedThroughputCollections) { collection = await TestCommon.CreateCollectionAsync(client, database.SelfLink, new DocumentCollection { Id = uniqCollectionName, PartitionKey = partitionKeyDefinition }); } else { collection = await TestCommon.CreateCollectionAsync(client, database.SelfLink, new DocumentCollection { Id = uniqCollectionName, PartitionKey = partitionKeyDefinition }, options); } Assert.AreEqual(collection.AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, typeof(DocumentCollection))); results = await SqlQueryCollections(client, database.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqCollectionName), 10); // query through database link Assert.AreEqual(1, results.Count, "Should have queried and found 1 collection"); Assert.AreEqual(collection.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryCollections(client, database.CollectionsLink, string.Format(@"select r._rid, r.id from root r where r.id = ""{0}""", uniqCollectionName), 10); // query through CollectionsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 collection"); Assert.AreEqual(collection.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedCollections(client, database.SelfLink)).Count(item => item.Id == uniqCollectionName)); // read through database link Assert.AreEqual(1, (await ReadFeedCollections(client, database.CollectionsLink)).Count(item => item.Id == uniqCollectionName)); // read through CollectionsLink Assert.AreEqual(collection.ResourceId, (await client.ReadDocumentCollectionAsync(collection.SelfLink)).Resource.ResourceId); Assert.AreEqual(((DocumentCollection)results[0]).AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, typeof(DocumentCollection))); testCollections.Add(collection); string uniqDocumentName = "SmokeTestDocument" + Guid.NewGuid().ToString("N"); LinqGeneralBaselineTests.Book myDocument = new LinqGeneralBaselineTests.Book(); myDocument.Id = uniqDocumentName; myDocument.Title = "My Book"; //Simple Property. myDocument.Languages = new LinqGeneralBaselineTests.Language[] { new LinqGeneralBaselineTests.Language { Name = "English", Copyright = "London Publication" }, new LinqGeneralBaselineTests.Language { Name = "French", Copyright = "Paris Publication" } }; //Array Property myDocument.Author = new LinqGeneralBaselineTests.Author { Name = "Don", Location = "France" }; //Complex Property myDocument.Price = 9.99; myDocument.Editions = new List <LinqGeneralBaselineTests.Edition>() { new LinqGeneralBaselineTests.Edition() { Name = "First", Year = 2001 }, new LinqGeneralBaselineTests.Edition() { Name = "Second", Year = 2005 } }; Document document = await client.CreateDocumentAsync(collection.SelfLink, myDocument); Assert.AreEqual(document.AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, uniqDocumentName, typeof(Document))); results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDocumentName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryDocuments(client, collection.DocumentsLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDocumentName), 10); // query through DocumentsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedDocuments(client, collection.SelfLink)).Count(item => item.Id == uniqDocumentName)); // read through collection link Assert.AreEqual(1, (await ReadFeedDocuments(client, collection.DocumentsLink)).Count(item => item.Id == uniqDocumentName)); // read through DocumentsLink if (client.QueryCompatibilityMode != QueryCompatibilityMode.SqlQuery) { //Test query with parameters results = await SqlQueryDocuments(client, collection.SelfLink, new SqlQuerySpec { QueryText = @"select r._rid from root r where r.id = @id", Parameters = new SqlParameterCollection() { new SqlParameter("@id", uniqDocumentName) } }, 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); } RequestOptions docReplaceRequestOptions = new RequestOptions { PartitionKey = new PartitionKey(document.Id) }; FeedOptions docReplaceFeedOptions = new FeedOptions { EnableCrossPartitionQuery = true, PartitionKey = new PartitionKey(document.Id) }; myDocument.Title = "My_Book_v2"; document = await client.ReplaceDocumentAsync(document.AltLink, myDocument); results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqDocumentName), 10, docReplaceFeedOptions); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(document.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqDocumentName), 10); // query through collection Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); results = await SqlQueryDocuments(client, collection.DocumentsLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqDocumentName), 10); // query through DocumentsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); Assert.AreEqual(((Document)results[0]).AltLink, ClientTestsUtils.GenerateAltLink(uniqDatabaseName, uniqCollectionName, uniqDocumentName, typeof(Document))); // No Range Index on ts - override with scan FeedOptions queryFeedOptions1 = new FeedOptions() { EnableScanInQuery = true, EnableCrossPartitionQuery = true }; results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.name FROM root r WHERE r.Price>0"), 10, queryFeedOptions1); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); FeedOptions queryFeedOptions2 = new FeedOptions() { EmitVerboseTracesInQuery = true, EnableCrossPartitionQuery = true }; results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.name FROM root r WHERE r.Price=9.99"), 10, queryFeedOptions2); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); FeedOptions queryFeedOptions3 = new FeedOptions() { EmitVerboseTracesInQuery = false, EnableCrossPartitionQuery = true }; results = await SqlQueryDocuments(client, collection.SelfLink, string.Format(@"SELECT r.name FROM root r WHERE r.Price=9.99"), 10, queryFeedOptions3); Assert.AreEqual(1, results.Count, "Should have queried and found 1 document"); string uniqStoredProcedureName = "SmokeTestStoredProcedure" + Guid.NewGuid().ToString(); StoredProcedure storedProcedure = await client.CreateStoredProcedureAsync(collection.SelfLink, new StoredProcedure { Id = uniqStoredProcedureName, Body = "function f() {var x = 10;}" }); results = await SqlQueryStoredProcedures(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id = ""{0}""", uniqStoredProcedureName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); Assert.AreEqual(storedProcedure.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryStoredProcedures(client, collection.StoredProceduresLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id = ""{0}""", uniqStoredProcedureName), 10); // query through StoredProceduresLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); Assert.AreEqual(storedProcedure.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedStoredProcedures(client, collection.SelfLink)).Count(item => item.Id == uniqStoredProcedureName)); // read through collection link Assert.AreEqual(1, (await ReadFeedStoredProcedures(client, collection.StoredProceduresLink)).Count(item => item.Id == uniqStoredProcedureName)); // read through StoredProceduresLink storedProcedure.Body = "function f() {var x= 20;}"; storedProcedure = await client.ReplaceStoredProcedureAsync(storedProcedure); results = await SqlQueryStoredProcedures(client, collection.StoredProceduresLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", storedProcedure.Id), 10); // query through StoredProceduresLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); Assert.AreEqual(storedProcedure.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(storedProcedure.ResourceId, (await client.ReadStoredProcedureAsync(storedProcedure.SelfLink)).Resource.ResourceId); Assert.AreEqual(1, results.Count, "Should have queried and found 1 storedProcedure"); string uniqTriggerName = "SmokeTestTrigger" + Guid.NewGuid().ToString("N"); Trigger trigger = await client.CreateTriggerAsync(collection.SelfLink, new Trigger { Id = uniqTriggerName, Body = "function f() {var x = 10;}", TriggerOperation = TriggerOperation.All, TriggerType = TriggerType.Pre }); results = await SqlQueryTriggers(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqTriggerName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); Assert.AreEqual(trigger.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryTriggers(client, collection.TriggersLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqTriggerName), 10); // query through TriggersLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); Assert.AreEqual(trigger.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedTriggers(client, collection.SelfLink)).Count(item => item.Id == uniqTriggerName)); // read through collection link Assert.AreEqual(1, (await ReadFeedTriggers(client, collection.TriggersLink)).Count(item => item.Id == uniqTriggerName)); // read through TriggersLink trigger.Body = "function f() {var x = 10;}"; trigger = await client.ReplaceTriggerAsync(trigger); results = await SqlQueryTriggers(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqTriggerName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); Assert.AreEqual(trigger.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(trigger.ResourceId, (await client.ReadTriggerAsync(trigger.SelfLink)).Resource.ResourceId); results = await SqlQueryTriggers(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqTriggerName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); results = await SqlQueryTriggers(client, collection.TriggersLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqTriggerName), 10); // query through TriggersLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 trigger"); string uniqUserDefinedFunctionName = "SmokeTestUserDefinedFunction" + Guid.NewGuid().ToString("N"); UserDefinedFunction userDefinedFunction = await client.CreateUserDefinedFunctionAsync(collection.SelfLink, new UserDefinedFunction { Id = uniqUserDefinedFunctionName, Body = "function (){ var x = 10;}" }); results = await SqlQueryUserDefinedFunctions(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqUserDefinedFunctionName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); Assert.AreEqual(userDefinedFunction.ResourceId, ((QueryResult)results[0]).ResourceId); results = await SqlQueryUserDefinedFunctions(client, collection.UserDefinedFunctionsLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqUserDefinedFunctionName), 10); // query through UserDefinedFunctionsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); Assert.AreEqual(userDefinedFunction.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(1, (await ReadFeedUserDefinedFunctions(client, collection.SelfLink)).Count(item => item.Id == uniqUserDefinedFunctionName)); // read through collection link Assert.AreEqual(1, (await ReadFeedUserDefinedFunctions(client, collection.UserDefinedFunctionsLink)).Count(item => item.Id == uniqUserDefinedFunctionName)); // read through UserDefinedFunctionsLink userDefinedFunction.Body = "function (){ var x = 10;}"; userDefinedFunction = await client.ReplaceUserDefinedFunctionAsync(userDefinedFunction); results = await SqlQueryUserDefinedFunctions(client, collection.SelfLink, string.Format(@"select r._rid from root r where r.id = ""{0}""", uniqUserDefinedFunctionName), 10); Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); Assert.AreEqual(userDefinedFunction.ResourceId, ((QueryResult)results[0]).ResourceId); Assert.AreEqual(userDefinedFunction.ResourceId, (await client.ReadUserDefinedFunctionAsync(userDefinedFunction.SelfLink)).Resource.ResourceId); results = await SqlQueryUserDefinedFunctions(client, collection.SelfLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqUserDefinedFunctionName), 10); // query through collection link Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); results = await SqlQueryUserDefinedFunctions(client, collection.UserDefinedFunctionsLink, string.Format(@"SELECT r.id, r._rid FROM root r WHERE r.id=""{0}""", uniqUserDefinedFunctionName), 10); // query through UserDefinedFunctionsLink Assert.AreEqual(1, results.Count, "Should have queried and found 1 userDefinedFunction"); //Test select array var queryArray = client.CreateDocumentQuery(collection.SelfLink, "SELECT VALUE [1, 2, 3, 4]").AsDocumentQuery(); JArray result = queryArray.ExecuteNextAsync().Result.FirstOrDefault(); Assert.AreEqual(result[0], 1); Assert.AreEqual(result[1], 2); Assert.AreEqual(result[2], 3); Assert.AreEqual(result[3], 4); RequestOptions requestOptions = new RequestOptions { PartitionKey = new PartitionKey(document.Id) }; await client.DeleteDocumentAsync(document.SelfLink, requestOptions); } foreach (DocumentCollection collection in testCollections) { await client.DeleteDocumentCollectionAsync(collection.SelfLink); } await client.DeleteDatabaseAsync(database.SelfLink); }