-
Notifications
You must be signed in to change notification settings - Fork 524
[Internal] Binary Encoding: Fixes DateTime Parsing Issue with Trailing Zeros in the Milli-Seconds Precision. #5218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
df13832
Fix for datetime formatting.
aavasthy f38716b
Code changes to simplify the datetime write value logic.
kundadebdatta 6757cbe
Update tests.
aavasthy 2d1e84b
Code clean up
aavasthy 4045be1
Code changes to remove trim and use a custom datetime format.
kundadebdatta 2b53bb2
Merge branch 'master' into users/aavasthy/5213_FixDateFormatBE
kundadebdatta 1b63d6d
Code changes to add more tests.
kundadebdatta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| namespace Microsoft.Azure.Cosmos.SDK.EmulatorTests | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net; | ||
| using System.Text; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using System.Threading; | ||
|
|
@@ -31,8 +33,8 @@ public class CosmosItemIntegrationTests | |
|
|
||
| [TestInitialize] | ||
| public async Task TestInitAsync() | ||
| { | ||
| this.connectionString = ConfigurationManager.GetEnvironmentVariable<string>("COSMOSDB_MULTI_REGION", null); | ||
| { | ||
| this.connectionString = ConfigurationManager.GetEnvironmentVariable<string>("COSMOSDB_MULTI_REGION", null); | ||
|
|
||
| JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions() | ||
| { | ||
|
|
@@ -148,7 +150,93 @@ public async Task ReadMany2UnreachablePartitionsTest() | |
| rule.Disable(); | ||
| fiClient.Dispose(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [Timeout(70000)] | ||
| [TestCategory("MultiRegion")] | ||
| public async Task DateTimeArrayRoundtrip_BinaryEncoding_CompareExtraDates_IntegrationTest() | ||
| { | ||
| string binaryEncodingEnabled = "binaryEncodingEnabled" + Guid.NewGuid().ToString("N"); | ||
| string binaryEncodingDisabled = "binaryEncodingDisabled" + Guid.NewGuid().ToString("N"); | ||
| string pk = "pk"; | ||
| string testId = Guid.NewGuid().ToString(); | ||
|
|
||
| string[] dateStrings = | ||
| { | ||
| "12/25/2023","2023-12-25","12-25-2023","25.12.2023","25/12/2023", | ||
| "Dec 25, 2023","Dec 25 2023","2023-12-25T10:00:00","2023-12-25T10:00:00.123", | ||
| "12/25/2023 10:00 AM","12/25/2023 10:00:00 AM","12/25/2023 10:00:00.123 AM","9999-12-31T23:59:59", | ||
| "2023-12-25T10:00:00.1","2023-12-25T10:00:00.12", | ||
| "2023-12-25T10:00:00.1234","2023-12-25T10:00:00.1234567" | ||
| }; | ||
| string[] formats = | ||
| { | ||
| "MM/dd/yyyy","yyyy-MM-dd","MM-dd-yyyy","dd.MM.yyyy","dd/MM/yyyy", | ||
| "MMM dd, yyyy","MMM dd yyyy","yyyy-MM-ddTHH:mm:ss","yyyy-MM-ddTHH:mm:ss.fff", | ||
| "yyyy-MM-ddTHH:mm:ss.f","yyyy-MM-ddTHH:mm:ss.ff","yyyy-MM-ddTHH:mm:ss.ffff", | ||
| "yyyy-MM-ddTHH:mm:ss.fffffff","MM/dd/yyyy hh:mm tt","MM/dd/yyyy hh:mm:ss tt", | ||
| "MM/dd/yyyy hh:mm:ss.fff tt" | ||
| }; | ||
| DateTime[] parsedDates = dateStrings | ||
| .Select(s => DateTime.ParseExact(s, formats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None)) | ||
| .ToArray(); | ||
|
|
||
| TestCosmosItem testItem = new TestCosmosItem( | ||
| id: testId, | ||
| pk: pk, | ||
| title: "title", | ||
| email: "[email protected]", | ||
| body: "Binary encoding test document.", | ||
| createdUtc: DateTime.UtcNow, | ||
| modifiedUtc: DateTime.Parse("2025-03-26T20:22:20Z", null, System.Globalization.DateTimeStyles.AdjustToUniversal), | ||
| extraDates: parsedDates); | ||
|
|
||
| Database db = this.database; | ||
| ContainerResponse containerBEEnabledResponse = await db.CreateContainerAsync(binaryEncodingEnabled, "/pk"); | ||
| ContainerResponse containerBEDisabledResponse = await db.CreateContainerAsync(binaryEncodingDisabled, "/pk"); | ||
|
|
||
| try | ||
| { | ||
| // BinaryEncodingEnabled = True | ||
| Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, "True"); | ||
| string rawJsonBEEnabled; | ||
| string rawJsonBEDisabled; | ||
| using (CosmosClient clientBinaryEncodingEnabled = new CosmosClient(this.connectionString)) | ||
| { | ||
| Container containerBinaryEncodingEnabled = clientBinaryEncodingEnabled.GetDatabase(db.Id).GetContainer(binaryEncodingEnabled); | ||
| await containerBinaryEncodingEnabled.CreateItemAsync(testItem, new Microsoft.Azure.Cosmos.PartitionKey(pk)); | ||
| using ResponseMessage response = await containerBinaryEncodingEnabled.ReadItemStreamAsync(testId, new Microsoft.Azure.Cosmos.PartitionKey(pk)); | ||
| using StreamReader reader = new StreamReader(response.Content, Encoding.UTF8); | ||
| rawJsonBEEnabled = await reader.ReadToEndAsync(); | ||
|
|
||
| } | ||
|
|
||
| // BinaryEncodingEnabled = False | ||
| Environment.SetEnvironmentVariable(ConfigurationManager.BinaryEncodingEnabled, "False"); | ||
| using (CosmosClient clientBinaryEncodingDisabled = new CosmosClient(this.connectionString)) | ||
| { | ||
| Container containerBinaryEncodingDisabled = clientBinaryEncodingDisabled.GetDatabase(db.Id).GetContainer(binaryEncodingDisabled); | ||
| await containerBinaryEncodingDisabled.CreateItemAsync(testItem, new Microsoft.Azure.Cosmos.PartitionKey(pk)); | ||
| using ResponseMessage response = await containerBinaryEncodingDisabled.ReadItemStreamAsync(testId, new Microsoft.Azure.Cosmos.PartitionKey(pk)); | ||
| using StreamReader reader = new StreamReader(response.Content, Encoding.UTF8); | ||
| rawJsonBEDisabled = await reader.ReadToEndAsync(); | ||
| } | ||
|
|
||
| using JsonDocument docTrue = JsonDocument.Parse(rawJsonBEEnabled); | ||
| using JsonDocument docFalse = JsonDocument.Parse(rawJsonBEDisabled); | ||
|
|
||
| string extraDatesTrue = docTrue.RootElement.GetProperty("ExtraDates").GetRawText(); | ||
| string extraDatesFalse = docFalse.RootElement.GetProperty("ExtraDates").GetRawText(); | ||
|
|
||
| Assert.AreEqual(extraDatesTrue, extraDatesFalse, $"ExtraDates JSON mismatch:\nTrue: {extraDatesTrue}\nFalse: {extraDatesFalse}"); | ||
| } | ||
| finally | ||
| { | ||
| await containerBEEnabledResponse.Container.DeleteContainerAsync(); | ||
| await containerBEDisabledResponse.Container.DeleteContainerAsync(); | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("MultiRegion")] | ||
|
|
@@ -1376,6 +1464,41 @@ await this.container.DeleteItemAsync<CosmosIntegrationTestObject>( | |
| { | ||
| // Ignore | ||
| } | ||
| } | ||
|
|
||
| public sealed class TestCosmosItem | ||
| { | ||
| [JsonConstructor] | ||
| public TestCosmosItem( | ||
| string id, | ||
| string pk, | ||
| string title, | ||
| string email, | ||
| string body, | ||
| DateTime createdUtc, | ||
| DateTime modifiedUtc, | ||
| DateTime[] extraDates) | ||
| { | ||
| this.id = id; | ||
| this.pk = pk; | ||
| this.title = title; | ||
| this.email = email; | ||
| this.body = body; | ||
| this.CreatedUtc = createdUtc; | ||
| this.ModifiedUtc = modifiedUtc; | ||
| this.ExtraDates = extraDates; | ||
| } | ||
|
|
||
| #pragma warning disable IDE1006 | ||
| public string id { get; } | ||
| public string pk { get; } | ||
| public string title { get; } | ||
| public string email { get; } | ||
| public string body { get; } | ||
| #pragma warning restore IDE1006 // Naming Styles | ||
| public DateTime CreatedUtc { get; } | ||
| public DateTime ModifiedUtc { get; } | ||
| public DateTime[] ExtraDates { get; } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.