PageRenderTime 50ms CodeModel.GetById 13ms app.highlight 32ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://diffkit.googlecode.com/
Groovy | 175 lines | 130 code | 26 blank | 19 comment | 6 complexity | 80430e103911f0e94bade2665563c295 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 */
 16package org.diffkit.diff.sns.tst
 17
 18import java.io.File;
 19
 20
 21
 22import org.diffkit.db.DKDBColumn 
 23import org.diffkit.db.DKDBConnectionInfo 
 24import org.diffkit.db.DKDatabase 
 25import org.diffkit.db.DKDBFlavor;
 26import org.diffkit.db.DKDBPrimaryKey;
 27import org.diffkit.db.DKDBTable
 28import org.diffkit.db.DKDBH2Loader
 29import org.diffkit.db.DKDBTableDataAccess;
 30import org.diffkit.diff.engine.DKColumnModel;
 31import org.diffkit.diff.engine.DKTableModel;
 32import org.diffkit.diff.sns.DKDBSource 
 33import org.diffkit.diff.sns.DKTableModelUtil;
 34import org.diffkit.util.DKResourceUtil
 35import org.diffkit.util.DKTimeUtil;
 36
 37import groovy.util.GroovyTestCase;
 38
 39
 40/**
 41 * @author jpanico
 42 */
 43public class TestDBSource extends GroovyTestCase {
 44   
 45   public void testValidateKeyColumnNames() {
 46      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 47      println "connectionInfo->$connectionInfo"
 48      DKDatabase database = [connectionInfo]
 49      def connection = database.connection
 50      def dbTable = this.createCustomerMetaTable()
 51      assert database.createTable( dbTable)
 52      
 53      shouldFail(RuntimeException) {
 54         DKDBSource source = new DKDBSource(dbTable.tableName, null, database, null, (String[])['maiden_name'], null)
 55      }
 56      assert database.dropTable( dbTable)
 57   }
 58   
 59   public void testDescription() {
 60      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 61      println "connectionInfo->$connectionInfo"
 62      DKDatabase database = [connectionInfo]
 63      def connection = database.connection
 64      def dbTable = this.createCustomerMetaTable()
 65      assert database.createTable( dbTable)
 66      
 67      DKDBSource source = new DKDBSource(dbTable.tableName, null, database, null, (String[])['first_name'], null)
 68      println "source->$source"
 69      assert source.description  == 'DKDBSource[tableName=CUSTOMER, whereClause=null, keyColumnNames=[first_name], database=DKDBConnectionInfo[test(flavor=H2,database=mem:test,host=null,port=null)]]'
 70      assert database.dropTable( dbTable)
 71   }
 72   
 73   public void testToString() {
 74      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 75      println "connectionInfo->$connectionInfo"
 76      DKDatabase database = [connectionInfo]
 77      def connection = database.connection
 78      def dbTable = this.createCustomerMetaTable()
 79      assert database.createTable( dbTable)
 80      
 81      DKDBSource source = new DKDBSource(dbTable.tableName, null, database, null, (String[])['first_name'], null)
 82      println "source->$source"
 83      assert source.toString().startsWith( 'DKDBSource')
 84      assert source.toString().endsWith( '[CUSTOMER,jdbc:h2:mem:test]')
 85      assert database.dropTable( dbTable)
 86   }
 87   
 88   public void testKeyColumnNames() {
 89      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 90      println "connectionInfo->$connectionInfo"
 91      DKDatabase database = [connectionInfo]
 92      def connection = database.connection
 93      def dbTable = this.createCustomerMetaTable()
 94      assert database.createTable( dbTable)
 95      
 96      DKDBSource source = new DKDBSource(dbTable.tableName, null, database, null, (String[])['first_name'], null)
 97      def model = source.model
 98      assert model
 99      assert model.key == [0]
100      assert database.dropTable( dbTable)
101   }
102   
103   public void testRead(){
104      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
105      println "connectionInfo->$connectionInfo"
106      DKDatabase database = [connectionInfo]
107      def connection = database.connection
108      def dbTable = this.createCustomerMetaTable()
109      assert database.createTable( dbTable)
110      DKDBH2Loader loader = [database]
111      def DKDBTableDataAccess tableDataAccess = [database]
112      def fetchedTable = tableDataAccess.getTable(dbTable.tableName)
113      def csvFile = this.getCsvFile()
114      assert loader.load(dbTable, csvFile)
115      def tableModel = DKTableModelUtil.createDefaultTableModel(database.flavor,dbTable, null)
116      assert tableModel
117      
118      // goof up the model to force a fail
119      def columns = tableModel.columns
120      columns[0]= new DKColumnModel(0, 'does_not_exist', DKColumnModel.Type.STRING)
121      tableModel = new DKTableModel(tableModel.name, columns, tableModel.key)
122      shouldFail(IllegalArgumentException){
123         DKDBSource source = new DKDBSource(dbTable.tableName, null, database, tableModel, null, null)
124      }
125      
126      tableModel = DKTableModelUtil.createDefaultTableModel(database.flavor,dbTable, null)
127      DKDBSource source = new DKDBSource(dbTable.tableName, null, database, tableModel, null, null)
128      source.open(null)
129      
130      def row = source.nextRow
131      assert row
132      println "row->$row"
133      println "row[6]->${row[6].class}"
134      assert row[6] == DKTimeUtil.createDate(1956, 11, 12)
135      assert row[7] == DKTimeUtil.createDate(2004, 4, 23, 14, 25, 10, 487)
136      assert row == (Object[])['rob','smith', '100 spruce st', 'Phila', 'usa', 50, DKTimeUtil.createDate(1956, 11, 12), DKTimeUtil.createDate(2004, 4, 23, 14, 25, 10, 487)]
137      
138      row = source.nextRow
139      assert row == (Object[])['steve','jobs', 'infinite, loop', 'Cupertino', 'usa', 54, DKTimeUtil.createDate(1955, 0, 1), DKTimeUtil.createDate(2004, 4, 23, 14, 25, 10, 487)]
140      
141      row = source.nextRow
142      assert !row
143      row = source.nextRow
144      assert !row
145      
146      source.close(null)
147      
148      shouldFail() { row = source.nextRow }
149      
150      assert database.dropTable( dbTable)
151   }
152   
153   private File getCsvFile(){
154      def csvFile = DKResourceUtil.findResourceAsFile('org/diffkit/db/tst/customers.csv')
155      println "csvFile->$csvFile"
156      assert csvFile
157      return csvFile
158   }
159   
160   private DKDBTable createCustomerMetaTable(){
161      DKDBColumn column1 = ['FIRST_NAME', 1, 'VARCHAR', 50, true]
162      DKDBColumn column2 = ['LAST_NAME', 2, 'VARCHAR', 50, true]
163      DKDBColumn column3 = ['ADDRESS', 3, 'VARCHAR', 50, true]
164      DKDBColumn column4 = ['CITY', 4, 'VARCHAR', 50, true]
165      DKDBColumn column5 = ['COUNTRY', 5, 'VARCHAR', 25, true]
166      DKDBColumn column6 = ['AGE', 6, 'INTEGER', -1, true]
167      DKDBColumn column7 = ['BIRTH', 7, 'DATE', -1, true]
168      DKDBColumn column8 = ['NOW', 8, 'TIMESTAMP', -1, true]
169      DKDBColumn[] columns = [column1, column2, column3, column4, column5, column6, column7, column8]
170      String[] pkColNames = ['FIRST_NAME', 'LAST_NAME']
171      DKDBPrimaryKey pk = ['pk_customer', pkColNames]
172      DKDBTable table = [ null, null, 'CUSTOMER', columns, pk]
173      return table
174   }
175}