/tst/org/diffkit/db/tst/TestH2Load.groovy

http://diffkit.googlecode.com/ · Groovy · 143 lines · 99 code · 26 blank · 18 comment · 9 complexity · 48f33c2d1c345cd3dd68a1f46db35035 MD5 · raw file

  1. /**
  2. * Copyright 2010-2011 Joseph Panico
  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.diffkit.db.tst
  17. import org.diffkit.db.DKDBConnectionInfo
  18. import org.diffkit.db.DKDatabase
  19. import org.diffkit.db.DKDBFlavor
  20. import org.diffkit.db.DKDBH2Loader
  21. import org.diffkit.db.DKDBTableDataAccess;
  22. import org.diffkit.util.DKResourceUtil;
  23. import org.diffkit.util.DKSqlUtil;
  24. import groovy.util.GroovyTestCase;
  25. /**
  26. * @author jpanico
  27. */
  28. public class TestH2Load extends GroovyTestCase {
  29. public void testLoader(){
  30. DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
  31. println "connectionInfo->$connectionInfo"
  32. DKDatabase connectionSource = [connectionInfo]
  33. def connection = connectionSource.connection
  34. println "connection->$connection"
  35. assert connection
  36. DKDBTableDataAccess tableDataAccess = [connectionSource]
  37. def table = tableDataAccess.getTable(null, null, 'CUSTOMER')
  38. if(table )
  39. DKSqlUtil.executeUpdate('DROP TABLE CUSTOMER', connection)
  40. this.createTable(connection)
  41. def csvFile = this.getCsvFile()
  42. def loader = new DKDBH2Loader(connectionSource)
  43. table = tableDataAccess.getTable(null, null, 'CUSTOMER')
  44. assert table
  45. assert loader.load( table, csvFile)
  46. def rows = DKSqlUtil.readRows('SELECT * FROM CUSTOMER', connection)
  47. assert rows
  48. println "rows->$rows"
  49. assert rows.size() ==2
  50. def rob = rows.find { it['FIRST_NAME'] == 'rob'}
  51. assert rob
  52. assert rob['LAST_NAME'] == 'smith'
  53. assert rob['AGE'] == 50
  54. assert DKSqlUtil.executeUpdate('DROP TABLE CUSTOMER', connection)
  55. DKSqlUtil.close(connection)
  56. }
  57. public void testLoad(){
  58. DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
  59. println "connectionInfo->$connectionInfo"
  60. DKDatabase connectionSource = [connectionInfo]
  61. def connection = connectionSource.connection
  62. println "connection->$connection"
  63. assert connection
  64. this.createTable(connection)
  65. def csvFile = this.getCsvFile()
  66. def insertSql = "INSERT INTO customer (SELECT * FROM CSVREAD('${csvFile.getAbsolutePath()}') );"
  67. println "insertSql->$insertSql"
  68. DKSqlUtil.executeUpdate( insertSql, connection)
  69. def rows = DKSqlUtil.readRows('SELECT * FROM customer', connection)
  70. assert rows
  71. println "rows->$rows"
  72. assert rows.size() ==2
  73. assert rows.find { it['FIRST_NAME'] == 'rob'}
  74. assert rows.find { it['LAST_NAME'] == 'jobs'}
  75. assert DKSqlUtil.executeUpdate('DROP TABLE customer', connection)
  76. }
  77. public void testReadFromCSV(){
  78. def csvFile = this.getCsvFile()
  79. def selectSql = "SELECT * FROM CSVREAD('${csvFile.getAbsolutePath()}');"
  80. println "selectSql->$selectSql"
  81. DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
  82. println "connectionInfo->$connectionInfo"
  83. DKDatabase connectionSource = [connectionInfo]
  84. def connection = connectionSource.connection
  85. println "connection->$connection"
  86. assert connection
  87. def rows = DKSqlUtil.readRows(selectSql, connection)
  88. assert rows
  89. println "rows->$rows"
  90. assert rows.size() == 2
  91. assert rows.find { it['FIRST_NAME'] == 'rob'}
  92. assert rows.find { it['LAST_NAME'] == 'jobs'}
  93. }
  94. private File getCsvFile(){
  95. def csvFile = DKResourceUtil.findResourceAsFile('org/diffkit/db/tst/customers.csv')
  96. println "csvFile->$csvFile"
  97. assert csvFile
  98. return csvFile
  99. }
  100. private void createTable(def connection_) {
  101. def createTableSql =
  102. """CREATE TABLE customer
  103. ( first_name varchar(50),
  104. last_name varchar(50),
  105. address varchar(50),
  106. city varchar(50),
  107. country varchar(25),
  108. age integer,
  109. birth DATE,
  110. now TIMESTAMP )
  111. """
  112. assert DKSqlUtil.executeUpdate(createTableSql, connection_)
  113. }
  114. private void dropTable(def connection_){
  115. assert DKSqlUtil.executeUpdate('DROP TABLE customer', connection_)
  116. }
  117. }