mirror of
https://github.com/Forceu/Gokapi.git
synced 2026-05-06 22:39:18 -05:00
Added tests, fixed typo
This commit is contained in:
@@ -3,6 +3,7 @@ module github.com/forceu/gokapi
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||
github.com/NYTimes/gziphandler v1.1.1
|
||||
github.com/aws/aws-sdk-go v1.53.19
|
||||
github.com/caarlos0/env/v6 v6.10.1
|
||||
|
||||
@@ -3,6 +3,7 @@ module github.com/forceu/gokapi
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||
github.com/NYTimes/gziphandler v1.1.1
|
||||
github.com/aws/aws-sdk-go v1.53.19
|
||||
github.com/caarlos0/env/v6 v6.10.1
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=
|
||||
github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c=
|
||||
github.com/aws/aws-sdk-go v1.44.256/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
|
||||
@@ -32,6 +34,7 @@ github.com/johannesboyne/gofakes3 v0.0.0-20240513200200-99de01ee122d h1:9dIJ/sx3
|
||||
github.com/johannesboyne/gofakes3 v0.0.0-20240513200200-99de01ee122d/go.mod h1:AxgWC4DDX54O2WDoQO1Ceabtn6IbktjU/7bigor+66g=
|
||||
github.com/juju/ratelimit v1.0.2 h1:sRxmtRiajbvrcLQT7S+JbqU0ntsb9W2yhSdNN8tWfaI=
|
||||
github.com/juju/ratelimit v1.0.2/go.mod h1:qapgC/Gy+xNh9UxzV13HGGl/6UXNN+ct+vwSgWNm/qk=
|
||||
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
|
||||
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
|
||||
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
|
||||
@@ -1,12 +1,18 @@
|
||||
//go:build test
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/forceu/gokapi/internal/helper"
|
||||
"github.com/forceu/gokapi/internal/models"
|
||||
"github.com/forceu/gokapi/internal/test"
|
||||
"golang.org/x/exp/slices"
|
||||
"math"
|
||||
"os"
|
||||
"regexp"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -22,16 +28,25 @@ func TestMain(m *testing.M) {
|
||||
}
|
||||
|
||||
func TestInit(t *testing.T) {
|
||||
Init("./test", "gokapi.sqlite")
|
||||
test.IsEqualBool(t, sqliteDb == nil, true)
|
||||
Init("./test/newfolder", "gokapi.sqlite")
|
||||
test.IsEqualBool(t, sqliteDb != nil, true)
|
||||
// Test that second init doesn't raise an error
|
||||
Init("./test", "gokapi.sqlite")
|
||||
test.FolderExists(t, "./test/newfolder")
|
||||
Close()
|
||||
test.IsEqualBool(t, sqliteDb == nil, true)
|
||||
err := os.WriteFile("./test/newfolder/gokapi2.sqlite", []byte("invalid"), 0700)
|
||||
test.IsNil(t, err)
|
||||
Init("./test/newfolder", "gokapi2.sqlite")
|
||||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
test.IsEqualBool(t, sqliteDb != nil, true)
|
||||
Close()
|
||||
test.IsEqualBool(t, sqliteDb == nil, true)
|
||||
mock := setMockDb(t)
|
||||
mock.ExpectClose().WillReturnError(errors.New("test"))
|
||||
Close()
|
||||
restoreDb()
|
||||
Init("./test", "gokapi.sqlite")
|
||||
}
|
||||
|
||||
@@ -206,6 +221,21 @@ func TestColumnExists(t *testing.T) {
|
||||
exists, err = ColumnExists("FileMetaData", "ExpireAt")
|
||||
test.IsEqualBool(t, exists, true)
|
||||
test.IsNil(t, err)
|
||||
setMockDb(t).ExpectQuery(regexp.QuoteMeta("PRAGMA table_info(error)")).WillReturnError(errors.New("error"))
|
||||
exists, err = ColumnExists("error", "error")
|
||||
test.IsEqualBool(t, exists, false)
|
||||
test.IsNotNil(t, err)
|
||||
restoreDb()
|
||||
mock := setMockDb(t)
|
||||
|
||||
rows := mock.NewRows([]string{"invalid"}).
|
||||
AddRow(0).
|
||||
AddRow(1)
|
||||
mock.ExpectQuery(regexp.QuoteMeta("PRAGMA table_info(error)")).WillReturnRows(rows)
|
||||
exists, err = ColumnExists("error", "error")
|
||||
test.IsEqualBool(t, exists, false)
|
||||
test.IsNotNil(t, err)
|
||||
restoreDb()
|
||||
}
|
||||
|
||||
func TestGarbageCollectionUploads(t *testing.T) {
|
||||
@@ -479,3 +509,16 @@ func TestUploadStatus(t *testing.T) {
|
||||
allStatus = GetAllUploadStatus()
|
||||
test.IsEqualInt(t, len(allStatus), 6)
|
||||
}
|
||||
|
||||
var originalDb *sql.DB
|
||||
|
||||
func setMockDb(t *testing.T) sqlmock.Sqlmock {
|
||||
originalDb = sqliteDb
|
||||
db, mock, err := sqlmock.New()
|
||||
test.IsNil(t, err)
|
||||
sqliteDb = db
|
||||
return mock
|
||||
}
|
||||
func restoreDb() {
|
||||
sqliteDb = originalDb
|
||||
}
|
||||
|
||||
@@ -133,6 +133,20 @@ func FileExists(t MockT, name string) {
|
||||
}
|
||||
}
|
||||
|
||||
// FolderExists fails test a folder does not exist
|
||||
func FolderExists(t MockT, name string) {
|
||||
t.Helper()
|
||||
_, err := os.Stat(name)
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
if !os.IsNotExist(err) {
|
||||
t.Errorf("Assertion failed, folder does not exist: %s, want: Exists.", name)
|
||||
} else {
|
||||
t.Errorf("Assertion failed, could not check if folder exist: %s.", name)
|
||||
}
|
||||
}
|
||||
|
||||
// FileDoesNotExist fails test a file exists
|
||||
func FileDoesNotExist(t MockT, name string) {
|
||||
t.Helper()
|
||||
|
||||
@@ -47,7 +47,7 @@ func Create(initFiles bool) {
|
||||
})
|
||||
writeTestFiles()
|
||||
database.SaveHotlink(models.File{Id: "n1tSTAGj8zan9KaT4u6p", HotlinkId: "PhSs6mFtf8O5YGlLMfNw9rYXx9XRNkzCnJZpQBi7inunv3Z4A.jpg", ExpireAt: time.Now().Add(time.Hour).Unix()})
|
||||
writeApiKeyys()
|
||||
writeApiKeys()
|
||||
writeTestUploadStatus()
|
||||
database.Close()
|
||||
|
||||
@@ -188,10 +188,9 @@ func writeTestUploadStatus() {
|
||||
ChunkId: "validstatus_1",
|
||||
CurrentStatus: 1,
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func writeApiKeyys() {
|
||||
func writeApiKeys() {
|
||||
database.SaveApiKey(models.ApiKey{
|
||||
Id: "validkey",
|
||||
FriendlyName: "First Key",
|
||||
|
||||
Reference in New Issue
Block a user