/org.scalaprimavera.jdbc/src/test/scala/org/scalaprimavera/jdbc/core/simple/ScalaSimpleJdbcOperationsTest.scala

https://github.com/cobaltolabs/ScalaPrimavera · Scala · 285 lines · 184 code · 54 blank · 47 comment · 0 complexity · 684d3e67b3943513659f63e093faabd9 MD5 · raw file

  1. /*
  2. * Copyright 2009 - 2011 Cobalto Labs SAS
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.scalaprimavera.jdbc.core.simple
  17. import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
  18. import org.scalatest.testng.TestNGSuite
  19. import org.scalatest.matchers.ShouldMatchers
  20. import org.springframework.test.context.ContextConfiguration
  21. import org.testng.annotations.Test
  22. import org.springframework.beans.factory.annotation.Autowired
  23. import org.springframework.dao.EmptyResultDataAccessException
  24. import collection.immutable.Map
  25. import org.scalaprimavera.jdbc.TestBean
  26. import java.sql.ResultSet
  27. import org.springframework.jdbc.core.namedparam.MapSqlParameterSource
  28. import org.scalaprimavera.jdbc.TestJdbcHelper._
  29. /**
  30. * Created by IntelliJ IDEA.
  31. * User: mario
  32. * Date: 19/10/11
  33. * Time: 23:30
  34. */
  35. @ContextConfiguration
  36. class ScalaSimpleJdbcOperationsTest extends AbstractTransactionalTestNGSpringContextTests with TestNGSuite with ShouldMatchers {
  37. @Autowired
  38. var template: ScalaSimpleJdbcOperations = _
  39. @Test
  40. def testGetOperations() {
  41. template.getSimpleJdbcOperations should not be null
  42. }
  43. @Test
  44. def testQueryForInt() {
  45. //Map
  46. template.queryForInt(selectIdWithNamedParams, Map("description" -> "python")) should be(1)
  47. intercept[EmptyResultDataAccessException] {
  48. template.queryForInt(selectIdWithNamedParams, Map("description" -> "cobol"))
  49. }
  50. //Source
  51. template.queryForInt(selectIdWithNamedParams, new MapSqlParameterSource("description", "python")) should be(1)
  52. intercept[EmptyResultDataAccessException] {
  53. template.queryForInt(selectIdWithNamedParams, new MapSqlParameterSource("description", "cobol"))
  54. }
  55. //with params
  56. template.queryForInt(selectId, "python") should be(1)
  57. //without params
  58. template.queryForInt("select id from test_bean where description = 'python'") should be(1)
  59. intercept[EmptyResultDataAccessException] {
  60. template.queryForInt(selectId, "cobol")
  61. }
  62. }
  63. @Test
  64. def testQueryForOptionInt() {
  65. //Map
  66. template.queryForOptionInt(selectIdWithNamedParams, Map("description" -> "python")).get should be(1)
  67. template.queryForOptionInt(selectIdWithNamedParams, Map("description" -> "cobol")) should be(None)
  68. //Source
  69. template.queryForOptionInt(selectIdWithNamedParams, new MapSqlParameterSource("description", "python")).get should be(1)
  70. template.queryForOptionInt(selectIdWithNamedParams, new MapSqlParameterSource("description", "cobol")) should be(None)
  71. //Any*
  72. template.queryForOptionInt(selectId, "python").get should be(1)
  73. template.queryForOptionInt(selectId, "cobol") should be(None)
  74. }
  75. @Test
  76. def testQueryForLong() {
  77. //map
  78. template.queryForLong(selectIdWithNamedParams, Map("description" -> "python")) should be(1)
  79. intercept[EmptyResultDataAccessException] {
  80. template.queryForLong(selectIdWithNamedParams, Map("description" -> "cobol"))
  81. }
  82. //source
  83. template.queryForLong(selectIdWithNamedParams, new MapSqlParameterSource("description", "python")) should be(1)
  84. intercept[EmptyResultDataAccessException] {
  85. template.queryForLong(selectIdWithNamedParams, new MapSqlParameterSource("description", "cobol"))
  86. }
  87. //with params
  88. template.queryForLong(selectId, "python") should be(1)
  89. //without params
  90. template.queryForLong("select id from test_bean where description = 'python'") should be(1)
  91. intercept[EmptyResultDataAccessException] {
  92. template.queryForLong(selectId, "cobol")
  93. }
  94. }
  95. @Test
  96. def testQueryForOptionLong() {
  97. //Map
  98. template.queryForOptionLong(selectIdWithNamedParams, Map("description" -> "python")).get should be(1)
  99. template.queryForOptionLong(selectIdWithNamedParams, Map("description" -> "cobol")) should be(None)
  100. //Source
  101. template.queryForOptionLong(selectIdWithNamedParams, new MapSqlParameterSource("description", "python")).get should be(1)
  102. template.queryForOptionLong(selectIdWithNamedParams, new MapSqlParameterSource("description", "cobol")) should be(None)
  103. //Any*
  104. template.queryForOptionLong(selectId, "python").get should be(1)
  105. template.queryForOptionLong(selectId, "cobol") should be(None)
  106. }
  107. @Test
  108. def testQueryForObject() {
  109. //Map
  110. template.queryForObject(selectDescriptionWithNamedParams, classOf[String], Map("id" -> 1)) should be("python")
  111. template.queryForObject(selectWithNamedParams, mapperFunction, Map("id" -> 1)) should have(
  112. 'id(1),
  113. 'description("python")
  114. )
  115. intercept[EmptyResultDataAccessException] {
  116. template.queryForObject(selectDescriptionWithNamedParams, classOf[String], Map("id" -> -1))
  117. }
  118. intercept[EmptyResultDataAccessException] {
  119. template.queryForObject(selectDescriptionWithNamedParams, mapperFunction, Map("id" -> -1))
  120. }
  121. //Source
  122. template.queryForObject(selectDescriptionWithNamedParams, classOf[String], new MapSqlParameterSource("id", 1)) should be("python")
  123. template.queryForObject(selectWithNamedParams, mapperFunction, new MapSqlParameterSource("id", 1)) should have(
  124. 'id(1),
  125. 'description("python")
  126. )
  127. intercept[EmptyResultDataAccessException] {
  128. template.queryForObject(selectDescriptionWithNamedParams, classOf[String], new MapSqlParameterSource("id", -1))
  129. }
  130. intercept[EmptyResultDataAccessException] {
  131. template.queryForObject(selectDescriptionWithNamedParams, mapperFunction, new MapSqlParameterSource("id", -1))
  132. }
  133. //Any*
  134. template.queryForObject(selectDescription, classOf[String], 1) should be("python")
  135. template.queryForObject(selectObject, mapperFunction, 1) should have(
  136. 'id(1),
  137. 'description("python")
  138. )
  139. intercept[EmptyResultDataAccessException] {
  140. template.queryForObject(selectDescription, classOf[String], -1)
  141. }
  142. intercept[EmptyResultDataAccessException] {
  143. template.queryForObject(selectObject, mapperFunction, -1)
  144. }
  145. }
  146. @Test
  147. def testQueryForOption() {
  148. //Map
  149. template.queryForOption(selectDescriptionWithNamedParams, classOf[String], Map("id" -> 1)).get should be("python")
  150. template.queryForOption(selectWithNamedParams, mapperFunction, Map("id" -> 1)).get should have(
  151. 'id(1),
  152. 'description("python")
  153. )
  154. template.queryForOption(selectDescriptionWithNamedParams, classOf[String], Map("id" -> -1)) should be(None)
  155. template.queryForOption(selectDescriptionWithNamedParams, mapperFunction, Map("id" -> -1)) should be(None)
  156. //Source
  157. template.queryForOption(selectDescriptionWithNamedParams, classOf[String], new MapSqlParameterSource("id", 1)).get should be("python")
  158. template.queryForOption(selectWithNamedParams, mapperFunction, new MapSqlParameterSource("id", 1)).get should have(
  159. 'id(1),
  160. 'description("python")
  161. )
  162. template.queryForOption(selectDescriptionWithNamedParams, classOf[String], new MapSqlParameterSource("id", -1)) should be(None)
  163. template.queryForOption(selectWithNamedParams, mapperFunction, new MapSqlParameterSource("id", -1)) should be(None)
  164. //Any*
  165. template.queryForOption(selectDescription, classOf[String], 1).get should be("python")
  166. template.queryForOption(selectObject, mapperFunction, 1).get should have(
  167. 'id(1),
  168. 'description("python")
  169. )
  170. template.queryForOption(selectDescription, classOf[String], -1) should be(None)
  171. template.queryForOption(selectObject, mapperFunction, -1) should be(None)
  172. }
  173. @Test
  174. def testQuery() {
  175. template.query(selectLessThan, mapperFunction, Map("id" -> 4)) should have size 3
  176. template.query(selectLessThan, mapperFunction, new MapSqlParameterSource("id", 4)) should have size 3
  177. template.query(select, mapperFunction) should have size 5
  178. }
  179. @Test
  180. def testQueryForMap() {
  181. //Map
  182. template.queryForMap(selectWithNamedParams, Map("id" -> 1))("DESCRIPTION") should be("python")
  183. intercept[EmptyResultDataAccessException] {
  184. template.queryForMap(selectWithNamedParams, Map("id" -> -1))
  185. }
  186. //Source
  187. template.queryForMap(selectWithNamedParams, new MapSqlParameterSource("id", 1))("DESCRIPTION") should be("python")
  188. intercept[EmptyResultDataAccessException] {
  189. template.queryForMap(selectWithNamedParams, new MapSqlParameterSource("id", -1))
  190. }
  191. //Any*
  192. template.queryForMap(selectObject, 1)("DESCRIPTION") should be("python")
  193. intercept[EmptyResultDataAccessException] {
  194. template.queryForMap(selectObject, -1)
  195. }
  196. }
  197. @Test
  198. def testQueryForOptionMap() {
  199. //Map
  200. template.queryForOptionMap(selectWithNamedParams, Map("id" -> 1)).get("DESCRIPTION") should be("python")
  201. template.queryForOptionMap(selectWithNamedParams, Map("id" -> -1)) should be(None)
  202. //Source
  203. template.queryForOptionMap(selectWithNamedParams, new MapSqlParameterSource("id", 1)).get("DESCRIPTION") should be("python")
  204. template.queryForOptionMap(selectWithNamedParams, new MapSqlParameterSource("id", -1)) should be(None)
  205. //Any*
  206. template.queryForOptionMap(selectObject, 1).get("DESCRIPTION") should be("python")
  207. template.queryForOptionMap(selectObject, -1) should be(None)
  208. }
  209. @Test
  210. def testQueryForList() {
  211. template.queryForList(selectLessThan, Map("id" -> 4)) should have size 3
  212. template.queryForList(selectLessThan, new MapSqlParameterSource("id", 4)) should have size 3
  213. template.queryForList(select) should have size 5
  214. }
  215. private def checkCount(count: Int) {
  216. template.queryForInt(selectCount) should be(count)
  217. }
  218. @Test
  219. def testUpdate() {
  220. template.update(insertWithNamedParams, Map("description" -> "haskell")) should be(1)
  221. template.update(insertWithNamedParams, new MapSqlParameterSource("description", "clojure")) should be(1)
  222. template.update(insert, "erlang") should be(1)
  223. checkCount(8)
  224. }
  225. @Test
  226. def testBatchUpdate() {
  227. template.batchUpdate(deleteWithNamedParams, Seq(new MapSqlParameterSource("id", 1), new MapSqlParameterSource("id", 2)))(0) should be(1)
  228. template.batchUpdate(deleteObject, Seq(Seq(3), Seq(4)), Seq.empty)
  229. checkCount(1)
  230. }
  231. }