public async Task DatabaseContractTest() { DatabaseResponse response = await this.CreateDatabaseHelper(); Assert.IsNotNull(response); Assert.IsTrue(response.RequestCharge > 0); Assert.IsNotNull(response.Headers); Assert.IsNotNull(response.Headers.ActivityId); CosmosDatabaseProperties databaseSettings = response.Resource; Assert.IsNotNull(databaseSettings.Id); Assert.IsNotNull(databaseSettings.ResourceId); Assert.IsNotNull(databaseSettings.ETag); Assert.IsTrue(databaseSettings.LastModified.HasValue); Assert.IsTrue(databaseSettings.LastModified.Value > new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), databaseSettings.LastModified.Value.ToString()); CosmosDatabaseCore databaseCore = response.Database as CosmosDatabaseCore; Assert.IsNotNull(databaseCore); Assert.IsNotNull(databaseCore.LinkUri); Assert.IsFalse(databaseCore.LinkUri.ToString().StartsWith("/")); response = await response.Database.DeleteAsync(cancellationToken : this.cancellationToken); Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); }
public void DatabaseStreamDeserialzieTest() { string dbId = "946ad017-14d9-4cee-8619-0cbc62414157"; string rid = "vu9cAA=="; string self = "dbs\\/vu9cAA==\\/"; string etag = "00000000-0000-0000-f8ea-31d6e5f701d4"; double ts = 1555923784; DateTime UnixStartTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); DateTime expected = UnixStartTime.AddSeconds(ts); string testPyaload = "{\"id\":\"" + dbId + "\",\"_rid\":\"" + rid + "\",\"_self\":\"" + self + "\",\"_etag\":\"" + etag + "\",\"_colls\":\"colls\\/\",\"_users\":\"users\\/\",\"_ts\":" + ts + "}"; CosmosDatabaseProperties deserializedPayload = JsonConvert.DeserializeObject <CosmosDatabaseProperties>(testPyaload); Assert.IsTrue(deserializedPayload.LastModified.HasValue); Assert.AreEqual(expected, deserializedPayload.LastModified.Value); Assert.AreEqual(dbId, deserializedPayload.Id); Assert.AreEqual(rid, deserializedPayload.ResourceId); Assert.AreEqual(etag, deserializedPayload.ETag); }
public void DatabaseSettingsDeSerializeTest() { string dbResponsePayload = @"{ _colls : 'dbs/6GoAAA==/colls/', _users: 'dbs/6GoAAA==/users/', id: 'QuickStarts', _rid: '6GoAAA==', _self: 'dbs/6GoAAA==/', _ts: 1530581163, _etag: '00002000-0000-0000-0000-5b3ad0ab0000' }"; CosmosDatabaseProperties databaseSettings = SettingsContractTests.CosmosDeserialize <CosmosDatabaseProperties>(dbResponsePayload); Database db = SettingsContractTests.DirectDeSerialize <Database>(dbResponsePayload); // Not all are exposed in CosmosDatabaseSettings // so lets only validate relevant parts Assert.AreEqual(db.Id, databaseSettings.Id); Assert.AreEqual(db.ETag, databaseSettings.ETag); Assert.AreEqual(db.ResourceId, databaseSettings.ResourceId); Assert.AreEqual("QuickStarts", databaseSettings.Id); Assert.AreEqual("00002000-0000-0000-0000-5b3ad0ab0000", databaseSettings.ETag); Assert.AreEqual("6GoAAA==", databaseSettings.ResourceId); }
private async Task <CosmosDatabase> CreateDatabaseStreamHelper( string databaseId = null, int?throughput = null, bool databaseExists = false) { if (string.IsNullOrEmpty(databaseId)) { databaseId = Guid.NewGuid().ToString(); } CosmosDatabaseProperties databaseSettings = new CosmosDatabaseProperties() { Id = databaseId }; CosmosResponseMessage response = await this.cosmosClient.CreateDatabaseStreamAsync( databaseSettings, requestUnitsPerSecond : 400); Assert.IsNotNull(response); Assert.IsNotNull(response.Headers.RequestCharge); Assert.IsNotNull(response.Headers.ActivityId); Assert.IsTrue(response.StatusCode == HttpStatusCode.OK || (response.StatusCode == HttpStatusCode.Created && !databaseExists)); return(this.cosmosClient.GetDatabase(databaseId)); }
public async Task ImplicitConversion() { string databaseName = Guid.NewGuid().ToString(); DatabaseResponse cosmosDatabaseResponse = await this.cosmosClient.GetDatabase(databaseName).ReadAsync(cancellationToken: this.cancellationToken); CosmosDatabase cosmosDatabase = cosmosDatabaseResponse; CosmosDatabaseProperties cosmosDatabaseSettings = cosmosDatabaseResponse; Assert.IsNotNull(cosmosDatabase); Assert.IsNull(cosmosDatabaseSettings); cosmosDatabaseResponse = await this.CreateDatabaseHelper(); cosmosDatabase = cosmosDatabaseResponse; cosmosDatabaseSettings = cosmosDatabaseResponse; Assert.IsNotNull(cosmosDatabase); Assert.IsNotNull(cosmosDatabaseSettings); cosmosDatabaseResponse = await cosmosDatabase.DeleteAsync(cancellationToken : this.cancellationToken); cosmosDatabase = cosmosDatabaseResponse; cosmosDatabaseSettings = cosmosDatabaseResponse; Assert.IsNotNull(cosmosDatabase); Assert.IsNull(cosmosDatabaseSettings); }
public async Task StreamCreateConflictTestAsync() { CosmosDatabaseProperties databaseSettings = new CosmosDatabaseProperties() { Id = Guid.NewGuid().ToString() }; using (CosmosResponseMessage response = await this.cosmosClient.CreateDatabaseStreamAsync(databaseSettings)) { Assert.AreEqual(HttpStatusCode.Created, response.StatusCode); Assert.IsNotNull(response.Headers); Assert.IsTrue(response.Headers.RequestCharge > 0); } // Stream operations do not throw exceptions. using (CosmosResponseMessage response = await this.cosmosClient.CreateDatabaseStreamAsync(databaseSettings)) { Assert.AreEqual(HttpStatusCode.Conflict, response.StatusCode); Assert.IsNotNull(response.Headers); Assert.IsTrue(response.Headers.RequestCharge > 0); } using (CosmosResponseMessage response = await this.cosmosClient.GetDatabase(databaseSettings.Id).DeleteStreamAsync()) { Assert.AreEqual(HttpStatusCode.NoContent, response.StatusCode); Assert.IsNotNull(response.Headers); Assert.IsTrue(response.Headers.RequestCharge > 0); } }
public void DatabaseSettingsDefaults() { CosmosDatabaseProperties dbSettings = new CosmosDatabaseProperties(); Assert.IsNull(dbSettings.LastModified); Assert.IsNull(dbSettings.ResourceId); Assert.IsNull(dbSettings.Id); Assert.IsNull(dbSettings.ETag); SettingsContractTests.TypeAccessorGuard(typeof(CosmosDatabaseProperties), "Id"); }
public void DatabaseSettingsSerializeTest() { string id = Guid.NewGuid().ToString(); CosmosDatabaseProperties databaseSettings = new CosmosDatabaseProperties() { Id = id }; Database db = new Database() { Id = id }; string cosmosSerialized = SettingsContractTests.CosmosSerialize(databaseSettings); string directSerialized = SettingsContractTests.DirectSerialize(db); // Swap de-serialize and validate CosmosDatabaseProperties dbDeserSettings = SettingsContractTests.CosmosDeserialize <CosmosDatabaseProperties>(directSerialized); Database dbDeser = SettingsContractTests.DirectDeSerialize <Database>(cosmosSerialized); Assert.AreEqual(dbDeserSettings.Id, dbDeser.Id); Assert.AreEqual(dbDeserSettings.Id, db.Id); }
public async Task ValidateResponseFactoryJsonSerializer() { CosmosResponseMessage databaseResponse = this.CreateResponse(); CosmosResponseMessage containerResponse = this.CreateResponse(); CosmosResponseMessage storedProcedureExecuteResponse = this.CreateResponse(); CosmosResponseMessage storedProcedureResponse = this.CreateResponse(); CosmosResponseMessage triggerResponse = this.CreateResponse(); CosmosResponseMessage udfResponse = this.CreateResponse(); CosmosResponseMessage itemResponse = this.CreateResponse(); CosmosResponseMessage feedResponse = this.CreateResponse(); Mock <CosmosJsonSerializer> mockUserJsonSerializer = new Mock <CosmosJsonSerializer>(); Mock <CosmosJsonSerializer> mockDefaultJsonSerializer = new Mock <CosmosJsonSerializer>(); CosmosResponseFactory cosmosResponseFactory = new CosmosResponseFactory( defaultJsonSerializer: mockDefaultJsonSerializer.Object, userJsonSerializer: mockUserJsonSerializer.Object); // Test the user specified response mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(itemResponse.Content)).Returns(new ToDoActivity()); mockUserJsonSerializer.Setup(x => x.FromStream <ToDoActivity>(storedProcedureExecuteResponse.Content)).Returns(new ToDoActivity()); mockUserJsonSerializer.Setup(x => x.FromStream <CosmosFeedResponseUtil <ToDoActivity> >(feedResponse.Content)).Returns(new CosmosFeedResponseUtil <ToDoActivity>() { Data = new Collection <ToDoActivity>() }); // Verify all the user types use the user specified version await cosmosResponseFactory.CreateItemResponseAsync <ToDoActivity>(Task.FromResult(itemResponse)); await cosmosResponseFactory.CreateStoredProcedureExecuteResponseAsync <ToDoActivity>(Task.FromResult(storedProcedureExecuteResponse)); cosmosResponseFactory.CreateResultSetQueryResponse <ToDoActivity>(feedResponse); // Throw if the setups were not called mockUserJsonSerializer.VerifyAll(); // Test the system specified response CosmosContainerProperties containerSettings = new CosmosContainerProperties("mockId", "/pk"); CosmosDatabaseProperties databaseSettings = new CosmosDatabaseProperties() { Id = "mock" }; CosmosStoredProcedureProperties cosmosStoredProcedureSettings = new CosmosStoredProcedureProperties() { Id = "mock" }; CosmosTriggerProperties cosmosTriggerSettings = new CosmosTriggerProperties() { Id = "mock" }; CosmosUserDefinedFunctionProperties cosmosUserDefinedFunctionSettings = new CosmosUserDefinedFunctionProperties() { Id = "mock" }; mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosDatabaseProperties>(databaseResponse.Content)).Returns(databaseSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosContainerProperties>(containerResponse.Content)).Returns(containerSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosStoredProcedureProperties>(storedProcedureResponse.Content)).Returns(cosmosStoredProcedureSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosTriggerProperties>(triggerResponse.Content)).Returns(cosmosTriggerSettings); mockDefaultJsonSerializer.Setup(x => x.FromStream <CosmosUserDefinedFunctionProperties>(udfResponse.Content)).Returns(cosmosUserDefinedFunctionSettings); Mock <CosmosContainer> mockContainer = new Mock <CosmosContainer>(); Mock <CosmosDatabase> mockDatabase = new Mock <CosmosDatabase>(); // Verify all the system types that should always use default await cosmosResponseFactory.CreateContainerResponseAsync(mockContainer.Object, Task.FromResult(containerResponse)); await cosmosResponseFactory.CreateDatabaseResponseAsync(mockDatabase.Object, Task.FromResult(databaseResponse)); await cosmosResponseFactory.CreateStoredProcedureResponseAsync(Task.FromResult(storedProcedureResponse)); await cosmosResponseFactory.CreateTriggerResponseAsync(Task.FromResult(triggerResponse)); await cosmosResponseFactory.CreateUserDefinedFunctionResponseAsync(Task.FromResult(udfResponse)); // Throw if the setups were not called mockDefaultJsonSerializer.VerifyAll(); }