pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedTests for prepared insert statements
2+ messages / 2 participants
[nested] [flat]
* Tests for prepared insert statements
@ 2004-11-17 15:28 Xavier Poinsard <xpoinsard@openpricer.com>
2004-11-17 19:16 ` Re: Tests for prepared insert statements Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 2+ messages in thread
From: Xavier Poinsard @ 2004-11-17 15:28 UTC (permalink / raw)
To: pgsql-jdbc
Hi,
Since I was suspecting the driver, I wrote a two tests regarding
prepared insert statements. May be they could be included.
Xavier.
Attachments:
PreparedInsertTest.diff (text/plain, 5.3K) download
Index: Jdbc2TestSuite.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/test/jdbc2/Jdbc2TestSuite.java,v
retrieving revision 1.18
diff -u -r1.18 Jdbc2TestSuite.java
--- Jdbc2TestSuite.java 9 Nov 2004 08:54:37 -0000 1.18
+++ Jdbc2TestSuite.java 17 Nov 2004 15:16:40 -0000
@@ -55,6 +55,7 @@
// PreparedStatement
suite.addTestSuite(PreparedStatementTest.class);
suite.addTestSuite(StatementTest.class);
+ suite.addTestSuite(BatchPreparedInsertTest.class);
// ServerSide Prepared Statements
suite.addTestSuite(ServerPreparedStmtTest.class);
Index: BatchPreparedInsertTest.java
===================================================================
RCS file: BatchPreparedInsertTest.java
diff -N BatchPreparedInsertTest.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ BatchPreparedInsertTest.java 1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,151 @@
+/*-------------------------------------------------------------------------
+*
+* Copyright (c) 2004, PostgreSQL Global Development Group
+*
+* IDENTIFICATION
+* $PostgreSQL: pgjdbc/org/postgresql/test/jdbc2/BatchExecuteTest.java,v 1.11 2004/11/09 08:54:00 jurka Exp $
+*
+*-------------------------------------------------------------------------
+*/
+package org.postgresql.test.jdbc2;
+
+import org.postgresql.test.TestUtil;
+import junit.framework.TestCase;
+import java.sql.*;
+
+
+/*
+ * Test case for prepared insert statements
+ */
+public class BatchPreparedInsertTest extends TestCase
+{
+
+ private Connection con;
+
+ public BatchPreparedInsertTest(String name)
+ {
+ super(name);
+ }
+
+ // Set up the fixture for this testcase: a connection to a database with
+ // a table for this test.
+ protected void setUp() throws Exception
+ {
+ con = TestUtil.openDB();
+ Statement stmt = con.createStatement();
+
+ // Drop the test table if it already exists for some reason. It is
+ // not an error if it doesn't exist.
+ TestUtil.createTable(con, "testbatchinsert", "pk INTEGER, colInt INTEGER,colReal REAL, colVarchar VARCHAR(10)");
+
+ stmt.executeUpdate("INSERT INTO testbatchinsert VALUES (1, 0,0,'')");
+
+ // Generally recommended with batch updates. By default we run all
+ // tests in this test case with autoCommit disabled.
+ con.setAutoCommit(false);
+ }
+
+ // Tear down the fixture for this test case.
+ protected void tearDown() throws Exception
+ {
+ con.setAutoCommit(true);
+
+ TestUtil.dropTable(con, "testbatchinsert");
+ TestUtil.closeDB(con);
+ }
+
+ private void assertColIntHasValue(int expected) throws Exception
+ {
+ Statement getCol1 = con.createStatement();
+
+ ResultSet rs =
+ getCol1.executeQuery("SELECT colInt FROM testbatchinsert WHERE pk = 1");
+ assertTrue(rs.next());
+
+ int actual = rs.getInt("colInt");
+
+ assertEquals(expected, actual);
+
+ assertEquals(false, rs.next());
+
+ rs.close();
+ getCol1.close();
+ }
+
+ private void assertColRealHasValue(float expected) throws Exception
+ {
+ Statement getCol1 = con.createStatement();
+
+ ResultSet rs =
+ getCol1.executeQuery("SELECT colReal FROM testbatchinsert WHERE pk = 1");
+ assertTrue(rs.next());
+
+ float actual = rs.getFloat("colReal");
+
+ assertEquals(expected, actual,0f);
+
+ assertEquals(false, rs.next());
+
+ rs.close();
+ getCol1.close();
+ }
+
+ private void assertColVarcharHasValue(String expected) throws Exception
+ {
+ Statement getCol1 = con.createStatement();
+
+ ResultSet rs =
+ getCol1.executeQuery("SELECT colVarchar FROM testbatchinsert WHERE pk = 1");
+ assertTrue(rs.next());
+
+ String actual = rs.getString("colVarchar");
+
+ assertEquals(expected, actual);
+
+ assertEquals(false, rs.next());
+
+ rs.close();
+ getCol1.close();
+ }
+
+
+ /**
+ * test basic insert prepared statement
+ * @throws Exception
+ */
+ public void testPreparedStatement() throws Exception
+ {
+ PreparedStatement pstmt = con.prepareStatement(
+ "INSERT INTO testbatchinsert (pk,colInt,colReal,colVarchar)" +
+ "VALUES (1,?,?,?)" );
+
+ pstmt.setInt(1, 1);
+ pstmt.setFloat(2, 0.3f);
+ pstmt.setString(3,"test");
+ pstmt.execute();
+ con.commit();
+ pstmt.close();
+ }
+ /**
+ * test a batch of prepared insert statements
+ * @throws Exception
+ */
+ public void testBatchPreparedStatement() throws Exception
+ {
+ PreparedStatement pstmt = con.prepareStatement(
+ "INSERT INTO testbatchinsert (pk,colInt,colReal,colVarchar)" +
+ "VALUES (?,?,?,?)" );
+
+ for (int i=1;i<=TestUtil.getPrepareThreshold()+3;i++){
+ pstmt.setInt(1, i+1);
+ pstmt.setInt(2, i+1);
+ pstmt.setFloat(3, i*0.3f);
+ pstmt.setString(4,"test"+i);
+ pstmt.addBatch();
+ }
+ pstmt.executeBatch();
+ con.commit();
+ pstmt.close();
+ }
+
+}
^ permalink raw reply [nested|flat] 2+ messages in thread
* Re: Tests for prepared insert statements
2004-11-17 15:28 Tests for prepared insert statements Xavier Poinsard <xpoinsard@openpricer.com>
@ 2004-11-17 19:16 ` Kris Jurka <books@ejurka.com>
0 siblings, 0 replies; 2+ messages in thread
From: Kris Jurka @ 2004-11-17 19:16 UTC (permalink / raw)
To: Xavier Poinsard <xpoinsard@openpricer.com>; +Cc: pgsql-jdbc
On Wed, 17 Nov 2004, Xavier Poinsard wrote:
> Hi,
> Since I was suspecting the driver, I wrote a two tests regarding
> prepared insert statements. May be they could be included.
>
Could you describe what additional code paths this is testing? Comparing
it to BatchExecuteTest you seem to be doing an INSERT vs an UPDATE and you
test a few additional datatypes. It doesn't seem to be worth a whole new
TestCase for this. Am I missing something? If not perhaps just modifying
the existing BatchExecuteTest to use more datatypes would be the way to
go.
Kris Jurka
^ permalink raw reply [nested|flat] 2+ messages in thread
end of thread, other threads:[~2004-11-17 19:16 UTC | newest]
Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-11-17 15:28 Tests for prepared insert statements Xavier Poinsard <xpoinsard@openpricer.com>
2004-11-17 19:16 ` Kris Jurka <books@ejurka.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox