-
-
Notifications
You must be signed in to change notification settings - Fork 366
refactor: make limit checkers lazy and add unit tests #3488
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dbd1ede
refactor: make limit checkers lazy and add unit tests
dkrizan f792edd
fix: revert unlimited check to < 0 — zero is a real limit
dkrizan b85770a
feat: add usage increase logic for translation updates, adjusted logi…
dkrizan 31be2dd
fix: add logic to track usage decrease when non-empty text becomes empty
dkrizan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...end/tests/src/test/kotlin/io/tolgee/ee/unit/limitsAndReporting/GenericLimitCheckerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package io.tolgee.ee.unit.limitsAndReporting | ||
|
|
||
| import io.tolgee.dtos.UsageLimits | ||
| import io.tolgee.ee.component.limitsAndReporting.generic.GenericLimitChecker | ||
| import org.junit.jupiter.api.Assertions.assertDoesNotThrow | ||
| import org.junit.jupiter.api.Assertions.assertFalse | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.assertThrows | ||
|
|
||
| class GenericLimitCheckerTest { | ||
| private fun fixedLimit( | ||
| included: Long, | ||
| total: Long = included, | ||
| ) = UsageLimits.Limit(included = included, limit = total) | ||
|
|
||
| private fun unlimitedLimit() = UsageLimits.Limit(included = -1, limit = -1) | ||
|
|
||
| private fun zeroLimit() = UsageLimits.Limit(included = 0, limit = 0) | ||
|
|
||
| private fun checker( | ||
| limit: UsageLimits.Limit, | ||
| isPayAsYouGo: Boolean = false, | ||
| includedProvider: (Long) -> Exception = { req -> RuntimeException("included: $req") }, | ||
| spendingProvider: (Long) -> Exception = { req -> RuntimeException("spending: $req") }, | ||
| ) = GenericLimitChecker( | ||
| limit = limit, | ||
| isPayAsYouGo = isPayAsYouGo, | ||
| includedUsageExceededExceptionProvider = includedProvider, | ||
| spendingLimitExceededExceptionProvider = spendingProvider, | ||
| ) | ||
|
|
||
| @Test | ||
| fun `zero limit - throws included exception (not treated as unlimited)`() { | ||
| val checker = checker(zeroLimit()) | ||
|
|
||
| val ex = assertThrows<RuntimeException> { checker.check { 999L } } | ||
|
|
||
| assert(ex.message == "included: 999") | ||
| } | ||
|
|
||
| @Test | ||
| fun `minus one limit - no exception and provider not called`() { | ||
| var providerCalled = false | ||
| val checker = | ||
| checker( | ||
| unlimitedLimit(), | ||
| includedProvider = { | ||
| providerCalled = true | ||
| RuntimeException() | ||
| }, | ||
| ) | ||
|
|
||
| assertDoesNotThrow { checker.check { 999L } } | ||
| assertFalse(providerCalled) | ||
| } | ||
|
|
||
| @Test | ||
| fun `provider returns null - no exception`() { | ||
| val checker = checker(fixedLimit(100)) | ||
| assertDoesNotThrow { checker.check { null } } | ||
| } | ||
|
|
||
| @Test | ||
| fun `fixed plan - required within included - no exception`() { | ||
| val checker = checker(fixedLimit(100)) | ||
| assertDoesNotThrow { checker.check { 100L } } | ||
| } | ||
|
|
||
| @Test | ||
| fun `fixed plan - required exceeds included - throws included exception`() { | ||
| val checker = checker(fixedLimit(included = 100, total = 200)) | ||
|
|
||
| val ex = assertThrows<RuntimeException> { checker.check { 150L } } | ||
|
|
||
| assert(ex.message == "included: 150") | ||
| } | ||
|
dkrizan marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| fun `payg - required exceeds included but within limit - no exception`() { | ||
| val checker = checker(fixedLimit(included = 100, total = 200), isPayAsYouGo = true) | ||
| assertDoesNotThrow { checker.check { 150L } } | ||
| } | ||
|
|
||
| @Test | ||
| fun `payg - required equals limit - no exception`() { | ||
| val checker = checker(fixedLimit(included = 100, total = 200), isPayAsYouGo = true) | ||
| assertDoesNotThrow { checker.check { 200L } } | ||
| } | ||
|
|
||
| @Test | ||
| fun `payg - required exceeds limit - throws spending exception`() { | ||
| val checker = checker(fixedLimit(included = 100, total = 200), isPayAsYouGo = true) | ||
|
|
||
| val ex = assertThrows<RuntimeException> { checker.check { 201L } } | ||
|
|
||
| assert(ex.message == "spending: 201") | ||
| } | ||
|
|
||
| @Test | ||
| fun `check nullable overload with value - behaves same as lambda overload`() { | ||
| val checker = checker(fixedLimit(included = 100, total = 200)) | ||
|
|
||
| val ex = assertThrows<RuntimeException> { checker.check(150L) } | ||
|
|
||
| assert(ex.message == "included: 150") | ||
| } | ||
|
|
||
| @Test | ||
| fun `check nullable overload with null - no exception`() { | ||
| val checker = checker(fixedLimit(100)) | ||
| assertDoesNotThrow { checker.check(null) } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.