<commit_before>from io import StringIO
import tempfile

from mediawords.db import connect_to_db
from mediawords.db.export.export_tables import *
from mediawords.util.paths import mc_sql_schema_path
from mediawords.util.process import run_command_in_foreground
from mediawords.util.text import random_string


def test_print_exported_tables_to_backup_crawler():
    # Basic sanity test to make sure something gets printed out to STDOUT and that it can be imported back with `psql`
    db = connect_to_db()

    orig_stdout = sys.stdout
    sys.stdout = captured_stdout = StringIO()

    export_dump_exception = None
    try:
        print_exported_tables_to_backup_crawler(db=db)
    except Exception as ex:
        export_dump_exception = str(ex)

    sys.stdout = orig_stdout

    assert export_dump_exception is None

    sql_dump = captured_stdout.getvalue()
    assert 'COPY media' in sql_dump

    # Try importing with psql (because this is how it's going to be imported)
    temp_database_name = 'mediacloud_test_%s' % random_string(length=16)

    run_command_in_foreground(['createdb', temp_database_name])

    temp = tempfile.NamedTemporaryFile()
    temp.write(bytes(sql_dump, 'UTF-8'))
    temp.flush()
    temp_sql_dump_path = temp.name

    mediawords_sql_path = mc_sql_schema_path()

    run_command_in_foreground(['psql', '-v', 'ON_ERROR_STOP=1', '-f', mediawords_sql_path, '-d', temp_database_name])
    run_command_in_foreground(['psql', '-v', 'ON_ERROR_STOP=1', '-f', temp_sql_dump_path, '-d', temp_database_name])

    run_command_in_foreground(['dropdb', temp_database_name])<commit_msg>(Temporarily?) remove exported dump's import test<commit_after>from io import StringIO

from mediawords.db import connect_to_db
from mediawords.db.export.export_tables import *


def test_print_exported_tables_to_backup_crawler():
    # Basic sanity test to make sure something gets printed out to STDOUT
    # FIXME try importing the dump into a test database
    db = connect_to_db()

    orig_stdout = sys.stdout
    sys.stdout = captured_stdout = StringIO()

    export_dump_exception = None
    try:
        print_exported_tables_to_backup_crawler(db=db)
    except Exception as ex:
        export_dump_exception = str(ex)

    sys.stdout = orig_stdout

    assert export_dump_exception is None

    sql_dump = captured_stdout.getvalue()
    assert 'COPY media' in sql_dump
{'fraction_non_alphanumeric': 0.07795473595976529, 'fraction_numerical': 0.0020955574182732607, 'mean_word_length': 4.437357630979498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1082041', 'n_tokens_mistral': 766, 'n_tokens_neox': 738, 'n_words': 189}