Skip to content

Commit 5fdf218

Browse files
updating tests
1 parent 8459805 commit 5fdf218

File tree

4 files changed

+71
-51
lines changed

4 files changed

+71
-51
lines changed

cmd/ability/ability_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
package ability
22

33
import (
4+
"os"
5+
"testing"
6+
47
"github.com/digitalghost-dev/poke-cli/cmd/utils"
58
"github.com/digitalghost-dev/poke-cli/styling"
69
"github.com/stretchr/testify/assert"
7-
"os"
8-
"testing"
910
)
1011

1112
func TestAbilityCommand(t *testing.T) {
12-
err := os.Setenv("GO_TESTING", "1")
13-
if err != nil {
14-
t.Fatalf("Failed to set GO_TESTING env var: %v", err)
15-
}
16-
17-
defer func() {
18-
err := os.Unsetenv("GO_TESTING")
19-
if err != nil {
20-
t.Logf("Warning: failed to unset GO_TESTING: %v", err)
21-
}
22-
}()
23-
2413
tests := []struct {
2514
name string
2615
args []string
@@ -53,6 +42,11 @@ func TestAbilityCommand(t *testing.T) {
5342
args: []string{"ability", "anger-point", "--pokemon"},
5443
expectedOutput: utils.LoadGolden(t, "ability_flag_pokemon.golden"),
5544
},
45+
{
46+
name: "Ability command: special character in API call",
47+
args: []string{"ability", "poison-point"},
48+
expectedOutput: utils.LoadGolden(t, "ability_poison_point.golden"),
49+
},
5650
}
5751

5852
for _, tt := range tests {

connections/connection_test.go

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ func TestAbilityApiCall(t *testing.T) {
114114
}))
115115
defer ts.Close()
116116

117-
ability, _, err := AbilityApiCall("/ability", "non-existent-ability", ts.URL)
117+
ability, name, err := AbilityApiCall("/ability", "non-existent-ability", ts.URL)
118118

119119
require.Error(t, err, "Expected an error for invalid ability")
120120
assert.Equal(t, structs.AbilityJSONStruct{}, ability, "Expected empty ability struct on error")
121+
assert.Equal(t, "", name, "Expected empty name string on error")
121122

122123
assert.Contains(t, err.Error(), "Ability not found", "Expected 'Ability not found' in error message")
123124
assert.Contains(t, err.Error(), "Perhaps a typo?", "Expected helpful suggestion in error message")
@@ -151,10 +152,11 @@ func TestItemApiCall(t *testing.T) {
151152
}))
152153
defer ts.Close()
153154

154-
item, _, err := ItemApiCall("/item", "non-existent-item", ts.URL)
155+
item, name, err := ItemApiCall("/item", "non-existent-item", ts.URL)
155156

156157
require.Error(t, err, "Expected an error for invalid item")
157158
assert.Equal(t, structs.ItemJSONStruct{}, item, "Expected empty item struct on error")
159+
assert.Equal(t, "", name, "Expected empty name string on error")
158160

159161
assert.Contains(t, err.Error(), "Item not found", "Expected 'Item not found' in error message")
160162
assert.Contains(t, err.Error(), "Perhaps a typo?", "Expected helpful suggestion in error message")
@@ -188,10 +190,11 @@ func TestMoveApiCall(t *testing.T) {
188190
}))
189191
defer ts.Close()
190192

191-
move, _, err := MoveApiCall("/move", "non-existent-move", ts.URL)
193+
move, name, err := MoveApiCall("/move", "non-existent-move", ts.URL)
192194

193195
require.Error(t, err, "Expected an error for invalid move")
194196
assert.Equal(t, structs.MoveJSONStruct{}, move, "Expected empty move struct on error")
197+
assert.Equal(t, "", name, "Expected empty name string on error")
195198

196199
assert.Contains(t, err.Error(), "Move not found", "Expected 'Move not found' in error message")
197200
assert.Contains(t, err.Error(), "Perhaps a typo?", "Expected helpful suggestion in error message")
@@ -225,10 +228,11 @@ func TestPokemonApiCall(t *testing.T) {
225228
}))
226229
defer ts.Close()
227230

228-
pokemon, _, err := PokemonApiCall("/pokemon", "non-existent-pokemon", ts.URL)
231+
pokemon, name, err := PokemonApiCall("/pokemon", "non-existent-pokemon", ts.URL)
229232

230233
require.Error(t, err, "Expected an error for invalid pokemon")
231234
assert.Equal(t, structs.PokemonJSONStruct{}, pokemon, "Expected empty pokemon struct on error")
235+
assert.Equal(t, "", name, "Expected empty name string on error")
232236

233237
assert.Contains(t, err.Error(), "Pokémon not found", "Expected 'Pokémon not found' in error message")
234238
assert.Contains(t, err.Error(), "Perhaps a typo?", "Expected helpful suggestion in error message")
@@ -237,40 +241,58 @@ func TestPokemonApiCall(t *testing.T) {
237241

238242
// TestTypesApiCall - Test for the TypesApiCall function
239243
func TestTypesApiCall(t *testing.T) {
240-
expectedTypes := structs.TypesJSONStruct{
241-
Name: "electric",
242-
ID: 13,
243-
Pokemon: []struct {
244-
Pokemon struct {
245-
Name string `json:"name"`
246-
URL string `json:"url"`
247-
} `json:"pokemon"`
248-
Slot int `json:"slot"`
249-
}{
250-
{Pokemon: struct {
251-
Name string `json:"name"`
252-
URL string `json:"url"`
253-
}{Name: "pikachu", URL: "https://pokeapi.co/api/v2/pokemon/25/"},
254-
Slot: 1},
255-
},
256-
}
244+
t.Run("Successful API call returns expected type", func(t *testing.T) {
245+
expectedTypes := structs.TypesJSONStruct{
246+
Name: "electric",
247+
ID: 13,
248+
Pokemon: []struct {
249+
Pokemon struct {
250+
Name string `json:"name"`
251+
URL string `json:"url"`
252+
} `json:"pokemon"`
253+
Slot int `json:"slot"`
254+
}{
255+
{Pokemon: struct {
256+
Name string `json:"name"`
257+
URL string `json:"url"`
258+
}{Name: "pikachu", URL: "https://pokeapi.co/api/v2/pokemon/25/"},
259+
Slot: 1},
260+
},
261+
}
257262

258-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
259-
w.WriteHeader(http.StatusOK)
260-
err := json.NewEncoder(w).Encode(expectedTypes)
261-
assert.NoError(t, err, "Expected no error for skipHTTPSCheck")
262-
}))
263-
defer ts.Close()
263+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
264+
w.WriteHeader(http.StatusOK)
265+
err := json.NewEncoder(w).Encode(expectedTypes)
266+
assert.NoError(t, err, "Expected no error for encoding response")
267+
}))
268+
defer ts.Close()
269+
270+
typesStruct, name, err := TypesApiCall("/type", "electric", ts.URL)
271+
272+
require.NoError(t, err, "Expected no error on successful API call")
273+
assert.Equal(t, expectedTypes, typesStruct, "Expected types struct does not match")
274+
assert.Equal(t, "electric", name, "Expected type name does not match")
275+
})
264276

265-
typesStruct, name, id := TypesApiCall("/type", "electric", ts.URL)
277+
t.Run("Failed API call returns styled error", func(t *testing.T) {
278+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
279+
// Simulate API failure (e.g., 404 Not Found)
280+
http.Error(w, "Not Found", http.StatusNotFound)
281+
}))
282+
defer ts.Close()
283+
284+
typesStruct, name, err := TypesApiCall("/type", "non-existent-type", ts.URL)
285+
286+
require.Error(t, err, "Expected an error for invalid type")
287+
assert.Equal(t, structs.TypesJSONStruct{}, typesStruct, "Expected empty types struct on error")
288+
assert.Equal(t, "", name, "Expected empty name string on error")
266289

267-
assert.Equal(t, expectedTypes, typesStruct)
268-
assert.Equal(t, "electric", name)
269-
assert.Equal(t, 13, id)
290+
assert.Contains(t, err.Error(), "Type not found", "Expected 'Type not found' in error message")
291+
assert.Contains(t, err.Error(), "Perhaps a typo?", "Expected helpful suggestion in error message")
292+
})
270293
}
271294

272295
func TestPokemonSpeciesApiCall(t *testing.T) {
273-
// Successful API call returns expected species data
274296
t.Run("Successful API call returns expected species", func(t *testing.T) {
275297
expectedSpecies := structs.PokemonSpeciesJSONStruct{
276298
Name: "flareon",
@@ -283,26 +305,27 @@ func TestPokemonSpeciesApiCall(t *testing.T) {
283305
}))
284306
defer ts.Close()
285307

286-
species, err := PokemonSpeciesApiCall("/pokemon-species", "flareon", ts.URL)
308+
species, name, err := PokemonSpeciesApiCall("/pokemon-species", "flareon", ts.URL)
287309

288310
require.NoError(t, err, "Expected no error on successful API call")
289311
assert.Equal(t, expectedSpecies, species, "Expected species struct does not match")
312+
assert.Equal(t, "flareon", name, "Expected species name does not match")
290313
})
291314

292-
// Failed API call returns styled error
293315
t.Run("Failed API call returns styled error", func(t *testing.T) {
294316
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
295317
// Simulate API failure (e.g., 404 Not Found)
296318
http.Error(w, "Not Found", http.StatusNotFound)
297319
}))
298320
defer ts.Close()
299321

300-
species, err := PokemonSpeciesApiCall("/pokemon-species", "non-existent-species", ts.URL)
322+
species, name, err := PokemonSpeciesApiCall("/pokemon-species", "non-existent-species", ts.URL)
301323

302324
require.Error(t, err, "Expected an error for invalid species")
303325
assert.Equal(t, structs.PokemonSpeciesJSONStruct{}, species, "Expected empty species struct on error")
326+
assert.Equal(t, "", name, "Expected empty name string on error")
304327

305-
assert.Contains(t, err.Error(), "Pokémon not found", "Expected 'Pokémon not found' in error message")
328+
assert.Contains(t, err.Error(), "PokémonSpecies not found", "Expected 'PokémonSpecies not found' in error message")
306329
assert.Contains(t, err.Error(), "Perhaps a typo?", "Expected helpful suggestion in error message")
307330
})
308331
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Poison Point
2+
• First introduced in generation III
3+
• Effect: Has a 30% chance of poisoning attacking Pokémon on contact.

testdata/main_latest_flag.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
┃ ┃
33
┃ Latest available release ┃
44
┃ on GitHub: ┃
5-
┃ • v1.7.4
5+
┃ • v1.8.0
66
┃ ┃
77
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

0 commit comments

Comments
 (0)