DROP FUNCTION IF EXISTS test_given;
CREATE FUNCTION test_given()
    RETURNS void
    LANGUAGE 'plpgsql'
VOLATILE
AS
$BODY$
BEGIN
    INSERT INTO hive.blocks
    VALUES
          ( 1, '\xBADD10', '\xCAFE10', '2016-06-22 19:10:21-07'::timestamp )
        , ( 2, '\xBADD20', '\xCAFE20', '2016-06-22 19:10:24-07'::timestamp )
    ;

    PERFORM hive.app_create_context( 'context' );
    CREATE SCHEMA A;
    CREATE TABLE A.table1(id  INTEGER ) INHERITS( hive.context );
END;
$BODY$
;

DROP FUNCTION IF EXISTS test_when;
CREATE FUNCTION test_when()
    RETURNS void
    LANGUAGE 'plpgsql'
    VOLATILE
AS
$BODY$
BEGIN
    BEGIN
        PERFORM hive.app_context_detached_get_block_num( 'context' );
        ASSERT FALSE, 'No exception when context is attached';
        EXCEPTION WHEN OTHERS THEN
    END;

    PERFORM hive.app_context_detach( 'context' );

    BEGIN
        PERFORM hive.app_context_detached_get_block_num( 'unexisted_context', 2 );
        ASSERT FALSE, 'No exception when unexisted_context';
        EXCEPTION WHEN OTHERS THEN
    END;
END;
$BODY$
;

DROP FUNCTION IF EXISTS test_then;
CREATE FUNCTION test_then()
    RETURNS void
    LANGUAGE 'plpgsql'
STABLE
AS
$BODY$
BEGIN
    return;
    ASSERT ( SELECT hive.app_context_detached_get_block_num( 'context' ) ) IS NULL, 'NULL was not returned';
END;
$BODY$
;




