(t *testing.T)
| 10 | ) |
| 11 | |
| 12 | func TestCalculateRecomScore(t *testing.T) { |
| 13 | cfg := &config.Config{} |
| 14 | rc, err := NewRecomComponent(cfg) |
| 15 | if err != nil { |
| 16 | t.Fatal(err) |
| 17 | } |
| 18 | ctx := context.Background() |
| 19 | |
| 20 | // Test case 1: repository created 24 hours ago |
| 21 | repo1 := &database.Repository{} |
| 22 | repo1.CreatedAt = time.Now().Add(-24 * time.Hour) |
| 23 | weights1 := map[string]string{ |
| 24 | "freshness": expFreshness, |
| 25 | } |
| 26 | score1 := rc.calcTotalScore(ctx, repo1, weights1) |
| 27 | if score1 > 100 || score1 < 98 { |
| 28 | t.Errorf("Expected score1 should in range [98,100], got: %f", score1) |
| 29 | } |
| 30 | |
| 31 | // Test case 2: repository created 48 hours ago |
| 32 | repo2 := &database.Repository{} |
| 33 | repo2.CreatedAt = time.Now().Add(-50 * time.Hour) |
| 34 | weights2 := map[string]string{ |
| 35 | "freshness": expFreshness, |
| 36 | } |
| 37 | score2 := rc.calcTotalScore(ctx, repo2, weights2) |
| 38 | if score2 > 98.0 || score2 < 60.0 { |
| 39 | t.Errorf("Expected score1 should in range [60,98), got: %f", score2) |
| 40 | } |
| 41 | |
| 42 | // Test case 3: repository created 168 hours ago |
| 43 | repo3 := &database.Repository{} |
| 44 | repo3.CreatedAt = time.Now().Add(-168 * time.Hour) |
| 45 | weights3 := map[string]string{ |
| 46 | "freshness": expFreshness, |
| 47 | } |
| 48 | score3 := rc.calcTotalScore(ctx, repo3, weights3) |
| 49 | if score3 < 0 || score3 > 60 { |
| 50 | t.Errorf("Expected score1 should in range [0,60), got: %f", score2) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | const expFreshness = ` |
| 55 | if hours <= 48{ |
nothing calls this directly
no test coverage detected