package nkode

import (
	"github.com/stretchr/testify/assert"
	m "go-nkode/core/model"
	"os"
	"testing"
)

func TestNewSqliteDB(t *testing.T) {
	dbFile := "test.db"
	db, err := NewSqliteDB(dbFile)
	assert.NoError(t, err)
	nkode_policy := m.NewDefaultNKodePolicy()
	customerOrig, err := m.NewCustomer(nkode_policy)
	assert.NoError(t, err)
	err = db.WriteNewCustomer(*customerOrig)
	assert.NoError(t, err)
	customer, err := db.GetCustomer(customerOrig.Id)
	assert.NoError(t, err)
	assert.Equal(t, customerOrig, customer)
	username := m.Username("test_user")
	kp := m.KeypadDefault
	passcodeIdx := []int{0, 1, 2, 3}
	ui, err := m.NewUserInterface(&kp)
	assert.NoError(t, err)
	userOrig, err := NewUser(*customer, username, passcodeIdx, *ui, kp)
	assert.NoError(t, err)
	err = db.WriteNewUser(*userOrig)
	assert.NoError(t, err)
	user, err := db.GetUser(username, customer.Id)
	assert.NoError(t, err)
	assert.Equal(t, userOrig, user)

	err = db.Renew(customer.Id)
	assert.NoError(t, err)

	if _, err := os.Stat(dbFile); err == nil {
		err = os.Remove(dbFile)
		assert.NoError(t, err)
	} else {
		assert.NoError(t, err)
	}
}
