Skip to content

Commit

Permalink
Merge pull request #235 from hossain-khan/feature/add-tests-by-ai
Browse files Browse the repository at this point in the history
[ADDED] Tests by AI (github copilot)
  • Loading branch information
hossain-khan authored Dec 22, 2023
2 parents e05231e + f69fcf7 commit 3947677
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/main/kotlin/dev/hossain/time/ZonedDateTimeExtension.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.hossain.time

import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
Expand Down Expand Up @@ -163,7 +162,7 @@ internal fun ZonedDateTime.isAfterWorkingHour(): Boolean {
internal fun ZonedDateTime.format(): String {
val formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
.withLocale(Locale.US)
.withZone(ZoneId.systemDefault())
.withZone(zone)

return this.format(formatter)
}
58 changes: 58 additions & 0 deletions src/test/kotlin/dev/hossain/githubstats/util/ErrorProcessorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package dev.hossain.githubstats.util

import com.google.common.truth.Truth.assertThat
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.ResponseBody.Companion.toResponseBody
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import retrofit2.HttpException
import retrofit2.Response

/**
* Test for [ErrorProcessor]
*/
class ErrorProcessorTest {

@BeforeEach
fun setUp() {
}

@AfterEach
fun tearDown() {
}

@Test
fun `getDetailedError - given HttpException with error body - returns IllegalStateException with detailed message`() {
val httpException = HttpException(Response.error<Any>(404, "Not found".toResponseBody("text/plain".toMediaTypeOrNull())))
val errorProcessor = ErrorProcessor()

val exception = errorProcessor.getDetailedError(httpException)

assertThat(exception).isInstanceOf(IllegalStateException::class.java)
assertThat(exception.message).contains("HTTP 404")
assertThat(exception.message).contains("Not found")
}

@Test
fun `getDetailedError - given HttpException without error body - returns IllegalStateException with detailed message`() {
val httpException = HttpException(Response.error<Any>(404, "".toResponseBody("text/plain".toMediaTypeOrNull())))
val errorProcessor = ErrorProcessor()

val exception = errorProcessor.getDetailedError(httpException)

assertThat(exception).isInstanceOf(IllegalStateException::class.java)
assertThat(exception.message).contains("HTTP 404")
}

@Test
fun `getDetailedError - given non HttpException - returns IllegalStateException with detailed message`() {
val exception = Exception("Some error")
val errorProcessor = ErrorProcessor()

val detailedException = errorProcessor.getDetailedError(exception)

assertThat(detailedException).isInstanceOf(IllegalStateException::class.java)
assertThat(detailedException.message).contains("Some error")
}
}
50 changes: 50 additions & 0 deletions src/test/kotlin/dev/hossain/time/DateTimeDifferTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,55 @@ internal class DateTimeDifferTest {
assertThat(diffWorkingHours).isEqualTo("9h 33m".duration())
}

@Test
fun `diffWorkingHours - given start time is equal to end time - provides zero working hour`() {
val startTime: Instant = Instant.parse("2022-09-05T10:00:00-04:00") // 10:00am
val endTime: Instant = Instant.parse("2022-09-05T10:00:00-04:00") // 10:00am same day

val diffWorkingHours = DateTimeDiffer.diffWorkingHours(startTime, endTime, zoneId)

assertThat(diffWorkingHours).isEqualTo(Duration.ZERO)
}

@Test
fun `diffWorkingHours - given start time is during working hour and end time is after working hour on the same day - provides diff of working hour only`() {
val startTime: Instant = Instant.parse("2022-09-05T10:00:00-04:00") // 10:00am
val endTime: Instant = Instant.parse("2022-09-05T18:00:00-04:00") // 6:00pm same day

val diffWorkingHours = DateTimeDiffer.diffWorkingHours(startTime, endTime, zoneId)

assertThat(diffWorkingHours).isEqualTo("7h".duration())
}

@Test
fun `diffWorkingHours - given start time is during working hour and end time is before working hour on the same day - provides zero working hour`() {
val startTime: Instant = Instant.parse("2022-09-05T10:00:00-04:00") // 10:00am
val endTime: Instant = Instant.parse("2022-09-05T08:00:00-04:00") // 8:00am same day

Assertions.assertThrows(IllegalArgumentException::class.java) {
DateTimeDiffer.diffWorkingHours(startTime, endTime, zoneId)
}
}

@Test
fun `diffWorkingHours - given start time is during working hour and end time is before working hour on the next day - provides diff of working hour only`() {
val startTime: Instant = Instant.parse("2022-09-05T10:00:00-04:00") // 10:00am
val endTime: Instant = Instant.parse("2022-09-06T08:00:00-04:00") // 8:00am next day

val diffWorkingHours = DateTimeDiffer.diffWorkingHours(startTime, endTime, zoneId)

assertThat(diffWorkingHours).isEqualTo("7h".duration())
}

@Test
fun `diffWorkingHours - given start time is during working hour and end time is after working hour on the next day - provides diff of working hour only`() {
val startTime: Instant = Instant.parse("2022-09-05T10:00:00-04:00") // 10:00am
val endTime: Instant = Instant.parse("2022-09-06T18:00:00-04:00") // 6:00pm next day

val diffWorkingHours = DateTimeDiffer.diffWorkingHours(startTime, endTime, zoneId)

assertThat(diffWorkingHours).isEqualTo("15h".duration())
}

private fun String.duration(): Duration = Duration.parse(this)
}
81 changes: 81 additions & 0 deletions src/test/kotlin/dev/hossain/time/DurationExtensionKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package dev.hossain.time

import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.time.Duration

/**
* Contains unit tests for [DurationExtension].
*/
class DurationExtensionKtTest {

@BeforeEach
fun setUp() {
}

@AfterEach
fun tearDown() {
}

@Test
fun toWorkingHour() {
}

@Test
fun `toWorkingHour - given duration less than working hours - returns same duration`() {
val duration = "2h".duration()

val result = duration.toWorkingHour()

assertThat(result).isEqualTo("2h")
}

@Test
fun `toWorkingHour - given duration equal to working hours - returns one day`() {
val duration = "8h".duration()

val result = duration.toWorkingHour()

assertThat(result).isEqualTo("1 day [Based on 8h on a working day]")
}

@Test
fun `toWorkingHour - given duration more than working hours but less than two working days - returns days and hours`() {
val duration = "10h".duration()

val result = duration.toWorkingHour()

assertThat(result).isEqualTo("1 day and 2h [Based on 8h on a working day]")
}

@Test
fun `toWorkingHour - given duration equal to two working days - returns two days`() {
val duration = "16h".duration()

val result = duration.toWorkingHour()

assertThat(result).isEqualTo("2 days [Based on 8h on a working day]")
}

@Test
fun `toWorkingHour - given duration of one day - returns three days`() {
val duration = "24h".duration()

val result = duration.toWorkingHour()

assertThat(result).isEqualTo("3 days [Based on 8h on a working day]")
}

@Test
fun `toWorkingHour - given duration of one day and custom working hours - returns custom days`() {
val duration = "24h".duration()

val result = duration.toWorkingHour(6)

assertThat(result).isEqualTo("4 days [Based on 6h on a working day]")
}

private fun String.duration(): Duration = Duration.parse(this)
}
28 changes: 28 additions & 0 deletions src/test/kotlin/dev/hossain/time/InstantExtensionKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.hossain.time

import com.google.common.truth.Truth.assertThat
import kotlinx.datetime.Instant
import org.junit.jupiter.api.Test

/**
* Contains unit tests for [InstantExtensionKt].
*/
class InstantExtensionKtTest {
@Test
fun `toZdt - given Instant - returns ZonedDateTime at New York timezone`() {
val instant: Instant = Instant.parse("2022-09-05T10:00:00Z")

val zonedDateTime = instant.toZdt()

assertThat(zonedDateTime.zone.id).isEqualTo("America/New_York")
}

@Test
fun `format - given Instant - returns formatted string in New York timezone`() {
val instant: Instant = Instant.parse("2022-09-05T10:00:00Z")

val formattedString = instant.format()

assertThat(formattedString).isEqualTo("Monday, September 5, 2022 at 6:00:00 AM Eastern Daylight Time")
}
}
90 changes: 90 additions & 0 deletions src/test/kotlin/dev/hossain/time/ZonedDateTimeExtensionTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hossain.time

import com.google.common.truth.Truth.assertThat
import kotlinx.datetime.DayOfWeek
import kotlinx.datetime.Instant
import kotlinx.datetime.toInstant
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -138,4 +139,93 @@ internal class ZonedDateTimeExtensionTest {
val dateTime = Instant.parse("2022-09-05T06:00:00-04:00").toZdt() // 06:00am Monday
assertThat(dateTime.isAfterWorkingHour()).isFalse()
}

// region Tests by AI

@Test
fun `startOfDay - given ZonedDateTime - returns ZonedDateTime at start of day`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-05T10:00:00-04:00[America/New_York]")

val startOfDay = zonedDateTime.startOfDay()

assertThat(startOfDay.hour).isEqualTo(0)
assertThat(startOfDay.minute).isEqualTo(0)
assertThat(startOfDay.second).isEqualTo(0)
}

@Test
fun `nextWorkingDay - given ZonedDateTime on weekend - returns ZonedDateTime on next Monday`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-04T10:00:00-04:00[America/New_York]") // Sunday

val nextWorkingDay = zonedDateTime.nextWorkingDay()

assertThat(nextWorkingDay.dayOfWeek).isEqualTo(DayOfWeek.MONDAY)
}

@Test
fun `nextWorkingDay - given ZonedDateTime on weekday - returns ZonedDateTime on next day`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-05T10:00:00-04:00[America/New_York]") // Monday

val nextWorkingDay = zonedDateTime.nextWorkingDay()

assertThat(nextWorkingDay.dayOfWeek).isEqualTo(DayOfWeek.TUESDAY)
}

@Test
fun `nextWorkingDayOrSame - given ZonedDateTime on weekend - returns ZonedDateTime on next Monday`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-04T10:00:00-04:00[America/New_York]") // Sunday

val nextWorkingDayOrSame = zonedDateTime.nextWorkingDayOrSame()

assertThat(nextWorkingDayOrSame.dayOfWeek).isEqualTo(DayOfWeek.MONDAY)
}

@Test
fun `nextWorkingDayOrSame - given ZonedDateTime on weekday - returns same ZonedDateTime`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-05T10:00:00-04:00[America/New_York]") // Monday

val nextWorkingDayOrSame = zonedDateTime.nextWorkingDayOrSame()

assertThat(nextWorkingDayOrSame).isEqualTo(zonedDateTime)
}

@Test
fun `nextWorkingHourOrSame - given ZonedDateTime on weekend - returns same ZonedDateTime`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-04T10:00:00-04:00[America/New_York]") // Sunday

val nextWorkingHourOrSame = zonedDateTime.nextWorkingHourOrSame()

assertThat(nextWorkingHourOrSame).isEqualTo(zonedDateTime)
}

@Test
fun `nextWorkingHourOrSame - given ZonedDateTime on weekday during working hours - returns same ZonedDateTime`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-05T10:00:00-04:00[America/New_York]") // Monday

val nextWorkingHourOrSame = zonedDateTime.nextWorkingHourOrSame()

assertThat(nextWorkingHourOrSame).isEqualTo(zonedDateTime)
}

@Test
fun `nextWorkingHourOrSame - given ZonedDateTime on weekday after working hours - returns ZonedDateTime at start of next working day`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-05T18:00:00-04:00[America/New_York]") // Monday

val nextWorkingHourOrSame = zonedDateTime.nextWorkingHourOrSame()

assertThat(nextWorkingHourOrSame.dayOfWeek).isEqualTo(DayOfWeek.TUESDAY)
assertThat(nextWorkingHourOrSame.hour).isEqualTo(9)
}

@Test
fun `nextWorkingHourOrSame - given ZonedDateTime on weekday before working hours - returns ZonedDateTime at start of same working day`() {
val zonedDateTime: ZonedDateTime = ZonedDateTime.parse("2022-09-05T06:00:00-04:00[America/New_York]") // Monday

val nextWorkingHourOrSame = zonedDateTime.nextWorkingHourOrSame()

assertThat(nextWorkingHourOrSame.dayOfWeek).isEqualTo(DayOfWeek.MONDAY)
assertThat(nextWorkingHourOrSame.hour).isEqualTo(9)
}

// endregion Tests by AI
}

0 comments on commit 3947677

Please sign in to comment.