Skip to content

Commit

Permalink
CamEvent deserialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jan 1, 2025
1 parent 72a9873 commit af24344
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 15 deletions.
9 changes: 1 addition & 8 deletions TeslaCam.Data/CamClip.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Globalization;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace TeslaCam.Data;
Expand Down Expand Up @@ -45,19 +44,13 @@ public static IEnumerable<CamClip> GetClipFolders(string rootDirectory)
}
}

private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString,
};

public static CamEvent GetEventData(string filePath)
{
if (!File.Exists(filePath))
return null;

var json = File.ReadAllText(filePath);
var camEvent = JsonSerializer.Deserialize<CamEvent>(json, JsonSerializerOptions);
return camEvent;
return CamEvent.Deserialize(json);
}

public override string ToString()
Expand Down
32 changes: 25 additions & 7 deletions TeslaCam.Data/CamEvent.cs
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;
}
}
}
93 changes: 93 additions & 0 deletions TeslaCam.Tests/CamEventTests.cs
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();
}
}
1 change: 1 addition & 0 deletions TeslaCam.Tests/TeslaCam.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="7.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0" PrivateAssets="all">
Expand Down

0 comments on commit af24344

Please sign in to comment.