Skip to content

Commit c4ba7a2

Browse files
committed
Add tests for fetch API
1 parent 179aef4 commit c4ba7a2

2 files changed

Lines changed: 123 additions & 12 deletions

File tree

api/api_test.go

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package api
22

33
import (
4+
"fmt"
45
"io"
56
"net/http"
67
"net/http/httptest"
@@ -11,22 +12,92 @@ import (
1112
"github.com/stretchr/testify/assert"
1213
)
1314

15+
func respondWithFixture(w http.ResponseWriter, name string) error {
16+
f, err := os.Open("../fixtures/" + name)
17+
if err != nil {
18+
return err
19+
}
20+
21+
io.Copy(w, f)
22+
f.Close()
23+
24+
return nil
25+
}
26+
func TestFetchAllProblem(t *testing.T) {
27+
APIKey := "mykey"
28+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
29+
allProblemsAPI := fmt.Sprintf("/v2/exercises?key=%s", APIKey)
30+
assert.Equal(t, allProblemsAPI, req.RequestURI)
31+
32+
if err := respondWithFixture(w, "problems.json"); err != nil {
33+
t.Fatal(err)
34+
}
35+
}))
36+
defer ts.Close()
37+
38+
client := NewClient(&config.Config{XAPI: ts.URL, APIKey: APIKey})
39+
40+
problems, err := client.Fetch([]string{})
41+
assert.NoError(t, err)
42+
43+
assert.Equal(t, len(problems), 3)
44+
}
45+
46+
func TestFetchATrack(t *testing.T) {
47+
var (
48+
APIKey = "mykey"
49+
language = "go"
50+
)
51+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
52+
languageProblemsAPI := fmt.Sprintf("/v2/exercises/%s?key=%s", language, APIKey)
53+
assert.Equal(t, languageProblemsAPI, req.RequestURI)
54+
55+
if err := respondWithFixture(w, "problems.json"); err != nil {
56+
t.Fatal(err)
57+
}
58+
}))
59+
defer ts.Close()
60+
61+
client := NewClient(&config.Config{XAPI: ts.URL, APIKey: APIKey})
62+
63+
_, err := client.Fetch([]string{language})
64+
assert.NoError(t, err)
65+
}
66+
67+
func TestFetchASpecificProblem(t *testing.T) {
68+
var (
69+
APIKey = "mykey"
70+
language = "go"
71+
problem = "leap"
72+
)
73+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
74+
languageProblemsAPI := fmt.Sprintf("/v2/exercises/%s/%s", language, problem)
75+
assert.Equal(t, languageProblemsAPI, req.RequestURI)
76+
77+
if err := respondWithFixture(w, "problems.json"); err != nil {
78+
t.Fatal(err)
79+
}
80+
}))
81+
defer ts.Close()
82+
83+
client := NewClient(&config.Config{XAPI: ts.URL, APIKey: APIKey})
84+
85+
_, err := client.Fetch([]string{language, problem})
86+
assert.NoError(t, err)
87+
}
88+
1489
func TestListTrack(t *testing.T) {
15-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
90+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
1691
// check that we correctly built the URI path
17-
assert.Equal(t, "/tracks/clojure", r.RequestURI)
92+
assert.Equal(t, "/tracks/clojure", req.RequestURI)
1893

19-
f, err := os.Open("../fixtures/tracks.json")
20-
if err != nil {
94+
if err := respondWithFixture(w, "tracks.json"); err != nil {
2195
t.Fatal(err)
2296
}
23-
io.Copy(w, f)
24-
f.Close()
2597
}))
2698
defer ts.Close()
2799

28-
conf := &config.Config{XAPI: ts.URL}
29-
client := NewClient(conf)
100+
client := NewClient(&config.Config{XAPI: ts.URL})
30101

31102
problems, err := client.List("clojure")
32103
assert.NoError(t, err)
@@ -36,13 +107,12 @@ func TestListTrack(t *testing.T) {
36107
}
37108

38109
func TestUnknownLanguage(t *testing.T) {
39-
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
40-
http.NotFound(w, r)
110+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
111+
http.NotFound(w, req)
41112
}))
42113
defer ts.Close()
43114

44-
conf := &config.Config{XAPI: ts.URL}
45-
client := NewClient(conf)
115+
client := NewClient(&config.Config{XAPI: ts.URL})
46116

47117
_, err := client.List("rubbbby")
48118
assert.Equal(t, err, ErrUnknownLanguage)

fixtures/problems.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"problems": [
3+
{
4+
"files": {
5+
"README.md": "# Gigasecond\n\nWrite a program that will calculate the date that someone turned or will celebrate their 1 Gs anniversary.\n\nA gigasecond is one billion (10**9) seconds.\n\n\n## Source\n\nChapter 9 in Chris Pine's online Learn to Program tutorial. [view source](http://pine.fm/LearnToProgram/?Chapter=09)\n",
6+
"cases_test.go": "package gigasecond\n\n// Source: exercism/x-common\n// Commit: f362340 Merge pull request #36 from soniakeys/gigasecond-test-data\n\n// Add one gigasecond to the input.\nvar addCases = []struct {\n\tin string\n\twant string\n}{\n\t{\n\t\t\"2011-04-25\",\n\t\t\"2043-01-01T01:46:40\",\n\t},\n\t{\n\t\t\"1977-06-13\",\n\t\t\"2009-02-19T01:46:40\",\n\t},\n\t{\n\t\t\"1959-07-19\",\n\t\t\"1991-03-27T01:46:40\",\n\t},\n}\n",
7+
"gigasecond_test.go": "package gigasecond\n\n// Write a function AddGigasecond that works with time.Time.\n// Also define a variable Birthday set to your (or someone else's) birthday.\n// Run go test -v to see your gigasecond anniversary.\n\nimport (\n\t\"os\"\n\t\"testing\"\n\t\"time\"\n)\n\nconst testVersion = 1\n\n// Retired testVersions\n// (none) 98807b314216ff27492378a00df60410cc971d32\n\n// date formats used in test data\nconst (\n\tfmtD = \"2006-01-02\"\n\tfmtDT = \"2006-01-02T15:04:05\"\n)\n\nfunc TestAddGigasecond(t *testing.T) {\n\tif TestVersion != testVersion {\n\t\tt.Fatalf(\"Found TestVersion = %v, want %v.\", TestVersion, testVersion)\n\t}\n\tfor _, tc := range addCases {\n\t\tin := parse(tc.in, fmtD, t)\n\t\twant := parse(tc.want, fmtDT, t)\n\t\tgot := AddGigasecond(in)\n\t\tif !got.Equal(want) {\n\t\t\tt.Fatalf(`AddGigasecond(%s)\n = %s\nwant %s`, in, got, want)\n\t\t}\n\t}\n\tt.Log(\"Tested\", len(addCases), \"cases.\")\n}\n\nfunc TestYourAnniversary(t *testing.T) {\n\tt.Logf(`\nYour birthday: %s\nYour gigasecond anniversary: %s`, Birthday, AddGigasecond(Birthday))\n}\n\nfunc parse(s string, f string, t *testing.T) time.Time {\n\ttt, err := time.Parse(f, s)\n\tif err != nil {\n\t\t// can't run tests if input won't parse. if this seems to be a\n\t\t// development or ci environment, raise an error. if this condition\n\t\t// makes it to the solver though, ask for a bug report.\n\t\t_, statErr := os.Stat(\"example_gen.go\")\n\t\tif statErr == nil || os.Getenv(\"TRAVIS_GO_VERSION\") > \"\" {\n\t\t\tt.Fatal(err)\n\t\t} else {\n\t\t\tt.Log(err)\n\t\t\tt.Skip(\"(Not your problem. \" +\n\t\t\t\t\"please file issue at https://github.com/exercism/xgo.)\")\n\t\t}\n\t}\n\treturn tt\n}\n\nfunc BenchmarkAddGigasecond(b *testing.B) {\n\tfor i := 0; i < b.N; i++ {\n\t\tAddGigasecond(time.Time{})\n\t}\n}\n"
8+
},
9+
"fresh": false,
10+
"id": "go/gigasecond",
11+
"language": "go",
12+
"name": "Gigasecond",
13+
"slug": "gigasecond",
14+
"track_id": "go"
15+
},
16+
{
17+
"files": {
18+
"README.md": "# Leap\n\nWrite a program that will take a year and report if it is a leap year.\n\nThe tricky thing here is that a leap year occurs:\n\n```plain\non every year that is evenly divisible by 4\n except every year that is evenly divisible by 100\n unless the year is also evenly divisible by 400\n```\n\nFor example, 1997 is not a leap year, but 1996 is. 1900 is not a leap\nyear, but 2000 is.\n\nIf your language provides a method in the standard library that does\nthis look-up, pretend it doesn't exist and implement it yourself.\n\n## Notes\n\nFor a delightful, four minute explanation of the whole leap year\nphenomenon, go watch [this youtube video][video].\n\n[video]: http://www.youtube.com/watch?v=xX96xng7sAE\n\n\n## Source\n\nJavaRanch Cattle Drive, exercise 3 [view source](http://www.javaranch.com/leap.jsp)\n",
19+
"leap_test.go": "package leap\n\nimport (\n\t\"testing\"\n)\n\nvar testCases = []struct {\n\tyear int\n\texpected bool\n\tdescription string\n}{\n\t{1996, true, \"vanilla leap year\"},\n\t{1997, false, \"normal year\"},\n\t{1900, false, \"century\"},\n\t{2400, true, \"exceptional century\"},\n}\n\nfunc TestLeapYears(t *testing.T) {\n\tfor _, test := range testCases {\n\t\tobserved := IsLeapYear(test.year)\n\t\tif observed != test.expected {\n\t\t\tt.Fatalf(\"IsLeapYear(%d) = %t, want %t (%s)\",\n\t\t\t\ttest.year, observed, test.expected, test.description)\n\t\t}\n\t}\n}\n\nfunc BenchmarkLeapYears(b *testing.B) {\n\tfor i := 0; i < b.N; i++ {\n\t\tfor _, test := range testCases {\n\t\t\tIsLeapYear(test.year)\n\t\t}\n\t}\n}\n"
20+
},
21+
"fresh": false,
22+
"id": "go/leap",
23+
"language": "go",
24+
"name": "Leap",
25+
"slug": "leap",
26+
"track_id": "go"
27+
},
28+
{
29+
"files": {
30+
"README.md": "# Raindrops\n\nWrite a program that converts a number to a string, the contents of which depends on the number's prime factors.\n\n- If the number contains 3 as a prime factor, output 'Pling'.\n- If the number contains 5 as a prime factor, output 'Plang'.\n- If the number contains 7 as a prime factor, output 'Plong'.\n- If the number does not contain 3, 5, or 7 as a prime factor,\n just pass the number's digits straight through.\n\n## Examples\n\n- 28's prime-factorization is 2, 2, 7.\n - In raindrop-speak, this would be a simple \"Plong\".\n- 1755 prime-factorization is 3, 3, 3, 5, 13.\n - In raindrop-speak, this would be a \"PlingPlang\".\n- The prime factors of 34 are 2 and 17.\n - Raindrop-speak doesn't know what to make of that,\n so it just goes with the straightforward \"34\".\n\n\n## Source\n\nA variation on a famous interview question intended to weed out potential candidates. [view source](http://jumpstartlab.com)\n",
31+
"raindrops_test.go": "package raindrops\n\nimport \"testing\"\n\nvar tests = []struct {\n\tinput int\n\texpected string\n}{\n\t{1, \"1\"},\n\t{3, \"Pling\"},\n\t{5, \"Plang\"},\n\t{7, \"Plong\"},\n\t{6, \"Pling\"},\n\t{9, \"Pling\"},\n\t{10, \"Plang\"},\n\t{14, \"Plong\"},\n\t{15, \"PlingPlang\"},\n\t{21, \"PlingPlong\"},\n\t{25, \"Plang\"},\n\t{35, \"PlangPlong\"},\n\t{49, \"Plong\"},\n\t{52, \"52\"},\n\t{105, \"PlingPlangPlong\"},\n\t{12121, \"12121\"},\n}\n\nfunc TestConvert(t *testing.T) {\n\tfor _, test := range tests {\n\t\tif actual := Convert(test.input); actual != test.expected {\n\t\t\tt.Errorf(\"Convert(%d) = %q, expected %q.\",\n\t\t\t\ttest.input, actual, test.expected)\n\t\t}\n\t}\n}\n\nfunc BenchmarkConvert(b *testing.B) {\n\tfor i := 0; i < b.N; i++ {\n\t\tfor _, test := range tests {\n\t\t\tConvert(test.input)\n\t\t}\n\t}\n}\n"
32+
},
33+
"fresh": false,
34+
"id": "go/raindrops",
35+
"language": "go",
36+
"name": "Raindrops",
37+
"slug": "raindrops",
38+
"track_id": "go"
39+
}
40+
]
41+
}

0 commit comments

Comments
 (0)