Skip to content

Commit

Permalink
fix: projects-with-stats response for single language (#2695)
Browse files Browse the repository at this point in the history
  • Loading branch information
tom--k authored Nov 14, 2024
1 parent 306a6c5 commit 961c197
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]" }

organizationBuilder = admin.defaultOrganizationBuilder

addProject { name = "Project" }.build {
val en = addEnglish()

addKey { name = "test_key" }.build {
addTranslation {
text = "${en.self.name} text"
language = en.self
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 961c197

Please sign in to comment.