Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Search/Search.Tests/Search.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,9 @@
<None Include="SessionRecords\Microsoft.Azure.Search.Tests.SearchTests\CanSearchStaticallyTypedDocuments.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Search.Tests.SearchTests\CanSearchWithDateTimeInStaticModel.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Search.Tests.SearchTests\CanSearchWithMinimumCoverage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -280,6 +283,9 @@
<None Include="SessionRecords\Microsoft.Azure.Search.Tests.SuggestTests\CanSuggestStaticallyTypedDocuments.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Search.Tests.SuggestTests\CanSuggestWithDateTimeInStaticModel.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="SessionRecords\Microsoft.Azure.Search.Tests.SuggestTests\CanSuggestWithMinimumCoverage.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

24 changes: 8 additions & 16 deletions src/Search/Search.Tests/Tests/IndexingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,25 @@ public void StaticallyTypedDateTimesRoundTripAsUtc()

SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name);

DateTime localDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);
// Can't test local date time since we might be testing against a pre-recorded mock response.
DateTime utcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime unspecifiedDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);

var batch =
IndexBatch.Create(
new[]
{
IndexAction.Create(new Book() { ISBN = "1", PublishDate = localDateTime }),
IndexAction.Create(new Book() { ISBN = "2", PublishDate = utcDateTime }),
IndexAction.Create(new Book() { ISBN = "3", PublishDate = unspecifiedDateTime })
IndexAction.Create(new Book() { ISBN = "1", PublishDate = utcDateTime }),
IndexAction.Create(new Book() { ISBN = "2", PublishDate = unspecifiedDateTime })
});

indexClient.Documents.Index(batch);
SearchTestUtilities.WaitForIndexing();

DocumentGetResponse<Book> getResponse = indexClient.Documents.Get<Book>("1");
Assert.Equal(localDateTime.ToUniversalTime(), getResponse.Document.PublishDate);

getResponse = indexClient.Documents.Get<Book>("2");
Assert.Equal(utcDateTime, getResponse.Document.PublishDate);

getResponse = indexClient.Documents.Get<Book>("3");
getResponse = indexClient.Documents.Get<Book>("2");
Assert.Equal(utcDateTime, getResponse.Document.PublishDate);
});
}
Expand Down Expand Up @@ -326,29 +322,25 @@ public void DynamicDocumentDateTimesRoundTripAsUtc()

SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name);

DateTime localDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);
// Can't test local date time since we might be testing against a pre-recorded mock response.
DateTime utcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime unspecifiedDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);

var batch =
new IndexBatch(
new[]
{
new IndexAction(new Document() { { "ISBN", "1" }, { "PublishDate", localDateTime } }),
new IndexAction(new Document() { { "ISBN", "2" }, { "PublishDate", utcDateTime } }),
new IndexAction(new Document() { { "ISBN", "3" }, { "PublishDate", unspecifiedDateTime } })
new IndexAction(new Document() { { "ISBN", "1" }, { "PublishDate", utcDateTime } }),
new IndexAction(new Document() { { "ISBN", "2" }, { "PublishDate", unspecifiedDateTime } })
});

indexClient.Documents.Index(batch);
SearchTestUtilities.WaitForIndexing();

DocumentGetResponse getResponse = indexClient.Documents.Get("1");
Assert.Equal(new DateTimeOffset(localDateTime), getResponse.Document["PublishDate"]);

getResponse = indexClient.Documents.Get("2");
Assert.Equal(new DateTimeOffset(utcDateTime), getResponse.Document["PublishDate"]);

getResponse = indexClient.Documents.Get("3");
getResponse = indexClient.Documents.Get("2");
Assert.Equal(new DateTimeOffset(utcDateTime), getResponse.Document["PublishDate"]);
});
}
Expand Down
13 changes: 11 additions & 2 deletions src/Search/Search.Tests/Tests/Models/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public override bool Equals(object obj)
return false;
}

return ISBN == other.ISBN && Title == other.Title && Author == other.Author;
return
ISBN == other.ISBN &&
Title == other.Title &&
Author == other.Author &&
PublishDate == other.PublishDate;
}

public override int GetHashCode()
Expand All @@ -46,7 +50,12 @@ public override int GetHashCode()

public override string ToString()
{
return string.Format("ISBN: {0}; Title: {1}; Author: {2}", ISBN, Title, Author);
return string.Format(
"ISBN: {0}; Title: {1}; Author: {2}; PublishDate: {3}",
ISBN,
Title,
Author,
PublishDate);
}
}
}
38 changes: 38 additions & 0 deletions src/Search/Search.Tests/Tests/SearchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Hyak.Common;
using Microsoft.Azure.Search.Models;
using Microsoft.Azure.Search.Tests.Utilities;
using Microsoft.Azure.Test;
using Microsoft.Azure.Test.TestCategories;
using Xunit;

Expand Down Expand Up @@ -561,6 +562,43 @@ public void CanSearchWithMinimumCoverage()
});
}

[Fact]
public void CanSearchWithDateTimeInStaticModel()
{
Run(() =>
{
SearchServiceClient serviceClient = Data.GetSearchServiceClient();

Index index =
new Index()
{
Name = TestUtilities.GenerateName(),
Fields = new[]
{
new Field("ISBN", DataType.String) { IsKey = true },
new Field("Title", DataType.String) { IsSearchable = true },
new Field("Author", DataType.String),
new Field("PublishDate", DataType.DateTimeOffset)
}
};

IndexDefinitionResponse createIndexResponse = serviceClient.Indexes.Create(index);
SearchIndexClient indexClient = Data.GetSearchIndexClient(createIndexResponse.Index.Name);

var doc1 = new Book() { ISBN = "123", Title = "Lord of the Rings", Author = "J.R.R. Tolkien" };
var doc2 = new Book() { ISBN = "456", Title = "War and Peace", PublishDate = new DateTime(2015, 8, 18) };
var batch = IndexBatch.Create(IndexAction.Create(doc1), IndexAction.Create(doc2));

indexClient.Documents.Index(batch);
SearchTestUtilities.WaitForIndexing();

DocumentSearchResponse<Book> response = indexClient.Documents.Search<Book>("War and Peace");
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(1, response.Results.Count);
Assert.Equal(doc2, response.Results[0].Document);
});
}

private IEnumerable<string> IndexDocuments(SearchIndexClient client, int totalDocCount)
{
int existingDocumentCount = Data.TestDocuments.Length;
Expand Down
127 changes: 120 additions & 7 deletions src/Search/Search.Tests/Tests/Serialization/DateTimeConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,39 @@

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Azure.Search.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Microsoft.Azure.Search.Tests
{
public sealed class DateTimeConverterTests
{
private readonly JsonSerializerSettings _jsonSettings =
private readonly JsonSerializerSettings _serializerSettings =
new JsonSerializerSettings() { Converters = new List<JsonConverter>() { new DateTimeConverter() } };

private readonly JsonSerializerSettings _deserializerSettings =
new JsonSerializerSettings()
{
DateParseHandling = DateParseHandling.DateTimeOffset,
Converters = new List<JsonConverter>() { new DateTimeConverter() }
};

[Fact]
public void CanWriteLocalDateTime()
{
var localDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Local);
var expectedDateTime = new DateTimeOffset(localDateTime);
string expectedJson =
String.Format(
@"""{0}-{1}""",
@"""{0}{1}{2}""",
expectedDateTime.DateTime.ToString("s"),
expectedDateTime.Offset < TimeSpan.Zero ? "-" : "+",
expectedDateTime.Offset.ToString("hh\\:mm"));

string json = JsonConvert.SerializeObject(localDateTime, _jsonSettings);
string json = JsonConvert.SerializeObject(localDateTime, _serializerSettings);

Assert.Equal(expectedJson, json);
}
Expand All @@ -47,7 +57,7 @@ public void CanWriteUtcDateTime()
{
var utcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

string json = JsonConvert.SerializeObject(utcDateTime, _jsonSettings);
string json = JsonConvert.SerializeObject(utcDateTime, _serializerSettings);

Assert.Equal(@"""2000-01-01T00:00:00+00:00""", json);
}
Expand All @@ -57,7 +67,7 @@ public void CanWriteUnspecifiedDateTime()
{
var utcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Unspecified);

string json = JsonConvert.SerializeObject(utcDateTime, _jsonSettings);
string json = JsonConvert.SerializeObject(utcDateTime, _serializerSettings);

Assert.Equal(@"""2000-01-01T00:00:00+00:00""", json);
}
Expand All @@ -70,7 +80,7 @@ public void CanWriteNullableDateTime()
// Due to some JSON.NET weirdness, we need to wrap it in an object to trigger the DateTime? detection path.
object obj = new { UtcDateTime = utcDateTime };

string json = JsonConvert.SerializeObject(obj, _jsonSettings);
string json = JsonConvert.SerializeObject(obj, _serializerSettings);

Assert.Equal(@"{""UtcDateTime"":""2000-01-01T00:00:00+00:00""}", json);
}
Expand All @@ -83,9 +93,112 @@ public void CanWriteNullDateTime()
// Due to some JSON.NET weirdness, we need to wrap it in an object to trigger the DateTime? detection path.
object obj = new { UtcDateTime = utcDateTime };

string json = JsonConvert.SerializeObject(obj, _jsonSettings);
string json = JsonConvert.SerializeObject(obj, _serializerSettings);

Assert.Equal(@"{""UtcDateTime"":null}", json);
}

[Fact]
public void CanReadUtcDateTime()
{
var expectedUtcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

DateTime actualDateTime =
JsonConvert.DeserializeObject<DateTime>(@"""2000-01-01T00:00:00Z""", _deserializerSettings);

Assert.Equal(expectedUtcDateTime, actualDateTime);
}

[Fact]
public void CanReadUtcDateTimeOffset()
{
var expectedUtcDateTimeOffset = new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc));

DateTimeOffset actualDateTimeOffset =
JsonConvert.DeserializeObject<DateTimeOffset>(@"""2000-01-01T00:00:00Z""", _deserializerSettings);

Assert.Equal(expectedUtcDateTimeOffset, actualDateTimeOffset);
}

[Fact]
public void ReadingDateTimeWithOffsetConvertsToUtc()
{
DateTime expectedUtcTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

DateTime actualDateTime =
JsonConvert.DeserializeObject<DateTime>(@"""1999-12-31T16:00:00-08:00""", _deserializerSettings);

Assert.Equal(expectedUtcTime, actualDateTime);
}

[Fact]
public void CanReadDateTimeWithOffsetToDateTimeOffset()
{
var expectedUtcDateTimeOffset =
new DateTimeOffset(new DateTime(1999, 12, 31, 16, 0, 0), TimeSpan.FromHours(-8));

DateTimeOffset actualDateTimeOffset =
JsonConvert.DeserializeObject<DateTimeOffset>(@"""1999-12-31T16:00:00-08:00""", _deserializerSettings);

Assert.Equal(expectedUtcDateTimeOffset, actualDateTimeOffset);
}

[Fact]
public void CanReadNullableDateTime()
{
DateTime? expectedUtcDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);

DateTime? actualDateTime =
JsonConvert.DeserializeObject<DateTime?>(@"""2000-01-01T00:00:00Z""", _deserializerSettings);

Assert.Equal(expectedUtcDateTime, actualDateTime);
}

[Fact]
public void CanReadNullableDateTimeOffset()
{
DateTimeOffset? expectedUtcDateTimeOffset =
new DateTimeOffset(new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc));

DateTimeOffset? actualDateTimeOffset =
JsonConvert.DeserializeObject<DateTimeOffset?>(@"""2000-01-01T00:00:00Z""", _deserializerSettings);

Assert.Equal(expectedUtcDateTimeOffset, actualDateTimeOffset);
}

[Fact]
public void CanReadNullDateTime()
{
DateTime? nullUtcDateTime = null;
DateTime? actualDateTime = JsonConvert.DeserializeObject<DateTime?>("null", _deserializerSettings);
Assert.Equal(nullUtcDateTime, actualDateTime);
}

[Fact]
public void CanReadNullDateTimeOffset()
{
DateTimeOffset? nullUtcDateTimeOffset = null;
DateTimeOffset? actualDateTimeOffset =
JsonConvert.DeserializeObject<DateTimeOffset?>("null", _deserializerSettings);
Assert.Equal(nullUtcDateTimeOffset, actualDateTimeOffset);
}

[Fact]
public void CanReadPreParsedDateTime()
{
const string Json = @"{""CreatedAt"":""2000-01-01T00:00:00Z""}";
using (JsonReader reader = new JsonTextReader(new StringReader(Json)))
{
JsonSerializer serializer = JsonSerializer.Create(_deserializerSettings);
JObject propertyBag = serializer.Deserialize<JObject>(reader);
Model model = serializer.Deserialize<Model>(new JTokenReader(propertyBag));
Assert.Equal(new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc), model.CreatedAt);
}
}

private class Model
{
public DateTime CreatedAt { get; set; }
}
}
}
Loading