2021-11-13 22:33:21 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_shortenPath(t *testing.T) {
|
|
|
|
app := &goBlog{
|
|
|
|
cfg: &config{
|
|
|
|
Db: &configDb{
|
|
|
|
File: filepath.Join(t.TempDir(), "test.db"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_ = app.initDatabase(false)
|
2022-01-26 19:16:26 +00:00
|
|
|
defer app.db.close()
|
2021-11-13 22:33:21 +00:00
|
|
|
db := app.db
|
|
|
|
|
|
|
|
res1, err := db.shortenPath("/a")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-11 09:33:27 +00:00
|
|
|
db.spc.Del("/a")
|
2021-11-13 22:33:21 +00:00
|
|
|
|
|
|
|
res2, err := db.shortenPath("/a")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
res3, err := db.shortenPath("/b")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-11 09:33:27 +00:00
|
|
|
res4, err := db.shortenPath("/a")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-13 22:33:21 +00:00
|
|
|
assert.Equal(t, res1, res2)
|
|
|
|
assert.Equal(t, "/s/1", res1)
|
|
|
|
|
|
|
|
assert.NotEqual(t, res1, res3)
|
|
|
|
assert.Equal(t, "/s/2", res3)
|
2021-11-14 06:58:49 +00:00
|
|
|
|
2022-03-11 09:33:27 +00:00
|
|
|
assert.Equal(t, res2, res4)
|
|
|
|
assert.Equal(t, "/s/1", res4)
|
|
|
|
|
|
|
|
db.spc.Del("/a")
|
2021-12-07 13:13:09 +00:00
|
|
|
_, _ = db.exec("delete from shortpath where id = 1")
|
2021-11-14 06:58:49 +00:00
|
|
|
|
2022-03-11 09:33:27 +00:00
|
|
|
res5, err := db.shortenPath("/c")
|
2021-11-14 06:58:49 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-11 09:33:27 +00:00
|
|
|
assert.Equal(t, "/s/1", res5)
|
2021-11-13 22:33:21 +00:00
|
|
|
}
|