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

Commit

Permalink
Tests for query builder, fix count
Browse files Browse the repository at this point in the history
  • Loading branch information
kevcodez committed Feb 3, 2021
1 parent d986e94 commit 4e98662
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
id "com.jfrog.bintray" version "1.8.5"
}

String currentVersion = '0.1.0'
String currentVersion = '0.2.0'

group = 'io.supabase'
version = currentVersion
Expand Down
12 changes: 12 additions & 0 deletions src/main/kotlin/io/supabase/postgrest/builder/PostgrestBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ open class PostgrestBuilder<T : Any> {
return searchParams
}

fun getBody(): Any? {
return this.body
}

fun getHeaders(): Map<String, String> {
return this.headers
}

fun getMethod(): Method? {
return this.method
}

fun execute(): PostgrestHttpResponse {
checkNotNull(method) { "Method cannot be null" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import java.net.URI
class PostgrestQueryBuilder<T : Any>(url: URI, postgrestHttpClient: PostgrestHttpClient, jsonConverter: PostgrestJsonConverter, defaultHeaders: Map<String, String>, schema: String?)
: PostgrestBuilder<T>(url, postgrestHttpClient, jsonConverter, defaultHeaders, schema) {

companion object {
const val HEADER_PREFER = "Prefer"
}

/**
* Performs vertical filtering with SELECT.
*
Expand All @@ -20,18 +24,18 @@ class PostgrestQueryBuilder<T : Any>(url: URI, postgrestHttpClient: PostgrestHtt
head: Boolean = false,
count: Count? = null
): PostgrestFilterBuilder<T> {
setMethod(Method.GET)
if (head) {
setMethod(Method.HEAD)
} else {
setMethod(Method.GET)
}

val cleanedColumns = cleanColumns(columns)

setSearchParam("select", cleanedColumns)

if (count != null) {
setHeader("Prefer", "count=${count.identifier}")
}

if (head) {
setMethod(Method.HEAD)
setHeader(HEADER_PREFER, "count=${count.identifier}")
}

return PostgrestFilterBuilder(this)
Expand All @@ -55,10 +59,10 @@ class PostgrestQueryBuilder<T : Any>(url: URI, postgrestHttpClient: PostgrestHtt
setBody(values)

if (count != null) {
preferHeaders.add("count=${count}")
preferHeaders.add("count=${count.identifier}")
}

setHeader("Prefer", preferHeaders.joinToString(","))
setHeader(HEADER_PREFER, preferHeaders.joinToString(","))

return PostgrestFilterBuilder(this)
}
Expand Down Expand Up @@ -88,9 +92,9 @@ class PostgrestQueryBuilder<T : Any>(url: URI, postgrestHttpClient: PostgrestHtt
val prefersHeaders = mutableListOf("return=${returning.identifier}")

if (count != null) {
prefersHeaders.add("count=${count}")
prefersHeaders.add("count=${count.identifier}")
}
setHeader("Prefer", prefersHeaders.joinToString(","))
setHeader(HEADER_PREFER, prefersHeaders.joinToString(","))

return PostgrestFilterBuilder(this)
}
Expand All @@ -105,9 +109,9 @@ class PostgrestQueryBuilder<T : Any>(url: URI, postgrestHttpClient: PostgrestHtt

val prefersHeaders = mutableListOf("return=${returning.identifier}")
if (count != null) {
prefersHeaders.add("count=${count}")
prefersHeaders.add("count=${count.identifier}")
}
setHeader("Prefer", prefersHeaders.joinToString(","))
setHeader(HEADER_PREFER, prefersHeaders.joinToString(","))

return PostgrestFilterBuilder(this)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package io.supabase.postgrest.builder

import assertk.assertThat
import assertk.assertions.isEqualTo
import io.mockk.mockk
import org.apache.hc.core5.http.Method
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.net.URI

internal class PostgrestQueryBuilderTest {

private var queryBuilder: PostgrestQueryBuilder<Any>? = null

@BeforeEach
fun beforeEach() {
queryBuilder = PostgrestQueryBuilder(URI("123"), mockk(), mockk(), emptyMap(), null)
}

@Nested
inner class Select {

@Test
fun `basic select`() {
queryBuilder!!.select(columns = "*")
assertMethod(Method.GET)
assertSearchParam("select", "*")
}

@Test
fun `select with head`() {
queryBuilder!!.select(head = true)
assertMethod(Method.HEAD)
}

@Test
fun `select with count`() {
queryBuilder!!.select(count = Count.PLANNED)
assertHeader("Prefer", "count=planned")
}
}

@Nested
inner class Insert {

private val value = mapOf("foo" to "bar")

@Test
fun `insert single`() {
queryBuilder!!.insert(value = value)
assertMethod(Method.POST)
assertBody(listOf(value))
}

@Test
fun `insert multiple`() {
val values = listOf(value, value)
queryBuilder!!.insert(values = values)
assertBody(values)
}

@Test
fun `insert with count`() {
queryBuilder!!.insert(value = value, count = Count.ESTIMATED)
assertHeader("Prefer", "return=representation,count=estimated")
}

@Test
fun `insert with different returning`() {
queryBuilder!!.insert(value = value, returning = Returning.MINIMAL)
assertHeader("Prefer", "return=minimal")
}

@Test
fun `upsert`() {
queryBuilder!!.insert(value = value, upsert = true)
assertHeader("Prefer", "return=representation,resolution=merge-duplicates")
}

@Test
fun `upsert with count`() {
queryBuilder!!.insert(value = value, upsert = true, count = Count.EXACT)
assertHeader("Prefer", "return=representation,resolution=merge-duplicates,count=exact")
}

@Test
fun `upsert with onconflict`() {
queryBuilder!!.insert(value = value, upsert = true, onConflict = "foobar")
assertHeader("Prefer", "return=representation,resolution=merge-duplicates")
assertSearchParam("on_conflict", "foobar")
}
}

@Nested
inner class Update {

private val value = mapOf("foo" to "bar")

@Test
fun `update`() {
queryBuilder!!.update(value = value)
assertMethod(Method.PATCH)
}

@Test
fun `update with count`() {
queryBuilder!!.update(value = value, count = Count.EXACT)
assertHeader("Prefer", "return=representation,count=exact")
}

@Test
fun `insert with different returning`() {
queryBuilder!!.update(value = value, returning = Returning.MINIMAL)
assertHeader("Prefer", "return=minimal")
}
}

@Nested
inner class Delete {

@Test
fun `delete`() {
queryBuilder!!.delete()
assertMethod(Method.DELETE)
}

@Test
fun `delete with count`() {
queryBuilder!!.delete(count = Count.PLANNED)
assertHeader("Prefer", "return=representation,count=planned")
}

@Test
fun `delete with different returning`() {
queryBuilder!!.delete(returning = Returning.MINIMAL)
assertHeader("Prefer", "return=minimal")
}
}

@Nested
inner class Rpc {

@Test
fun `rpc without body`() {
queryBuilder!!.rpc(null)
assertMethod(Method.POST)
assertBody(null)
}

@Test
fun `rpc with body`() {
val params = mapOf("foo" to "bar")
queryBuilder!!.rpc(params)
assertBody(params)
}
}

private fun assertHeader(name: String, value: String) {
assertThat(queryBuilder!!.getHeaders()[name]).isEqualTo(value)
}

private fun assertMethod(method: Method) {
assertThat(queryBuilder!!.getMethod()).isEqualTo(method)
}

private fun assertBody(body: Any?) {
assertThat(queryBuilder!!.getBody()).isEqualTo(body)
}

private fun assertSearchParam(name: String, value: String) {
val searchParams = queryBuilder!!.getSearchParams()
assertThat(searchParams[name]).isEqualTo(value)
}

}

0 comments on commit 4e98662

Please sign in to comment.