Created
November 23, 2014 18:10
-
-
Save philsturgeon/29a75ec4ec3043f87d9f to your computer and use it in GitHub Desktop.
Test Remote CSV Downlaod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"testing" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"net/http/httptest" | |
) | |
func TestConvertCsv(t *testing.T) { | |
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
jsonData, err := ioutil.ReadFile("./testdata/vehicles.csv") | |
if err != nil { | |
panic(err) | |
} | |
io.WriteString(w, string(jsonData)) | |
})) | |
defer ts.Close() | |
csvData, err := DownloadCsvFile(ts.URL) | |
if err != nil { | |
panic(err) | |
} | |
vehicle1 := convertCsv(csvData[1]) | |
if expect := "1984"; vehicle1.Model_year != expect { | |
t.Error("Expected", expect, "got", vehicle1.Model_year) | |
} | |
if expect := "AM General"; vehicle1.Make != expect { | |
t.Error("Expected", expect, "got", vehicle1.Make) | |
} | |
if expect := "regular"; vehicle1.Primary_fuel != expect { | |
t.Error("Expected", expect, "got", vehicle1.Primary_fuel) | |
} | |
if expect := "Special Purpose Vehicle 2WD"; vehicle1.Vehicle_class != expect { | |
t.Error("Expected", expect, "got", vehicle1.Vehicle_class) | |
} | |
if expect := "Automatic 3-spd"; vehicle1.Transmission != expect { | |
t.Error("Expected", expect, "got", vehicle1.Transmission) | |
} | |
if expect := "2.5"; vehicle1.Displacement != expect { | |
t.Error("Expected", expect, "got", vehicle1.Displacement) | |
} | |
if expect := "2-Wheel Drive"; vehicle1.Drive != expect { | |
t.Error("Expected", expect, "got", vehicle1.Drive) | |
} | |
if expect := "18.0"; vehicle1.City_mpg != expect { | |
t.Error("Expected", expect, "got", vehicle1.City_mpg) | |
} | |
if expect := "17.0"; vehicle1.Highway_mpg != expect { | |
t.Error("Expected", expect, "got", vehicle1.Highway_mpg) | |
} | |
if expect := "17.0"; vehicle1.Comb_mpg != expect { | |
t.Error("Expected", expect, "got", vehicle1.Comb_mpg) | |
} | |
} | |
func TestGetFuelType(t *testing.T) { | |
expectations := map[string]string{ | |
"Regular Gasoline": "regular", | |
"Diesel": "diesel", | |
"Midgrade Gasoline": "mid", | |
"Premium Gasoline": "premium", | |
"Natural Gas": "regular", | |
"Totally Invented": "regular", | |
} | |
for input, expect := range expectations { | |
if actual := getFuelType(input); actual != expect { | |
t.Error("For", input, "the expected fuel type was", expect, ", got", actual) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment