// TestStation checks the station service call is working func TestStation(t *testing.T) { // Perform call to find a station. stationID := "42002" station, err := buoy.FindStation(stationID) // We should not get an error. t.Log("There should be no error after the call to FindStation.") if err != nil { t.Fatalf("ERROR : %s", err) } // We should not have a nil pointer for the station. t.Log("We should not have a nil pointer for the station.") if station == nil { t.Fatalf("ERROR : %s", err) } // We should get back the station document. t.Logf("StationID \"%s\" should match \"%s\" in the station document.", stationID, station.StationID) if station.StationID != stationID { t.Errorf("ERROR : Expecting[%s] Received[%s]", stationID, station.StationID) } // The name in this document should match this known value. name := "Station 42002 - West Gulf" t.Logf("Name \"%s\" should match \"%s\" in the station document.", name, station.Name) if station.Name != name { t.Errorf("ERROR : Expecting[%s] Received[%s]", name, station.Name) } }
// TestTableStation checks the station service call is working func TestTableStation(t *testing.T) { // Iterate through the slice of station ids. for index := 0; index < len(stationIDs); index++ { // Station id to test. stationID := stationIDs[index] // Perform call to find a station. station, err := buoy.FindStation(stationID) // We should not get an error. t.Log("There should be no error after the call to FindStation.") if err != nil { t.Fatalf("ERROR : %s", err) } // We should not have a nil pointer for the station. t.Log("We should not have a nil pointer for the station.") if station == nil { t.Fatalf("ERROR : %s", err) } // We should get back the station document. t.Logf("StationID \"%s\" should match \"%s\" in the station document.", stationID, station.StationID) if station.StationID != stationID { t.Errorf("ERROR : Expecting[%s] Received[%s]", stationID, station.StationID) } } }
// Test_InvalidStation checks that an error occurs with an invalid station id. func Test_InvalidStation(t *testing.T) { // Perform call to find a station. stationID := "00000" station, err := buoy.FindStation(stationID) // We should not get an error. t.Log("There should be no error after the call to FindStation.") if err != nil { t.Fatalf("ERROR : %s", err) } // We should have a nil pointer for the station. t.Log("We should get back a nil pointer for the station.") if station != nil { t.Errorf("ERROR : %s", err) } }
// main is the entry point for the application. func main() { // Retrieve a document for station 42002. stationID := "42002" station, err := buoy.FindStation(stationID) if err != nil { log.Printf("main : ERROR : %s\n", err) os.Exit(1) } buoy.Print(station) // Retrieve a slice of documents for the // Gulf Of Mexico region. region := "Gulf Of Mexico" stations, err := buoy.FindRegion(region, 5) if err != nil { log.Printf("main : ERROR : %s\n", err) os.Exit(1) } for _, station := range stations { buoy.Print(&station) } }
// BenchmarkStation provides stats for how this code is performing. func BenchmarkStation(b *testing.B) { // Perform call to find a station. for i := 0; i < b.N; i++ { buoy.FindStation("42002") } }