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 unit tests for DurationToXXX and XXXToDuration #5530

Conversation

arzonus
Copy link
Contributor

@arzonus arzonus commented Dec 22, 2023

What changed?
Non-used DurationToXXX and XXXToDuration have been removed. Unit tests have been added for others.

Why?
The funcs were not covered by unit tests

How did you test it?
Unit tests are passed

Potential risks

Release notes

Documentation Changes

// DurationToSeconds converts time.Duration to number of seconds
func DurationToSeconds(d time.Duration) int64 {
return int64(d / time.Second)
}

// DurationToMilliseconds converts time.Duration to number of milliseconds
func DurationToMilliseconds(d time.Duration) int64 {
Copy link
Member

Choose a reason for hiding this comment

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

Hmmm. I we can create a generic function:

func DurationToUnit(d time.Duration, unit time.Duration) int64{
     return int64(d / unit)
}

Copy link
Member

Choose a reason for hiding this comment

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

you can go a bit crazier and do
https://go.dev/play/p/syBQijmw6OW
for both functions :)

Copy link
Member

Choose a reason for hiding this comment

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

that'll be the first instance of Generics in the repo probably. Still, we're at 1.20 now, so should be ok

// DaysToDuration converts number of 24 hour days to time.Duration
func DaysToDuration(d int32) time.Duration {
return time.Duration(d) * (24 * time.Hour)
}

// HoursToDuration converts number of hours to time.Duration
func HoursToDuration(d int64) time.Duration {
Copy link
Member

Choose a reason for hiding this comment

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

The same here

// ToDuration converts a number of units to time.Duration with the specified unit
func ToDuration(value int64, unit time.Duration) time.Duration {
	return time.Duration(value) * unit
}

@@ -693,3 +693,58 @@ func TestValidateRetryPolicy_Error(t *testing.T) {
})
}
}

func TestDurationToDays(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

Then you can test generic implementation.

…tests_for_duration_to_duration

# Conflicts:
#	common/util_test.go
@arzonus
Copy link
Contributor Author

arzonus commented Dec 22, 2023

@3vilhamster I understand your suggestions, but it will not improve the coverage, because it will still require us to write tests calling these functions, not only the generic function. If we're going to replace these functions with a generic function it will require replacing it in all places. Also, there is one change because DurationToDays returns int32, but the generic function returns int64, the same works for DaysToDuration.

@3vilhamster
Copy link
Member

If you don't want to introduce big changes, you can introduce this functions, test them separately and make existing wrappers call them. Also, you can add annotation that functions are deprecated and we propose usage of generic version.

…tests_for_duration_to_duration

# Conflicts:
#	common/util_test.go
…tests_for_duration_to_duration

# Conflicts:
#	common/util_test.go
@arzonus
Copy link
Contributor Author

arzonus commented Jan 8, 2024

I am not sure that we should use a generic function here. The goal of the PR is to improve test coverage. If we change the internals of these functions to the generic function, we still need to have tests to verify that these changes are correct and backward compatible. Of course, a 1-line function shouldn't cause bugs there, but it's better to be sure that these changes don't break something.

So, I'd like to leave it as it is and don't use the generic function here right now. But I agree that it makes to use the generic function if we're going to use these functions somewhere additionally. WDYT? @3vilhamster @davidporter-id-au @taylanisikdemir

@coveralls
Copy link

Pull Request Test Coverage Report for Build 018ced49-ccee-4365-bef4-c0d3488c6bff

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 62.302%

Totals Coverage Status
Change from base Build 018ceb07-c985-4bbe-a4d6-7e12c3d557d7: -0.03%
Covered Lines: 93405
Relevant Lines: 149924

💛 - Coveralls

Copy link
Member

@3vilhamster 3vilhamster left a comment

Choose a reason for hiding this comment

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

If we are focusing on coverage, sadly, while generic implementation will make testing all cases a bit easier, we will still need to cover all typed functions, so lets land this.

@arzonus arzonus merged commit 381182f into cadence-workflow:master Jan 9, 2024
16 checks passed
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.

4 participants