-
Notifications
You must be signed in to change notification settings - Fork 121
Fix flaky test_isExpired_when_current_date_then_returns_true
#16074
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,10 +31,34 @@ struct POSCouponTests { | |||||||||||||
|
|
||||||||||||||
| @Test func test_isExpired_when_current_date_then_returns_true() { | ||||||||||||||
| // Given | ||||||||||||||
| let now = Date() | ||||||||||||||
| let now = fixedDate(daysFromReference: 0) | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using a previous date for A common approach is to mock the woocommerce-ios/Modules/Sources/Yosemite/PointOfSale/Coupons/POSCoupon.swift Lines 16 to 21 in fa8489e
to be a function that takes in public func isExpired(currentDate: Date = Date()) -> Bool {
guard let dateExpires else {
return false
}
return dateExpires < currentDate
}This way, we can pass any date values for the current date and expiration date. Also, from the logic, wouldn't
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, so my point with "As a 3rd option, we could change the details of POSCoupon, but I don't see a good reason to change the interface only because of tests." is: If we would change the I'm happy with making the change if you feel is worth it, just questioning in case this comes up again on a different place with a similar shape 🤔 otherwise might be better to just remove it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👋 I hadn't seen this PR, but fixed this in a simpler way... WDYT about just making it
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks Josh! Let's go ahead with your PR, its more correct to treat them as expired as soon as the date is equal to expiration, fixing the flakiness as well. I'll mark this one as closed. |
||||||||||||||
| let coupon = POSCoupon(id: UUID(), code: "expired-now", dateExpires: now) | ||||||||||||||
|
|
||||||||||||||
| // Then | ||||||||||||||
| #expect(coupon.isExpired == true, "A coupon expiring at the current time should be considered expired") | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| private extension POSCouponTests { | ||||||||||||||
| /// Returns a fixed date with optional day offset | ||||||||||||||
| /// - Parameter daysFromReference: Number of days to add/subtract from the reference date (0 = reference date) | ||||||||||||||
| /// - Returns: A fixed date | ||||||||||||||
| func fixedDate(daysFromReference: Int = 0) -> Date { | ||||||||||||||
| var components = DateComponents() | ||||||||||||||
| components.year = 2024 | ||||||||||||||
| components.month = 6 | ||||||||||||||
| components.day = 15 | ||||||||||||||
| components.hour = 12 | ||||||||||||||
| components.minute = 0 | ||||||||||||||
| components.second = 0 | ||||||||||||||
| components.timeZone = TimeZone(identifier: "UTC") | ||||||||||||||
|
|
||||||||||||||
| guard let baseDate = Calendar(identifier: .gregorian).date(from: components) else { | ||||||||||||||
| // Fallback to the same date (2024-06-15 12:00:00 UTC) if calendar creation fails | ||||||||||||||
| return Date(timeIntervalSince1970: 1718452800) | ||||||||||||||
| } | ||||||||||||||
| guard daysFromReference != 0 else { return baseDate } | ||||||||||||||
|
|
||||||||||||||
| return Calendar.current.date(byAdding: .day, value: daysFromReference, to: baseDate) ?? baseDate | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I cannot compile the test target unless I add this import, which seems to come from a missing
rake generatesomewhere else.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest commit in trunk doesn't have this import and passed CI though, could it be from packages not being fully resolved / a need to build the app before tests / a need for a clean build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried removing this line, building the app then ran
POSCouponTests, both the app and tests ran. I've had frustrating times when it took a few tries rebuilding/cleaning though. If you get to build and run tests without this line, let's remove this.