package model

import (
	"sparrow/pkg/models"
	"testing"

	"github.com/jinzhu/gorm"
)

func TestRoleCreate(t *testing.T) {
	role := newRole(t)
	err := role.Create(&models.Role{
		RoleCode: 0,
		RoleName: "管理员",
		MenuList: "home",
	})
	if err != nil {
		t.Fatal(err)
	}
}

func TestRoleUpdate(t *testing.T) {
	role := newRole(t)
	data := &models.Role{
		RoleCode: 0,

		RoleName: "管理员修改",
		MenuList: "home",
	}
	data.ID = 1
	err := role.Update(data)
	if err != nil {
		t.Fatal(err)
	}
}
func TestRoleDelete(t *testing.T) {
	role := newRole(t)
	data := &models.Role{
		RoleCode: 0,
		RoleName: "管理员修改",
		MenuList: "home",
	}
	data.ID = 2
	err := role.Delete(data)
	if err != nil {
		t.Fatal(err)
	}
}

func TestQueryRole(t *testing.T) {
	role := newRole(t)
	data, err := role.QueryRole(2)
	if err != nil && err == gorm.ErrRecordNotFound {
		t.Error(err)
	}
	if err != nil && err != gorm.ErrRecordNotFound {
		t.Fatal(err)
	}
	t.Log(data)
}
func TestQueryAllRole(t *testing.T) {
	role := newRole(t)
	datas, count, err := role.QueryAll(1, 10, "")
	if err != nil {
		t.Fatal(err)
	}
	t.Logf("记录数:%d,list:%v", count, datas)
}
func newRole(t *testing.T) *Role {
	db, err := getDB()
	if err != nil {
		t.Fatal(err)
	}
	Role := new(Role).Init(db)
	return Role
}
