Skip to content

Commit 9c81178

Browse files
author
AimanYosofi
committed
Merge branch 'refs/heads/development' into feature/suggest-clothes-test
# Conflicts: # src/main/kotlin/domain/models/CurrentWeather.kt
2 parents 0ca87e1 + c9335dc commit 9c81178

File tree

5 files changed

+54
-38
lines changed

5 files changed

+54
-38
lines changed

src/main/kotlin/data/repository/WeatherRepositoryImpl.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package data.repository
2+
23
import WeatherRepository
34
import domain.models.CurrentWeather
45
import domain.models.exceptions.FailedFetchWeatherDataException
@@ -7,13 +8,18 @@ import io.ktor.client.call.*
78
import io.ktor.client.request.*
89
import io.ktor.client.statement.*
910
import io.ktor.http.*
10-
const val OPEN_METO_API="https://api.open-meteo.com/v1/forecast"
11+
12+
const val OPEN_METO_API = "https://api.open-meteo.com/v1/forecast"
13+
1114
class WeatherRepositoryImpl(private val httpClient: HttpClient) : WeatherRepository {
1215
override suspend fun getCurrentWeather(latitude: Double, longitude: Double): CurrentWeather {
1316
val response: HttpResponse = httpClient.get(OPEN_METO_API) {
1417
parameter("latitude", latitude)
1518
parameter("longitude", longitude)
16-
parameter("current", "temperature_2m,relative_humidity_2m,apparent_temperature,is_day,wind_speed_10m,snowfall,rain,weather_code,cloud_cover")
19+
parameter(
20+
"current",
21+
"temperature_2m,relative_humidity_2m,apparent_temperature,is_day,wind_speed_10m,snowfall,rain,weather_code,cloud_cover"
22+
)
1723
}
1824

1925
if (!response.status.isSuccess()) {

src/main/kotlin/domain/models/CurrentWeather.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import kotlinx.serialization.Serializable
55

66
@Serializable
77
data class CurrentWeather(
8-
val time: String,
9-
val interval: Double,
108
@SerialName("temperature_2m") val temperature2m: Double,
119
@SerialName("relative_humidity_2m") val relativeHumidity2m: Double,
1210
@SerialName("apparent_temperature") val apparentTemperature: Double,
1311
@SerialName("is_day") val isDay: Int,
1412
val rain: Double,
1513
val snowfall: Double,
16-
@SerialName("weather_code") val weatherCode: Double,
1714
@SerialName("cloud_cover") val cloudCover: Double,
18-
@SerialName("wind_speed_10m") val windSpeed10m: Double
15+
@SerialName("wind_speed_10m") val windSpeed10m: Double,
1916
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import domain.models.CurrentWeather
22

33
interface WeatherRepository {
4-
suspend fun getCurrentWeather(latitude: Double, longitude: Double): CurrentWeather
4+
suspend fun getCurrentWeather(latitude: Double, longitude: Double): CurrentWeather
55
}

src/main/kotlin/domain/use_cases/GetCurrentWeatherUseCase.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import domain.models.CurrentWeather
2+
23
class GetCurrentWeatherUseCase(private val repository: WeatherRepository) {
34
suspend fun getCurrentWeather(latitude: Double, longitude: Double): CurrentWeather {
45
return repository.getCurrentWeather(latitude, longitude)

src/test/kotlin/domain/use_cases/GetCurrentWeatherUseCaseTest.kt

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ package domain.use_cases
22

33
import GetCurrentWeatherUseCase
44
import WeatherRepository
5+
import com.google.common.truth.Truth
6+
import com.google.common.truth.Truth.assertThat
57
import domain.models.CurrentWeather
68
import io.mockk.coEvery
79
import io.mockk.coVerify
810
import io.mockk.mockk
9-
import kotlinx.coroutines.ExperimentalCoroutinesApi
1011
import kotlinx.coroutines.test.runTest
11-
import org.junit.Assert.assertEquals
12-
import org.junit.Assert.fail
1312
import org.junit.jupiter.api.BeforeEach
1413
import org.junit.jupiter.api.Test
15-
import com.google.common.truth.Truth.assertThat
1614
import org.junit.jupiter.api.assertThrows
1715

18-
@OptIn(ExperimentalCoroutinesApi::class)
1916
class GetCurrentWeatherUseCaseTest {
2017

2118
private lateinit var weatherRepository: WeatherRepository
@@ -24,15 +21,12 @@ class GetCurrentWeatherUseCaseTest {
2421
private val latitude = 55.7558
2522
private val longitude = 37.6173
2623
private val expectedWeather = CurrentWeather(
27-
time = "2025-05-06T15:00",
28-
interval = 100.0,
2924
temperature2m = 12.5,
3025
relativeHumidity2m = 67.0,
3126
apparentTemperature = 11.2,
3227
isDay = 1,
3328
rain = 0.1,
3429
snowfall = 0.0,
35-
weatherCode = 3.0,
3630
cloudCover = 65.0,
3731
windSpeed10m = 6.8,
3832
)
@@ -43,6 +37,19 @@ class GetCurrentWeatherUseCaseTest {
4337
getCurrentWeatherUseCase = GetCurrentWeatherUseCase(weatherRepository)
4438
}
4539

40+
@Test
41+
fun `should get the current weather from the WeatherRepository when getting the current weather`() =
42+
runTest {
43+
// Given
44+
coEvery { weatherRepository.getCurrentWeather(latitude, longitude) } returns expectedWeather
45+
46+
// When
47+
val result = getCurrentWeatherUseCase.getCurrentWeather(latitude, longitude)
48+
49+
// Then
50+
coVerify(exactly = 1) { weatherRepository.getCurrentWeather(latitude, longitude) }
51+
}
52+
4653
@Test
4754
fun `should return current weather for given coordinates when getting the current weather`() = runTest {
4855
// Given
@@ -52,13 +59,13 @@ class GetCurrentWeatherUseCaseTest {
5259
val result = getCurrentWeatherUseCase.getCurrentWeather(latitude, longitude)
5360

5461
// Then
55-
coVerify(exactly = 1) { weatherRepository.getCurrentWeather(latitude, longitude) }
56-
assertEquals(expectedWeather, result)
62+
Truth.assertThat(expectedWeather).isEqualTo(result)
5763
}
5864

5965
@Test
6066

61-
fun `should throw exception when repository fails`() = runTest {
67+
fun `fun getCurrentWeather() should throw exception when repository fails`() = runTest {
68+
6269
// Given
6370
val exceptionMessage = "API error"
6471
coEvery { weatherRepository.getCurrentWeather(latitude, longitude) } throws RuntimeException(exceptionMessage)
@@ -73,29 +80,34 @@ class GetCurrentWeatherUseCaseTest {
7380
}
7481

7582
@Test
76-
fun `should return weather with valid temperature value`() = runTest {
77-
// Given
78-
val modifiedWeather = expectedWeather.copy(temperature2m = 20.8)
79-
coEvery { weatherRepository.getCurrentWeather(latitude, longitude) } returns modifiedWeather
80-
81-
// When
82-
val result = getCurrentWeatherUseCase.getCurrentWeather(latitude, longitude)
83+
fun `getCurrentWeather() should return expected weather data when valid latitude and longitude are provided()`() =
84+
runTest {
85+
// Given
86+
val testLat = 33.3
87+
val testLon = 44.4
88+
val expected = expectedWeather.copy(temperature2m = 25.0)
89+
coEvery { weatherRepository.getCurrentWeather(testLat, testLon) } returns expected
90+
91+
// When
92+
val result = getCurrentWeatherUseCase.getCurrentWeather(testLat, testLon)
93+
94+
// Then
95+
coVerify { weatherRepository.getCurrentWeather(testLat, testLon) }
96+
assertThat(result).isEqualTo(expected)
97+
}
8398

84-
// Then
85-
assertThat(result.temperature2m).isEqualTo(20.8)
86-
}
8799

88100
@Test
89-
fun `should call repository with zero coordinates`() = runTest {
90-
// Given
91-
val zeroLat = 0.0
92-
val zeroLon = 0.0
93-
coEvery { weatherRepository.getCurrentWeather(zeroLat, zeroLon) } returns expectedWeather
101+
fun `getCurrentWeather() should call repository with zero coordinates when latitude and longitude are both zeros`() =
102+
runTest {
103+
val zeroLat = 0.0
104+
val zeroLon = 0.0
105+
coEvery { weatherRepository.getCurrentWeather(zeroLat, zeroLon) } returns expectedWeather
94106

95-
// When
96-
val result = getCurrentWeatherUseCase.getCurrentWeather(zeroLat, zeroLon)
107+
// When
108+
getCurrentWeatherUseCase.getCurrentWeather(zeroLat, zeroLon)
97109

98-
// Then
99-
coVerify { weatherRepository.getCurrentWeather(zeroLat, zeroLon) }
100-
}
110+
// Then
111+
coVerify { weatherRepository.getCurrentWeather(zeroLat, zeroLon) }
112+
}
101113
}

0 commit comments

Comments
 (0)