2019-07-15 16:46:24 +00:00
|
|
|
package webshit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jmoiron/sqlx"
|
2019-07-16 02:00:19 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
2019-07-15 17:39:40 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-07-16 02:00:19 +00:00
|
|
|
"os"
|
2019-07-15 16:46:24 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2019-07-16 02:00:19 +00:00
|
|
|
func init() {
|
|
|
|
log.Logger = log.Logger.Output(zerolog.ConsoleWriter{Out: os.Stderr})
|
|
|
|
}
|
|
|
|
|
2019-07-16 19:49:35 +00:00
|
|
|
func makeWS(t *testing.T) *Webshit {
|
2019-07-15 16:46:24 +00:00
|
|
|
db := sqlx.MustOpen("sqlite3", "file::memory:?mode=memory&cache=shared")
|
|
|
|
w := New(db)
|
2019-07-15 17:39:40 +00:00
|
|
|
assert.Equal(t, w.db, db)
|
2019-07-15 16:46:24 +00:00
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebshit_GetWeekly(t *testing.T) {
|
2019-07-16 19:49:35 +00:00
|
|
|
w := makeWS(t)
|
2019-07-15 20:55:35 +00:00
|
|
|
weekly, pub, err := w.GetWeekly()
|
|
|
|
t.Logf("Pub: %v", pub)
|
|
|
|
assert.NotNil(t, pub)
|
2019-07-15 17:39:40 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotEmpty(t, weekly)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebshit_GetHeadlines(t *testing.T) {
|
2019-07-16 19:49:35 +00:00
|
|
|
w := makeWS(t)
|
2019-07-15 17:39:40 +00:00
|
|
|
headlines, err := w.GetHeadlines()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotEmpty(t, headlines)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebshit_getStoryByURL(t *testing.T) {
|
2019-07-16 19:49:35 +00:00
|
|
|
w := makeWS(t)
|
2019-07-15 17:39:40 +00:00
|
|
|
expected := "Developer Tropes: “Google Does It”"
|
|
|
|
s, err := w.getStoryByURL("https://news.ycombinator.com/item?id=20432887")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, s.Title, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebshit_getStoryByURL_BadURL(t *testing.T) {
|
2019-07-16 19:49:35 +00:00
|
|
|
w := makeWS(t)
|
2019-07-15 17:39:40 +00:00
|
|
|
_, err := w.getStoryByURL("https://google.com")
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebshit_GetBalance(t *testing.T) {
|
2019-07-16 19:49:35 +00:00
|
|
|
w := makeWS(t)
|
2019-07-15 17:39:40 +00:00
|
|
|
expected := 100
|
|
|
|
actual := w.GetBalance("foo")
|
|
|
|
assert.Equal(t, expected, actual)
|
2019-07-15 16:46:24 +00:00
|
|
|
}
|
2019-07-16 02:00:19 +00:00
|
|
|
|
|
|
|
func TestWebshit_checkBids(t *testing.T) {
|
2019-07-16 19:49:35 +00:00
|
|
|
w := makeWS(t)
|
2019-07-16 02:00:19 +00:00
|
|
|
bids := []Bid{
|
|
|
|
Bid{User: "foo", Title: "bar", URL: "baz", Bid: 10},
|
|
|
|
Bid{User: "foo", Title: "bar2", URL: "baz2", Bid: 10},
|
|
|
|
}
|
|
|
|
storyMap := map[string]Story{
|
2019-07-16 19:49:35 +00:00
|
|
|
"baz": Story{Title: "bar", URL: "baz"},
|
2019-07-16 02:00:19 +00:00
|
|
|
}
|
|
|
|
result := w.checkBids(bids, storyMap)
|
|
|
|
assert.Len(t, result, 1)
|
|
|
|
if len(result) > 0 {
|
|
|
|
assert.Len(t, result[0].WinningArticles, 1)
|
|
|
|
assert.Len(t, result[0].LosingArticles, 1)
|
|
|
|
}
|
|
|
|
}
|