cpod

Yet another cron-friendly podcatcher

git clone https://git.8pit.net/cpod.git

  1// Copyright (C) 2013-2015 Sören Tempel
  2//
  3// This program is free software: you can redistribute it and/or modify
  4// it under the terms of the GNU General Public License as published by
  5// the Free Software Foundation, either version 3 of the License, or
  6// (at your option) any later version.
  7//
  8// This program is distributed in the hope that it will be useful,
  9// but WITHOUT ANY WARRANTY; without even the implied warranty of
 10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 11// GNU General Public License for more details.
 12//
 13// You should have received a copy of the GNU General Public License
 14// along with this program. If not, see <http://www.gnu.org/licenses/>.
 15
 16package store
 17
 18import (
 19	"io/ioutil"
 20	"os"
 21	"path/filepath"
 22	"testing"
 23)
 24
 25func TestLoad(t *testing.T) {
 26	store, err := Load("testdata/testLoad.txt")
 27	if err != nil {
 28		t.Fatal(err)
 29	}
 30
 31	urls := []string{
 32		"http://feed.thisamericanlife.org/talpodcast",
 33		"http://www.npr.org/rss/podcast.php?id=510294",
 34	}
 35
 36	for _, url := range urls {
 37		if !store.Contains(url) {
 38			t.Fail()
 39		}
 40	}
 41}
 42
 43func TestAdd(t *testing.T) {
 44	url := "http://example.com"
 45	store := new(Store)
 46
 47	store.Add(url)
 48	if !store.Contains(url) {
 49		t.Fail()
 50	}
 51}
 52
 53func TestContains(t *testing.T) {
 54	url := "http://foo.com"
 55	store := &Store{"", []string{url}}
 56
 57	if !store.Contains(url) {
 58		t.Fail()
 59	}
 60
 61	if store.Contains("http://foo.bar") {
 62		t.Fail()
 63	}
 64}
 65
 66func TestFetch(t *testing.T) {
 67	url := "http://feed.thisamericanlife.org/talpodcast"
 68	store := &Store{"", []string{url}}
 69
 70	channel := store.Fetch()
 71	podcast := <-channel
 72
 73	if podcast.Error != nil {
 74		t.Fatal(podcast.Error)
 75	}
 76
 77	feed := podcast.Feed
 78	if url != podcast.URL {
 79		t.Fatalf("Expected %q - got %q", podcast.URL, url)
 80	}
 81
 82	expected := "This American Life"
 83	if feed.Title != expected {
 84		t.Fatalf("Expected %q - got %q", expected, feed.Title)
 85	}
 86}
 87
 88func TestSave(t *testing.T) {
 89	url := "http://example.io"
 90	fp := filepath.Join(os.TempDir(), "testSave")
 91
 92	store := &Store{fp, []string{url}}
 93	if err := store.Save(); err != nil {
 94		t.Fatal(err)
 95	}
 96
 97	defer os.Remove(fp)
 98	data, err := ioutil.ReadFile(fp)
 99	if err != nil {
100		t.Fatal(err)
101	}
102
103	expected := url + "\n"
104	if string(data) != expected {
105		t.Fatalf("Expected %q - got %q", string(data), expected)
106	}
107}