diff --git a/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/organizationController/OrganizationProjectsControllerTest.kt b/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/organizationController/OrganizationProjectsControllerTest.kt index e5476ac6b1..ae50607e37 100644 --- a/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/organizationController/OrganizationProjectsControllerTest.kt +++ b/backend/app/src/test/kotlin/io/tolgee/api/v2/controllers/organizationController/OrganizationProjectsControllerTest.kt @@ -1,6 +1,7 @@ package io.tolgee.api.v2.controllers.organizationController import io.tolgee.development.testDataBuilder.data.PermissionsTestData +import io.tolgee.development.testDataBuilder.data.ProjectTranslationsStatsTestData import io.tolgee.fixtures.andAssertThatJson import io.tolgee.fixtures.andIsOk import io.tolgee.fixtures.andPrettyPrint @@ -126,4 +127,23 @@ class OrganizationProjectsControllerTest : AuthorizedControllerTest() { node("_embedded.projects").isArray.hasSize(1) } } + + @Test + fun `project with single language should show correct translation percentages`() { + val testData = ProjectTranslationsStatsTestData() + val user = testData.admin.self + + testDataService.saveTestData(testData.root) + userAccount = user + performAuthGet("/v2/organizations/${testData.organizationBuilder.self.id}/projects-with-stats").andPrettyPrint + .andAssertThatJson { + node("_embedded.projects") { + node("").isArray.hasSize(1) + node("[0].stats.translationStatePercentages") { + node("TRANSLATED").isNumber.isEqualTo("100.0") + node("UNTRANSLATED").isNumber.isEqualTo("0.0") + } + } + } + } } diff --git a/backend/data/src/main/kotlin/io/tolgee/development/testDataBuilder/data/ProjectTranslationsStatsTestData.kt b/backend/data/src/main/kotlin/io/tolgee/development/testDataBuilder/data/ProjectTranslationsStatsTestData.kt new file mode 100644 index 0000000000..55f308c0e1 --- /dev/null +++ b/backend/data/src/main/kotlin/io/tolgee/development/testDataBuilder/data/ProjectTranslationsStatsTestData.kt @@ -0,0 +1,28 @@ +package io.tolgee.development.testDataBuilder.data + +import io.tolgee.development.testDataBuilder.builders.OrganizationBuilder +import io.tolgee.development.testDataBuilder.builders.TestDataBuilder +import io.tolgee.development.testDataBuilder.builders.UserAccountBuilder + +class ProjectTranslationsStatsTestData { + var organizationBuilder: OrganizationBuilder + var admin: UserAccountBuilder + + val root: TestDataBuilder = + TestDataBuilder().apply { + admin = addUserAccount { username = "admin@admin.com" } + + organizationBuilder = admin.defaultOrganizationBuilder + + addProject { name = "Project" }.build { + val en = addEnglish() + + addKey { name = "test_key" }.build { + addTranslation { + text = "${en.self.name} text" + language = en.self + } + } + } + } +} diff --git a/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectStatsService.kt b/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectStatsService.kt index 22539399fb..c57cf17e55 100644 --- a/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectStatsService.kt +++ b/backend/data/src/main/kotlin/io/tolgee/service/project/ProjectStatsService.kt @@ -61,6 +61,14 @@ class ProjectStatsService( val baseWordsCount = baseStats.translatedWords + baseStats.reviewedWords val nonBaseLanguages = languageStats.filterNot { it.languageId == baseLanguage.id } + if (nonBaseLanguages.isEmpty()) { + return ProjectStateTotals( + baseWordsCount = baseWordsCount, + translatedPercent = baseStats.translatedPercentage, + reviewedPercent = baseStats.reviewedPercentage, + ) + } + val allNonBaseTotalBaseWords = baseWordsCount * nonBaseLanguages.size val allNonBaseTotalTranslatedWords = nonBaseLanguages.sumOf { it.translatedWords } val allNonBaseTotalReviewedWords = nonBaseLanguages.sumOf { it.reviewedWords }