Add timeseries, discriminator

This commit is contained in:
George Suntres
2026-04-17 11:38:12 -04:00
parent cba3e326e8
commit 99b36e577e
18 changed files with 897 additions and 130 deletions

View File

@@ -1,10 +1,14 @@
package mongo
import (
"os"
"context"
"testing"
"encoding/json"
"go.mongodb.org/mongo-driver/v2/bson"
"git.gsuntres.com/general/commons"
)
func TestFind_Default(t *testing.T) {
@@ -49,3 +53,69 @@ func TestFind_Default(t *testing.T) {
t.Fatalf("Expected total to be 1 but found %d", total)
}
}
func TestFind_Discriminator(t *testing.T) {
// 1. Register schemas
schemaStore, err := os.ReadFile("./.test/store.json")
if err != nil { t.Fatal(err) }
schemaOffer, err := os.ReadFile("./.test/offer.json")
if err != nil { t.Fatal(err) }
var store bson.M
if err := json.Unmarshal(schemaStore, &store); err != nil {
t.Fatalf("Length: %d, First bytes: %x\n", len(schemaStore), schemaStore[:4])
}
var offer bson.M
if err := json.Unmarshal(schemaOffer, &offer); err != nil {
t.Fatalf("Length: %d, First bytes: %x\n", len(schemaOffer), schemaOffer[:4])
}
client := GetMongoClient()
client.AddDefinition(store)
client.AddDefinition(offer)
// Save two offers with the similar name in different stores each.
ctx1 := context.Background()
ctx1 = context.WithValue(ctx1, "account", "xxxxxx")
ctx1 = context.WithValue(ctx1, "store", "str_1234")
// One offer in str_1234
offer1 := map[string]any { "name": "OSRAM 1" }
_, err = client.InsertOne(ctx1, "mydb", "offer", offer1)
if err != nil { t.Fatalf("Failed to insertOne %#v", err) }
// The other in str_4321
ctx2 := context.Background()
ctx2 = context.WithValue(ctx2, "account", "xxxxxx")
ctx2 = context.WithValue(ctx2, "store", "str_4321")
offer2 := map[string]any { "name": "OSRAM 2" }
_, err = client.InsertOne(ctx2, "mydb", "offer", offer2)
if err != nil { t.Fatalf("Failed to insertOne %#v", err) }
// Now searching in store str_1234 for OSRAM should return only one
filter := bson.M{"name": bson.M{"$regex": "OSRAM*"}}
findResult, err := client.Find(ctx1, "mydb", "offer", filter, 0)
if err != nil { t.Fatalf("Failed to find %#v", err) }
dataAny, hasData := findResult["data"]
if !hasData { t.Fatal("no data") }
data := dataAny.(bson.A)
if len(data) != 1 {
t.Fatalf("Expected to return 1 document but got %d", len(data))
}
arr, _ := commons.BsonAToSlice(data)
o := arr[0]
name := o["name"]
if name != "OSRAM 1" {
t.Fatalf("Expected OSRAM 1 not %s", name)
}
}