Skip to content

Commit

Permalink
Refactored code and updated test case based on review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashishdhingra committed Feb 27, 2025
1 parent b7fb176 commit 037f1b2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"core": {
"changeLogMessages": [
"Added method AWSSDKUtils.ConvertFromUnixLongEpochSeconds() for converting Unix epoch seconds to DateTime structure."
],
"type": "patch",
"updateMinimum": true
},
"services": [
{
"serviceName": "DynamoDBv2",
Expand Down
22 changes: 10 additions & 12 deletions sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,12 @@ internal static DynamoDBEntry DateTimeToEpochSeconds(DynamoDBEntry entry, string
// Differs from DateTimeToEpochSeconds by supporting dates after 2038.
internal static DynamoDBEntry DateTimeToEpochSecondsLong(DynamoDBEntry entry, string attributeName)
{
string epochSecondsAsString = null;
try
{
var dateTime = entry.AsDateTime();
epochSecondsAsString = AWSSDKUtils.ConvertToUnixEpochSecondsString(dateTime);
string epochSecondsAsString = AWSSDKUtils.ConvertToUnixEpochSecondsString(dateTime);
entry = new Primitive(epochSecondsAsString, saveAsNumeric: true);
return entry;
}
catch (Exception e)
{
Expand All @@ -327,16 +328,13 @@ internal static DynamoDBEntry DateTimeToEpochSecondsLong(DynamoDBEntry entry, st
attributeName, entry, e);

// To prevent the data from being (de)serialized incorrectly, we choose to throw the exception instead
// of swallowing it.
throw;
}

if (epochSecondsAsString != null)
{
entry = new Primitive(epochSecondsAsString, saveAsNumeric: true);
}

return entry;
// of swallowing it. This was a purposeful change from the existing DateTimeToEpochSeconds to avoid issues
// silently fallback to string format DateTime.
throw new AmazonDynamoDBException(
string.Format("Encountered error attempting to convert '{0}' with value '{1}' to epoch seconds.",
attributeName, entry.AsDateTime()),
e);
}
}

internal static Document FromAttributeMap(Dictionary<string, AttributeValue> data, IEnumerable<string> epochAttributes, IEnumerable<string> epochLongAttributes)
Expand Down
54 changes: 39 additions & 15 deletions sdk/test/Services/DynamoDBv2/IntegrationTests/DataModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ public void TestContext_DisableFetchingTableMetadata_DateTimeAsHashKey()
EpochDate2 = EpochDate,
NonEpochDate1 = EpochDate,
NonEpochDate2 = EpochDate,
LongEpochDate = LongEpochDate
LongEpochDate1 = LongEpochDate,
LongEpochDate2 = LongEpochDate.AddDays(12)
};

Context.Save(employee);
Expand All @@ -154,7 +155,8 @@ public void TestContext_DisableFetchingTableMetadata_DateTimeAsHashKey()
ApproximatelyEqual(EpochDate, storedEmployee.EpochDate2);
ApproximatelyEqual(EpochDate, storedEmployee.NonEpochDate1);
ApproximatelyEqual(EpochDate, storedEmployee.NonEpochDate2);
ApproximatelyEqual(LongEpochDate, storedEmployee.LongEpochDate);
ApproximatelyEqual(LongEpochDate, storedEmployee.LongEpochDate1);
ApproximatelyEqual(LongEpochDate.AddDays(12), storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
Expand Down Expand Up @@ -277,6 +279,7 @@ public void TestContext_RetrieveDateTimeInUtc(bool retrieveDateTimeInUtc)

var currTime = DateTime.Now;
var longEpochTime = new DateTime(2039, 2, 5, 17, 49, 55, DateTimeKind.Local); // DateTime.Now returns Local kind.
var longEpochTimeBefore1970 = new DateTime(1969, 12, 30, 23, 59, 59, DateTimeKind.Local);

var employee = new AnnotatedNumericEpochEmployee
{
Expand All @@ -286,12 +289,14 @@ public void TestContext_RetrieveDateTimeInUtc(bool retrieveDateTimeInUtc)
EpochDate2 = currTime,
NonEpochDate1 = currTime,
NonEpochDate2 = currTime,
LongEpochDate = longEpochTime
LongEpochDate1 = longEpochTime,
LongEpochDate2 = longEpochTimeBefore1970
};

Context.Save(employee);
var expectedCurrTime = retrieveDateTimeInUtc ? currTime.ToUniversalTime() : currTime.ToLocalTime();
var expectedLongEpochTime = retrieveDateTimeInUtc ? longEpochTime.ToUniversalTime() : longEpochTime.ToLocalTime();
var expectedLongEpochTimeBefore1970 = retrieveDateTimeInUtc ? longEpochTimeBefore1970.ToUniversalTime() : longEpochTimeBefore1970.ToLocalTime();

// Load
var storedEmployee = Context.Load<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name);
Expand All @@ -300,7 +305,8 @@ public void TestContext_RetrieveDateTimeInUtc(bool retrieveDateTimeInUtc)
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);

Expand All @@ -313,7 +319,8 @@ public void TestContext_RetrieveDateTimeInUtc(bool retrieveDateTimeInUtc)
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);

Expand All @@ -324,7 +331,8 @@ public void TestContext_RetrieveDateTimeInUtc(bool retrieveDateTimeInUtc)
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
Expand Down Expand Up @@ -355,6 +363,7 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)

var currTime = DateTime.Now;
var longEpochTime = new DateTime(2039, 2, 5, 17, 49, 55, DateTimeKind.Local); // DateTime.Now returns Local kind.
var longEpochTimeBefore1970 = new DateTime(1950, 12, 30, 19, 43, 30, DateTimeKind.Local);

var employee = new AnnotatedNumericEpochEmployee
{
Expand All @@ -364,7 +373,8 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
EpochDate2 = currTime,
NonEpochDate1 = currTime,
NonEpochDate2 = currTime,
LongEpochDate = longEpochTime
LongEpochDate1 = longEpochTime,
LongEpochDate2 = longEpochTimeBefore1970
};

Context.Save(employee);
Expand All @@ -373,6 +383,7 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
// regardless of RetrieveDateTimeInUtc value.
var expectedCurrTime = currTime.ToUniversalTime();
var expectedLongEpochTime = longEpochTime.ToUniversalTime();
var expectedLongEpochTimeBefore1970 = longEpochTimeBefore1970.ToUniversalTime();

// Load
var storedEmployee = Context.Load<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name);
Expand All @@ -381,7 +392,8 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);

Expand All @@ -394,7 +406,8 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);

Expand All @@ -405,7 +418,8 @@ public void TestContext_CustomDateTimeConverter(bool retrieveDateTimeInUtc)
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
Expand All @@ -428,6 +442,7 @@ public void TestContext_RetrieveDateTimeInUtc_OperationConfig(bool retrieveDateT

var currTime = DateTime.Now;
var longEpochTime = new DateTime(2039, 7, 23, 2, 3, 4, DateTimeKind.Local);
var longEpochTimeBefore1970 = new DateTime(1955, 12, 30, 23, 59, 59, DateTimeKind.Local);

var employee = new AnnotatedNumericEpochEmployee
{
Expand All @@ -437,12 +452,14 @@ public void TestContext_RetrieveDateTimeInUtc_OperationConfig(bool retrieveDateT
EpochDate2 = currTime,
NonEpochDate1 = currTime,
NonEpochDate2 = currTime,
LongEpochDate = longEpochTime
LongEpochDate1 = longEpochTime,
LongEpochDate2 = longEpochTimeBefore1970
};

Context.Save(employee);
var expectedCurrTime = retrieveDateTimeInUtc ? currTime.ToUniversalTime() : currTime.ToLocalTime();
var expectedLongEpochTime = retrieveDateTimeInUtc ? longEpochTime.ToUniversalTime() : longEpochTime.ToLocalTime();
var expectedLongEpochTimeBefore1970 = retrieveDateTimeInUtc ? longEpochTimeBefore1970.ToUniversalTime() : longEpochTimeBefore1970.ToLocalTime();

// Load
var storedEmployee = Context.Load<AnnotatedNumericEpochEmployee>(employee.CreationTime, employee.Name, operationConfig);
Expand All @@ -451,7 +468,8 @@ public void TestContext_RetrieveDateTimeInUtc_OperationConfig(bool retrieveDateT
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);

Expand All @@ -464,7 +482,8 @@ public void TestContext_RetrieveDateTimeInUtc_OperationConfig(bool retrieveDateT
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);

Expand All @@ -475,7 +494,8 @@ public void TestContext_RetrieveDateTimeInUtc_OperationConfig(bool retrieveDateT
ApproximatelyEqual(expectedCurrTime, storedEmployee.EpochDate2);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate1);
ApproximatelyEqual(expectedCurrTime, storedEmployee.NonEpochDate2);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate);
ApproximatelyEqual(expectedLongEpochTime, storedEmployee.LongEpochDate1);
ApproximatelyEqual(expectedLongEpochTimeBefore1970, storedEmployee.LongEpochDate2);
Assert.AreEqual(employee.Name, storedEmployee.Name);
Assert.AreEqual(employee.Age, storedEmployee.Age);
}
Expand Down Expand Up @@ -2085,6 +2105,7 @@ public class EpochEmployee : Employee
[DynamoDBProperty(StoreAsEpoch = true)]
public virtual DateTime CreationTime { get; set; }

[DynamoDBProperty(StoreAsEpoch = true)]
public DateTime EpochDate2 { get; set; }

[DynamoDBProperty(StoreAsEpoch = false)]
Expand All @@ -2093,7 +2114,10 @@ public class EpochEmployee : Employee
public DateTime NonEpochDate2 { get; set; }

[DynamoDBProperty(StoreAsEpochLong = true)]
public DateTime LongEpochDate { get; set; }
public DateTime LongEpochDate1 { get; set; }

[DynamoDBProperty(StoreAsEpochLong = true)]
public DateTime LongEpochDate2 { get; set; }
}

/// <summary>
Expand Down
Loading

0 comments on commit 037f1b2

Please sign in to comment.