Skip to content
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

Add duration property to the Item model to support the new duration feature. #35

Merged
merged 7 commits into from
Sep 30, 2023

Conversation

AhmedZaki99
Copy link
Contributor

Fixes #34

Changes include:

  • Adding a Duration model to be mapped against the task duration JSON object.
  • Adding a DurationUnit model that extends StringEnum and supports two values: minute & day.
  • Adding a Duration property of type Duration in the Item model.

…pendent if statement.

To follow the clean code rule: csharpsquid:S3358 (Ternary operators should not be nested)
Copy link
Owner

@olsh olsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please write at least one integration tests with duration?

{
return TimeSpan.FromDays(Amount);
}
throw new NotImplementedException();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to change ifs to switch pattern in this way static analyzers will find an issue if we add a new DurationUnit in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a good point, but the problem is that we are dealing with static values that won't work with switch patterns.
I've looked it up and found a way to make the code more readable, following this SO answer, I did the following:

switch (Unit)
{
    case var _ when Unit == DurationUnit.Minute:
        return TimeSpan.FromMinutes(Amount);
    case var _ when Unit == DurationUnit.Day:
        return TimeSpan.FromDays(Amount);
    default:
        throw new NotImplementedException();
}

Unfortunately, this still doesn't guide static analyzers to help us add support for new values.

I thought of adding an enum that can be mapped to each of the DurationUnit values, but again, analyzers will be useful only if new values are added both to the class and the enum.

Maybe there's something that I'm missing, what do you think?

@olsh
Copy link
Owner

olsh commented Sep 29, 2023

Thank you for the contribution! 👍🏻

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@AhmedZaki99
Copy link
Contributor Author

AhmedZaki99 commented Sep 29, 2023

Could you please write at least one integration tests with duration?

You were right about this, the test I wrote was failing at first, but then I found that the Duration property was being ignored when set to null, so I added an exception for it in the JsonProperty attribute:

[JsonProperty("duration", NullValueHandling = NullValueHandling.Include)]
public Duration Duration { get; set; }

In addition to the integration test, I wrote a couple of unit tests for the Duration object, which have guided me later to move the validation checks on the constructor parameters to be executed within property setters instead, e.g.:

public int Amount
{
    get => _amount;
    set => _amount = value > 0
        ? value
        : throw new ArgumentOutOfRangeException(nameof(Amount), "Duration amount must be greater than zero.");
}

All tests are passing now, but on my machine, the integration test I added was failing due to inconsistency in conversion between DueDate.StringDate and DueDate.Date properties.
When the task contains a floating due date, it is treated like a local time when being converted from DateTime to string in the DueDate.StringDate property getter, hence, its string value changes from what it was given by the API.

In my case (time zone is GMT +2), the value "2021-12-22T09:15:00" fetched from the API was converted to a DateTime of kind DateTimeKind.Unspecified, and then converted back to a "2021-12-22T07:15:00" when sent back to the API for update.
That results in an unwanted update on the task without even changing any field.

I think I might've faced a similar issue while I was working on my project and it's clear now why it's happening. I will wait until we get done with this pull request and then open an issue ticket for it.

@olsh olsh merged commit 2a0bead into olsh:master Sep 30, 2023
@AhmedZaki99 AhmedZaki99 deleted the patch-3 branch September 30, 2023 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding duration property for Item model.
2 participants