Skip to content

Commit 61b0fe3

Browse files
authored
Adding additional tests for mid-flight deletions and additions
1 parent 02145cc commit 61b0fe3

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

internal/codespaces/api/api_test.go

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@ func generateCodespaceList(start int, end int) []*Codespace {
2020
return codespacesList
2121
}
2222

23-
func TestListCodespaces(t *testing.T) {
24-
25-
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
23+
func createFakeListEndpointServer(t *testing.T, initalTotal int, finalTotal int) *httptest.Server {
24+
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2625
if r.URL.Path != "/user/codespaces" {
2726
t.Fatal("Incorrect path")
2827
}
@@ -37,23 +36,29 @@ func TestListCodespaces(t *testing.T) {
3736
per_page, _ = strconv.Atoi(r.URL.Query().Get("per_page"))
3837
}
3938

40-
codespaces := []*Codespace{}
41-
if page == 1 {
42-
codespaces = generateCodespaceList(0, per_page)
43-
} else if page == 2 {
44-
codespaces = generateCodespaceList(per_page, per_page*2)
45-
}
46-
4739
response := struct {
4840
Codespaces []*Codespace `json:"codespaces"`
4941
TotalCount int `json:"total_count"`
5042
}{
51-
Codespaces: codespaces,
52-
TotalCount: 100,
43+
Codespaces: []*Codespace{},
44+
TotalCount: finalTotal,
5345
}
46+
47+
if page == 1 {
48+
response.Codespaces = generateCodespaceList(0, per_page)
49+
response.TotalCount = initalTotal
50+
} else if page == 2 {
51+
response.Codespaces = generateCodespaceList(per_page, per_page*2)
52+
response.TotalCount = finalTotal
53+
}
54+
5455
data, _ := json.Marshal(response)
5556
fmt.Fprint(w, string(data))
5657
}))
58+
}
59+
60+
func TestListCodespaces(t *testing.T) {
61+
svr := createFakeListEndpointServer(t, 100, 100)
5762
defer svr.Close()
5863

5964
api := API{
@@ -77,5 +82,42 @@ func TestListCodespaces(t *testing.T) {
7782
if codespaces[99].Name != "codespace-99" {
7883
t.Fatalf("expected codespace-99, got %s", codespaces[0].Name)
7984
}
85+
}
8086

87+
func TestMidIterationDeletion(t *testing.T) {
88+
svr := createFakeListEndpointServer(t, 100, 99)
89+
defer svr.Close()
90+
91+
api := API{
92+
githubAPI: svr.URL,
93+
client: &http.Client{},
94+
token: "faketoken",
95+
}
96+
ctx := context.TODO()
97+
codespaces, err := api.ListCodespaces(ctx)
98+
if err != nil {
99+
t.Fatal(err)
100+
}
101+
if len(codespaces) != 100 {
102+
t.Fatalf("expected 100 codespace, got %d", len(codespaces))
103+
}
104+
}
105+
106+
func TestMidIterationAddition(t *testing.T) {
107+
svr := createFakeListEndpointServer(t, 99, 100)
108+
defer svr.Close()
109+
110+
api := API{
111+
githubAPI: svr.URL,
112+
client: &http.Client{},
113+
token: "faketoken",
114+
}
115+
ctx := context.TODO()
116+
codespaces, err := api.ListCodespaces(ctx)
117+
if err != nil {
118+
t.Fatal(err)
119+
}
120+
if len(codespaces) != 100 {
121+
t.Fatalf("expected 100 codespace, got %d", len(codespaces))
122+
}
81123
}

0 commit comments

Comments
 (0)