LCOV - code coverage report
Current view: top level - pairinteraction/unit_test - sqlite_test.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 56 56 100.0 %
Date: 2024-04-29 00:41:50 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2017 Sebastian Weber, Henri Menke. All rights reserved.
       3             :  *
       4             :  * This file is part of the pairinteraction library.
       5             :  *
       6             :  * The pairinteraction library is free software: you can redistribute it and/or modify
       7             :  * it under the terms of the GNU Lesser General Public License as published by
       8             :  * the Free Software Foundation, either version 3 of the License, or
       9             :  * (at your option) any later version.
      10             :  *
      11             :  * The pairinteraction library is distributed in the hope that it will be useful,
      12             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14             :  * GNU Lesser General Public License for more details.
      15             :  *
      16             :  * You should have received a copy of the GNU Lesser General Public License
      17             :  * along with the pairinteraction library. If not, see <http://www.gnu.org/licenses/>.
      18             :  */
      19             : 
      20             : #include "SQLite.hpp"
      21             : 
      22             : #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
      23             : #include <doctest/doctest.h>
      24             : 
      25             : #include <sstream>
      26             : #include <string>
      27             : #include <vector>
      28             : 
      29           1 : TEST_CASE("sqlite_thread_safety") // NOLINT
      30             : {
      31             :     // The sqlite3_threadsafe() function returns zero if and only if
      32             :     // SQLite was compiled with mutexing code omitted due to the
      33             :     // SQLITE_THREADSAFE compile-time option being set to 0.
      34           1 :     CHECK(sqlite3_threadsafe());
      35           1 : }
      36             : 
      37           1 : TEST_CASE("sqlite_query_test") // NOLINT
      38             : {
      39           3 :     CHECK_THROWS_AS(sqlite::handle("no such database", SQLITE_OPEN_READWRITE), sqlite::error);
      40             :     // Open an in-memory database for tests
      41           3 :     sqlite::handle db(":memory:");
      42             : 
      43           2 :     sqlite::statement stmt(db);
      44           2 :     CHECK_NOTHROW(stmt.set("This is not valid SQL"));
      45           1 :     CHECK_THROWS_AS(stmt.step(), sqlite::error);
      46           1 :     CHECK_THROWS_AS(stmt.prepare(), sqlite::error);
      47           3 :     CHECK_THROWS_AS(stmt.exec("Neither is this"), sqlite::error);
      48             : 
      49             :     // Check string calling
      50           2 :     std::string string_query("create table test(text,integer,real);");
      51           1 :     CHECK_NOTHROW(stmt.reset());
      52           1 :     CHECK_NOTHROW(stmt.set(string_query));
      53           1 :     CHECK_NOTHROW(stmt.prepare());
      54           1 :     CHECK(stmt.step() == false);
      55             : 
      56             :     // Check stringstream calling
      57           2 :     std::stringstream ss_query;
      58           1 :     ss_query << "insert into test values(?1,?2,?3);";
      59           1 :     CHECK_NOTHROW(stmt.reset());
      60           1 :     CHECK_NOTHROW(stmt.set(ss_query));
      61             :     // Insert some stuff
      62           1 :     CHECK_NOTHROW(stmt.prepare());
      63           2 :     CHECK_NOTHROW(stmt.bind(1, "Hello World!"));
      64           1 :     CHECK_NOTHROW(stmt.bind(2, 1729));
      65           1 :     CHECK_NOTHROW(stmt.bind(3, 0.5));
      66           1 :     CHECK(stmt.step() == false);
      67             :     // Reuse the query set above
      68           1 :     CHECK_NOTHROW(stmt.reset());
      69           1 :     CHECK_NOTHROW(stmt.prepare());
      70           2 :     CHECK_NOTHROW(stmt.bind(1, "Goodbye Earth!"));
      71           1 :     CHECK_NOTHROW(stmt.bind(2, 42));
      72           1 :     CHECK_NOTHROW(stmt.bind(3, 1.125));
      73           1 :     CHECK(stmt.step() == false);
      74           1 :     CHECK_THROWS_AS(stmt.step(), sqlite::error);
      75             : 
      76             :     // Check result
      77           1 :     CHECK_NOTHROW(stmt.reset());
      78           2 :     CHECK_NOTHROW(stmt.set("select * from test;"));
      79           1 :     CHECK_NOTHROW(stmt.prepare());
      80           1 :     CHECK(stmt.step() == true);
      81           1 :     CHECK(stmt.get<std::string>(0) == "Hello World!");
      82           1 :     CHECK(stmt.get<int>(1) == 1729);
      83           1 :     CHECK(stmt.get<double>(2) == 0.5);
      84           1 :     CHECK(stmt.step() == true);
      85           1 :     CHECK(stmt.get<std::string>(0) == "Goodbye Earth!");
      86           1 :     CHECK(stmt.get<int>(1) == 42);
      87           1 :     CHECK(stmt.get<double>(2) == 1.125);
      88           1 :     CHECK(stmt.step() == false);
      89             : 
      90             :     // Check iteration
      91           1 :     CHECK_NOTHROW(stmt.reset());
      92           2 :     CHECK_NOTHROW(stmt.set("select * from test;"));
      93           1 :     CHECK_NOTHROW(stmt.prepare());
      94             : 
      95           1 :     int count = 0;
      96           3 :     for (auto &&r : stmt) {
      97           2 :         CHECK_NOTHROW(r.get<std::string>(0));
      98           2 :         CHECK_NOTHROW(r.get<int>(1));
      99           2 :         CHECK_NOTHROW(r.get<double>(2));
     100           2 :         ++count;
     101             :     }
     102           1 :     CHECK(count == 2);
     103             : 
     104             : #ifndef NDEBUG
     105           1 :     CHECK_THROWS_AS(*(stmt.end()), std::out_of_range);
     106             : #endif
     107           1 : }

Generated by: LCOV version 1.14