Skip to content

Commit

Permalink
Add a top-level function allowing to create a an Insert with the DSL
Browse files Browse the repository at this point in the history
  • Loading branch information
jnizet committed Jun 2, 2016
1 parent 529c683 commit d665e20
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.ninja_squad.dbsetup_kotlin

import com.ninja_squad.dbsetup.DbSetup
import com.ninja_squad.dbsetup.operation.Insert

/**
* Top-level function allowing to create a DbSetup by passing a lambda expression configuring it.
Expand Down Expand Up @@ -54,3 +55,24 @@ fun dbSetup(configure: DbSetupBuilder.() -> Unit): DbSetup {
builder.configure()
return builder.build()
}

/**
* Top-level function allowing to create an Insert operation by passing a lambda expression configuring it.
*
* Example usage:
*
* ```
* val insertUsers = insertInto("user") {
* columns("id", "name")
* values(1, "John Doe")
* values(2, "Jane Doe")
* }
* ```
*
* @author JB Nizet
*/
fun insertInto(table: String, configure: Insert.Builder.() -> Unit): Insert {
val builder = Insert.into(table)
builder.configure()
return builder.build()
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.ninja_squad.dbsetup_kotlin

import com.ninja_squad.dbsetup.generator.ValueGenerators
import com.ninja_squad.dbsetup.operation.Insert
import org.junit.Assert.assertEquals
import org.junit.Test

Expand All @@ -11,28 +10,28 @@ import org.junit.Test
class InsertBuilderTest {
@Test
fun `should construct an insert with mapped values`() {
val insert = Insert.into("user")
.mappedValues("id" to 1L, "name" to "John")
.build();
val insert = insertInto("user") {
mappedValues("id" to 1L, "name" to "John")
}

val expected = Insert.into("user")
.values(mapOf("id" to 1L, "name" to "John"))
.build();
val expected = insertInto("user") {
values(mapOf("id" to 1L, "name" to "John"))
}

assertEquals(expected, insert);
}

@Test
fun `should construct an insert with repeating mapped values`() {
val insert = Insert.into("user")
.withGeneratedValue("id", ValueGenerators.sequence())
.repeatingMappedValues("name" to "John").times(10)
.build();
val insert = insertInto("user") {
withGeneratedValue("id", ValueGenerators.sequence())
repeatingMappedValues("name" to "John").times(10)
}

val expected = Insert.into("user")
.withGeneratedValue("id", ValueGenerators.sequence())
.repeatingValues(mapOf("name" to "John")).times(10)
.build();
val expected = insertInto("user") {
withGeneratedValue("id", ValueGenerators.sequence())
repeatingValues(mapOf("name" to "John")).times(10)
}

assertEquals(expected, insert);
}
Expand Down

0 comments on commit d665e20

Please sign in to comment.