Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Commit

Permalink
fix and add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
bayusuryadana committed Aug 16, 2023
1 parent 3a8673e commit 2322362
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/main/scala/com/seanmcapp/service/MamenService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import scala.concurrent.Future

class MamenService(stallRepo: StallRepo, googleClient: GoogleClient) {

//$COVERAGE-OFF$
def fetch(): Future[Seq[Int]] = {
for {
stalls <- stallRepo.getAll
Expand All @@ -19,6 +20,7 @@ class MamenService(stallRepo: StallRepo, googleClient: GoogleClient) {
updatedStalls <- stallRepo.update(fetchedStalls)
} yield updatedStalls
}
//$COVERAGE-ON$

def search(request: MamenRequest): Future[Seq[Stall]] = {
for {
Expand Down
85 changes: 85 additions & 0 deletions src/test/scala/com/seanmcapp/external/GoogleClientSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.seanmcapp.external

import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito
import org.mockito.Mockito.when
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.matchers.should.Matchers

class GoogleClientSpec extends AnyWordSpec with Matchers {

"fetchLatLng" in {
val http = Mockito.mock(classOf[HttpRequestClient])
val googleClient = new GoogleClient(http)
val response =
s"""
|{
| "results" : [
| {
| "address_components" : [
| {
| "long_name" : "M9WC+GJ",
| "short_name" : "M9WC+GJ",
| "types" : [ "plus_code" ]
| },
| {
| "long_name" : "Benteng Pasar Atas",
| "short_name" : "Benteng Pasar Atas",
| "types" : [ "administrative_area_level_4", "political" ]
| },
| {
| "long_name" : "Guguk Panjang",
| "short_name" : "Guguk Panjang",
| "types" : [ "administrative_area_level_3", "political" ]
| },
| {
| "long_name" : "Bukittinggi City",
| "short_name" : "Bukittinggi City",
| "types" : [ "administrative_area_level_2", "political" ]
| },
| {
| "long_name" : "West Sumatra",
| "short_name" : "West Sumatra",
| "types" : [ "administrative_area_level_1", "political" ]
| },
| {
| "long_name" : "Indonesia",
| "short_name" : "ID",
| "types" : [ "country", "political" ]
| }
| ],
| "formatted_address" : "M9WC+GJ, Benteng Pasar Atas, Guguk Panjang, Bukittinggi City, West Sumatra, Indonesia",
| "geometry" : {
| "location" : {
| "lat" : -0.3036875,
| "lng" : 100.3715625
| },
| "location_type" : "GEOMETRIC_CENTER",
| "viewport" : {
| "northeast" : {
| "lat" : -0.302338519708498,
| "lng" : 100.3729114802915
| },
| "southwest" : {
| "lat" : -0.305036480291502,
| "lng" : 100.3702135197085
| }
| }
| },
| "place_id" : "ElVNOVdDK0dKLCBCZW50ZW5nIFBhc2FyIEF0YXMsIEd1Z3VrIFBhbmphbmcsIEJ1a2l0dGluZ2dpIENpdHksIFdlc3QgU3VtYXRyYSwgSW5kb25lc2lhIiY6JAoKDTWp0f8VKXzTOxAKGhQKEgmZvt0bozjVLxECR9Uuay8ybQ",
| "plus_code" : {
| "compound_code" : "M9WC+GJ Benteng Pasar Atas, Bukittinggi City, West Sumatra, Indonesia",
| "global_code" : "6PF2M9WC+GJ"
| },
| "types" : [ "street_address" ]
| }
| ],
| "status" : "OK"
|}
|""".stripMargin
when(http.sendGetRequest(any(), any())).thenReturn(response)
val expected = (Some(-0.3036875),Some(100.3715625))
googleClient.fetchLatLng("") shouldBe expected
}

}
20 changes: 13 additions & 7 deletions src/test/scala/com/seanmcapp/service/MamenServiceSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,30 @@ package com.seanmcapp.service
import com.seanmcapp.external.{GeoFilter, LatLng, MamenFilter, MamenRequest}
import com.seanmcapp.repository.seanmcmamen.{Cities, Stall, StallRepo}
import org.mockito.Mockito
import org.mockito.Mockito.when
import org.mockito.Mockito.{times, verify, when}
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AsyncWordSpec

import scala.concurrent.Future
import scala.concurrent.{Await, Future}
import com.seanmcapp.external.GoogleClient
import org.scalatest.time.Second

import scala.concurrent.duration.Duration

class MamenServiceSpec extends AsyncWordSpec with Matchers {

val stallRepo = Mockito.mock(classOf[StallRepo])
val googleClient = Mockito.mock(classOf[GoogleClient])

val stallRepoData = Seq(
Stall(1, "Ayam Suharti", "7JPF+GX2, Ps. Gn. Sitoli, Kec. Gunungsitoli, Kota Gunungsitoli, Sumatera Utara", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67), None),
Stall(2, "Rm. Sederhana", "MRQJ+WR9, Aek Tolang, Kec. Pandan, Kabupaten Tapanuli Tengah, Sumatera Utara 22611", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67), None),
Stall(3, "Warteg Bahari", "39VX+XG Gunung Sarik, Padang City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67), None),
Stall(4, "Sate Madura Pak Kumis", "PJM8+72 Limbukan, Payakumbuh City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67), None)
Stall(1, "Ayam Suharti", "7JPF+GX2, Ps. Gn. Sitoli, Kec. Gunungsitoli, Kota Gunungsitoli, Sumatera Utara", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67)),
Stall(2, "Rm. Sederhana", "MRQJ+WR9, Aek Tolang, Kec. Pandan, Kabupaten Tapanuli Tengah, Sumatera Utara 22611", Cities.Jakarta, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-6.23), Some(106.67)),
Stall(3, "Warteg Bahari", "39VX+XG Gunung Sarik, Padang City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67)),
Stall(4, "Sate Madura Pak Kumis", "PJM8+72 Limbukan, Payakumbuh City, West Sumatra, Indonesia", Cities.Surabaya, "https://goo.gl/maps/dxcXghzHrhn96AZy6", "https://youtube.com", Some(-8.23), Some(110.67))
)
when(stallRepo.getAll).thenReturn(Future.successful(stallRepoData))

val mamenService = new MamenService(stallRepo)
val mamenService = new MamenService(stallRepo, googleClient)
val mamenRequest = MamenRequest(MamenFilter())

"search by name should return correct result" in {
Expand Down

0 comments on commit 2322362

Please sign in to comment.