-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72a9873
commit af24344
Showing
4 changed files
with
120 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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,24 +1,42 @@ | ||
using System.Text.Json.Serialization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace TeslaCam.Data; | ||
|
||
public record class CamEvent | ||
{ | ||
[JsonPropertyName("timestamp")] | ||
public DateTime Timestamp { get; set; } | ||
public required DateTime Timestamp { get; init; } | ||
|
||
[JsonPropertyName("city")] | ||
public string City { get; set; } | ||
public string City { get; init; } | ||
|
||
[JsonPropertyName("est_lat")] | ||
public decimal EstLat { get; set; } | ||
public decimal EstLat { get; init; } | ||
|
||
[JsonPropertyName("est_lon")] | ||
public decimal EstLon { get; set; } | ||
public decimal EstLon { get; init; } | ||
|
||
[JsonPropertyName("reason")] | ||
public string Reason { get; set; } | ||
public string Reason { get; init; } | ||
|
||
[JsonPropertyName("camera")] | ||
public int Camera { get; set; } | ||
public int Camera { get; init; } | ||
|
||
private static readonly JsonSerializerOptions JsonSerializerOptions = new() | ||
{ | ||
NumberHandling = JsonNumberHandling.AllowReadingFromString, | ||
}; | ||
|
||
public static CamEvent Deserialize(string json) | ||
{ | ||
try | ||
{ | ||
return JsonSerializer.Deserialize<CamEvent>(json, JsonSerializerOptions); | ||
} | ||
catch (JsonException) | ||
{ | ||
return null; | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using FluentAssertions; | ||
using TeslaCam.Data; | ||
|
||
namespace TeslaCam.Tests; | ||
|
||
public static class CamEventTests | ||
{ | ||
[Fact] | ||
public static void Deserializes_Correctly() | ||
{ | ||
// Arrange | ||
var json = """ | ||
{ | ||
"timestamp":"2023-06-03T15:54:27", | ||
"city":"Taylor", | ||
"est_lat":"30.6075", | ||
"est_lon":"-97.4812", | ||
"reason":"user_interaction_honk", | ||
"camera":"0" | ||
} | ||
"""; | ||
|
||
// Act | ||
var camEvent = CamEvent.Deserialize(json); | ||
|
||
// Assert | ||
camEvent.Should().NotBeNull(); | ||
camEvent.Timestamp.Should().Be(new DateTime(2023, 6, 3, 15, 54, 27)); | ||
camEvent.City.Should().Be("Taylor"); | ||
camEvent.EstLat.Should().Be(30.6075m); | ||
camEvent.EstLon.Should().Be(-97.4812m); | ||
camEvent.Reason.Should().Be("user_interaction_honk"); | ||
camEvent.Camera.Should().Be(0); | ||
} | ||
|
||
[Fact] | ||
public static void Deserialization_OptionalProperties() | ||
{ | ||
var json = """ | ||
{ | ||
"timestamp":"2023-06-03T15:54:27" | ||
} | ||
"""; | ||
|
||
var camEvent = CamEvent.Deserialize(json); | ||
|
||
camEvent.Should().NotBeNull(); | ||
camEvent.Timestamp.Should().Be(new DateTime(2023, 6, 3, 15, 54, 27)); | ||
camEvent.City.Should().BeNull(); | ||
camEvent.EstLat.Should().Be(default); | ||
camEvent.EstLon.Should().Be(default); | ||
camEvent.Reason.Should().BeNull(); | ||
camEvent.Camera.Should().Be(default); | ||
} | ||
|
||
[Fact] | ||
public static void Deserialization_TimestampRequired() | ||
{ | ||
var json = """ | ||
{ | ||
"city":"Taylor", | ||
"est_lat":"30.6075", | ||
"est_lon":"-97.4812", | ||
"reason":"user_interaction_honk", | ||
"camera":"0" | ||
} | ||
"""; | ||
|
||
CamEvent.Deserialize(json).Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public static void Deserialization_DoesNotThrowOnMalformedJson() | ||
{ | ||
// Arrange | ||
var json = """ | ||
{ | ||
"timestamp":"2023T15:54:27", | ||
"city":"Taylor", | ||
"est_lat":"lat", | ||
"est_lon":"lon", | ||
"reason":"user_interaction!", | ||
"camera":"first" | ||
} | ||
"""; | ||
|
||
// Act | ||
var camEvent = CamEvent.Deserialize(json); | ||
|
||
// Assert | ||
camEvent.Should().BeNull(); | ||
} | ||
} |
This file contains 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