2020-10-05 08:24:33 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
2020-10-19 09:26:23 +00:00
|
|
|
"1bet.fr/scraper/match"
|
2020-10-05 08:24:33 +00:00
|
|
|
"testing"
|
2020-10-10 17:08:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"1bet.fr/scraper/news"
|
|
|
|
"1bet.fr/scraper/utils"
|
2020-10-05 08:24:33 +00:00
|
|
|
)
|
|
|
|
|
2020-10-19 09:26:23 +00:00
|
|
|
var (
|
|
|
|
se *news.Source
|
|
|
|
ns *news.News
|
|
|
|
tm *match.Team
|
|
|
|
mh *match.Match
|
|
|
|
)
|
2020-10-10 17:08:06 +00:00
|
|
|
|
2020-10-05 08:24:33 +00:00
|
|
|
func TestConnect(t *testing.T) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-10 17:08:06 +00:00
|
|
|
func TestListLeagues(t *testing.T) {
|
|
|
|
leagues, err := ListLeagues()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error : %s", err)
|
|
|
|
}
|
|
|
|
if len(leagues) == 0 {
|
|
|
|
t.Errorf("no league got from ListLeagues function")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 09:26:23 +00:00
|
|
|
func TestUpdateLeague(t *testing.T) {
|
|
|
|
league := &match.League{Id: 1}
|
|
|
|
updated, err := UpdateLeague(league)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if updated != 1 {
|
|
|
|
t.Errorf("unexpected %d updated rows", updated)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 08:24:33 +00:00
|
|
|
func TestListSources(t *testing.T) {
|
|
|
|
sources, err := ListSources()
|
2020-10-19 09:26:23 +00:00
|
|
|
se = sources[0]
|
2020-10-05 08:24:33 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error : %s", err)
|
|
|
|
}
|
|
|
|
if len(sources) == 0 {
|
2020-10-10 17:08:06 +00:00
|
|
|
t.Errorf("no source got from ListSources function")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertNews(t *testing.T) {
|
|
|
|
tags := []string{"Test", "Hello Toto"}
|
|
|
|
cleanTags := []string{"test", "hello-toto"}
|
|
|
|
nowTime := time.Now()
|
2020-10-19 09:26:23 +00:00
|
|
|
ns = &news.News{
|
|
|
|
Source: se,
|
2020-10-10 17:08:06 +00:00
|
|
|
PubDate: &nowTime,
|
|
|
|
Link: "https://test.com/toto",
|
|
|
|
Title: "Hello toto",
|
|
|
|
CleanTitle: "hello-toto",
|
|
|
|
Tags: &tags,
|
|
|
|
CleanTags: &cleanTags,
|
|
|
|
}
|
2020-10-19 09:26:23 +00:00
|
|
|
err := InsertNews(ns)
|
2020-10-10 17:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2020-10-19 09:26:23 +00:00
|
|
|
if ns.Id == 0 {
|
2020-10-10 17:08:06 +00:00
|
|
|
t.Errorf("unexpected value 0 for n.Id")
|
2020-10-05 08:24:33 +00:00
|
|
|
}
|
2020-10-10 17:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdateNews(t *testing.T) {
|
|
|
|
content := []string{"toto", "test"}
|
2020-10-19 09:26:23 +00:00
|
|
|
ns.Content = &content
|
|
|
|
ns.Author = utils.StringPointer("T. Toto")
|
|
|
|
updated, err := UpdateNews(ns)
|
2020-10-10 17:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if updated != 1 {
|
2020-10-19 09:26:23 +00:00
|
|
|
t.Errorf("unexpected %d updated rows", updated)
|
2020-10-10 17:08:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNews(t *testing.T) {
|
2020-10-19 09:26:23 +00:00
|
|
|
deleted, err := DeleteNews(ns)
|
2020-10-10 17:08:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if deleted != 1 {
|
|
|
|
t.Errorf("unexpected %d news deleted", deleted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 09:26:23 +00:00
|
|
|
func TestInsertTeamBySourceName(t *testing.T) {
|
|
|
|
teamNames := map[string]string{utils.HostMatchendirect: "Toto"}
|
2020-12-02 16:58:49 +00:00
|
|
|
lg := &match.League{
|
2020-10-19 09:26:23 +00:00
|
|
|
Id: 1,
|
|
|
|
Sport: &match.Sport{Id: 1},
|
|
|
|
Country: &match.Country{Id: 1},
|
|
|
|
Gender: utils.IntPointer(match.GenderMale),
|
|
|
|
}
|
2020-12-02 16:58:49 +00:00
|
|
|
tm = match.NewTeam(utils.IntPointer(match.GenderMale), lg.Sport, lg.Country)
|
|
|
|
tm.Names = &teamNames
|
|
|
|
for range []int{0, 1} {
|
|
|
|
if err := InsertTeamBySourceName(tm); err != nil {
|
2020-10-19 09:26:23 +00:00
|
|
|
t.Errorf("unexpected error : %s", err)
|
|
|
|
}
|
|
|
|
if tm.Id == 0 {
|
|
|
|
t.Error("unexpected zero team.Id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInsertMatch(t *testing.T) {
|
|
|
|
startDate := time.Now()
|
|
|
|
mh = match.NewMatch(&match.League{Id: 1}, tm, tm, &match.Player{}, &match.Player{}, "", 0, 0)
|
|
|
|
mh.StartDate = &startDate
|
|
|
|
mh.BaseUrl = utils.StringPointer("https://test.com/toto")
|
2020-12-02 16:58:49 +00:00
|
|
|
for range []int{0, 1} {
|
2020-10-19 09:26:23 +00:00
|
|
|
if err := InsertMatch(mh); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if mh.Id == 0 {
|
|
|
|
t.Errorf("unexpected zero match.Id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteMatch(t *testing.T) {
|
|
|
|
deleted, err := DeleteMatch(mh)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if deleted != 1 {
|
|
|
|
t.Errorf("unexpected %d matches deleted", deleted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteTeam(t *testing.T) {
|
|
|
|
deleted, err := DeleteTeam(tm)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if deleted != 1 {
|
|
|
|
t.Errorf("unexpected %d teams deleted", deleted)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-10 17:08:06 +00:00
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
Close()
|
|
|
|
}
|