/tst/org/diffkit/db/tst/TestDBTable.groovy

http://diffkit.googlecode.com/ · Groovy · 149 lines · 107 code · 20 blank · 22 comment · 11 complexity · e6012732c7b89cae7e47f1f18be550e7 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 java.sql.Timestamp;
  18. import java.util.Calendar;
  19. import org.apache.commons.lang.time.DateUtils;
  20. import org.diffkit.db.DKDBColumn;
  21. import org.diffkit.db.DKDBConnectionInfo
  22. import org.diffkit.db.DKDatabase
  23. import org.diffkit.db.DKDBFlavor;
  24. import org.diffkit.db.DKDBPrimaryKey
  25. import org.diffkit.db.DKDBTable
  26. import org.diffkit.db.DKDBTableDataAccess;
  27. import org.diffkit.util.DKSqlUtil;
  28. import groovy.util.GroovyTestCase;
  29. /**
  30. * @author jpanico
  31. */
  32. public class TestDBTable extends GroovyTestCase {
  33. public void testKeyValues() {
  34. DKDBTable table = this.createCustomerMetaTable()
  35. assert table
  36. Object[] row = ['bob', 'smith', 'addr1', 'city', 'country', 55]
  37. def keyValues = table.getPrimaryKeyValues(row)
  38. assert keyValues
  39. assert keyValues.length ==2
  40. assert keyValues[0] == 'bob'
  41. assert keyValues[1] == 'smith'
  42. }
  43. public void testPrimaryKeyColumns() {
  44. DKDBTable table = this.createCustomerMetaTable()
  45. assert table
  46. def primaryKeyColumns = table.getPrimaryKeyColumns()
  47. assert primaryKeyColumns
  48. assert primaryKeyColumns.length ==2
  49. assert primaryKeyColumns[0].name == 'first_name'
  50. assert primaryKeyColumns[1].name == 'last_name'
  51. }
  52. public void testCreateRowMap() {
  53. DKDBTable table = this.createCustomerMetaTable()
  54. assert table
  55. Object[] row = ['bob', 'smith', 'addr1', 'city', 'country', 55]
  56. def rowMap = table.createRowMap( row)
  57. println "rowMap->$rowMap"
  58. assert rowMap == ['first_name':'bob', 'last_name':'smith', 'address':'addr1', 'city':'city', 'country':'country', 'age':55]
  59. }
  60. /**
  61. * are MEM tables specific to a single connection (like temp tables),
  62. * or are they visible across connections?
  63. */
  64. public void testH2MemTable(){
  65. DKDBConnectionInfo connectionInfo = ['test',DKDBFlavor.H2,"mem:test;DB_CLOSE_DELAY=-1;TRACE_LEVEL_SYSTEM_OUT=2", null, null, 'test', 'test']
  66. println "connectionInfo->$connectionInfo"
  67. DKDatabase database = [connectionInfo]
  68. DKDBTableDataAccess tableDataAccess = [database]
  69. DKDBTable table = this.createCustomerMetaTable()
  70. if(database.tableExists(table))
  71. database.dropTable(table)
  72. def connection = database.connection
  73. assert database.createTable( table)
  74. DKSqlUtil.close(connection)
  75. def fetchedTable = tableDataAccess.getTable(null, null, table.tableName)
  76. assert fetchedTable
  77. assert database.dropTable( fetchedTable)
  78. fetchedTable = tableDataAccess.getTable(null, null, table.tableName)
  79. assert !fetchedTable
  80. }
  81. public void testInsert(){
  82. DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
  83. println "connectionInfo->$connectionInfo"
  84. DKDatabase database = [connectionInfo]
  85. def table = this.createContextMetaTable()
  86. DKDBTableDataAccess tableDataAccess = [database]
  87. def connection = database.connection
  88. assert database.createTable( table)
  89. def fetchedTable = tableDataAccess.getTable(null, null, table.tableName)
  90. assert fetchedTable
  91. def date = DateUtils.round(new Date(10000), Calendar.DAY_OF_MONTH)
  92. def row = [ID:1000, LHS_SOURCE: 'lhs source', RHS_SOURCE: 'rhs source', WHEN: new Timestamp(date.time), RUN_DATE: date ]
  93. assert database.insertRow(row, fetchedTable)
  94. def fetchedRows = database.readAllRows( fetchedTable)
  95. println "fetchedRows->$fetchedRows"
  96. assert fetchedRows
  97. assert fetchedRows.size() == 1
  98. assert fetchedRows[0]['LHS_SOURCE'] == row['LHS_SOURCE']
  99. assert fetchedRows[0]['ID'] == row['ID']
  100. assert fetchedRows[0]['WHEN'] == row['WHEN']
  101. assert fetchedRows[0]['RUN_DATE'] == row['RUN_DATE']
  102. assert database.dropTable( table)
  103. fetchedTable = tableDataAccess.getTable(null, null, table.tableName)
  104. assert !fetchedTable
  105. }
  106. private DKDBTable createContextMetaTable() {
  107. DKDBColumn column1 = ['ID', 1, 'BIGINT', -1, false]
  108. DKDBColumn column2 = ['LHS_SOURCE', 2, 'VARCHAR', -1, true]
  109. DKDBColumn column3 = ['RHS_SOURCE', 3, 'VARCHAR', -1, true]
  110. DKDBColumn column4 = ['WHEN', 4, 'TIMESTAMP', -1, true]
  111. DKDBColumn column5 = ['RUN_DATE', 5, 'DATE', -1, true]
  112. DKDBColumn[] columns = [column1, column2, column3, column4, column5]
  113. DKDBTable table = [null, null, 'DIFF_CONTEXT', columns, null]
  114. return table
  115. }
  116. private DKDBTable createCustomerMetaTable(){
  117. DKDBColumn column1 = ['first_name', 1, 'VARCHAR', 20, true]
  118. DKDBColumn column2 = ['last_name', 2, 'VARCHAR', -1, true]
  119. DKDBColumn column3 = ['address', 2, 'VARCHAR', -1, true]
  120. DKDBColumn column4 = ['city', 2, 'VARCHAR', -1, true]
  121. DKDBColumn column5 = ['country', 2, 'VARCHAR', -1, true]
  122. DKDBColumn column6 = ['age', 2, 'INTEGER', -1, true]
  123. DKDBColumn[] columns = [column1, column2, column3, column4, column5, column6]
  124. String[] pkColNames = ['first_name', 'last_name']
  125. DKDBPrimaryKey pk = ['pk_customer', pkColNames]
  126. DKDBTable table = [null, null, 'CUSTOMER', columns, pk]
  127. return table
  128. }
  129. }