You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
3.5 KiB
Go
173 lines
3.5 KiB
Go
package shrty
|
|
|
|
import (
|
|
"github.com/stretchr/testify/assert"
|
|
"testing"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
const path = "short_test.db"
|
|
|
|
// This test seems to be not working correctly
|
|
func TestStorage_Short(t *testing.T) {
|
|
s, err := Open(path)
|
|
defer os.Remove(path)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
var token string
|
|
|
|
err = s.Short("http://bit.ly/2scBYES", &token)
|
|
|
|
expected := &Data{
|
|
URL:"http://bit.ly/2scBYES",
|
|
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
CanonicalURL:"",
|
|
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
Domain:"www.youtube.com",
|
|
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
|
Token:"c",
|
|
Meta: Meta{
|
|
Title:"",
|
|
Image:"",
|
|
Description:"",
|
|
Type:""},
|
|
FavIconLink:"",
|
|
HTTPStatusCode: 200,
|
|
Category:"",
|
|
Created:s.Store.Token[token].Created,
|
|
Clicks:0,
|
|
}
|
|
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, s.Store.Token[token])
|
|
|
|
// If already exist the same Token will be return
|
|
err = s.Short("http://bit.ly/2scBYES", &token)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, s.Store.Token[token])
|
|
}
|
|
|
|
// Whats wrong with this Test?
|
|
func TestStorage_Get(t *testing.T) {
|
|
s, err := Open(path)
|
|
defer os.Remove(path)
|
|
|
|
var d Data
|
|
empty := Data{}
|
|
|
|
ok := s.Get("notexist", &d)
|
|
|
|
assert.Equal(t, empty, d)
|
|
assert.Equal(t, false, ok)
|
|
|
|
var token string
|
|
err = s.Short("http://bit.ly/2scBYES", &token)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "c", token)
|
|
|
|
expected := &Data{
|
|
URL:"http://bit.ly/2scBYES",
|
|
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
CanonicalURL:"",
|
|
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
Domain:"www.youtube.com",
|
|
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
|
Token:"c",
|
|
Meta: Meta{
|
|
Title:"",
|
|
Image:"",
|
|
Description:"",
|
|
Type:""},
|
|
FavIconLink:"",
|
|
HTTPStatusCode: 200,
|
|
Category:"",
|
|
Created:s.Store.Token[token].Created,
|
|
Clicks:0,
|
|
}
|
|
|
|
ok = s.Get(token, &d)
|
|
|
|
assert.Equal(t, expected, &d)
|
|
}
|
|
|
|
func TestStore_Exist(t *testing.T) {
|
|
s, err := Open(path)
|
|
defer os.Remove(path)
|
|
|
|
d := Data{
|
|
URL:"http://bit.ly/2scBYES",
|
|
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
CanonicalURL:"",
|
|
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
Domain:"www.youtube.com",
|
|
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
|
Token:"c",
|
|
Meta: Meta{
|
|
Title:"",
|
|
Image:"",
|
|
Description:"",
|
|
Type:""},
|
|
FavIconLink:"",
|
|
HTTPStatusCode: 200,
|
|
Category:"",
|
|
Created:time.Now().String(),
|
|
Clicks:0,
|
|
}
|
|
|
|
notexist := s.Exist(&d)
|
|
assert.Equal(t, false, notexist)
|
|
|
|
var token string
|
|
|
|
err = s.Short(d.URL, &token)
|
|
assert.NoError(t, err)
|
|
|
|
exist := s.Exist(&d)
|
|
|
|
assert.Equal(t, true, exist)
|
|
}
|
|
|
|
func TestStore_Redirect(t *testing.T) {
|
|
s, err := Open(path)
|
|
defer os.Remove(path)
|
|
|
|
url, ok := s.Redirect("notexist")
|
|
|
|
assert.Equal(t, "", url)
|
|
assert.Equal(t, false, ok)
|
|
|
|
var token string
|
|
err = s.Short("http://bit.ly/2scBYES", &token)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
expected := &Data{
|
|
URL:"http://bit.ly/2scBYES",
|
|
URLFetched:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
CanonicalURL:"",
|
|
OriginalURL:"https://www.youtube.com/watch?v=bouIpFd9VGM",
|
|
Domain:"www.youtube.com",
|
|
Hash:"c921ca733b92ca7b57782e9f12a3fe60dbc6b91a317fc419e13f8f5b5805232f",
|
|
Token:"c",
|
|
Meta: Meta{
|
|
Title:"",
|
|
Image:"",
|
|
Description:"",
|
|
Type:""},
|
|
FavIconLink:"",
|
|
HTTPStatusCode: 200,
|
|
Category:"",
|
|
Created:time.Now().String(),
|
|
Clicks:0,
|
|
}
|
|
|
|
url, ok = s.Redirect(token)
|
|
|
|
assert.Equal(t, expected.OriginalURL, url)
|
|
}
|
|
|
|
//func TestStore_GetAll(t *testing.T) {
|
|
|
|
//}
|