-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_router_test.go
More file actions
91 lines (74 loc) · 1.99 KB
/
basic_router_test.go
File metadata and controls
91 lines (74 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package example_test
import (
"testing"
"net/http"
"net/http/httptest"
"github.com/gin-gonic/gin"
"github.com/philiphil/restman/orm"
"github.com/philiphil/restman/orm/entity"
"github.com/philiphil/restman/orm/gormrepository"
"github.com/philiphil/restman/route"
"github.com/philiphil/restman/router"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/logger"
)
//This is the most basic example of how to use the restman package
// We have an entity, a struct that implements the entity.Entity interface
// here we chose to use the BaseEntity struct that implements the ID field and random common fields
type Test struct {
entity.BaseEntity
}
// This function is required by the Entity interface
func (e Test) GetId() entity.ID {
return e.Id
}
// and this one, note that we do not use pointer here
func (e Test) SetId(id any) entity.Entity {
e.Id = entity.CastId(id)
return e
}
// This one is for the Model Interface, since we are using the same struct for the Entity and the Model
// we can just return the struct itself
func (t Test) ToEntity() Test {
return t
}
// same thing
func (t Test) FromEntity(entity Test) any {
return entity
}
func getDB() *gorm.DB {
db, err := gorm.Open(sqlite.Open("file:test?mode=memory&cache=shared&_fk=1"), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
CreateBatchSize: 1000,
})
if err != nil {
panic(err)
}
db.Clauses(clause.OnConflict{
UpdateAll: true,
})
db.Session(&gorm.Session{FullSaveAssociations: true})
return db
}
func SetupRouter() *gin.Engine {
r := gin.New()
r.Use(gin.Recovery())
test_ := router.NewApiRouter(
*orm.NewORM(gormrepository.NewRepository[Test](getDB())),
route.DefaultApiRoutes(),
)
getDB().AutoMigrate(&Test{})
test_.AllowRoutes(r)
return r
}
func TestMain(m *testing.M) {
r := SetupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/api/test", nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
panic("Failed to start server")
}
}