Database Integration Test – Persiapan

Ada kalanya, sampai pada titik tertentu, testing akan memerlukan koneksi ke database service. Mari kita jalankan test, dan lihat log nya.

go test -v ./...
=== RUN   TestForm_Has
--- PASS: TestForm_Has (0.00s)
=== RUN   TestForm_Required
--- PASS: TestForm_Required (0.00s)        
=== RUN   TestForm_Check
--- PASS: TestForm_Check (0.00s)
=== RUN   TestForm_ErrorGet
--- PASS: TestForm_ErrorGet (0.00s)        
=== RUN   Test_application_handlers        
--- PASS: Test_application_handlers (0.06s)
=== RUN   TestAppHome
--- PASS: TestAppHome (0.00s)
=== RUN   TestApp_renderWBadTemplate
--- PASS: TestApp_renderWBadTemplate (0.01s)
=== RUN   Test_app_Login
--- PASS: Test_app_Login (1.82s)
=== RUN   Test_application_addIPToContext
    middleware_test.go:41: 192.0.2.1
    middleware_test.go:41: unknown
    middleware_test.go:41: 192.163.1.3
    middleware_test.go:41: hello
--- PASS: Test_application_addIPToContext (0.00s)
=== RUN   Test_application_ipFromContext
--- PASS: Test_application_ipFromContext (0.00s)
=== RUN   Test_app_auth
--- PASS: Test_app_auth (0.00s)
=== RUN   Test_application_routes
--- PASS: Test_application_routes (0.00s)
PASS
ok      webapp/cmd/web  2.397s
?       webapp/pkg/data [no test files]
?       webapp/pkg/repository   [no test files]
?       webapp/pkg/repository/dbrepo    [no test files]

Dapat dilihat, untuk file dbrepbo belum terseia test files. Jika kita analisa, untuk melakukan test diperlukan koneksi kedalam database.

Instalasi Docktertest

Untuk itu kita dapat gunakan third party yang berguna untuk menjalankan docker dalam unit test lalu mematikan service docker setelah test selesai. Third party tersebut adalah dockertest (GitHub – ory/dockertest: Write better integration tests! Dockertest helps you boot up ephermal docker images for your Go tests with minimal work.).

Mari kita install package tersebut, buka command prompt, masuk ke root directory dari aplikasi, lalu jalankan perintah berikut:

$ go get -u github.com/ory/dockertest/v3

Perhatian, gunakan versi yang sama dengan tutorial untuk mencegah error.

Docker Setting

Agar dockertest dapat connect ke Docker daemon, buka docker setting, pilih check box Expose daemon on tcp://localhost:2375 without TLS.

Pada modul selanjutnya kita akan membuat file test untuk dbrepository.

Sharing is caring:

Leave a Comment