GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — test: add comprehensive test suite for buildURL function · schiemon/m2@b0516c2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • scrape_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎scrape_test.go‎

Copy file name to clipboard
+170Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package main
2+
3+
import (
4+
"net/url"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestBuildURL(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
season string
13+
year int
14+
day int
15+
group string
16+
wantURL string
17+
validate func(t *testing.T, got string)
18+
}{
19+
{
20+
name: "Spring 2024 Day 1 Group A",
21+
season: "F",
22+
year: 2024,
23+
day: 1,
24+
group: "A",
25+
wantURL: "https://www.medi-learn.de/statistik/stex/fragenstatistiken/index.php?ex=S-F24-Tag%201&gruppe=Gruppe%20A",
26+
},
27+
{
28+
name: "Autumn 2024 Day 2 Group B",
29+
season: "H",
30+
year: 2024,
31+
day: 2,
32+
group: "B",
33+
wantURL: "https://www.medi-learn.de/statistik/stex/fragenstatistiken/index.php?ex=S-H24-Tag%202&gruppe=Gruppe%20B",
34+
},
35+
{
36+
name: "Spring 2016 Day 1 Group A",
37+
season: "F",
38+
year: 2016,
39+
day: 1,
40+
group: "A",
41+
wantURL: "https://www.medi-learn.de/statistik/stex/fragenstatistiken/index.php?ex=S-F16-Tag%201&gruppe=Gruppe%20A",
42+
},
43+
{
44+
name: "Autumn 2023 Day 2 Group B",
45+
season: "H",
46+
year: 2023,
47+
day: 2,
48+
group: "B",
49+
wantURL: "https://www.medi-learn.de/statistik/stex/fragenstatistiken/index.php?ex=S-H23-Tag%202&gruppe=Gruppe%20B",
50+
},
51+
{
52+
name: "URL is properly encoded",
53+
season: "F",
54+
year: 2024,
55+
day: 1,
56+
group: "A",
57+
validate: func(t *testing.T, got string) {
58+
// Verify the URL is valid
59+
_, err := url.Parse(got)
60+
if err != nil {
61+
t.Errorf("buildURL returned invalid URL: %v", err)
62+
}
63+
64+
// Verify base URL is present
65+
if !strings.HasPrefix(got, baseURL) {
66+
t.Errorf("URL should start with baseURL: %s, got: %s", baseURL, got)
67+
}
68+
69+
// Verify query parameters are present
70+
if !strings.Contains(got, "ex=S-F24-Tag%201") {
71+
t.Errorf("URL should contain ex parameter with season and year")
72+
}
73+
if !strings.Contains(got, "gruppe=Gruppe%20A") {
74+
t.Errorf("URL should contain gruppe parameter with group")
75+
}
76+
},
77+
},
78+
{
79+
name: "Year modulo 100 for 2021",
80+
season: "F",
81+
year: 2021,
82+
day: 1,
83+
group: "A",
84+
validate: func(t *testing.T, got string) {
85+
// Year 2021 should be encoded as 21
86+
if !strings.Contains(got, "S-F21") {
87+
t.Errorf("Year 2021 should be encoded as 21 in URL, got: %s", got)
88+
}
89+
},
90+
},
91+
{
92+
name: "Day 1 is URL encoded properly",
93+
season: "F",
94+
year: 2024,
95+
day: 1,
96+
group: "A",
97+
validate: func(t *testing.T, got string) {
98+
if !strings.Contains(got, "Tag%201") {
99+
t.Errorf("Day 1 should be URL encoded as Tag%%201, got: %s", got)
100+
}
101+
},
102+
},
103+
{
104+
name: "Day 2 is URL encoded properly",
105+
season: "F",
106+
year: 2024,
107+
day: 2,
108+
group: "A",
109+
validate: func(t *testing.T, got string) {
110+
if !strings.Contains(got, "Tag%202") {
111+
t.Errorf("Day 2 should be URL encoded as Tag%%202, got: %s", got)
112+
}
113+
},
114+
},
115+
}
116+
117+
for _, tt := range tests {
118+
t.Run(tt.name, func(t *testing.T) {
119+
got := buildURL(tt.season, tt.year, tt.day, tt.group)
120+
121+
if tt.wantURL != "" {
122+
if got != tt.wantURL {
123+
t.Errorf("buildURL() = %s, want %s", got, tt.wantURL)
124+
}
125+
}
126+
127+
if tt.validate != nil {
128+
tt.validate(t, got)
129+
}
130+
})
131+
}
132+
}
133+
134+
func TestBuildURLParameterOrder(t *testing.T) {
135+
// Test that parameters appear in the correct order
136+
url := buildURL("F", 2024, 1, "A")
137+
138+
exIndex := strings.Index(url, "ex=")
139+
gruppeIndex := strings.Index(url, "gruppe=")
140+
141+
if exIndex == -1 || gruppeIndex == -1 {
142+
t.Fatal("URL missing required parameters")
143+
}
144+
145+
if exIndex > gruppeIndex {
146+
t.Errorf("Parameters in wrong order: ex parameter should come before gruppe parameter")
147+
}
148+
}
149+
150+
func TestBuildURLConsistency(t *testing.T) {
151+
// Test that calling buildURL multiple times with same parameters produces same result
152+
season := "F"
153+
year := 2024
154+
day := 1
155+
group := "A"
156+
157+
url1 := buildURL(season, year, day, group)
158+
url2 := buildURL(season, year, day, group)
159+
url3 := buildURL(season, year, day, group)
160+
161+
if url1 != url2 || url2 != url3 {
162+
t.Errorf("buildURL should be consistent: %s, %s, %s", url1, url2, url3)
163+
}
164+
}
165+
166+
func BenchmarkBuildURL(b *testing.B) {
167+
for i := 0; i < b.N; i++ {
168+
buildURL("F", 2024, 1, "A")
169+
}
170+
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.