mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-04 21:30:24 +08:00
Add db sync test.
This commit is contained in:
45
original/adapter.go
Normal file
45
original/adapter.go
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package original
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/astaxie/beego"
|
||||||
|
"github.com/casdoor/casdoor/object"
|
||||||
|
_ "github.com/go-sql-driver/mysql" // db = mysql
|
||||||
|
//_ "github.com/lib/pq" // db = postgres
|
||||||
|
)
|
||||||
|
|
||||||
|
var adapter *object.Adapter
|
||||||
|
|
||||||
|
func initConfig() {
|
||||||
|
err := beego.LoadAppConfig("ini", "../conf/app.conf")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
initAdapter()
|
||||||
|
}
|
||||||
|
|
||||||
|
func initAdapter() {
|
||||||
|
adapter = object.NewAdapter(beego.AppConfig.String("driverName"), beego.AppConfig.String("dataSourceName"), dbName)
|
||||||
|
createTable(adapter)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTable(a *object.Adapter) {
|
||||||
|
err := a.Engine.Sync2(new(User))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
18
original/conf.go
Normal file
18
original/conf.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package original
|
||||||
|
|
||||||
|
var dbName = "dbName"
|
||||||
|
var tableName = "tableName"
|
38
original/user_original.go
Normal file
38
original/user_original.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package original
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Id int `xorm:"int notnull pk autoincr" json:"id"`
|
||||||
|
Name string `xorm:"varchar(128)" json:"name"`
|
||||||
|
Password string `xorm:"varchar(128)" json:"password"`
|
||||||
|
Cellphone string `xorm:"varchar(128)" json:"cellphone"`
|
||||||
|
Avatar string `xorm:"varchar(128)" json:"avatar"`
|
||||||
|
Deleted int `xorm:"tinyint(1)" json:"deleted"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (User) TableName() string {
|
||||||
|
return tableName
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUsers() []*User {
|
||||||
|
users := []*User{}
|
||||||
|
err := adapter.Engine.Asc("id").Find(&users)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return users
|
||||||
|
}
|
30
original/user_original_test.go
Normal file
30
original/user_original_test.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package original
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGetUsers(t *testing.T) {
|
||||||
|
initConfig()
|
||||||
|
initAdapter()
|
||||||
|
|
||||||
|
users := getUsers()
|
||||||
|
for _, user := range users {
|
||||||
|
fmt.Printf("%v\n", user)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user