mongo_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package api
  2. import (
  3. "fmt"
  4. "testing"
  5. "time"
  6. "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/api/errs"
  7. "github.com/davecgh/go-spew/spew"
  8. "go.mongodb.org/mongo-driver/bson"
  9. "go.mongodb.org/mongo-driver/bson/primitive"
  10. )
  11. var (
  12. _mongo *Mongo
  13. _run = map[string]bool{
  14. "TestList": false,
  15. "TestOne": false,
  16. "TestPatchOne": true,
  17. "TestInsertOne": false,
  18. "TestInsertMany": false,
  19. }
  20. )
  21. func run(id string, fn func()) {
  22. if run, found := _run[id]; found && run {
  23. fn()
  24. }
  25. }
  26. func init() {
  27. _mongo = &Mongo{Config: "mongodb://localhost:27020:codegen"}
  28. if err := _mongo.Init(); err != nil {
  29. panic(err)
  30. }
  31. fmt.Println("\ninit....")
  32. }
  33. type Method struct {
  34. Id primitive.ObjectID `bson:"_id,omitempty" json:"id"`
  35. Technique string `bson:"technique" json:"technique" valid:"required"` // Nome da técnica.
  36. Name string `bson:"name" json:"name" valid:"required"`
  37. }
  38. func (t *Method) Patch() bson.A {
  39. return bson.A{
  40. bson.M{"$set": t},
  41. }
  42. }
  43. func TestToken(t *testing.T) {
  44. // grant := map[string]interface{}{}
  45. // _, err := _mongo.FindOne(&Filter{
  46. // DB: ACCOUNT_DB,
  47. // Collection: AccessTokensCollection,
  48. // Entity: &grant,
  49. // Query: &bson.M{
  50. // "clientId": clientID,
  51. // "sessionId": session,
  52. // },
  53. // })
  54. // if err != nil {
  55. // t.Error(err)
  56. // }
  57. // spew.Dump(grant)
  58. // spew.Dump(err)
  59. }
  60. func TestList(t *testing.T) {
  61. run("TestList", func() {
  62. var (
  63. err *errs.Error
  64. entities = []*Method{}
  65. )
  66. filter := &Filter{
  67. DB: "codegen",
  68. Collection: "caracterization_method",
  69. Entities: &entities,
  70. }
  71. if _, err = _mongo.FindMany(filter); err != nil {
  72. t.Error(err)
  73. }
  74. spew.Dump(entities)
  75. })
  76. }
  77. func TestOne(t *testing.T) {
  78. run("TestOne", func() {
  79. var (
  80. err *errs.Error
  81. entities = &Method{}
  82. oid, errd = primitive.ObjectIDFromHex("5c0e9050a54f4c5d592c24bb")
  83. )
  84. if errd != nil {
  85. t.Error(errd)
  86. return
  87. }
  88. filter := &Filter{
  89. DB: "codegen",
  90. Collection: "caracterization_method",
  91. Entity: entities,
  92. Id: oid,
  93. }
  94. if _, err = _mongo.FindOne(filter); err != nil {
  95. t.Error(err)
  96. }
  97. spew.Dump(entities)
  98. })
  99. }
  100. func TestPatchOne(t *testing.T) {
  101. run("TestPatchOne", func() {
  102. var (
  103. err *errs.Error
  104. // now
  105. entity = &Method{
  106. Name: "xxxla" + time.Now().Format(time.RFC3339),
  107. }
  108. oid, errd = primitive.ObjectIDFromHex("5c0e9809a54f4c5d592c24db")
  109. // oid, errd = primitive.ObjectIDFromHex("5c0e9809a54f7c5d592c24db")
  110. )
  111. if errd != nil {
  112. t.Error(errd)
  113. return
  114. }
  115. filter := &Filter{
  116. DB: "codegen",
  117. Collection: "caracterization_method",
  118. Entity: entity,
  119. Id: oid,
  120. }
  121. if _, err = _mongo.PatchOne(filter); err != nil {
  122. t.Error(err)
  123. }
  124. spew.Dump(entity)
  125. })
  126. }
  127. func TestInsertOne(t *testing.T) {
  128. run("TestInsertOne", func() {
  129. var (
  130. err *errs.Error
  131. entity = &Method{
  132. Name: "xxlaONE",
  133. Technique: "casaONE",
  134. }
  135. )
  136. filter := &Filter{
  137. DB: "codegen",
  138. Collection: "caracterization_method",
  139. Entity: entity,
  140. }
  141. if _, err = _mongo.InsertOne(filter); err != nil {
  142. t.Error(err)
  143. }
  144. spew.Dump(entity)
  145. })
  146. }
  147. func TestInsertMany(t *testing.T) {
  148. run("TestInsertMany", func() {
  149. var (
  150. err *errs.Error
  151. entities = []*Method{
  152. &Method{
  153. Name: "xxla",
  154. Technique: "casa",
  155. },
  156. &Method{
  157. Name: "xxla2",
  158. Technique: "casa2",
  159. },
  160. }
  161. )
  162. filter := &Filter{
  163. DB: "codegen",
  164. Collection: "caracterization_method",
  165. Entities: entities,
  166. }
  167. if _, err = _mongo.InsertMany(filter); err != nil {
  168. t.Error(err)
  169. }
  170. spew.Dump(entities)
  171. })
  172. }