PageRenderTime 78ms CodeModel.GetById 64ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/tst/org/diffkit/db/tst/TestDBDatabase.groovy

http://diffkit.googlecode.com/
Groovy | 144 lines | 109 code | 17 blank | 18 comment | 6 complexity | beb77a218a0e3990203b5893aeb7fc0b 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.db.tst
 17
 18
 19import org.diffkit.db.DKDBColumn 
 20import org.diffkit.db.DKDBConnectionInfo;
 21import org.diffkit.db.DKDBType;
 22import org.diffkit.db.DKDatabase 
 23import org.diffkit.db.DKDBFlavor;
 24import org.diffkit.db.DKDBPrimaryKey 
 25import org.diffkit.db.DKDBTable;
 26
 27import groovy.util.GroovyTestCase;
 28
 29
 30/**
 31 * @author jpanico
 32 */
 33public class TestDBDatabase extends GroovyTestCase {
 34   
 35   public void testConcreteKeyTypeInfos() {
 36   }
 37   
 38   public void testGenerateUpdate() {
 39      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 40      println "connectionInfo->$connectionInfo"
 41      DKDatabase database = [connectionInfo]
 42      DKDBTable table = this.createCustomerMetaTable()
 43      assert table
 44      Object[] row = ['bob', 'smith', 'update-addr1', 'city', 'update-country', 55]
 45      int[] updateIndices = [2,4]
 46      def updateSQLString = database.generateUpdateDML(row, updateIndices, table)
 47      println "updateSQLString->$updateSQLString"
 48      assert updateSQLString == "UPDATE CUSTOMER\nSET address='update-addr1', country='update-country'\nWHERE (first_name='bob' ) AND (last_name='smith' )"
 49   }
 50   
 51   public void testGenerateDelete() {
 52      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 53      println "connectionInfo->$connectionInfo"
 54      DKDatabase database = [connectionInfo]
 55      DKDBTable table = this.createCustomerMetaTable()
 56      assert table
 57      Object[] row = ['bob', 'smith', 'addr1', 'city', 'country', 55]
 58      def deleteSQLString = database.generateDeleteDML(row, table)
 59      println "deleteSQLString->$deleteSQLString"
 60      assert deleteSQLString == "DELETE FROM CUSTOMER\nWHERE (first_name='bob' ) AND (last_name='smith' )"
 61   }
 62   
 63   public void testKeyTypeInfos() {
 64      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 65      println "connectionInfo->$connectionInfo"
 66      DKDatabase database = [connectionInfo]
 67      DKDBTable table = this.createCustomerMetaTable()
 68      assert table
 69      def keyTypeInfos = database.getKeyConcreteTypeInfos(table)
 70      assert keyTypeInfos
 71      assert keyTypeInfos.length ==2
 72      assert keyTypeInfos[0].type == DKDBType.VARCHAR
 73      assert keyTypeInfos[1].type == DKDBType.VARCHAR
 74   }
 75   
 76   public void testGenerateInsert() {
 77      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 78      println "connectionInfo->$connectionInfo"
 79      DKDatabase database = [connectionInfo]
 80      DKDBTable table = this.createCustomerMetaTable()
 81      assert table
 82      Object[] row = ['bob', 'smith', 'addr1', 'city', 'country', 55]
 83      def insertSQLString = database.generateInsertDML(row, table)
 84      assert insertSQLString == "INSERT INTO CUSTOMER (first_name, last_name, address, city, country, age)\nVALUES ('bob', 'smith', 'addr1', 'city', 'country', 55)"
 85   }
 86   
 87   public void testSupportsType() {
 88      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 89      println "connectionInfo->$connectionInfo"
 90      DKDatabase database = [connectionInfo]
 91      assert database.canConnect()
 92      assert database.supportsType('VARCHAR')
 93      assert database.supportsType('BOOLEAN')
 94   }
 95   
 96   public void testCanConnect(){
 97      
 98      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
 99      println "connectionInfo->$connectionInfo"
100      DKDatabase database = [connectionInfo]
101      assert database.canConnect()
102   }
103   
104   public void testTableExists(){
105      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test;DB_CLOSE_DELAY=-1", null, null, 'test', 'test']
106      DKDatabase database = [connectionInfo]
107      def table = this.createCustomerMetaTable()
108      assert table
109      if(database.tableExists(table))
110         database.dropTable(table)
111      assert !database.tableExists(table)
112      assert database.createTable(table)
113      assert database.tableExists(table)
114      assert database.dropTable(table)
115      assert !database.tableExists(table)
116   }
117   
118   public void testH2(){
119      DKDBConnectionInfo connectionInfo = ['test', DKDBFlavor.H2,"mem:test", null, null, 'test', 'test']
120      println "connectionInfo->$connectionInfo"
121      DKDatabase database = [connectionInfo]
122      def connection = database.connection
123      println "connection->$connection"
124      
125      assert connection
126      def meta = connection.metaData
127      println "meta->$meta"
128      assert meta
129   }
130   
131   private DKDBTable createCustomerMetaTable(){
132      DKDBColumn column1 = ['first_name', 1, 'VARCHAR', 20, true]
133      DKDBColumn column2 = ['last_name', 2, 'VARCHAR', -1, true]
134      DKDBColumn column3 = ['address', 2, 'VARCHAR', -1, true]
135      DKDBColumn column4 = ['city', 2, 'VARCHAR', -1, true]
136      DKDBColumn column5 = ['country', 2, 'VARCHAR', -1, true]
137      DKDBColumn column6 = ['age', 2, 'INTEGER', -1, true]
138      DKDBColumn[] columns = [column1, column2, column3, column4, column5, column6]
139      String[] pkColNames = ['first_name', 'last_name']
140      DKDBPrimaryKey pk = ['pk_customer', pkColNames]
141      DKDBTable table = [ null, null, 'CUSTOMER', columns, pk]
142      return table
143   }
144}