<commit_before>%% -------------------------------------------------------------------
%%
%% bdberl: DB API Tests
%% Copyright (c) 2008 The Hive.  All rights reserved.
%%
%% -------------------------------------------------------------------
-module(bdberl_SUITE).

-compile(export_all).

-include_lib("ct.hrl").

ball() ->
    [open_should_create_database_if_none_exists,
     open_should_allow_opening_multiple_databases,
     close_should_fail_with_invalid_db_handle,
     get_should_fail_when_getting_a_nonexistant_record,
     get_should_return_a_value_when_getting_a_valid_record,
     put_should_succeed_with_manual_transaction,
     put_should_rollback_with_failed_manual_transaction,
     transaction_should_commit_on_success,
     transaction_should_abort_on_exception,
     transaction_should_abort_on_user_abort,
     update_should_save_value_if_successful,
     update_should_accept_args_for_fun,
     port_should_tune_transaction_timeouts,
     cursor_should_iterate, cursor_should_fail_if_not_open,
     put_commit_should_end_txn,
     data_dir_should_be_priv_dir,
     delete_should_remove_file, 
     delete_should_fail_if_db_inuse].


init_per_suite(Config) ->
    DbHome = ?config(priv_dir, Config),
    os:putenv("DB_HOME", DbHome),
    ok = file:write_file(DbHome ++ "DB_CONFIG", <<"set_data_dir ", (list_to_binary(DbHome))/binary, "\n">>),
    Config.

end_per_suite(_Config) ->
    ok.

init_per_testcase(_TestCase, Config) ->
    {ok, Db} = bdberl:open("api_test.db", btree, [create, exclusive]),
    [{db, Db}|Config].

end_per_testcase(_TestCase, Config) ->
    ok = bdberl:close(?config(db, Config)),
    ok = file:delete(filename:join([?config(priv_dir, Config), "api_test.db"])).


open_should_create_database_if_none_exists(Config) ->
    DbName = filename:join([?config(priv_dir, Config), "api_test.db"]),
    true = filelib:is_file(DbName).

open_should_allow_opening_multiple_databases(_Config) ->
    %% Open up another db -- should use dbref 1 as that's the first available
    {ok, 1} = bdberl:open("api_test2.db", btree).

close_should_fail_with_invalid_db_handle(_Config) ->
    {error, invalid_db} = bdberl:close(21000).

get_should_fail_when_getting_a_nonexistant_record(Config) ->
    not_found = bdberl:get(?config(db, Config), bad_key).

get_should_return_a_value_when_getting_a_valid_record(Config) ->
    Db = ?config(db, Config),<commit_msg>Add more triumphant configuration system for controlling BDB in unit tests<commit_after>%% -------------------------------------------------------------------
%%
%% bdberl: DB API Tests
%% Copyright (c) 2008 The Hive.  All rights reserved.
%%
%% -------------------------------------------------------------------
-module(bdberl_SUITE).

-compile(export_all).

-include_lib("ct.hrl").

all() ->
    [open_should_create_database_if_none_exists,
     open_should_allow_opening_multiple_databases,
     close_should_fail_with_invalid_db_handle,
     get_should_fail_when_getting_a_nonexistant_record,
     get_should_return_a_value_when_getting_a_valid_record,
     put_should_succeed_with_manual_transaction,
     put_should_rollback_with_failed_manual_transaction,
     transaction_should_commit_on_success,
     transaction_should_abort_on_exception,
     transaction_should_abort_on_user_abort,
     update_should_save_value_if_successful,
     update_should_accept_args_for_fun,
     port_should_tune_transaction_timeouts,
     cursor_should_iterate, cursor_should_fail_if_not_open,
     put_commit_should_end_txn,
     data_dir_should_be_priv_dir,
     delete_should_remove_file, 
     delete_should_fail_if_db_inuse].

dbconfig(Config) ->
    Cfg = [{set_data_dir, ?config(priv_dir, Config)},
           {set_flags, 'DB_TXN_NOSYNC'}],
    list_to_binary(lists:flatten([io_lib:format("~s ~s\n", [K,V]) || {K, V} <- Cfg])).


init_per_suite(Config) ->
    DbHome = ?config(priv_dir, Config),
    os:putenv("DB_HOME", DbHome),
    ok = file:write_file(DbHome ++ "DB_CONFIG", dbconfig(Config)),
    Config.

end_per_suite(_Config) ->
    ok.

init_per_testcase(_TestCase, Config) ->
    {ok, Db} = bdberl:open("api_test.db", btree, [create, exclusive]),
    [{db, Db}|Config].

end_per_testcase(_TestCase, Config) ->
    ok = bdberl:close(?config(db, Config)),
    ok = file:delete(filename:join([?config(priv_dir, Config), "api_test.db"])).


open_should_create_database_if_none_exists(Config) ->
    DbName = filename:join([?config(priv_dir, Config), "api_test.db"]),
    true = filelib:is_file(DbName).

open_should_allow_opening_multiple_databases(_Config) ->
    %% Open up another db -- should use dbref 1 as that's the first available
    {ok, 1} = bdberl:open("api_test2.db", btree).

close_should_fail_with_invalid_db_handle(_Config) ->
    {error, invalid_db} = bdberl:close(21000).

get_should_fail_when_getting_a_nonexistant_record(Config) ->
    not_found = bdberl:get(?config(db, Config), bad_key).

get_should_return_a_value_when_getting_a_valid_record(Config) ->
    Db = ?config(db, Config),
{'fraction_non_alphanumeric': 0.16059271125350422, 'fraction_numerical': 0.004805766920304365, 'mean_word_length': 3.980059820538385, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 26, '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': '731679', 'n_tokens_mistral': 1767, 'n_tokens_neox': 1706, 'n_words': 236}