<commit_before>package MediaWords::Test::DB;

# database utility functions for testing.  includes functionality to run tests on a temporary db

use strict;
use warnings;

use File::Path;

use DBIx::Simple::MediaWords;
use MediaWords::DB;
use MediaWords::Pg::Schema;
use MediaWords::Util::Config;

# create a new database to use for testing; execute the schema definition sql
sub _create_test_database
{
    my $base_db = DBIx::Simple::MediaWords->connect( MediaWords::DB::connect_info );

    my $test_db_name = 'mediacloud_test_' . time() . '_' . $$;

    # print "creating database $test_db_name ...\n";
    $base_db->query( "create database $test_db_name" );

    $base_db->disconnect();

    my $test_connect_info = [ MediaWords::DB::connect_info ];
    $test_connect_info->[ 0 ] =~ s/dbname=[a-z0-9_]*/dbname=$test_db_name/i;

    # print "connecting to test database: $test_connect_info->[0] ...\n";
    my $test_db = DBIx::Simple::MediaWords->connect( @{ $test_connect_info } );

    if ( !open( FILE, "$FindBin::Bin/../script/mediawords.sql" ) )
    {
        die( "Unable to open schema file: $!" );
    }

    my $schema_sql = join( "\n", ( <FILE> ) );

    close( FILE );

    $test_db->query( $schema_sql );
    $test_db->query( MediaWords::Pg::Schema::get_sql_function_definitions() );

    # make sure the stories table exists as a sanity check for the schema
    $test_db->query( "select * from stories" );

    return ( $test_db, $test_db_name, $test_connect_info );
}

# drop the test databse
sub _drop_test_database
{
    my ( $test_db_name ) = @_;

    my $base_db = DBIx::Simple::MediaWords->connect( MediaWords::DB::connect_info );

    $base_db->query( "drop database $test_db_name" );
}

# run the given function on a temporary, clean database
sub test_on_test_database
{
    my ( $sub ) = @_;

    my $result = MediaWords::Pg::Schema::recreate_db( 'test' );

    die "Error recreating the test db" unless $result == 0;

    my $db = MediaWords::DB::connect_to_db( 'test' );
    eval { $sub->( $db ); };

    if ( $@ )
    {
        die( $@ );
    }

    if ( $db )<commit_msg>Before initializing test database, set $ENV{ MEDIAWORDS_FORCE_USING_TEST_DATABASE } to 1 so that parts of code that want to reconnect to the database themselves and not use $db would know to use the test database<commit_after>package MediaWords::Test::DB;

# database utility functions for testing.  includes functionality to run tests on a temporary db

use strict;
use warnings;

use File::Path;

use DBIx::Simple::MediaWords;
use MediaWords::DB;
use MediaWords::Pg::Schema;
use MediaWords::Util::Config;

# create a new database to use for testing; execute the schema definition sql
sub _create_test_database
{
    my $base_db = DBIx::Simple::MediaWords->connect( MediaWords::DB::connect_info );

    my $test_db_name = 'mediacloud_test_' . time() . '_' . $$;

    # print "creating database $test_db_name ...\n";
    $base_db->query( "create database $test_db_name" );

    $base_db->disconnect();

    my $test_connect_info = [ MediaWords::DB::connect_info ];
    $test_connect_info->[ 0 ] =~ s/dbname=[a-z0-9_]*/dbname=$test_db_name/i;

    # print "connecting to test database: $test_connect_info->[0] ...\n";
    my $test_db = DBIx::Simple::MediaWords->connect( @{ $test_connect_info } );

    if ( !open( FILE, "$FindBin::Bin/../script/mediawords.sql" ) )
    {
        die( "Unable to open schema file: $!" );
    }

    my $schema_sql = join( "\n", ( <FILE> ) );

    close( FILE );

    $test_db->query( $schema_sql );
    $test_db->query( MediaWords::Pg::Schema::get_sql_function_definitions() );

    # make sure the stories table exists as a sanity check for the schema
    $test_db->query( "select * from stories" );

    return ( $test_db, $test_db_name, $test_connect_info );
}

# drop the test databse
sub _drop_test_database
{
    my ( $test_db_name ) = @_;

    my $base_db = DBIx::Simple::MediaWords->connect( MediaWords::DB::connect_info );

    $base_db->query( "drop database $test_db_name" );
}

# run the given function on a temporary, clean database
sub test_on_test_database
{
    my ( $sub ) = @_;

    $ENV{ MEDIAWORDS_FORCE_USING_TEST_DATABASE } = 1;

    my $result = MediaWords::Pg::Schema::recreate_db( 'test' );

    die "Error recreating the test db" unless $result == 0;

    my $db = MediaWords::DB::connect_to_db( 'test' );
    eval { $sub->( $db ); };

    if ( $@ )
    {
        die( $@ );
    }

    if ( $db )
{'fraction_non_alphanumeric': 0.14818140996856757, 'fraction_numerical': 0.002694207453973956, 'mean_word_length': 3.699367088607595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3711334', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1511, 'n_words': 385}