-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/query' into develop
Implemented query methods: 1. By County 2. By Town See #2.
- Loading branch information
Showing
8 changed files
with
579 additions
and
313 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package twweather | ||
|
||
type StationMap map[string]StationStatus | ||
|
||
func (weather *Weather) GetStationBy(predictor func(StationStatus) bool) StationMap { | ||
candidate := make(StationMap, 2) | ||
for _, location := range weather.stationList.Locations { | ||
if predictor(location) { | ||
// return a copy | ||
cp := location | ||
candidate[location.StationName] = cp | ||
} | ||
} | ||
return candidate | ||
} | ||
|
||
// GetStationByCityName returns StationMap that contains stations matched by town name. | ||
func (weather *Weather) GetStationByTownName(townName string) StationMap { | ||
return weather.GetStationBy(func(station StationStatus) bool { | ||
return station.TownName == townName | ||
}) | ||
} | ||
|
||
// GetStationByCityName returns StationMap that contains stations matched by city name. | ||
func (weather *Weather) GetStationsByCityName(cityName string) StationMap { | ||
return weather.GetStationBy(func(station StationStatus) bool { | ||
return station.CityName == cityName | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package twweather | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestGetStationByTownName(t *testing.T) { | ||
weather.UpdateStationStatusWithData(sampleXML) | ||
stationMap := weather.GetStationByTownName("橫山鄉") | ||
station, ok := stationMap["橫山"] | ||
if !ok { | ||
t.Log(stationMap) | ||
t.Error("Cannot find station 橫山") | ||
if station.TownSN != 78 { | ||
t.Logf("Got Town number %v", station.TownSN) | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
func testHasStation(list map[string]StationStatus, name string) error { | ||
_, ok := list[name] | ||
if !ok { | ||
return fmt.Errorf("Missing station %s", name) | ||
} | ||
return nil | ||
} | ||
|
||
func TestGetStationsByCityName(t *testing.T) { | ||
weather.UpdateStationStatusWithData(sampleXML) | ||
stationMap := weather.GetStationsByCityName("新竹縣") | ||
logIfError := func(err error) { | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
} | ||
if len(stationMap) != 2 { | ||
t.Fail() | ||
} else { | ||
logIfError(testHasStation(stationMap, "橫山")) | ||
logIfError(testHasStation(stationMap, "新豐")) | ||
} | ||
} |
Oops, something went wrong.