Wednesday, June 21, 2023

Unit Testing JSON with FluentAssertions.Json Vs. FluentAssertions

In this post, let's see why it's important to use FluentAssertions.Json instead of FluentAssertions when unit testing JSON. FluentAssertions has been one of my favorite go-to libraries when writing unit tests. FluentAssertions.Json is a Fluent Assertions extension for Newtonsoft.Json (yes, there are still use cases to use Newtonsoft.Json and not System.Text.Json).

Let's consider the following test (please note it's not a real "Unit Test"). I have installed the FluentAssertions.Json package (which is dependent on FluentAssertions)

using FluentAssertions;
using Newtonsoft.Json.Linq;

public class ExampleTests
{
    [Fact]
    public void CompareJson_WhenItIsSame_ShouldReturnTrue()
    {
        JToken actualJson = JToken.Parse("""
        {
            "parameters": {
                "hoursWorked": 0,
                "hourlyRate": 100
            }
        }
        """
);

        JToken expectedJson = JToken.Parse("""
        {
            "parameters": {
                "hoursWorked": 40,
                "hourlyRate": 100
            }
        }
        """
);

        actualJson.Should().BeEquivalentTo(expectedJson);
    }
}

Note here I am using FluentAssertions. And if I run the test, surprisingly it's going to PASS. But we can clearly see, the actual and expected JSON is not the same.

using FluentAssertions

Now let's change our using to using FluentAssertions.Json. And now if I run the test, it's giving me  FAIL which is the expected result.

using FluentAssertions.Json
And now if I fix the code to pass the test, I can see the test is getting passed.
using FluentAssertions.Json

So if you are using FluentAssertions.Json to unit test JSON, always make sure you are using the correct namespace. You could easily be using FluentAssertions (which isn't easily noticeable) and scratching your head for a while trying to figure out why tests aren't giving your expected results.

Hope this helps.

Happy Coding.

Regards,
Jaliya

Friday, June 16, 2023

Visual Studio 2022 17.7.0 Preview 2.0: Improved File Comparisons

Visual Studio 2022 17.7.0 Preview 2.0 is released and in this post, let's have a look at one of the handy features that got improved. This feature was initially introduced with Visual Studio 2022 17.7.0 Preview 1.0, but with the latest release, the user experience is more improved.

With this feature, you can compare files within Visual Studio and you no longer have to leave the current context and use an external tool (I was using Visual Studio Code) to compare files.

You can use this feature in multiple ways. The easiest is from the Solution Explorer, multi-select two files by holding down the Ctrl button, then right-click and compare them using the "Compare Selected" option.
Compare Selected
The other options are,
  • Right-click on a single file, and select "Select for Compare". Then navigate to the second file in the Solution Explorer, right-click on that, and click on "Compare with <SelectedFile>".
  • Right-click on a single file, and then click "Compare With…". This will open up the File Explorer. Now you can navigate to any file on disk and select it for comparison.
It's a small feature, but very handy.

Happy Coding.

Regards,
Jaliya

Tuesday, June 13, 2023

Entity Framework Core 7.0 (EF 7.0): In-Built Support for Transact-SQL AT TIME ZONE

In this post, let's have a look at the new AtTimeZone functions that got introduced in Entity Framework Core 7.0 (or Entity Framework 7.0) for DateTime and DateTimeOffset.

Prior to EF 7, in order to use Transact-SQL AT TIME ZONE, we had to do something like I have described in this post: Custom EF Core Function to Use Transact-SQL AT TIME ZONE. But we don't have to do that anymore.

Let's have a look.

Consider the following orders.
List<Order> orders = new()
{
    new()
    {
        OrderDate = new DateTimeOffset(new DateTime(2023, 6, 1, 8, 30, 0), TimeSpan.Zero)
    },
    new()
    {
        OrderDate = new DateTimeOffset(new DateTime(2023, 6, 1, 9, 0, 0), TimeSpan.Zero)
    }
};
Once these are in the database, I can use AtTimeZone functions as follows.
var query = await context.Orders
    .Select(x => new
    {
        UtcTime = EF.Functions.AtTimeZone(x.OrderDate, "UTC"),
        EasternTime = EF.Functions.AtTimeZone(x.OrderDate, "Eastern Standard Time"),
        NztTime = EF.Functions.AtTimeZone(x.OrderDate, "New Zealand Standard Time")
    })
    .ToListAsync();

foreach (var order in query) {     Console.WriteLine(order); }
This is generating the following SQL.
SELECT  [o].[OrderDate] AT TIME ZONE 'UTC' AS [UtcTime], 
        [o].[OrderDate] AT TIME ZONE 'Eastern Standard Time' AS [EasternTime], 
        [o].[OrderDate] AT TIME ZONE 'New Zealand Standard Time' AS [NztTime]
FROM    [Orders] AS [o]
And the output is as follows.
{ UtcTime = 1/06/2023 8:30:00 am +00:00, EasternTime = 1/06/2023 4:30:00 am -04:00, NztTime = 1/06/2023 8:30:00 pm +12:00 }
{ UtcTime = 1/06/2023 9:00:00 am +00:00, EasternTime = 1/06/2023 5:00:00 am -04:00, NztTime = 1/06/2023 9:00:00 pm +12:00 }
Isn't it handy?

Hope this helps.

Happy Coding.

Regards,
Jaliya

Monday, June 12, 2023

.NET Upgrade Assistant CLI

In this post, let's have a look at the .NET Upgrade Assistant CLI, a CLI tool to upgrade .NET projects to use the latest .NET and benefit from all its nice features.

As you know Visual Studio has this extension .NET Upgrade Assistant for quite some time and the new CLI tool uses the same engine as the Visual Studio Extension.

First, you can install the CLI tool globally by running the following command.
dotnet tool install -g upgrade-assistant
You can upgrade the CLI tool to the latest version by running the following command.
dotnet tool update -g upgrade-assistant
Once the tool is installed, next you need to run the following command.
upgrade-assistant upgrade
upgrade-assistant --help
Why wait, bring your apps to the latest .NET.

Happy Coding.

Regards,
Jaliya