package rest

import (
	"fmt"
	"git.nspix.com/golang/rest/orm/schema"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"testing"
)

type TestModel struct {
	Id          int    `json:"id"`
	Name        string `json:"name"`
	Age         int    `json:"age"`
	Description string `json:"description"`
}

func TestLocalDriver(t *testing.T) {
	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		t.Fatal(err.Error())
	}
	Initialize(nil, db)
	if err = RegisterModel("test", &TestModel{}); err != nil {
		t.Fatal(err.Error())
	} else {
		t.Log("OK")
	}
}

func TestRemoteDriver(t *testing.T) {
	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		t.Fatal(err.Error())
	}
	schema.SetDriver(&schema.RemoteDriver{Url: "https://schema.nspix.com/schema"})
	Initialize(nil, db)
	if err = RegisterModel("test", &TestModel{}); err != nil {
		t.Fatal(err.Error())
	} else {
		t.Log("OK")
	}
}

func TestVisibleField(t *testing.T) {
	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		t.Fatal(err.Error())
	}
	schema.SetDriver(&schema.RemoteDriver{Url: "https://schema.nspix.com/schema"})
	Initialize(nil, db)
	ss := schema.VisibleField("test", "test_models", "")
	for _, s := range ss {
		fmt.Println(s)
	}
}
