104 lines
3.4 KiB
Go
104 lines
3.4 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestChartGenerationSortsByCurrentVotingStand(t *testing.T) {
|
|
entries := []ChartEntry{
|
|
{ID: 1, Position: 1, Artist: "Alt", Title: "Basis hoch", FixedVotePoints: 100, VotePoints: 105, VoteCount: 1},
|
|
{ID: 2, Position: 2, Artist: "Neu", Title: "Online stark", FixedVotePoints: 20, VotePoints: 130, VoteCount: 24},
|
|
{ID: 3, Position: 3, Artist: "Gleich", Title: "Gleicher Stand", FixedVotePoints: 70, VotePoints: 105, VoteCount: 9},
|
|
{ID: 4, Position: 4, Artist: "", Title: "", VotePoints: 999},
|
|
}
|
|
sortChartEntriesByVotingStand(entries)
|
|
got := []int{entries[0].ID, entries[1].ID, entries[2].ID, entries[3].ID}
|
|
want := []int{2, 1, 3, 4}
|
|
for i := range want {
|
|
if got[i] != want[i] {
|
|
t.Fatalf("unexpected generated order: got %v want %v", got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFrontendVotesAreRecordedCumulatively(t *testing.T) {
|
|
sql := chartVoteInsertSQL("listener", 42, 5, "visitor")
|
|
for _, want := range []string{
|
|
"INSERT INTO chart_vote_history(entry_id,rating,created_at)",
|
|
"ON CONFLICT(entry_id,voter_key) DO UPDATE",
|
|
"chart_votes.created_at <= now() - interval '24 hours'",
|
|
"EXISTS(SELECT 1 FROM accepted)",
|
|
} {
|
|
if !strings.Contains(sql, want) {
|
|
t.Fatalf("vote SQL missing %q", want)
|
|
}
|
|
}
|
|
if strings.Contains(sql, "DELETE FROM chart_vote_history") {
|
|
t.Fatal("accepted votes must not overwrite or delete vote history")
|
|
}
|
|
if strings.Contains(sql, "chart_vote_history(entry_id,rating,voter_key") {
|
|
t.Fatal("vote history must not duplicate the raw visitor key")
|
|
}
|
|
}
|
|
|
|
func TestChartTotalsUseVoteHistory(t *testing.T) {
|
|
raw, err := os.ReadFile("charts.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := string(raw)
|
|
for _, want := range []string{
|
|
"LEFT JOIN chart_vote_history v ON v.entry_id=e.id",
|
|
"COALESCE(e.fixed_vote_points,0)+COALESCE(sum(v.rating),0)",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("chart total query missing %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestChartVoteHistoryMigration(t *testing.T) {
|
|
raw, err := os.ReadFile(filepath.Join("migrations", "0036_chart_vote_history.sql"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := string(raw)
|
|
for _, want := range []string{
|
|
"CREATE TABLE IF NOT EXISTS chart_vote_history",
|
|
"REFERENCES chart_entries(id) ON DELETE CASCADE",
|
|
"INSERT INTO chart_vote_history",
|
|
"WHERE h.entry_id=v.entry_id AND h.rating=v.rating AND h.created_at=v.created_at",
|
|
} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("vote history migration missing %q", want)
|
|
}
|
|
}
|
|
if strings.Contains(strings.ToUpper(text), "BEGIN;") || strings.Contains(strings.ToUpper(text), "COMMIT;") {
|
|
t.Fatal("migration must leave transaction handling to the migrator")
|
|
}
|
|
}
|
|
|
|
func TestFixedChartEvaluationIsServerManaged(t *testing.T) {
|
|
templateRaw, err := os.ReadFile(filepath.Join("web", "admin", "management.html"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(templateRaw), `name="fixed_vote_points"`) {
|
|
t.Fatal("fixed vote points must not be submitted by the admin form")
|
|
}
|
|
if !strings.Contains(string(templateRaw), `<span>Voting-Stand</span>`) {
|
|
t.Fatal("admin chart table must label the automatic result as Voting-Stand")
|
|
}
|
|
serverRaw, err := os.ReadFile("admin_management.go")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := string(serverRaw)
|
|
if strings.Contains(server, `formInt(r, "fixed_vote_points")`) {
|
|
t.Fatal("server still accepts editable fixed vote points")
|
|
}
|
|
}
|