Pi

November 14, 2009

Unit Testing Postgres

Filed under: Uncategorized — rqmedes @ 11:55 am

I  have been using  Pg_Unit  to create unit tests for  my  latest postgres database application.
You can get PG_Unit from here  http://en.dklab.ru/lib/dklab_pgunit/

Pg_unit is pretty bare bones  allowing you to basically  assert values are the same and as the the  forums are in Russian it is hard to find any further information.

I find it useful to write tests for all my validation logic, checks and constraints. This is useful when you  have lots of tables and data validation checks.

A basic example:

insert into inventory(code) values (‘a”);
PERFORM   pgunit.assert_fail(‘insert into inventory(code) values (”a”);’);

which  inserts a record and then asserts that the same  record cannot be inserted again.

As pg_unit runs all the  tests within a transaction that is rolled back none of your test code will affect your data.

Here is the function to assert failures.

CREATE OR REPLACE FUNCTION pgunit.assert_fail(sql_statement text)
     RETURNS void AS
$BODY$
DECLARE ok boolean;
BEGIN
      BEGIN
            ok := false;
            execute  $1;
       EXCEPTION
       WHEN Others THEN
            ok := true;
       END;
       IF ok is false THEN
             PERFORM pgunit.fail('Should have Failed');
       END IF;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE
COST 100;

November 8, 2009

TryParse Shorthand

Filed under: Uncategorized — rqmedes @ 4:48 am

Because A property, indexer or dynamic member access may not be passed as an out or ref parameter TryParse can be annoying.
Here is the syntax I use to make it simpler.

int i;
Id = int.TryParse(“10”,out i) ? i : 0;

Blog at WordPress.com.