<commit_before>defmodule ConnectionTest do
  use ExUnit.Case, async: true
  alias Sqlite.DbConnection.Connection, as: P

  setup do
    opts = [database: ":memory:", backoff_type: :stop]
    {:ok, pid} = P.start_link(opts)
    {:ok, _} = Sqlite.DbConnection.Connection.query(pid, "CREATE TABLE uniques (a int UNIQUE)", [])
    {:ok, [pid: pid]}
  end

  test "prepare", context do
    pid = context[:pid]
    assert {:ok, stmt} = P.prepare(pid, "prepare_test", "SELECT 42", [])
    assert %Sqlite.DbConnection.Query{prepared: sqlitex,
                                      name: "prepare_test",
                                      statement: "SELECT 42"} = stmt
    assert %Sqlitex.Statement{column_names: [:"42"]} = sqlitex
  end

  test "prepare failure case", context do
    pid = context[:pid]
    assert {:error, err} = P.prepare(pid, "prepare_test", "huh", [])
    assert %Sqlite.DbConnection.Error{message: "near \"huh\": syntax error",
                                      sqlite: %{code: :sqlite_error}} = err
  end

  test "prepare!", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "prepare_test", "SELECT 42", [])
    assert %Sqlite.DbConnection.Query{prepared: sqlitex,
                                      name: "prepare_test",
                                      statement: "SELECT 42"} = stmt
    assert %Sqlitex.Statement{column_names: [:"42"]} = sqlitex
  end

  test "prepare! failure case", context do
    pid = context[:pid]
    assert_raise Sqlite.DbConnection.Error, fn ->
      P.prepare!(pid, "prepare_test", "huh", [])
    end
  end

  test "execute", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "execute_test", "SELECT 42", [])
    assert {:ok, result} = P.execute(pid, stmt, [])
    assert %Sqlite.DbConnection.Result{columns: ["42"],
                                       command: :select,
                                       num_rows: 1,
                                       rows: [[42]]} = result
  end

  test "execute failure case", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "execute_fail",
                      "INSERT INTO uniques values (1), (1)", [])
    assert {:error, err} = P.execute(pid, stmt, [])
    assert %Sqlite.DbConnection.Error{message: "UNIQUE constraint failed: uniques.a",
                                      sqlite: %{code: :constraint}} = err
  end
end<commit_msg>Add coverage for Connection.execute!.<commit_after>defmodule ConnectionTest do
  use ExUnit.Case, async: true
  alias Sqlite.DbConnection.Connection, as: P

  setup do
    opts = [database: ":memory:", backoff_type: :stop]
    {:ok, pid} = P.start_link(opts)
    {:ok, _} = Sqlite.DbConnection.Connection.query(pid, "CREATE TABLE uniques (a int UNIQUE)", [])
    {:ok, [pid: pid]}
  end

  test "prepare", context do
    pid = context[:pid]
    assert {:ok, stmt} = P.prepare(pid, "prepare_test", "SELECT 42", [])
    assert %Sqlite.DbConnection.Query{prepared: sqlitex,
                                      name: "prepare_test",
                                      statement: "SELECT 42"} = stmt
    assert %Sqlitex.Statement{column_names: [:"42"]} = sqlitex
  end

  test "prepare failure case", context do
    pid = context[:pid]
    assert {:error, err} = P.prepare(pid, "prepare_test", "huh", [])
    assert %Sqlite.DbConnection.Error{message: "near \"huh\": syntax error",
                                      sqlite: %{code: :sqlite_error}} = err
  end

  test "prepare!", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "prepare_test", "SELECT 42", [])
    assert %Sqlite.DbConnection.Query{prepared: sqlitex,
                                      name: "prepare_test",
                                      statement: "SELECT 42"} = stmt
    assert %Sqlitex.Statement{column_names: [:"42"]} = sqlitex
  end

  test "prepare! failure case", context do
    pid = context[:pid]
    assert_raise Sqlite.DbConnection.Error, fn ->
      P.prepare!(pid, "prepare_test", "huh", [])
    end
  end

  test "execute", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "execute_test", "SELECT 42", [])
    assert {:ok, result} = P.execute(pid, stmt, [])
    assert %Sqlite.DbConnection.Result{columns: ["42"],
                                       command: :select,
                                       num_rows: 1,
                                       rows: [[42]]} = result
  end

  test "execute failure case", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "execute_fail",
                      "INSERT INTO uniques values (1), (1)", [])
    assert {:error, err} = P.execute(pid, stmt, [])
    assert %Sqlite.DbConnection.Error{message: "UNIQUE constraint failed: uniques.a",
                                      sqlite: %{code: :constraint}} = err
  end

  test "execute!", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "execute_test", "SELECT 42", [])
    result = P.execute!(pid, stmt, [])
    assert %Sqlite.DbConnection.Result{columns: ["42"],
                                       command: :select,
                                       num_rows: 1,
                                       rows: [[42]]} = result
  end

  test "execute! failure case", context do
    pid = context[:pid]
    stmt = P.prepare!(pid, "execute_fail",
                      "INSERT INTO uniques values (1), (1)", [])
    assert_raise Sqlite.DbConnection.Error, fn ->
      P.execute!(pid, stmt, [])
    end
  end
end
{'fraction_non_alphanumeric': 0.14005499541704858, 'fraction_numerical': 0.009349220898258478, 'mean_word_length': 2.031111111111111, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3622535', 'n_tokens_mistral': 1704, 'n_tokens_neox': 1591, 'n_words': 425}