aboutsummaryrefslogtreecommitdiffstats
path: root/internal/exporter/exporter_test.go
blob: d4cc994aafb9b81ceaea2f6d952966620749e5b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package exporter

import (
	"encoding/json"
	"strings"
	"testing"

	"adammathes.com/neko/config"
	"adammathes.com/neko/models"
)

func setupTestDB(t *testing.T) {
	t.Helper()
	config.Config.DBFile = ":memory:"
	models.InitDB()
	t.Cleanup(func() {
		if models.DB != nil {
			models.DB.Close()
		}
	})
}

func seedFeeds(t *testing.T) {
	t.Helper()
	_, err := models.DB.Exec("INSERT INTO feed(url, web_url, title, category) VALUES(?, ?, ?, ?)",
		"https://a.com/feed", "https://a.com", "Alpha Feed", "tech")
	if err != nil {
		t.Fatal(err)
	}
	_, err = models.DB.Exec("INSERT INTO feed(url, web_url, title, category) VALUES(?, ?, ?, ?)",
		"https://b.com/feed", "https://b.com", "Beta Feed", "news")
	if err != nil {
		t.Fatal(err)
	}
}

func TestExportText(t *testing.T) {
	setupTestDB(t)
	seedFeeds(t)

	result := ExportFeeds("text")
	if !strings.Contains(result, "https://a.com/feed") {
		t.Error("text export should contain feed URL a")
	}
	if !strings.Contains(result, "https://b.com/feed") {
		t.Error("text export should contain feed URL b")
	}
}

func TestExportJSON(t *testing.T) {
	setupTestDB(t)
	seedFeeds(t)

	result := ExportFeeds("json")
	var feeds []interface{}
	err := json.Unmarshal([]byte(result), &feeds)
	if err != nil {
		t.Fatalf("JSON export should be valid JSON: %v", err)
	}
	if len(feeds) != 2 {
		t.Errorf("JSON export should contain 2 feeds, got %d", len(feeds))
	}
}

func TestExportOPML(t *testing.T) {
	setupTestDB(t)
	seedFeeds(t)

	result := ExportFeeds("opml")
	if !strings.Contains(result, "<opml") {
		t.Error("OPML export should contain opml tag")
	}
	if !strings.Contains(result, "Alpha Feed") || !strings.Contains(result, "Beta Feed") {
		t.Error("OPML export should contain feed titles")
	}
	if !strings.Contains(result, "</opml>") {
		t.Error("OPML export should close opml tag")
	}
}

func TestExportHTML(t *testing.T) {
	setupTestDB(t)
	seedFeeds(t)

	result := ExportFeeds("html")
	if !strings.Contains(result, "<html>") {
		t.Error("HTML export should contain html tag")
	}
	if !strings.Contains(result, "Alpha Feed") {
		t.Error("HTML export should contain feed title")
	}
}

func TestExportUnknownFormat(t *testing.T) {
	setupTestDB(t)
	seedFeeds(t)

	result := ExportFeeds("unknown")
	if result != "" {
		t.Errorf("Unknown format should return empty string, got %q", result)
	}
}

func TestExportEmpty(t *testing.T) {
	setupTestDB(t)

	result := ExportFeeds("text")
	if result != "" {
		t.Errorf("Export with no feeds should be empty, got %q", result)
	}
}