Skip to content

Commit

Permalink
Added new test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Willey committed Dec 19, 2019
1 parent af101e8 commit b8093c2
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using Microsoft.Azure.Cosmos.CosmosElements;
Expand All @@ -25,13 +23,8 @@ internal class CosmosSerializerCore
private readonly CosmosSerializer customSerializer;
private readonly CosmosSerializer sqlQuerySpecSerializer;

internal CosmosSerializerCore()
: this(customSerializer: null)
{
}

internal CosmosSerializerCore(
CosmosSerializer customSerializer)
CosmosSerializer customSerializer = null)
{
if (customSerializer == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Query;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

[TestClass]
public class CosmosJsonSerializerTests : BaseCosmosClientHelper
Expand All @@ -34,6 +39,102 @@ public async Task Cleanup()
await base.TestCleanup();
}

[TestMethod]
public async Task TestQueryWithCustomJsonSerializer()
{
int toStreamCount = 0;
int fromStreamCount = 0;
CosmosSerializer serializer = new CosmosSerializerHelper(new Newtonsoft.Json.JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore
},
(item) => fromStreamCount++,
(item) => toStreamCount++);

CosmosClient client = TestCommon.CreateCosmosClient(builder => builder.WithCustomSerializer(serializer));
Database database = await client.CreateDatabaseAsync(Guid.NewGuid().ToString());
try
{
Container container = await database.CreateContainerAsync(Guid.NewGuid().ToString(), "/id");
Assert.AreEqual(0, toStreamCount);
Assert.AreEqual(0, fromStreamCount);

double cost = 9001.42;
for (int i = 0; i < 5; i++)
{
ToDoActivity toDoActivity = new ToDoActivity()
{
id = "TestId" + i,
cost = cost
};

await container.CreateItemAsync<ToDoActivity>(toDoActivity, new PartitionKey(toDoActivity.id));
}

Assert.AreEqual(5, toStreamCount);
Assert.AreEqual(5, fromStreamCount);

toStreamCount = 0;
fromStreamCount = 0;

QueryDefinition query = new QueryDefinition("select * from T where T.id != @id").
WithParameter("@id", Guid.NewGuid());

FeedIterator<DatabaseProperties> feedIterator = client.GetDatabaseQueryIterator<DatabaseProperties>(
query);
List<DatabaseProperties> databases = new List<DatabaseProperties>();
while (feedIterator.HasMoreResults)
{
databases.AddRange(await feedIterator.ReadNextAsync());
}

Assert.AreEqual(1, toStreamCount, "parameter should use custom serializer");
Assert.AreEqual(0, fromStreamCount);

toStreamCount = 0;
fromStreamCount = 0;

FeedIterator<ToDoActivity> itemIterator = container.GetItemQueryIterator<ToDoActivity>(
query);
List<ToDoActivity> items = new List<ToDoActivity>();
while (itemIterator.HasMoreResults)
{
items.AddRange(await itemIterator.ReadNextAsync());
}

Assert.AreEqual(1, toStreamCount);
Assert.AreEqual(5, fromStreamCount);

toStreamCount = 0;
fromStreamCount = 0;

// Verify that the custom serializer is actually being used via stream
FeedIterator itemStreamIterator = container.GetItemQueryStreamIterator(
query);
while (itemStreamIterator.HasMoreResults)
{
ResponseMessage response = await itemStreamIterator.ReadNextAsync();
using (StreamReader reader = new StreamReader(response.Content))
{
string content = await reader.ReadToEndAsync();
Assert.IsTrue(content.Contains("9001.42"));
Assert.IsFalse(content.Contains("description"), "Description should be ignored and not in the JSON");
}
}

Assert.AreEqual(1, toStreamCount);
Assert.AreEqual(0, fromStreamCount);

}
finally
{
if(database != null)
{
using (await database.DeleteStreamAsync()) { }
}
}
}

[TestMethod]
public async Task TestCustomJsonSerializer()
{
Expand Down Expand Up @@ -111,6 +212,7 @@ private ToDoActivity CreateRandomToDoActivity(string pk = null)

public class ToDoActivity
{
[JsonProperty(PropertyName = "id")]
public string id { get; set; }
public int taskNum { get; set; }
public double cost { get; set; }
Expand Down

0 comments on commit b8093c2

Please sign in to comment.