package api import ( "fmt" "testing" "time" "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs" "github.com/davecgh/go-spew/spew" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/primitive" ) var ( _mongo *Mongo _run = map[string]bool{ "TestList": false, "TestOne": false, "TestPatchOne": true, "TestInsertOne": false, "TestInsertMany": false, } ) func run(id string, fn func()) { if run, found := _run[id]; found && run { fn() } } func init() { _mongo = &Mongo{Config: "mongodb://localhost:27020:codegen"} if err := _mongo.Init(); err != nil { panic(err) } fmt.Println("\ninit....") } type Method struct { Id primitive.ObjectID `bson:"_id,omitempty" json:"id"` Technique string `bson:"technique" json:"technique" valid:"required"` // Nome da técnica. Name string `bson:"name" json:"name" valid:"required"` } func (t *Method) Patch() bson.A { return bson.A{ bson.M{"$set": t}, } } func TestToken(t *testing.T) { // grant := map[string]interface{}{} // _, err := _mongo.FindOne(&Filter{ // DB: ACCOUNT_DB, // Collection: AccessTokensCollection, // Entity: &grant, // Query: &bson.M{ // "clientId": clientID, // "sessionId": session, // }, // }) // if err != nil { // t.Error(err) // } // spew.Dump(grant) // spew.Dump(err) } func TestList(t *testing.T) { run("TestList", func() { var ( err *errs.Error entities = []*Method{} ) filter := &Filter{ DB: "codegen", Collection: "caracterization_method", Entities: &entities, } if _, err = _mongo.FindMany(filter); err != nil { t.Error(err) } spew.Dump(entities) }) } func TestOne(t *testing.T) { run("TestOne", func() { var ( err *errs.Error entities = &Method{} oid, errd = primitive.ObjectIDFromHex("5c0e9050a54f4c5d592c24bb") ) if errd != nil { t.Error(errd) return } filter := &Filter{ DB: "codegen", Collection: "caracterization_method", Entity: entities, Id: oid, } if _, err = _mongo.FindOne(filter); err != nil { t.Error(err) } spew.Dump(entities) }) } func TestPatchOne(t *testing.T) { run("TestPatchOne", func() { var ( err *errs.Error // now entity = &Method{ Name: "xxxla" + time.Now().Format(time.RFC3339), } oid, errd = primitive.ObjectIDFromHex("5c0e9809a54f4c5d592c24db") // oid, errd = primitive.ObjectIDFromHex("5c0e9809a54f7c5d592c24db") ) if errd != nil { t.Error(errd) return } filter := &Filter{ DB: "codegen", Collection: "caracterization_method", Entity: entity, Id: oid, } if _, err = _mongo.PatchOne(filter); err != nil { t.Error(err) } spew.Dump(entity) }) } func TestInsertOne(t *testing.T) { run("TestInsertOne", func() { var ( err *errs.Error entity = &Method{ Name: "xxlaONE", Technique: "casaONE", } ) filter := &Filter{ DB: "codegen", Collection: "caracterization_method", Entity: entity, } if _, err = _mongo.InsertOne(filter); err != nil { t.Error(err) } spew.Dump(entity) }) } func TestInsertMany(t *testing.T) { run("TestInsertMany", func() { var ( err *errs.Error entities = []*Method{ &Method{ Name: "xxla", Technique: "casa", }, &Method{ Name: "xxla2", Technique: "casa2", }, } ) filter := &Filter{ DB: "codegen", Collection: "caracterization_method", Entities: entities, } if _, err = _mongo.InsertMany(filter); err != nil { t.Error(err) } spew.Dump(entities) }) }