Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mock WeatherProvider #59

Merged
merged 3 commits into from Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 42 additions & 1 deletion routes/weather.spec.ts
Expand Up @@ -5,6 +5,8 @@ import * as MockExpressResponse from 'mock-express-response';
import * as MockDate from 'mockdate';

import { getWateringData } from './weather';
import { GeoCoordinates, WateringData, WeatherData } from "../types";
import { WeatherProvider } from "./weatherProviders/WeatherProvider";

const expected = require( '../test/expected.json' );
const replies = require( '../test/replies.json' );
Expand Down Expand Up @@ -58,4 +60,43 @@ function mockOWM() {
.filteringPath( function() { return "/"; } )
.get( "/" )
.reply( 200, replies[location].OWMData );
}
}


/**
* A WeatherProvider for testing purposes that returns weather data that is provided in the constructor.
* This is a special WeatherProvider designed for testing purposes and should not be activated using the
* WEATHER_PROVIDER environment variable.
*/
export class MockWeatherProvider extends WeatherProvider {

private readonly mockData: MockWeatherData;

public constructor(mockData: MockWeatherData) {
super();
this.mockData = mockData;
}

public async getWateringData( coordinates: GeoCoordinates ): Promise< WateringData > {
const data = this.mockData.wateringData;
if ( !data.weatherProvider ) {
data.weatherProvider = "mock";
}

return data;
}

public async getWeatherData( coordinates: GeoCoordinates ): Promise< WeatherData > {
const data = this.mockData.weatherData;
if ( !data.weatherProvider ) {
data.weatherProvider = "mock";
}

return data;
}
}

interface MockWeatherData {
wateringData?: WateringData,
weatherData?: WeatherData
}
2 changes: 1 addition & 1 deletion types.ts
Expand Up @@ -85,4 +85,4 @@ export interface AdjustmentOptions {
d?: number;
}

export type WeatherProviderId = "OWM" | "DarkSky" | "local";
export type WeatherProviderId = "OWM" | "DarkSky" | "local" | "mock";