Add unit tests for tender recommendation translation handling
continuous-integration/drone/push Build is passing

- Introduced a new test file `recommendation_translation_test.go` to validate the behavior of the tender recommendation service regarding translation handling.
- Implemented tests to ensure that the service correctly applies stored translations and retains original text when no translation is available.
- Enhanced the `Recommend` method in the tender service to utilize a structured approach for managing recommended tenders, improving clarity and maintainability.

This update improves the testing coverage for translation handling in tender recommendations, ensuring accurate responses based on available translations and original content.
This commit is contained in:
Mazyar
2026-07-05 21:01:42 +03:30
parent 3221a31ac8
commit a762430bca
2 changed files with 162 additions and 7 deletions
+28 -7
View File
@@ -995,7 +995,13 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
lang := s.pickResponseLanguage(form.Language)
now := time.Now().Unix()
ordered := make([]TenderResponse, 0, len(recommendations))
type rankedRecommendedTender struct {
tender Tender
rank int
analysis string
tenderRef string
}
ordered := make([]rankedRecommendedTender, 0, len(recommendations))
for _, rec := range recommendations {
tenderRef := strings.TrimSpace(rec.TenderID)
tender, ok := tenderByRef[tenderRef]
@@ -1016,11 +1022,12 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
continue
}
resp := tender.ToResponseWithLanguage(lang)
resp.Rank = rec.Rank
resp.Analysis = rec.Analysis
resp.ProcedureRef = tenderRef
ordered = append(ordered, *resp)
ordered = append(ordered, rankedRecommendedTender{
tender: tender,
rank: rec.Rank,
analysis: rec.Analysis,
tenderRef: tenderRef,
})
}
total := int64(len(ordered))
@@ -1033,12 +1040,26 @@ func (s *tenderService) Recommend(ctx context.Context, form *SearchForm, paginat
end = len(ordered)
}
paged := make([]TenderResponse, 0, end-start)
for _, item := range ordered[start:end] {
paged = append(paged, s.buildRecommendedTenderResponse(ctx, item.tender, item.rank, item.analysis, item.tenderRef, lang))
}
return &SearchResponse{
Tenders: ordered[start:end],
Tenders: paged,
Metadata: pagination.ListMeta(total, "", end < len(ordered), start),
}, nil
}
func (s *tenderService) buildRecommendedTenderResponse(ctx context.Context, tender Tender, rank int, analysis, tenderRef, language string) TenderResponse {
resp := tender.ToResponseWithLanguage(language)
s.enrichWithTranslation(ctx, &tender, resp, language)
resp.Rank = rank
resp.Analysis = analysis
resp.ProcedureRef = tenderRef
return *resp
}
func statusAllowed(status TenderStatus, allowed []string) bool {
for _, s := range allowed {
if string(status) == strings.TrimSpace(s) {