Add timeseries, discriminator
This commit is contained in:
137
insert_test.go
137
insert_test.go
@@ -5,7 +5,6 @@ import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"encoding/json"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
@@ -122,54 +121,118 @@ func TestPrepareForInsert_ExistingId(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"Name": "My Name Is",
|
||||
func TestInsertOne_Discriminate(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])
|
||||
}
|
||||
|
||||
ensureId(data, "")
|
||||
|
||||
if id, okid := data["_id"]; !okid || id == "" {
|
||||
t.Fatal("Failed to add Id")
|
||||
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)
|
||||
|
||||
// Test
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "account", "xxxxxx")
|
||||
ctx = context.WithValue(ctx, "store", "str_1234")
|
||||
|
||||
// 2. Insert data
|
||||
offer1 := map[string]any {
|
||||
"name": "Offer 1",
|
||||
}
|
||||
|
||||
saved, err := client.InsertOne(ctx, "mydb", "offer", offer1)
|
||||
if err != nil { t.Fatalf("Failed to insertOne %#v", err) }
|
||||
|
||||
v, ok := saved["store"]
|
||||
if !ok || v != "str_1234" {
|
||||
t.Fatalf("Should have set store not %s", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureId_EmptyId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"_id": "myidxxxxxx",
|
||||
"name": "My Name Is",
|
||||
func TestInsertOne_Discriminate_NoStore(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])
|
||||
}
|
||||
|
||||
ensureId(data, "")
|
||||
|
||||
if data["_id"] == "" {
|
||||
t.Fatal("Id was updated")
|
||||
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)
|
||||
|
||||
// Test
|
||||
ctx := context.Background()
|
||||
|
||||
// 2. Insert data
|
||||
offer1 := map[string]any {
|
||||
"name": "Offer 1",
|
||||
}
|
||||
|
||||
_, err = client.InsertOne(ctx, "mydb", "offer", offer1)
|
||||
if err == nil {
|
||||
t.Fatal("Should have required store")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureId_ExistingId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"_id": "",
|
||||
"name": "My Name Is",
|
||||
func TestInsertOne_Discriminate_EnsureStore(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])
|
||||
}
|
||||
|
||||
ensureId(data, "")
|
||||
|
||||
if id, okid := data["_id"]; !okid || id == "" {
|
||||
t.Fatal("Failed to add Id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureCreatedAt(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"name": "My Name Is",
|
||||
var offer bson.M
|
||||
if err := json.Unmarshal(schemaOffer, &offer); err != nil {
|
||||
t.Fatalf("Length: %d, First bytes: %x\n", len(schemaOffer), schemaOffer[:4])
|
||||
}
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
now := ensureCreatedAt(data)
|
||||
client := GetMongoClient()
|
||||
client.AddDefinition(store)
|
||||
client.AddDefinition(offer)
|
||||
|
||||
if createdAt, _ := data["created_at"]; createdAt != now {
|
||||
t.Fatal("Failed to add CreatedAt")
|
||||
}
|
||||
})
|
||||
}
|
||||
// Test
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "store", "str_1234")
|
||||
|
||||
// 2. Insert data
|
||||
offer1 := map[string]any {
|
||||
"name": "Offer 100",
|
||||
"store": "str_0000",
|
||||
}
|
||||
|
||||
saved, err := client.InsertOne(ctx, "mydb", "offer", offer1)
|
||||
|
||||
if saved["store"] != "str_1234" {
|
||||
t.Fatal("Wrong store")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user