Skip to content
Snippets Groups Projects
Commit 83fe71c5 authored by Nicolas PAGNY's avatar Nicolas PAGNY
Browse files

Created client db

parent 0c411ba2
No related branches found
No related tags found
No related merge requests found
package client
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Client struct {
gorm.Model
ID uint `gorm:"primaryKey"`
Id_client string
Secret string
}
var db *gorm.DB
func Init() {
var err error
db, err = gorm.Open(sqlite.Open("glcpro.db"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
//Migrate the schema
db.AutoMigrate(&Client{})
}
func Exists(id string, secret string) bool {
var client Client
if err := db.Where("id_client = ? AND secret = ?", id, secret).First(&client).Error; err != nil {
return false
}
return true
}
func Create(id string, secret string) {
db.Create(&Client{Id_client: id, Secret: secret})
}
package client
import (
"testing"
)
func TestExists(t *testing.T) {
Init()
db.Unscoped().Where("id_client = ?", "ID_CLIENT").Delete(Client{})
Create("ID_CLIENT", "SECRET")
type args struct {
id_client string
secret string
}
tests := []struct {
name string
args args
want bool
}{
{"exists", args{id_client: "ID_CLIENT", secret: "SECRET"}, true},
{"not_exists_id_client", args{id_client: "NOTID_CLIENT", secret: "SECRET"}, false},
{"false_secret", args{id_client: "ID_CLIENT", secret: "WRONGSECRET"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := Exists(tt.args.id_client, tt.args.secret); got != tt.want {
t.Errorf("Exists() = %v, want %v", got, tt.want)
}
})
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment