package wmo import ( "encoding/json" "testing" ct "git.blackforestbytes.com/BlackForestBytes/goext/cursortoken" "git.blackforestbytes.com/BlackForestBytes/goext/exerr" "git.blackforestbytes.com/BlackForestBytes/goext/langext" "git.blackforestbytes.com/BlackForestBytes/goext/rfctime" "git.blackforestbytes.com/BlackForestBytes/goext/tst" "go.mongodb.org/mongo-driver/v2/bson" ) type TestExampleID string //@id:type func (i TestExampleID) MarshalBSONValue() (byte, []byte, error) { if objId, err := bson.ObjectIDFromHex(string(i)); err == nil { tp, data, err := bson.MarshalValue(objId) return byte(tp), data, err } else { return 0, nil, exerr.New(exerr.TypeMarshalEntityID, "Failed to marshal UserID("+i.String()+") to ObjectId").Str("value", string(i)).Type("type", i).Build() } } func (i TestExampleID) String() string { return string(i) } func (i TestExampleID) ObjID() (bson.ObjectID, error) { return bson.ObjectIDFromHex(string(i)) } func (i TestExampleID) Valid() bool { _, err := bson.ObjectIDFromHex(string(i)) return err == nil } func (i TestExampleID) IsZero() bool { return i == "" } var _ MongoEntityID = (*TestExampleID)(nil) func TestCreatePaginationPipelineWithInvalidObjectIDValue(t *testing.T) { // // {"v1":"2026-05-16T21:44:13.16Z","v2":"6a08e52d144a332c2e2d067b","dir":"DESC","dir2":"DESC","size":50} strtok := "tok_PMRHMMJCHIRDEMBSGYWTANJNGE3FIMRRHI2DIORRGMXDCNS2EIWCE5RSEI5CENTBGA4GKNJSMQYTINDBGMZTEYZSMUZGIMBWG5RCELBCMRUXEIR2EJCEKU2DEIWCEZDJOIZCEORCIRCVGQZCFQRHG2L2MURDUNJQPU======" tok, err := ct.Decode(strtok) if err != nil { t.Error("Failed to decode token:", err) return } type obj struct { ID TestExampleID `bson:"_id"` Created rfctime.RFC3339NanoTime `bson:"created"` } coll := Coll[obj]{coll: nil} coll.init() paginationPipeline, doubleSortPipeline, err := createPaginationPipeline[obj](&coll, tok.(ct.CTKeySort), "_id", ct.SortDESC, new("_id"), new(ct.SortDESC), new(15)) if err != nil { t.Error("Failed to create pagination Pipeline:", err) return } //fmt.Printf("# paginationPipeline:\n%+v\n\n", string(langext.Must(json.Marshal(paginationPipeline)))) //fmt.Printf("# doubleSortPipeline:\n%+v\n\n", string(langext.Must(json.Marshal(doubleSortPipeline)))) tst.AssertEqual(t, string(langext.Must(json.Marshal(paginationPipeline))), `[{"$sort":{"_id":-1}},{"$limit":16}]`) // wrong (_id with wrong date type) fields are not used tst.AssertEqual(t, string(langext.Must(json.Marshal(doubleSortPipeline))), `[{"$sort":{"_id":-1}}]`) // wrong (_id with wrong date type) fields are not used } func TestCreatePaginationPipelineWithValidObjectIDValue(t *testing.T) { // // {"v1":"6a08e52d144a332c2e2d067b","v2":"6a08e52d144a332c2e2d067b","dir":"DESC","dir2":"DESC","size":50} strtok := "tok_PMFCAIBAEARHMMJCHIQCENTBGA4GKNJSMQYTINDBGMZTEYZSMUZGIMBWG5RCELAKEAQCAIBCOYZCEORAEI3GCMBYMU2TEZBRGQ2GCMZTGJRTEZJSMQYDMN3CEIWAUIBAEAQCEZDJOIRDUIBCIRCVGQZCFQFCAIBAEARGI2LSGIRDUIBCIRCVGQZCFQFCAIBAEARHG2L2MURDUIBVGAFH2===" tok, err := ct.Decode(strtok) if err != nil { t.Error("Failed to decode token:", err) return } type obj struct { ID TestExampleID `bson:"_id"` Created rfctime.RFC3339NanoTime `bson:"created"` } coll := Coll[obj]{coll: nil} coll.init() paginationPipeline, doubleSortPipeline, err := createPaginationPipeline[obj](&coll, tok.(ct.CTKeySort), "_id", ct.SortDESC, new("_id"), new(ct.SortDESC), new(15)) if err != nil { t.Error("Failed to create pagination Pipeline:", err) return } //fmt.Printf("# paginationPipeline:\n%+v\n\n", string(langext.Must(json.Marshal(paginationPipeline)))) //fmt.Printf("# doubleSortPipeline:\n%+v\n\n", string(langext.Must(json.Marshal(doubleSortPipeline)))) tst.AssertEqual(t, string(langext.Must(json.Marshal(paginationPipeline))), `[{"$match":{"$or":[{"_id":{"$lt":"6a08e52d144a332c2e2d067b"}}]}},{"$sort":{"_id":-1}},{"$limit":16}]`) tst.AssertEqual(t, string(langext.Must(json.Marshal(doubleSortPipeline))), `[{"$sort":{"_id":-1}}]`) }