feat: refactor tests

This commit is contained in:
Martin Eyben 2024-11-23 22:01:37 +01:00
parent 69d5512220
commit 423e1a44f3
3 changed files with 91 additions and 57 deletions

View File

@ -111,4 +111,9 @@ public class WeatherAPI implements WeatherDataAPI{
public String getAPIName() {
return "WeatherAPI";
}
@Override
public String toString() {
return this.getAPIName();
}
}

View File

@ -23,35 +23,6 @@ public class WeatherAPITest {
this.weatherAPI.JSONFetcher = new FakeJSONFetcherWeatherAPI();
}
/**
* List of args for Temperature testing
* @return Args for testing
*/
private static Stream<Arguments> testGetTemperature(){
return Stream.of(
Arguments.arguments(0, 8.1F,WeatherData.Condition.PARTIAL, 17.45F, 142.08F),
Arguments.arguments(1, 13F, WeatherData.Condition.SUNNY, 23.03F, 142.58F),
Arguments.arguments(2, 12.7F, WeatherData.Condition.RAINY, 13.19F, 222.92F),
Arguments.arguments(3, 8.1F,WeatherData.Condition.CLOUDY, 17.45F, 142.08F)
);
}
@ParameterizedTest(name="Temperature fetch at Bordeaux D+{0}")
@MethodSource
public void testGetTemperature(int day, float expectedTemp, WeatherData.Condition expectedCond, float expectedWindSpeed, float expectedWindAngle) throws IOException {
String city = "Bordeaux";
WeatherData weatherData;
weatherData = weatherAPI.getTemperature(day, city);
/* Temperatures */
Assertions.assertEquals(expectedTemp, weatherData.getTemp());
/* Condition */
Assertions.assertEquals(expectedCond, weatherData.getCondition());
/* Wind */
Assertions.assertTrue(expectedWindSpeed - weatherData.getWindSpeed() < epsilon);
Assertions.assertTrue(expectedWindAngle - weatherData.getWindDirectionAngle() < epsilon);
}
/**
* For coverage (hour not yet implemented)
* @throws IOException never
@ -65,34 +36,6 @@ public class WeatherAPITest {
}
@Test
@DisplayName("Multiple day temperature fetch")
public void testGetTemperatures() throws IOException {
String city = "Bordeaux";
int days = 3;
float[] expectedTemperatures = {8.1F, 13F, 12.7F, 8.1F};
WeatherData.Condition[] expectedConditions = {WeatherData.Condition.PARTIAL, WeatherData.Condition.SUNNY, WeatherData.Condition.RAINY, WeatherData.Condition.CLOUDY};
float[] expectedWindSpeed = {17.45F, 23.03F, 13.19F, 17.45F};
float[] expectedWindDirection = {142.08F, 142.58F, 222.92F, 142.08F};
ArrayList<WeatherData> weatherDatas;
WeatherData weatherData;
weatherDatas = weatherAPI.getTemperatures(days, city);
for (int index = 0; index < days; index++) {
weatherData = weatherDatas.get(index);
/* Temperatures */
Assertions.assertEquals(expectedTemperatures[index], weatherData.getTemp());
/* Weather condition */
Assertions.assertEquals(expectedConditions[index], weatherData.getCondition());
/* Wind */
Assertions.assertTrue(expectedWindSpeed[index] - weatherData.getWindSpeed() < epsilon);
Assertions.assertTrue(expectedWindDirection[index] - weatherData.getWindDirectionAngle() < epsilon);
}
}
@Test
public void testGetAPIName() {
Assertions.assertEquals("WeatherAPI", weatherAPI.getAPIName());

View File

@ -0,0 +1,86 @@
package eirb.pg203;
import eirb.pg203.fakeJSONFetcher.FakeJSONFetcherWeatherAPI;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.stream.Stream;
public class WeatherDataAPITest {
private static final float epsilon = 0.01F;
private static final String APIKey = "realKey";
private static WeatherAPI weatherAPI(){
WeatherAPI weatherAPI = new WeatherAPI(APIKey);
weatherAPI.JSONFetcher = new FakeJSONFetcherWeatherAPI();
return weatherAPI;
}
/**
* List of args for Temperature testing
* @return Args for testing
*/
private static Stream<Arguments> testGetTemperature(){
return Stream.of(
Arguments.arguments(weatherAPI(), 0, 8.1F,WeatherData.Condition.PARTIAL, 17.45F, 142.08F),
Arguments.arguments(weatherAPI(), 1, 13F, WeatherData.Condition.SUNNY, 23.03F, 142.58F),
Arguments.arguments(weatherAPI(), 2, 12.7F, WeatherData.Condition.RAINY, 13.19F, 222.92F),
Arguments.arguments(weatherAPI(), 3, 8.1F,WeatherData.Condition.CLOUDY, 17.45F, 142.08F)
);
}
@ParameterizedTest(name="[{0}] Temperature fetch at Bordeaux D+{1}")
@MethodSource
public void testGetTemperature(WeatherDataAPI weatherDataAPI, int day, float expectedTemp, WeatherData.Condition expectedCond, float expectedWindSpeed, float expectedWindAngle) throws IOException {
String city = "Bordeaux";
WeatherData weatherData;
weatherData = weatherDataAPI.getTemperature(day, city);
/* Temperatures */
Assertions.assertEquals(expectedTemp, weatherData.getTemp());
/* Condition */
Assertions.assertEquals(expectedCond, weatherData.getCondition());
/* Wind */
Assertions.assertTrue(expectedWindSpeed - weatherData.getWindSpeed() < epsilon);
Assertions.assertTrue(expectedWindAngle - weatherData.getWindDirectionAngle() < epsilon);
}
private static Stream<Arguments> testGetTemperatures() {
float[] weatherAPIExpectedTemperatures = {8.1F, 13F, 12.7F, 8.1F};
WeatherData.Condition[] weatherAPIExpectedConditions = {WeatherData.Condition.PARTIAL, WeatherData.Condition.SUNNY, WeatherData.Condition.RAINY, WeatherData.Condition.CLOUDY};
float[] weatherAPIExpectedWindSpeed = {17.45F, 23.03F, 13.19F, 17.45F};
float[] weatherAPIExpectedWindDirection = {142.08F, 142.58F, 222.92F, 142.08F};
return Stream.of(
Arguments.arguments(weatherAPI(), 4, weatherAPIExpectedTemperatures, weatherAPIExpectedConditions, weatherAPIExpectedWindSpeed, weatherAPIExpectedWindDirection)
);
}
@ParameterizedTest(name = "[{0}] Fetch temperatures for {1} days")
@MethodSource
public void testGetTemperatures(WeatherDataAPI weatherDataAPI, int days, float[] expectedTemperatures, WeatherData.Condition[] expectedConditions, float[] expectedWindSpeed, float[] expectedWindDirection) throws IOException {
String city = "Bordeaux";
ArrayList<WeatherData> weatherDatas;
WeatherData weatherData;
weatherDatas = weatherDataAPI.getTemperatures(days, city);
for (int index = 0; index < days; index++) {
weatherData = weatherDatas.get(index);
/* Temperatures */
Assertions.assertEquals(expectedTemperatures[index], weatherData.getTemp());
/* Weather condition */
Assertions.assertEquals(expectedConditions[index], weatherData.getCondition());
/* Wind */
Assertions.assertTrue(expectedWindSpeed[index] - weatherData.getWindSpeed() < epsilon);
Assertions.assertTrue(expectedWindDirection[index] - weatherData.getWindDirectionAngle() < epsilon);
}
}
}