From: Carlos G. Á. <car...@us...> - 2005-09-08 18:45:20
|
Update of /cvsroot/pgsqlclient/pgsqlclient_10/source/UnitTests In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8269 Added Files: AssemblyInfo.cs PgArrayTest.cs PgBaseTest.cs PgCommandBuilderTest.cs PgCommandTest.cs PgConnectionTest.cs PgDataAdapterTest.cs PgDatabaseSchemaTest.cs PgDataReaderTest.cs PgGeometicTypesTest.cs PgTransactionTest.cs PostgreSql.Data.PgSqlClient.UnitTests.dll.config Log Message: Started the reorganization of the CVS module --- NEW FILE: PgGeometicTypesTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Data; using NUnit.Framework; using PostgreSql.Data.PgSqlClient; using PostgreSql.Data.PgTypes; namespace PostgreSql.Data.PgSqlClient.UnitTests { [TestFixture] public class PgGeometricTypesTest : PgBaseTest { [Test] public void PointTest() { PgCommand command = new PgCommand("select point_field from public.geometric_table where pk = @pk", Connection); try { command.Parameters.Add("@pk", PgDbType.Int4).Value = 50; PgPoint point = (PgPoint)command.ExecuteScalar(); Console.WriteLine("Point value: {0}", point.ToString()); Assertion.AssertEquals("Invalid X coord in point", 50, point.X); Assertion.AssertEquals("Invalid Y coord in point", 60, point.Y); } catch (Exception) { throw; } finally { command.Dispose(); } } [Test] public void BoxTest() { PgCommand command = new PgCommand("select box_field from public.geometric_table where pk = @pk", Connection); try { command.Parameters.Add("@pk", PgDbType.Int4).Value = 70; PgBox box = (PgBox)command.ExecuteScalar(); Console.WriteLine("Box value: {0}", box.ToString()); Assertion.AssertEquals("Invalid X coord in Lower Left corner", 0, box.LowerLeft.X); Assertion.AssertEquals("Invalid Y coord in Lower Left corner", 70, box.LowerLeft.Y); Assertion.AssertEquals("Invalid X coord in Upper Right corner", 70, box.UpperRight.X); Assertion.AssertEquals("Invalid Y coord in Upper Right corner", 70, box.UpperRight.Y); } catch (Exception) { throw; } finally { command.Dispose(); } } [Test] public void CircleTest() { PgCommand command = new PgCommand("select circle_field from public.geometric_table where pk = @pk", Connection); try { command.Parameters.Add("@pk", PgDbType.Int4).Value = 30; PgCircle circle = (PgCircle)command.ExecuteScalar(); Console.WriteLine("Circle value: {0}", circle.ToString()); Assertion.AssertEquals("Invalid X coord in circle", 30, circle.Center.X); Assertion.AssertEquals("Invalid Y coord in circle", 0, circle.Center.Y); Assertion.AssertEquals("Invalid RADIUS coord in circle", 30, circle.Radius); } catch (Exception) { throw; } finally { command.Dispose(); } } [Test] public void LineSegmentTest() { PgCommand command = new PgCommand("select lseg_field from public.geometric_table where pk = @pk", Connection); try { command.Parameters.Add("@pk", PgDbType.Int4).Value = 20; PgLSeg lseg = (PgLSeg)command.ExecuteScalar(); Console.WriteLine("LSeg value: {0}", lseg.ToString()); Assertion.AssertEquals("Invalid X coord in start point", -1, lseg.StartPoint.X); Assertion.AssertEquals("Invalid Y coord in start point", 0, lseg.StartPoint.Y); Assertion.AssertEquals("Invalid X coord in end point", 1, lseg.EndPoint.X); Assertion.AssertEquals("Invalid Y coord in end point", 0, lseg.EndPoint.Y); } catch (Exception) { throw; } finally { command.Dispose(); } } [Test] public void PathTest() { PgCommand command = new PgCommand("select path_field from public.geometric_table where pk = @pk", Connection); try { command.Parameters.Add("@pk", PgDbType.Int4).Value = 10; PgPath path = (PgPath)command.ExecuteScalar(); Console.WriteLine("Path value: {0}", path.ToString()); Assertion.AssertEquals("Invalid X coord in path point 0", 0, path.Points[0].X); Assertion.AssertEquals("Invalid Y coord in path point 0", 0, path.Points[0].Y); Assertion.AssertEquals("Invalid X coord in path point 1", 1, path.Points[1].X); Assertion.AssertEquals("Invalid Y coord in path point 1", 0, path.Points[1].Y); } catch (Exception) { throw; } finally { command.Dispose(); } } [Test] public void PolygonTest() { PgCommand command = new PgCommand("select polygon_field from public.geometric_table where pk = @pk", Connection); try { command.Parameters.Add("@pk", PgDbType.Int4).Value = 10; PgPolygon polygon = (PgPolygon)command.ExecuteScalar(); Console.WriteLine("Polygon value: {0}", polygon.ToString()); Assertion.AssertEquals("Invalid X coord in polygon point 0", 1, polygon.Points[0].X); Assertion.AssertEquals("Invalid Y coord in polygon point 0", 1, polygon.Points[0].Y); Assertion.AssertEquals("Invalid X coord in polygon point 1", 0, polygon.Points[1].X); Assertion.AssertEquals("Invalid Y coord in polygon point 1", 0, polygon.Points[1].Y); } catch (Exception) { throw; } finally { command.Dispose(); } } [Ignore("Test not implemented.")] public void BoxArrayTest() { } [Ignore("Test not implemented.")] public void PointArrayTest() { } [Ignore("Test not implemented.")] public void LineSegmentArrayTest() { } [Ignore("Test not implemented.")] public void PathArrayTest() { } [Ignore("Test not implemented.")] public void PolygonArrayTest() { } [Ignore("Test not implemented.")] public void CircleArrayTest() { } } } --- NEW FILE: PgDataAdapterTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Data; using PostgreSql.Data.PgSqlClient; using NUnit.Framework; namespace PostgreSql.Data.PgSqlClient.UnitTests { [TestFixture] public class PgDataAdapterTest : PgBaseTest { [Test] public void FillTest() { PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE date_field = @date_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@date_field", PgDbType.Date, 4, "date_field").Value = DateTime.Now; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); Console.WriteLine(); Console.WriteLine("DataAdapter - Fill Method - Test"); foreach (DataTable table in ds.Tables) { foreach (DataColumn col in table.Columns) { Console.Write(col.ColumnName + "\t\t"); } Console.WriteLine(); foreach (DataRow row in table.Rows) { for (int i = 0; i < table.Columns.Count; i++) { Console.Write(row[i] + "\t\t"); } Console.WriteLine(""); } } adapter.Dispose(); builder.Dispose(); command.Dispose(); } [Test] public void FillMultipleTest() { PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE date_field = @date_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@date_field", PgDbType.Date, 4, "date_field").Value = DateTime.Now; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds1 = new DataSet(); DataSet ds2 = new DataSet(); adapter.Fill(ds1, "public.test_table"); adapter.Fill(ds2, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); } [Test] public void InsertTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); DataRow newRow = ds.Tables["public.test_table"].NewRow(); newRow["int4_field"] = 100; newRow["char_field"] = "PostgreSQL"; newRow["varchar_field"] = "PostgreSQL CLient"; newRow["int8_field"] = 100000; newRow["int2_field"] = 100; newRow["double_field"] = 100.01; newRow["date_field"] = DateTime.Now; newRow["time_Field"] = DateTime.Now; newRow["timestamp_field"] = DateTime.Now; ds.Tables["public.test_table"].Rows.Add(newRow); adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); } [Test] public void UpdateCharTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 1; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0]["char_field"] = "PostgreSQL"; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); } [Test] public void UpdateVarCharTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 10; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0]["varchar_field"] = "PostgreSQL Client"; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); } [Test] public void UpdateInt2Test() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 40; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0]["int2_field"] = System.Int16.MaxValue; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT int2_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 40; short val = (short)command.ExecuteScalar(); Assertion.AssertEquals("int2_field has not correct value", System.Int16.MaxValue, val); } [Test] public void UpdateInt8Test() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 20; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0]["int8_field"] = System.Int32.MaxValue; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT int8_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 20; long val = (long)command.ExecuteScalar(); Assertion.AssertEquals("int8_field has not correct value", System.Int32.MaxValue, val); } [Test] public void UpdateDoubleTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 50; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0]["double_field"] = System.Int32.MaxValue; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT double_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 50; double val = (double)command.ExecuteScalar(); Assertion.AssertEquals("double_field has not correct value", System.Int32.MaxValue, val); } [Test] public void UpdateMoneyField() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 27; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0]["money_field"] = 200.20; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT money_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 27; float val = (float)command.ExecuteScalar(); Assertion.AssertEquals("money_field has not correct value", 200.20, val); } [Test] public void UpdateNumericTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 60; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0]["numeric_field"] = System.Int16.MaxValue; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT numeric_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 60; decimal val = (decimal)command.ExecuteScalar(); if (val != (decimal)System.Int16.MaxValue) { Assertion.Fail("numeric_field has not correct value"); } } [Test] public void UpdateDateTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 70; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); DateTime dt = DateTime.Now; ds.Tables["public.test_table"].Rows[0]["date_field"] = dt; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT date_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 70; DateTime val = (DateTime)command.ExecuteScalar(); Assertion.AssertEquals("date_field has not correct day", dt.Day, val.Day); Assertion.AssertEquals("date_field has not correct month", dt.Month, val.Month); Assertion.AssertEquals("date_field has not correct year", dt.Year, val.Year); } [Test] public void UpdateTimeTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 80; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); DateTime dt = DateTime.Now; ds.Tables["public.test_table"].Rows[0]["time_field"] = dt; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT time_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 80; DateTime val = (DateTime)command.ExecuteScalar(); Assertion.AssertEquals("time_field has not correct hour", dt.Hour, val.Hour); Assertion.AssertEquals("time_field has not correct minute", dt.Minute, val.Minute); Assertion.AssertEquals("time_field has not correct second", dt.Second, val.Second); } [Test] public void UpdateTimeStampTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 90; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); DateTime dt = DateTime.Now; ds.Tables["public.test_table"].Rows[0]["timestamp_field"] = dt; adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); command = new PgCommand("SELECT timestamp_field FROM public.test_table WHERE int4_field = @int4_field", Connection); command.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 90; DateTime val = (DateTime)command.ExecuteScalar(); Assertion.AssertEquals("timestamp_field has not correct day", dt.Day, val.Day); Assertion.AssertEquals("timestamp_field has not correct month", dt.Month, val.Month); Assertion.AssertEquals("timestamp_field has not correct year", dt.Year, val.Year); Assertion.AssertEquals("timestamp_field has not correct hour", dt.Hour, val.Hour); Assertion.AssertEquals("timestamp_field has not correct minute", dt.Minute, val.Minute); Assertion.AssertEquals("timestamp_field has not correct second", dt.Second, val.Second); } [Test] public void DeleteTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = @int4_field", Connection, transaction); PgDataAdapter adapter = new PgDataAdapter(command); adapter.SelectCommand.Parameters.Add("@int4_field", PgDbType.Int4, 4, "int4_field").Value = 35; PgCommandBuilder builder = new PgCommandBuilder(adapter); DataSet ds = new DataSet(); adapter.Fill(ds, "public.test_table"); ds.Tables["public.test_table"].Rows[0].Delete(); adapter.Update(ds, "public.test_table"); adapter.Dispose(); builder.Dispose(); command.Dispose(); transaction.Commit(); } } } --- NEW FILE: PgCommandBuilderTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Data; using PostgreSql.Data.PgSqlClient; using NUnit.Framework; namespace PostgreSql.Data.PgSqlClient.UnitTests { [TestFixture] public class PgCommandBuilderTest : PgBaseTest { [Test] public void GetInsertCommandTest() { PgCommand command = new PgCommand("select * from public.test_table where int4_field = @int4_field and varchar_field = @varchar_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); PgCommandBuilder builder = new PgCommandBuilder(adapter); Console.WriteLine(); Console.WriteLine("\r\nPgCommandBuilder - GetInsertCommand Method Test"); Console.WriteLine(builder.GetInsertCommand().CommandText); builder.Dispose(); adapter.Dispose(); command.Dispose(); } [Test] public void GetUpdateCommandTest() { PgCommand command = new PgCommand("select * from public.test_table where int4_field = @int4_field and varchar_field = @varchar_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); PgCommandBuilder builder = new PgCommandBuilder(adapter); Console.WriteLine(); Console.WriteLine("\r\nPgCommandBuilder - GetUpdateCommand Method Test"); Console.WriteLine(builder.GetUpdateCommand().CommandText); builder.Dispose(); adapter.Dispose(); command.Dispose(); } [Test] public void GetDeleteCommandTest() { PgCommand command = new PgCommand("select * from public.test_table where int4_field = @int4_field and varchar_field = @varchar_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); PgCommandBuilder builder = new PgCommandBuilder(adapter); Console.WriteLine(); Console.WriteLine("PgCommandBuilder - GetDeleteCommand Method Test"); Console.WriteLine( builder.GetDeleteCommand().CommandText ); builder.Dispose(); adapter.Dispose(); command.Dispose(); } [Test] public void RefreshSchemaTest() { PgCommand command = new PgCommand("select * from public.test_table where int4_field = @int4_field and varchar_field = @varchar_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); PgCommandBuilder builder = new PgCommandBuilder(adapter); Console.WriteLine(); Console.WriteLine("\r\nPgCommandBuilder - RefreshSchema Method Test - Commands for original SQL statement: "); Console.WriteLine(builder.GetInsertCommand().CommandText); Console.WriteLine(builder.GetUpdateCommand().CommandText); Console.WriteLine(builder.GetDeleteCommand().CommandText); adapter.SelectCommand.CommandText = "select int4_field, date_field from public.test_table where int4_field = @int4_field"; builder.RefreshSchema(); Console.WriteLine(); Console.WriteLine("\r\nPgCommandBuilder - RefreshSchema Method Test - Commands for new SQL statement: "); Console.WriteLine(builder.GetInsertCommand().CommandText); Console.WriteLine(builder.GetUpdateCommand().CommandText); Console.WriteLine(builder.GetDeleteCommand().CommandText); builder.Dispose(); adapter.Dispose(); command.Dispose(); } [Test] public void CommandBuilderWithExpressionFieldTest() { PgCommand command = new PgCommand("select public.test_table.*, 0 AS EXPR_VALUE from public.test_table where int4_field = @int4_field and varchar_field = @varchar_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); PgCommandBuilder builder = new PgCommandBuilder(adapter); Console.WriteLine(); Console.WriteLine("PgCommandBuilder - CommandBuilderWithExpressionFieldTest"); Console.WriteLine(builder.GetUpdateCommand().CommandText); builder.Dispose(); adapter.Dispose(); command.Dispose(); } [Test] public void DeriveParameters() { PgCommandBuilder builder = new PgCommandBuilder(); PgCommand command = new PgCommand("DeriveCount", Connection); command.CommandType = CommandType.StoredProcedure; PgCommandBuilder.DeriveParameters(command); Console.WriteLine("\r\nPgCommandBuilder - DeriveParameters static Method Test"); for (int i = 0; i < command.Parameters.Count; i++) { Console.WriteLine("Parameter name: {0}\tParameter Source Column:{1}\tDirection:{2}", command.Parameters[i].ParameterName, command.Parameters[i].SourceColumn, command.Parameters[i].Direction); } } [Test] public void DeriveParameters2() { PgTransaction transaction = Connection.BeginTransaction(); PgCommandBuilder builder = new PgCommandBuilder(); PgCommand command = new PgCommand("DeriveCount", Connection, transaction); command.CommandType = CommandType.StoredProcedure; PgCommandBuilder.DeriveParameters(command); Console.WriteLine("\r\nPgCommandBuilder - DeriveParameters static Method Test"); for (int i = 0; i < command.Parameters.Count; i++) { Console.WriteLine("Parameter name: {0}\tParameter Source Column:{1}\tDirection:{2}", command.Parameters[i].ParameterName, command.Parameters[i].SourceColumn, command.Parameters[i].Direction); } transaction.Commit(); } [Test] public void TestWithClosedConnection() { Connection.Close(); PgCommand command = new PgCommand("select * from public.test_table where int4_field = @int4_field and varchar_field = @varchar_field", Connection); PgDataAdapter adapter = new PgDataAdapter(command); PgCommandBuilder builder = new PgCommandBuilder(adapter); Console.WriteLine(); Console.WriteLine("\r\nPgCommandBuilder - RefreshSchema Method Test - Commands for original SQL statement: "); Console.WriteLine(builder.GetInsertCommand().CommandText); Console.WriteLine(builder.GetUpdateCommand().CommandText); Console.WriteLine(builder.GetDeleteCommand().CommandText); adapter.SelectCommand.CommandText = "select int4_field, date_field from public.test_table where int4_field = @int4_field"; builder.RefreshSchema(); Console.WriteLine(); Console.WriteLine("\r\nPgCommandBuilder - RefreshSchema Method Test - Commands for new SQL statement: "); Console.WriteLine(builder.GetInsertCommand().CommandText); Console.WriteLine(builder.GetUpdateCommand().CommandText); Console.WriteLine(builder.GetDeleteCommand().CommandText); builder.Dispose(); adapter.Dispose(); command.Dispose(); } } } --- NEW FILE: PgDatabaseSchemaTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Data; using PostgreSql.Data.PgSqlClient; using NUnit.Framework; namespace PostgreSql.Data.PgSqlClient.UnitTests { [TestFixture] public class PgDatabaseSchemaTest : PgBaseTest { [Test] public void Aggregates() { DataTable aggregates = Connection.GetSchema("Aggregates", null); } [Test] public void Casts() { DataTable casts = Connection.GetSchema("Casts", null); } [Test] public void CheckConstraints() { DataTable checkConstraints = Connection.GetSchema("CheckConstraints", null); } [Test] public void CheckConstraintsByTable() { DataTable checkConstraintsByTable = Connection.GetSchema("CheckConstraintsByTable", null); } [Test] public void Columns() { DataTable columns = Connection.GetSchema("Columns", null); } [Test] public void Databases() { DataTable databases = Connection.GetSchema("Database", null); } [Test] public void Domains() { DataTable domains = Connection.GetSchema("Domains", null); } [Test] public void ForeignKeys() { DataTable foreignKeys = Connection.GetSchema("ForeignKeys", null); } [Test] public void FunctionPrivileges() { DataTable functionPrivileges = Connection.GetSchema("FunctionPrivileges", null); } [Test] public void Functions() { DataTable functions = Connection.GetSchema("Functions", null); } [Test] public void Groups() { DataTable groups = Connection.GetSchema("Groups", null); } [Test] public void Indexes() { DataTable indexes = Connection.GetSchema("Indexes", null); } [Test] public void PrimaryKeys() { DataTable primaryKeys = Connection.GetSchema("PrimaryKeys", null); } [Test] public void ProviderTypes() { DataTable providerTypes = Connection.GetSchema("ProviderTypes", null); } [Test] public void Schemata() { DataTable schemata = Connection.GetSchema("Schemata", null); } [Test] public void SqlLanguages() { DataTable sqlLanguages = Connection.GetSchema("SqlLanguages", null); } [Test] [Ignore("Not implemented.")] public void Statistics() { DataTable statistics = Connection.GetSchema("Statistics", null); } [Test] public void TableConstraint() { DataTable tableConstraint = Connection.GetSchema("TableConstraint", null); } [Test] public void TablePrivileges() { DataTable tablePrivileges = Connection.GetSchema("TablePrivileges", null); } [Test] [Ignore("Not implemented.")] public void TableStatistics() { DataTable tableStatistics = Connection.GetSchema("TableStatistics", null); } [Test] public void Tables() { DataTable tables = Connection.GetSchema("Tables", null); } [Test] [Ignore("Not implemented.")] public void TablesInfo() { DataTable tablesInfo = Connection.GetSchema("TablesInfo", null); } [Test] [Ignore("Not implemented.")] public void TriggerParameters() { DataTable triggerParameters = Connection.GetSchema("TriggerParameters", null); } [Test] [Ignore("Not implemented.")] public void TriggerPrivileges() { DataTable triggerPrivileges = Connection.GetSchema("TriggerPrivileges", null); } [Test] public void Triggers() { DataTable triggers = Connection.GetSchema("Triggers", null); } [Test] [Ignore("Not implemented.")] public void UsagePrivileges() { DataTable usagePrivileges = Connection.GetSchema("UsagePrivileges", null); } [Test] [Ignore("Not implemented.")] public void ViewColumnUsage() { DataTable viewColumnUsage = Connection.GetSchema("ViewColumnUsage", null); } [Test] public void ViewPrivileges() { DataTable viewPrivileges = Connection.GetSchema("ViewPrivileges", null); } [Test] public void Views() { DataTable views = Connection.GetSchema("Views", null); } } } --- NEW FILE: AssemblyInfo.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyTitle("ADO.NET Data provider for PostgreSQL 7.4+")] [assembly: AssemblyDescription("ADO.NET Data provider for PostgreSQL 7.4+")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] [assembly: AssemblyProduct("PgSqlClient - ADO.NET Data provider for PostgreSQL 7.4+")] [assembly: AssemblyCopyright("2003 - Carlos Guzmán Álvarez")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: AssemblyVersion("1.0.*")] [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("")] [assembly: AssemblyKeyName("")] --- NEW FILE: PgBaseTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Text; using System.Data; using System.Configuration; using NUnit.Framework; using PostgreSql.Data.PgSqlClient; using PostgreSql.Data.PgTypes; namespace PostgreSql.Data.PgSqlClient.UnitTests { public class PgBaseTest { private PgConnection connection; public PgConnection Connection { get { return connection; } } public PgBaseTest() { } [SetUp] public void SetUp() { try { dropDatabase(); } catch{} createDatabase(); // Build the connection string StringBuilder connString = new StringBuilder(); connString.AppendFormat( "User={0};Password={1};Database={2};DataSource={3};Port={4};SSL={5}", ConfigurationSettings.AppSettings["User"], ConfigurationSettings.AppSettings["Password"], ConfigurationSettings.AppSettings["Database"], ConfigurationSettings.AppSettings["DataSource"], ConfigurationSettings.AppSettings["Port"], ConfigurationSettings.AppSettings["SSL"]); connection = new PgConnection(connString.ToString()); connection.StateChange += new StateChangeEventHandler(stateChange); connection.Open(); createTables(); createFunctions(); } [TearDown] public void TearDown() { connection.Close(); } private void stateChange(object sender, StateChangeEventArgs e) { Console.WriteLine("Connection state changed from {0} to {1}", e.OriginalState, e.CurrentState); } private void createDatabase() { StringBuilder connString = new StringBuilder(); connString.AppendFormat( "User={0};Password={1};Database={2};DataSource={3};Port={4};SSL={5}", ConfigurationSettings.AppSettings["User"], ConfigurationSettings.AppSettings["Password"], String.Empty, ConfigurationSettings.AppSettings["DataSource"], ConfigurationSettings.AppSettings["Port"], ConfigurationSettings.AppSettings["SSL"]); PgConnection connection = new PgConnection(connString.ToString()); connection.Open(); #warning "Create dtabase here" /* connection.CreateDatabase( ConfigurationSettings.AppSettings["Database"], null, null, null, "UNICODE"); */ connection.Close(); } private void dropDatabase() { StringBuilder connString = new StringBuilder(); connString.AppendFormat( "User={0};Password={1};Database={2};DataSource={3};Port={4};SSL={5}", ConfigurationSettings.AppSettings["User"], ConfigurationSettings.AppSettings["Password"], String.Empty, ConfigurationSettings.AppSettings["DataSource"], ConfigurationSettings.AppSettings["Port"], ConfigurationSettings.AppSettings["SSL"]); PgConnection connection = new PgConnection(connString.ToString()); connection.Open(); StringBuilder commandText = new StringBuilder(); commandText.AppendFormat( "drop database {0}", ConfigurationSettings.AppSettings["Database"]); PgCommand command = new PgCommand(commandText.ToString(), connection); command.ExecuteNonQuery(); command.Dispose(); connection.Close(); } private void createTables() { StringBuilder commandText = new StringBuilder(); // Table for general purpouse tests commandText.Append("CREATE TABLE public.test_table("); commandText.Append("int4_field int4 NOT NULL,"); commandText.Append("char_field char(10),"); commandText.Append("varchar_field varchar(30),"); commandText.Append("single_field float4,"); commandText.Append("double_field float8,"); commandText.Append("date_field date,"); commandText.Append("time_field time,"); commandText.Append("timestamp_field timestamp,"); commandText.Append("blob_field bytea,"); commandText.Append("bool_field bool,"); commandText.Append("int2_field int2,"); commandText.Append("int8_field int8,"); commandText.Append("money_field money,"); commandText.Append("numeric_field numeric(8,2),"); commandText.Append("bool_array bool[],"); commandText.Append("int2_array int2[],"); commandText.Append("int4_array int4[],"); commandText.Append("int8_array int8[],"); commandText.Append("mint2_array int2[][],"); commandText.Append("serial_field serial NOT NULL,"); commandText.Append("macaddr_field macaddr,"); commandText.Append("inet_field inet,"); commandText.Append("name_field name,"); commandText.Append("CONSTRAINT test_table_pkey PRIMARY KEY (int4_field)"); commandText.Append(") WITH OIDS;"); PgCommand command = new PgCommand(commandText.ToString(), connection); command.ExecuteNonQuery(); commandText = new StringBuilder(); // Table for Geometric types tests commandText.Append("CREATE TABLE public.geometric_table("); commandText.Append("pk int4 NOT NULL,"); commandText.Append("point_field point,"); commandText.Append("box_field box,"); commandText.Append("circle_field circle,"); commandText.Append("lseg_field lseg,"); commandText.Append("path_field path,"); commandText.Append("polygon_field polygon,"); commandText.Append("point_array point[],"); commandText.Append("box_array box[],"); commandText.Append("circle_array circle[],"); commandText.Append("lseg_array lseg[],"); commandText.Append("path_array path[],"); commandText.Append("polygon_array polygon[],"); commandText.Append("line_field line,"); commandText.Append("line_array line[],"); commandText.Append("CONSTRAINT geometric_test_pkey PRIMARY KEY (pk)"); commandText.Append(") WITH OIDS;"); command.CommandText = commandText.ToString(); command.ExecuteNonQuery(); command.Dispose(); insertTestData(); insertGeometricTestData(); } private void createFunctions() { // Create language functions StringBuilder commandText = new StringBuilder(); commandText.Append("CREATE OR REPLACE FUNCTION public.plpgsql_call_handler()"); commandText.Append("RETURNS language_handler AS"); commandText.Append("'$libdir/plpgsql', 'plpgsql_call_handler'"); commandText.Append("LANGUAGE 'c' VOLATILE;"); PgCommand command = new PgCommand(commandText.ToString(), connection); command.ExecuteNonQuery(); // Create languages commandText = new StringBuilder(); try { commandText.Append("CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql' HANDLER plpgsql_call_handler;"); command = new PgCommand(commandText.ToString(), connection); command.ExecuteNonQuery(); } catch { } // Create test function public.TestCount() commandText = new StringBuilder(); commandText.Append("CREATE OR REPLACE FUNCTION public.TestCount()"); commandText.Append("RETURNS int8 AS"); commandText.Append("'"); commandText.Append("select count(*) from test_table;"); commandText.Append("'"); commandText.Append("LANGUAGE 'sql' VOLATILE;"); command = new PgCommand(commandText.ToString(), connection); command.ExecuteNonQuery(); // Create test function public.DeriveCount() commandText = new StringBuilder(); commandText.Append("CREATE OR REPLACE FUNCTION public.DeriveCount(int4)"); commandText.Append("RETURNS int8 AS"); commandText.Append("'"); commandText.Append("select count(*) from test_table where int4_field < $1;"); commandText.Append("'"); commandText.Append("LANGUAGE 'sql' VOLATILE;"); command.CommandText = commandText.ToString(); command.ExecuteNonQuery(); // Create test function public.DeleteRows() commandText = new StringBuilder(); commandText.Append("CREATE OR REPLACE FUNCTION public.DeleteRows(int4)\r\n"); commandText.Append("RETURNS BOOLEAN AS '\r\n"); commandText.Append("DECLARE\r\n"); commandText.Append("\t\trows INTEGER;\r\n"); commandText.Append("BEGIN\r\n"); commandText.Append("DELETE FROM public.test_table WHERE int4_field > $1;\r\n"); commandText.Append("GET DIAGNOSTICS rows = ROW_COUNT;\r\n"); commandText.Append("IF rows > 0 THEN\r\n"); commandText.Append("\t\tRETURN TRUE;\r\n"); commandText.Append("ELSE\r\n"); commandText.Append("\t\tRETURN FALSE;\r\n"); commandText.Append("END IF;\r\n"); commandText.Append("END;\r\n"); commandText.Append("'\r\n"); commandText.Append("LANGUAGE 'plpgsql' VOLATILE;"); command.CommandText = commandText.ToString(); command.ExecuteNonQuery(); command.Dispose(); } private void insertTestData() { string commandText = "insert into public.test_table values(@int4_field, @char_field, @varchar_field, @single_field, @double_field, @date_Field, @time_field, @timestamp_field, @blob_field, @bool_field)"; PgTransaction transaction = connection.BeginTransaction(); PgCommand command = new PgCommand(commandText, connection, transaction); try { // Add command parameters command.Parameters.Add("@int4_field", PgDbType.Int4); command.Parameters.Add("@char_field", PgDbType.Char); command.Parameters.Add("@varchar_field", PgDbType.VarChar); command.Parameters.Add("@single_field", PgDbType.Float); command.Parameters.Add("@double_field", PgDbType.Double); command.Parameters.Add("@date_field", PgDbType.Date); command.Parameters.Add("@time_field", PgDbType.Time); command.Parameters.Add("@timestamp_field", PgDbType.Timestamp); command.Parameters.Add("@blob_field", PgDbType.Binary); command.Parameters.Add("@bool_field", PgDbType.Boolean); for (int i = 0; i < 100; i++) { command.Parameters["@int4_field"].Value = i; command.Parameters["@char_field"].Value = "IRow " + i.ToString(); command.Parameters["@varchar_field"].Value = "IRow Number" + i.ToString(); command.Parameters["@single_field"].Value = (float)(i + 10)/5; command.Parameters["@double_field"].Value = Math.Log(i, 10); command.Parameters["@date_field"].Value = DateTime.Now; command.Parameters["@time_field"].Value = DateTime.Now; command.Parameters["@timestamp_field"].Value = DateTime.Now; command.Parameters["@blob_field"].Value = Encoding.Default.GetBytes("IRow " + i.ToString()); command.Parameters["@bool_field"].Value = true; command.ExecuteNonQuery(); } // Commit transaction transaction.Commit(); } catch (PgException) { transaction.Rollback(); throw; } finally { command.Dispose(); } } private void insertGeometricTestData() { string commandText = "insert into public.geometric_table values(@pk, @point, @box, @circle, @lseg, @path, @polygon)"; PgTransaction transaction = connection.BeginTransaction(); PgCommand command = new PgCommand(commandText, connection, transaction); try { // Add command parameters command.Parameters.Add("@pk", PgDbType.Int4); command.Parameters.Add("@point", PgDbType.Point); command.Parameters.Add("@box", PgDbType.Box); command.Parameters.Add("@circle", PgDbType.Circle); command.Parameters.Add("@lseg", PgDbType.LSeg); command.Parameters.Add("@path", PgDbType.Path); command.Parameters.Add("@polygon", PgDbType.Polygon); for (int i = 0; i < 100; i++) { command.Parameters["@pk"].Value = i; command.Parameters["@point"].Value = new PgPoint(i, i + 10); command.Parameters["@box"].Value = new PgBox(new PgPoint(0,i), new PgPoint(i, i)); command.Parameters["@circle"].Value = new PgCircle(new PgPoint(i, 0), i); command.Parameters["@lseg"].Value = new PgLSeg(new PgPoint(-1,0), new PgPoint(1,0)); command.Parameters["@path"].Value = new PgPath(false, new PgPoint[]{new PgPoint(0,0), new PgPoint(1,0)}); command.Parameters["@polygon"].Value= new PgPolygon(new PgPoint[]{new PgPoint(1,1), new PgPoint(0,0)}); command.ExecuteNonQuery(); } // Commit transaction transaction.Commit(); } catch (PgException) { transaction.Rollback(); throw; } finally { command.Dispose(); } } } } --- NEW FILE: PgArrayTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Data; using System.Security.Cryptography; using PostgreSql.Data.PgSqlClient; using NUnit.Framework; namespace PostgreSql.Data.PgSqlClient.UnitTests { [TestFixture] public class PgArrayTest : PgBaseTest { private int testArrayLength = 100; [Test] public void Int2ArrayTest() { int id_value = System.DateTime.Now.Millisecond; string selectText = "SELECT int2_array FROM public.test_table WHERE int4_field = " + id_value.ToString(); string insertText = "INSERT INTO public.test_table (int4_field, int2_array) values (@int4_field, @int2_array)"; byte[] bytes = new byte[this.testArrayLength*2]; RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); rng.GetBytes(bytes); short[] insert_values = new short[this.testArrayLength]; Buffer.BlockCopy(bytes, 0, insert_values, 0, bytes.Length); Console.WriteLine("Executing insert command"); PgCommand command = new PgCommand(insertText, Connection); command.Parameters.Add("@int4_field", PgDbType.Int4).Value = id_value; command.Parameters.Add("@int2_array", PgDbType.Array).Value = insert_values; int updated = command.ExecuteNonQuery(); Assertion.AssertEquals("Invalid number of inserted rows", updated, 1); Console.WriteLine("Checking inserted values"); // Check that inserted values are correct PgCommand select = new PgCommand(selectText, Connection); PgDataReader reader = select.ExecuteReader(); if (reader.Read()) { if (!reader.IsDBNull(0)) { short[] select_values = new short[insert_values.Length]; System.Array.Copy((System.Array)reader.GetValue(0), select_values, select_values.Length); for (int i = 0; i < insert_values.Length; i++) { if (insert_values[i] != select_values[i]) { throw new Exception("differences at index " + i.ToString()); } } } } Console.WriteLine("Finishing test"); reader.Close(); } } } --- NEW FILE: PgCommandTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Data; using PostgreSql.Data.PgSqlClient; using NUnit.Framework; namespace PostgreSql.Data.PgSqlClient.UnitTests { [TestFixture] public class PgCommandTest : PgBaseTest { [Test] public void ExecuteNonQueryTest() { Console.WriteLine("\r\nPgCommandTest.ExecuteNonQueryTest"); string commandText = "update public.test_table set char_field = @char_field, varchar_field = @varchar_field where int4_field = @int4_field"; PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand(commandText, Connection, transaction); try { // Add command parameters command.Parameters.Add("@char_field", PgDbType.Char); command.Parameters.Add("@varchar_field", PgDbType.VarChar); command.Parameters.Add("@int4_field", PgDbType.Int4); for (int i = 0; i < 100; i++) { command.Parameters["@char_field"].Value = "Row " + i.ToString(); command.Parameters["@varchar_field"].Value = "Row Number" + i.ToString(); command.Parameters["@int4_field"].Value = i; command.ExecuteNonQuery(); } // Commit transaction transaction.Commit(); ExecuteReaderTest(); } catch (PgException) { transaction.Rollback(); throw; } finally { command.Dispose(); } } [Test] public void ExecuteReaderTest() { Console.WriteLine("\r\nPgCommandTest.ExecuteReaderTest"); PgCommand command = new PgCommand("SELECT * FROM public.test_table ORDER BY date_field", Connection); PgDataReader reader = command.ExecuteReader(); for (int i = 0; i < reader.FieldCount; i++) { Console.Write("{0}\t\t", reader.GetName(i)); } Console.Write("\r\n"); while (reader.Read()) { object[] values = new object[reader.FieldCount]; reader.GetValues(values); for (int i = 0; i < values.Length; i++) { Console.Write("{0}\t\t", values[i]); } Console.Write("\r\n"); } reader.Close(); command.Dispose(); } [Test] public void ExecuteScalarTest() { PgCommand command = Connection.CreateCommand(); command.CommandText = "SELECT char_field FROM public.test_table where int4_field = @int4_field"; command.Parameters.Add("@int4_field", 2); string charFieldValue = command.ExecuteScalar().ToString(); Console.WriteLine("Scalar value: {0}", charFieldValue); command.Dispose(); } [Test] public void PrepareTest() { PgCommand command = Connection.CreateCommand(); command.CommandText = "SELECT char_field FROM public.test_table where int4_field = @int4_field"; command.Parameters.Add("@int4_field", 2); command.Prepare(); command.Dispose(); } [Test] public void NamedParametersTest() { PgCommand command = Connection.CreateCommand(); command.CommandText = "SELECT char_field FROM public.test_table where int4_field = @int4_field or char_field = @char_field"; command.Parameters.Add("@int4_field", 2); command.Parameters.Add("@char_field", "IRow 20"); PgDataReader reader = command.ExecuteReader(); int count = 0; while (reader.Read()) { Console.WriteLine(reader.GetValue(0)); count++; } Console.WriteLine("\r\n Record fetched {0} \r\n", count); reader.Close(); command.Dispose(); } [Test] public void ExecuteStoredProcTest() { PgCommand command = new PgCommand("TestCount", Connection); command.CommandType = CommandType.StoredProcedure; command.Parameters.Add("@CountResult", PgDbType.Int8).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); Console.WriteLine("ExecuteStoredProcTest - Count result {0}", command.Parameters[0].Value); command.Dispose(); } [Test] public void RecordsAffectedTest() { // Execute a SELECT command PgCommand selectCommand = new PgCommand("SELECT * FROM public.test_table WHERE int4_field = 100", Connection); int recordsAffected = selectCommand.ExecuteNonQuery(); Console.WriteLine("\r\nRecords Affected by SELECT command: {0}", recordsAffected); selectCommand.Dispose(); Assertion.Assert(recordsAffected == -1); // Execute a DELETE command PgCommand deleteCommand = new PgCommand("DELETE FROM public.test_table WHERE int4_field = 45", Connection); recordsAffected = deleteCommand.ExecuteNonQuery(); Console.WriteLine("\r\nRecords Affected by DELETE command: {0}", recordsAffected); deleteCommand.Dispose(); Assertion.Assert(recordsAffected == 1); } [Test] public void TestError782096() { Console.WriteLine("\r\nPgCommandTest.TestError782096"); PgCommand command = new PgCommand("SELECT * FROM public.test_table ORDER BY date_field", Connection); PgDataReader reader = command.ExecuteReader(); for (int i = 0; i < reader.FieldCount; i++) { Console.Write("{0}\t\t", reader.GetName(i)); } Console.Write("\r\n"); while (reader.Read()) { object[] values = new object[reader.FieldCount]; reader.GetValues(values); for (int i = 0; i < values.Length; i++) { Console.Write("{0}\t\t", values[i]); } Console.Write("\r\n"); } command.Dispose(); reader.Close(); } } } --- NEW FILE: PgDataReaderTest.cs --- /* PgSqlClient - ADO.NET Data Provider for PostgreSQL 7.4+ * Copyright (c) 2003-2004 Carlos Guzman Alvarez * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using System; using System.Data; using PostgreSql.Data.PgSqlClient; using NUnit.Framework; namespace PostgreSql.Data.PgSqlClient.UnitTests { [TestFixture] public class PgDataReaderTest : PgBaseTest { [Test] public void ReadTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table", Connection, transaction); Console.WriteLine("\r\nDataReader - Read Method - Test"); PgDataReader reader = command.ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { Console.Write(reader.GetValue(i) + "\t"); } Console.WriteLine(); } reader.Close(); command.Dispose(); transaction.Rollback(); } [Test] public void GetValuesTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table", Connection, transaction); Console.WriteLine("\r\nDataReader - Read Method - Test"); PgDataReader reader = command.ExecuteReader(); while (reader.Read()) { object[] values = new object[reader.FieldCount]; reader.GetValues(values); for(int i = 0; i < values.Length; i++) { Console.Write(values[i] + "\t"); } Console.WriteLine(); } reader.Close(); transaction.Rollback(); command.Dispose(); } [Test] public void IndexerByIndexTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table", Connection, transaction); Console.WriteLine("\r\nDataReader - Read Method - Test"); PgDataReader reader = command.ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { Console.Write(reader[i] + "\t"); } Console.WriteLine(); } reader.Close(); transaction.Rollback(); command.Dispose(); } [Test] public void IndexerByNameTest() { PgTransaction transaction = Connection.BeginTransaction(); PgCommand command = new PgCommand("SELECT * FROM public.test_table", Connection, transaction); Console.WriteLine("\r\nDataReader - Read Method - Test"); PgDataReader reader = command.ExecuteReader(); while (reader.Read()) { for (int i = 0; i < reader.FieldCount; i++) { Console.Write(reader[reader.GetName(i)] + "\t"); } Console.WriteLine(); }... [truncated message content] |