c5e62a4061
- Added the `deadlineWithinWindowMatch` function to match raw deadline fields stored as Unix seconds or milliseconds, ensuring accurate filtering in MongoDB queries. - Updated the `ClosingSoon` method in the repository to utilize the new deadline matching logic, improving query accuracy for upcoming deadlines. - Introduced a new unit test, `TestDeadlineWithinWindowMatchCoversSecondsAndMilliseconds`, to validate the functionality of the deadline matching logic, ensuring both seconds and milliseconds are correctly handled. - Enhanced comments in the `widget_cache` and `search_list_cache` files for better clarity on cache behavior and staleness. This update improves the accuracy of deadline handling in the dashboard service and strengthens the test coverage for related functionalities.
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"go.mongodb.org/mongo-driver/v2/bson"
|
|
)
|
|
|
|
func TestFacetCountDecodesBSOND(t *testing.T) {
|
|
facet := bson.M{
|
|
"total": bson.A{
|
|
bson.D{{Key: "count", Value: int32(42)}},
|
|
},
|
|
"active": bson.A{
|
|
bson.D{{Key: "count", Value: int64(7)}},
|
|
},
|
|
}
|
|
|
|
if got := facetCount(facet, "total"); got != 42 {
|
|
t.Fatalf("expected total 42, got %d", got)
|
|
}
|
|
if got := facetCount(facet, "active"); got != 7 {
|
|
t.Fatalf("expected active 7, got %d", got)
|
|
}
|
|
}
|
|
|
|
func TestFacetCurrencyValueDecodesBSOND(t *testing.T) {
|
|
facet := bson.M{
|
|
"currency_value": bson.A{
|
|
bson.D{
|
|
{Key: "_id", Value: "eur"},
|
|
{Key: "total", Value: float64(12345.67)},
|
|
},
|
|
},
|
|
}
|
|
|
|
currency, total := facetCurrencyValue(facet)
|
|
if currency != "EUR" {
|
|
t.Fatalf("expected EUR, got %q", currency)
|
|
}
|
|
if total != 12345.67 {
|
|
t.Fatalf("expected 12345.67, got %f", total)
|
|
}
|
|
}
|
|
|
|
func TestDeadlineWithinWindowMatchCoversSecondsAndMilliseconds(t *testing.T) {
|
|
now := int64(1_700_000_000)
|
|
windowEnd := now + 86_400
|
|
|
|
match := deadlineWithinWindowMatch("submission_deadline", now, windowEnd)
|
|
orClause, ok := match["$or"].(bson.A)
|
|
if !ok || len(orClause) != 2 {
|
|
t.Fatalf("expected two range branches, got %#v", match)
|
|
}
|
|
|
|
secRange, ok := orClause[0].(bson.M)["submission_deadline"].(bson.M)
|
|
if !ok {
|
|
t.Fatalf("expected seconds range on submission_deadline, got %#v", orClause[0])
|
|
}
|
|
if secRange["$gt"] != now || secRange["$lte"] != windowEnd {
|
|
t.Fatalf("unexpected seconds range: %#v", secRange)
|
|
}
|
|
|
|
msRange, ok := orClause[1].(bson.M)["submission_deadline"].(bson.M)
|
|
if !ok {
|
|
t.Fatalf("expected milliseconds range on submission_deadline, got %#v", orClause[1])
|
|
}
|
|
if msRange["$gt"] != now*1000 || msRange["$lte"] != windowEnd*1000 {
|
|
t.Fatalf("unexpected milliseconds range: %#v", msRange)
|
|
}
|
|
}
|