Initial import
This commit is contained in:
175
insert_test.go
Normal file
175
insert_test.go
Normal file
@@ -0,0 +1,175 @@
|
||||
package mongo
|
||||
|
||||
import (
|
||||
"os"
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"encoding/json"
|
||||
|
||||
"go.mongodb.org/mongo-driver/v2/bson"
|
||||
)
|
||||
|
||||
func TestInsertOne(t *testing.T) {
|
||||
client := GetMongoClient()
|
||||
|
||||
data := map[string]any {
|
||||
"_id": "su_123456",
|
||||
"name": "MyName",
|
||||
"age": int32(25),
|
||||
}
|
||||
|
||||
o, err := client.InsertOne(context.Background(), "mydb", "mycollection", data)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to insertOne %#v", err)
|
||||
}
|
||||
|
||||
// raw query
|
||||
var results bson.M
|
||||
filter := bson.M{ "name": "MyName" }
|
||||
|
||||
c := client.Client.Database("mydb").Collection("mycollection")
|
||||
c.FindOne(context.Background(), filter).Decode(&results)
|
||||
|
||||
AssertSubset(t, o, results, "Should have been equal")
|
||||
}
|
||||
|
||||
func TestInsertOne_DataStruct(t *testing.T) {
|
||||
client := GetMongoClient()
|
||||
|
||||
data := &Sample {
|
||||
Id: "su_123457",
|
||||
Name: "MyName200",
|
||||
Age: 25,
|
||||
}
|
||||
|
||||
o, err := client.InsertOneFromStruct(context.Background(), "mydb", "mycollection", data)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to insertOne %#v", err)
|
||||
}
|
||||
|
||||
// raw query
|
||||
var results bson.M
|
||||
filter := map[string]any { "name": "MyName200" }
|
||||
|
||||
c := client.Client.Database("mydb").Collection("mycollection")
|
||||
c.FindOne(context.Background(), filter).Decode(&results)
|
||||
|
||||
AssertSubset(t, o, results, "Should have been equal")
|
||||
}
|
||||
|
||||
func TestInsertOne_WithIdPrefix(t *testing.T) {
|
||||
// Read JSON file
|
||||
data, err := os.ReadFile("./.test/user.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var user bson.M
|
||||
if err := json.Unmarshal(data, &user); err != nil {
|
||||
t.Fatalf("Length: %d, First bytes: %x\n", len(data), data[:4])
|
||||
}
|
||||
|
||||
client := GetMongoClient()
|
||||
client.AddDefinition(user)
|
||||
|
||||
in := map[string]any {
|
||||
"name": "MyName112",
|
||||
"age": int32(25),
|
||||
}
|
||||
|
||||
o, err := client.InsertOne(context.Background(), "mydb", "user", in)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to insertOne %#v", err)
|
||||
}
|
||||
|
||||
// raw query
|
||||
var results bson.M
|
||||
filter := map[string]any { "name": "MyName112" }
|
||||
c := client.Client.Database("mydb").Collection("user")
|
||||
c.FindOne(context.Background(), filter).Decode(&results)
|
||||
|
||||
if !strings.HasPrefix(results["_id"].(string), "usr_") {
|
||||
t.Fatal("_id should have been prefixed")
|
||||
}
|
||||
|
||||
AssertSubset(t, o, results, "Should have been equal")
|
||||
}
|
||||
|
||||
func TestPrepareForInsert_WithoutId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"name": "My Name Is",
|
||||
}
|
||||
|
||||
prepareForInsert(data, "")
|
||||
|
||||
if id, okid := data["_id"]; !okid || id == "" {
|
||||
t.Fatal("Failed to add Id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareForInsert_ExistingId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"_id": "myidxxxxxx",
|
||||
"name": "My Name Is",
|
||||
}
|
||||
|
||||
prepareForInsert(data, "")
|
||||
|
||||
if data["_id"] == "" {
|
||||
t.Fatal("id was updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"Name": "My Name Is",
|
||||
}
|
||||
|
||||
ensureId(data, "")
|
||||
|
||||
if id, okid := data["_id"]; !okid || id == "" {
|
||||
t.Fatal("Failed to add Id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureId_EmptyId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"_id": "myidxxxxxx",
|
||||
"name": "My Name Is",
|
||||
}
|
||||
|
||||
ensureId(data, "")
|
||||
|
||||
if data["_id"] == "" {
|
||||
t.Fatal("Id was updated")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureId_ExistingId(t *testing.T) {
|
||||
data := map[string]any {
|
||||
"_id": "",
|
||||
"name": "My Name Is",
|
||||
}
|
||||
|
||||
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",
|
||||
}
|
||||
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
now := ensureCreatedAt(data)
|
||||
|
||||
if createdAt, _ := data["created_at"]; createdAt != now {
|
||||
t.Fatal("Failed to add CreatedAt")
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user