123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- 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)
- })
- }
|