47 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
			
		
		
	
	
			47 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			Kotlin
		
	
	
	
	
	
| package solutions.bitbadger.documents
 | |
| 
 | |
| import org.junit.jupiter.api.DisplayName
 | |
| import kotlin.test.Test
 | |
| import kotlin.test.assertEquals
 | |
| import kotlin.test.assertNotSame
 | |
| import kotlin.test.assertSame
 | |
| 
 | |
| class ParametersTest {
 | |
| 
 | |
|     @Test
 | |
|     @DisplayName("nameFields works with no changes")
 | |
|     fun nameFieldsNoChange() {
 | |
|         val fields = listOf(Field.equal("a", "", ":test"), Field.exists("q"), Field.equal("b", "", ":me"))
 | |
|         val named = Parameters.nameFields(fields)
 | |
|         assertEquals(fields.size, named.size, "There should have been 3 fields in the list")
 | |
|         assertSame(fields.elementAt(0), named.elementAt(0), "The first field should be the same")
 | |
|         assertSame(fields.elementAt(1), named.elementAt(1), "The second field should be the same")
 | |
|         assertSame(fields.elementAt(2), named.elementAt(2), "The third field should be the same")
 | |
|     }
 | |
| 
 | |
|     @Test
 | |
|     @DisplayName("nameFields works when changing fields")
 | |
|     fun nameFieldsChange() {
 | |
|         val fields = listOf(
 | |
|             Field.equal("a", ""), Field.equal("e", "", ":hi"), Field.equal("b", ""), Field.notExists("z"))
 | |
|         val named = Parameters.nameFields(fields)
 | |
|         assertEquals(fields.size, named.size, "There should have been 4 fields in the list")
 | |
|         assertNotSame(fields.elementAt(0), named.elementAt(0), "The first field should not be the same")
 | |
|         assertEquals(":field0", named.elementAt(0).parameterName, "First parameter name incorrect")
 | |
|         assertSame(fields.elementAt(1), named.elementAt(1), "The second field should be the same")
 | |
|         assertNotSame(fields.elementAt(2), named.elementAt(2), "The third field should not be the same")
 | |
|         assertEquals(":field1", named.elementAt(2).parameterName, "Third parameter name incorrect")
 | |
|         assertSame(fields.elementAt(3), named.elementAt(3), "The fourth field should be the same")
 | |
|     }
 | |
| 
 | |
|     @Test
 | |
|     @DisplayName("replaceNamesInQuery replaces successfully")
 | |
|     fun replaceNamesInQuery() {
 | |
|         val parameters = listOf(Parameter(":data", ParameterType.JSON, "{}"),
 | |
|             Parameter(":data_ext", ParameterType.STRING, ""))
 | |
|         val query = "SELECT data, data_ext FROM tbl WHERE data = :data AND data_ext = :data_ext AND more_data = :data"
 | |
|         assertEquals("SELECT data, data_ext FROM tbl WHERE data = ? AND data_ext = ? AND more_data = ?",
 | |
|             Parameters.replaceNamesInQuery(query, parameters), "Parameters not replaced correctly")
 | |
|     }
 | |
| }
 |