/tst/org/diffkit/diff/sns/tst/TestSqlPatchSink.groovy

http://diffkit.googlecode.com/ · Groovy · 120 lines · 85 code · 17 blank · 18 comment · 1 complexity · a0bd285d8c34e6c9eab2f14376bbcd5b 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.diff.sns.tst
  17. import java.sql.Timestamp;
  18. import org.diffkit.db.DKDBColumn
  19. import org.diffkit.db.DKDBConnectionInfo
  20. import org.diffkit.db.DKDBFlavor
  21. import org.diffkit.db.DKDBPrimaryKey
  22. import org.diffkit.db.DKDBTable;
  23. import org.diffkit.db.DKDatabase
  24. import org.diffkit.diff.conf.DKAutomaticTableComparison;
  25. import org.diffkit.diff.engine.DKColumnDiff
  26. import org.diffkit.diff.engine.DKColumnDiffRow
  27. import org.diffkit.diff.engine.DKContext
  28. import org.diffkit.diff.engine.DKRowDiff
  29. import org.diffkit.diff.engine.DKSide;
  30. import org.diffkit.diff.engine.DKTableComparison
  31. import org.diffkit.diff.engine.DKTableModel
  32. import org.diffkit.diff.sns.DKSqlPatchSink
  33. import org.diffkit.diff.sns.DKTableModelUtil;
  34. /**
  35. * @author jpanico
  36. */
  37. public class TestSqlPatchSink extends GroovyTestCase {
  38. public void testSink() {
  39. DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
  40. println "connectionInfo->$connectionInfo"
  41. DKDatabase database = [connectionInfo]
  42. def connection = database.connection
  43. def dbTable = this.createCustomerMetaTable()
  44. assert database.createTable( dbTable)
  45. def writer = new StringWriter()
  46. DKSqlPatchSink sink = [connectionInfo, dbTable.tableName, writer]
  47. DKContext context = []
  48. println "sink->$sink"
  49. println "context->$context"
  50. sink.open(context)
  51. sink.close(context)
  52. assert writer.toString() == ''
  53. DKTableModel tableModel = DKTableModelUtil.createDefaultTableModel(DKDBFlavor.H2, dbTable, null)
  54. assert tableModel
  55. DKTableComparison tableComparison = DKAutomaticTableComparison.createDefaultTableComparison(tableModel, tableModel, null, null)
  56. assert tableModel
  57. Date date = [1000000000000]
  58. Object[] firstRow = ['bob', 'smith', 'update-addr1', 'city', 'update-country', 55, date, new Timestamp(date.time)]
  59. writer = new StringWriter()
  60. sink = [connectionInfo, dbTable.tableName, writer]
  61. context = new DKContext(sink, tableComparison)
  62. context.open()
  63. DKRowDiff diff = [1, firstRow, DKSide.LEFT, tableComparison]
  64. sink.record( diff, context)
  65. Object[] secondRow = ['john', 'candy', 'candy st', 'candy land', 'CANADA', -1, date, new Timestamp(date.time)]
  66. diff = [2, secondRow, DKSide.RIGHT, tableComparison]
  67. sink.record( diff, context)
  68. Object[] thirdRowLeft = ['elton', 'john', 'nyc', 'ny', 'USA', 80, date, new Timestamp(date.time)]
  69. Object[] thirdRowRight = ['elton', 'john', 'new york', 'new york', 'AMERICA', -1, date, new Timestamp(date.time)]
  70. DKColumnDiffRow columnDiffRow = [3, thirdRowLeft, thirdRowRight, tableComparison]
  71. context._columnStep = 3
  72. context._rowStep = 3
  73. context._lhsColumnIdx = 2
  74. context._rhsColumnIdx = 2
  75. DKColumnDiff columnDiff = [columnDiffRow, 3, 'nyc', 'new york']
  76. sink.record( columnDiff, context)
  77. context._columnStep = 4
  78. context._lhsColumnIdx = 3
  79. context._rhsColumnIdx = 3
  80. columnDiff = [columnDiffRow, 3, 'ny', 'new york']
  81. sink.record( columnDiff, context)
  82. context.close()
  83. def patchString = writer.toString()
  84. println "patchString->$patchString"
  85. assert patchString.startsWith("INSERT INTO PUBLIC.CUSTOMER (FIRST_NAME, LAST_NAME, ADDRESS, CITY, COUNTRY, AGE, BIRTH, NOW)\nVALUES ('bob', 'smith', 'update-addr1', 'city', 'update-country', 55, '2001-09-08', {ts '2001-09-08 21:46:40'});\n\nDELETE FROM PUBLIC.CUSTOMER\nWHERE (FIRST_NAME='john' ) AND (LAST_NAME='candy' );\n\nUPDATE PUBLIC.CUSTOMER\nSET ADDRESS='nyc', CITY='ny'\nWHERE (FIRST_NAME='elton' ) AND (LAST_NAME='john' );")
  86. database.dropTable(dbTable)
  87. }
  88. private DKDBTable createCustomerMetaTable(){
  89. DKDBColumn column1 = ['FIRST_NAME', 1, 'VARCHAR', 50, true]
  90. DKDBColumn column2 = ['LAST_NAME', 2, 'VARCHAR', 50, true]
  91. DKDBColumn column3 = ['ADDRESS', 3, 'VARCHAR', 50, true]
  92. DKDBColumn column4 = ['CITY', 4, 'VARCHAR', 50, true]
  93. DKDBColumn column5 = ['COUNTRY', 5, 'VARCHAR', 25, true]
  94. DKDBColumn column6 = ['AGE', 6, 'INTEGER', -1, true]
  95. DKDBColumn column7 = ['BIRTH', 7, 'DATE', -1, true]
  96. DKDBColumn column8 = ['NOW', 8, 'TIMESTAMP', -1, true]
  97. DKDBColumn[] columns = [column1, column2, column3, column4, column5, column6, column7, column8]
  98. String[] pkColNames = ['FIRST_NAME', 'LAST_NAME']
  99. DKDBPrimaryKey pk = ['pk_customer', pkColNames]
  100. DKDBTable table = [ null, null, 'CUSTOMER', columns, pk]
  101. return table
  102. }
  103. }