Skip to content

Commit

Permalink
Adding some unit tests for TransactionalBatchOperationResult
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 25, 2019
1 parent 9342593 commit 00b1787
Showing 1 changed file with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.IO;
using System.Net;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Microsoft.Azure.Cosmos
{
[TestClass]
public class BatchOperationResultTests
{
static readonly Mock<CosmosDiagnostics> MockCosmosDiagnostics = new Mock<CosmosDiagnostics>(MockBehavior.Strict);
static TransactionalBatchOperationResult CreateTestResult() => new TransactionalBatchOperationResult(HttpStatusCode.Unused)
{
SubStatusCode = Documents.SubStatusCodes.CanNotAcquireOfferOwnerLock,
ETag = "TestETag",
ResourceStream = new MemoryStream(),
RequestCharge = 1.5,
RetryAfter = TimeSpan.FromMilliseconds(1234),
Diagnostics = MockCosmosDiagnostics.Object
};

[TestMethod]
public void StatusCodeIsSetThroughCtor()
{
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(HttpStatusCode.Unused);

Assert.AreEqual(HttpStatusCode.Unused, result.StatusCode);
}

[TestMethod]
public void PropertiesAreSetThroughCopyCtor()
{
TransactionalBatchOperationResult other = CreateTestResult();
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult(other);

Assert.AreEqual(other.StatusCode, result.StatusCode);
Assert.AreEqual(other.SubStatusCode, result.SubStatusCode);
Assert.AreEqual(other.ETag, result.ETag);
Assert.AreEqual(other.RequestCharge, result.RequestCharge);
Assert.AreEqual(other.RetryAfter, result.RetryAfter);
Assert.AreSame(other.ResourceStream, result.ResourceStream);
}

[TestMethod]
public void PropertiesAreSetThroughGenericCtor()
{
TransactionalBatchOperationResult other = CreateTestResult();
object testObject = new object();
TransactionalBatchOperationResult<object> result = new TransactionalBatchOperationResult<object>(other, testObject);

Assert.AreEqual(other.StatusCode, result.StatusCode);
Assert.AreEqual(other.SubStatusCode, result.SubStatusCode);
Assert.AreEqual(other.ETag, result.ETag);
Assert.AreEqual(other.RequestCharge, result.RequestCharge);
Assert.AreEqual(other.RetryAfter, result.RetryAfter);
Assert.AreSame(other.ResourceStream, result.ResourceStream);
Assert.AreSame(testObject, result.Resource);
}

[TestMethod]
public void ToResponseMessageHasPropertiesMapped()
{
TransactionalBatchOperationResult result = CreateTestResult();

ResponseMessage response = result.ToResponseMessage();

Assert.AreEqual(result.StatusCode, response.StatusCode);
Assert.AreEqual(result.SubStatusCode, response.Headers.SubStatusCode);
Assert.AreEqual(result.ETag, response.Headers.ETag);
Assert.AreEqual(result.RequestCharge, response.Headers.RequestCharge);
Assert.AreEqual(result.RetryAfter, response.Headers.RetryAfter);
Assert.AreSame(result.ResourceStream, response.Content);
Assert.AreSame(result.Diagnostics, response.Diagnostics);
}

[TestMethod]
public void IsSuccessStatusCodeTrueFor200to299()
{
for (int x = 100; x < 999; ++x)
{
TransactionalBatchOperationResult result = new TransactionalBatchOperationResult((HttpStatusCode)x);
bool success = x >= 200 && x <= 299;
Assert.AreEqual(success, result.IsSuccessStatusCode);
}
}
}
}

0 comments on commit 00b1787

Please sign in to comment.