package postgres

import (
	"testing"
	"time"

	"1bet.fr/scraper/news"
	"1bet.fr/scraper/utils"
)

var n *news.News

func TestConnect(t *testing.T) {
	return
}

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")
	}
}

func TestListSources(t *testing.T) {
	sources, err := ListSources()
	if err != nil {
		t.Errorf("unexpected error : %s", err)
	}
	if len(sources) == 0 {
		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()
	n = &news.News{
		Source: &news.Source{Id: 1, Sport: &news.Sport{Id: 1}},
		PubDate: &nowTime,
		Link: "https://test.com/toto",
		Title: "Hello toto",
		CleanTitle: "hello-toto",
		Tags: &tags,
		CleanTags: &cleanTags,
	}
	err := InsertNews(n)
	if err != nil {
		t.Error(err)
	}
	if n.Id == 0 {
		t.Errorf("unexpected value 0 for n.Id")
	}
}

func TestUpdateNews(t *testing.T) {
	content := []string{"toto", "test"}
	n.Content = &content
	n.Author = utils.StringPointer("T. Toto")
	updated, err := UpdateNews(n)
	if err != nil {
		t.Error(err)
	}
	if updated != 1 {
		t.Errorf("unexpected %d update rows", updated)
	}
}

func TestDeleteNews(t *testing.T) {
	deleted, err := DeleteNews(n)
	if err != nil {
		t.Error(err)
	}
	if deleted != 1 {
		t.Errorf("unexpected %d news deleted", deleted)
	}
}

func TestClose(t *testing.T) {
	Close()
}
