LCOV - code coverage report
Current view: top level - sq - test_sq.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 78.6 % 70 55
Test Date: 2025-12-28 14:06:02 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   Copyright (C) 2020 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify it under the
       6              :   terms of the GNU General Public License as published by the Free Software
       7              :   Foundation; either version 3, or (at your option) any later version.
       8              : 
       9              :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10              :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11              :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12              : 
      13              :   You should have received a copy of the GNU General Public License along with
      14              :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15              : */
      16              : /**
      17              :  * @file sq/test_sq.c
      18              :  * @brief Tests for SQLite3 convenience API
      19              :  * @author Jonathan Buchanan
      20              :  */
      21              : #include "taler/platform.h"
      22              : #include "taler/taler_sq_lib.h"
      23              : 
      24              : 
      25              : /**
      26              :  * Run actual test queries.
      27              :  *
      28              :  * @return 0 on success
      29              :  */
      30              : static int
      31            1 : run_queries (sqlite3 *db)
      32              : {
      33              :   struct TALER_Amount hamount;
      34              :   struct TALER_AmountNBO namount;
      35              :   json_t *json;
      36              :   sqlite3_stmt *test_insert;
      37              :   sqlite3_stmt *test_select;
      38            1 :   struct GNUNET_SQ_PrepareStatement ps[] = {
      39            1 :     GNUNET_SQ_make_prepare ("INSERT INTO test_sq ("
      40              :                             " hamount_val"
      41              :                             ",hamount_frac"
      42              :                             ",namount_val"
      43              :                             ",namount_frac"
      44              :                             ",json"
      45              :                             ") VALUES "
      46              :                             "($1, $2, $3, $4, $5)",
      47              :                             &test_insert),
      48            1 :     GNUNET_SQ_make_prepare ("SELECT"
      49              :                             " hamount_val"
      50              :                             ",hamount_frac"
      51              :                             ",namount_val"
      52              :                             ",namount_frac"
      53              :                             ",json"
      54              :                             " FROM test_sq",
      55              :                             &test_select),
      56              :     GNUNET_SQ_PREPARE_END
      57              :   };
      58            1 :   int ret = 0;
      59              : 
      60            1 :   GNUNET_assert (GNUNET_OK ==
      61              :                  TALER_string_to_amount ("EUR:1.23",
      62              :                                          &hamount));
      63            1 :   TALER_amount_hton (&namount,
      64              :                      &hamount);
      65            1 :   json = json_object ();
      66            1 :   GNUNET_assert (NULL != json);
      67            1 :   GNUNET_assert (0 ==
      68              :                  json_object_set_new (json,
      69              :                                       "foo",
      70              :                                       json_integer (42)));
      71            1 :   GNUNET_assert (NULL != json);
      72            1 :   GNUNET_assert (GNUNET_OK ==
      73              :                  GNUNET_SQ_prepare (db,
      74              :                                     ps));
      75              : 
      76              :   {
      77            1 :     struct GNUNET_SQ_QueryParam params_insert[] = {
      78            1 :       TALER_SQ_query_param_amount (&hamount),
      79            1 :       TALER_SQ_query_param_amount_nbo (&namount),
      80            1 :       TALER_SQ_query_param_json (json),
      81              :       GNUNET_SQ_query_param_end
      82              :     };
      83            1 :     GNUNET_SQ_reset (db,
      84              :                      test_insert);
      85            1 :     GNUNET_assert (GNUNET_OK == GNUNET_SQ_bind (test_insert,
      86              :                                                 params_insert));
      87            1 :     GNUNET_assert (SQLITE_DONE == sqlite3_step (test_insert));
      88            1 :     sqlite3_finalize (test_insert);
      89              :   }
      90              : 
      91              :   {
      92              :     struct TALER_Amount result_amount;
      93              :     struct TALER_AmountNBO nresult_amount;
      94              :     struct TALER_Amount nresult_amount_converted;
      95              :     json_t *result_json;
      96            1 :     struct GNUNET_SQ_QueryParam params_select[] = {
      97              :       GNUNET_SQ_query_param_end
      98              :     };
      99            1 :     struct GNUNET_SQ_ResultSpec results_select[] = {
     100            1 :       TALER_SQ_result_spec_amount ("EUR",
     101              :                                    &result_amount),
     102            1 :       TALER_SQ_result_spec_amount_nbo ("EUR",
     103              :                                        &nresult_amount),
     104            1 :       TALER_SQ_result_spec_json (&result_json),
     105              :       GNUNET_SQ_result_spec_end
     106              :     };
     107              : 
     108            1 :     GNUNET_SQ_reset (db,
     109              :                      test_select);
     110            1 :     GNUNET_assert (GNUNET_OK == GNUNET_SQ_bind (test_select,
     111              :                                                 params_select));
     112            1 :     GNUNET_assert (SQLITE_ROW == sqlite3_step (test_select));
     113              : 
     114            1 :     GNUNET_assert (GNUNET_OK == GNUNET_SQ_extract_result (test_select,
     115              :                                                           results_select));
     116            1 :     TALER_amount_ntoh (&nresult_amount_converted,
     117              :                        &nresult_amount);
     118            1 :     if ((GNUNET_OK != TALER_amount_cmp_currency (&hamount,
     119            1 :                                                  &result_amount)) ||
     120            1 :         (0 != TALER_amount_cmp (&hamount,
     121            1 :                                 &result_amount)) ||
     122            1 :         (GNUNET_OK != TALER_amount_cmp_currency (&hamount,
     123            1 :                                                  &nresult_amount_converted)) ||
     124            1 :         (0 != TALER_amount_cmp (&hamount,
     125            1 :                                 &nresult_amount_converted)) ||
     126            1 :         (1 != json_equal (json,
     127              :                           result_json)) )
     128              :     {
     129            0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     130              :                   "Result from database doesn't match input\n");
     131            0 :       ret = 1;
     132              :     }
     133            1 :     GNUNET_SQ_cleanup_result (results_select);
     134            1 :     sqlite3_finalize (test_select);
     135              :   }
     136            1 :   json_decref (json);
     137              : 
     138            1 :   return ret;
     139              : }
     140              : 
     141              : 
     142              : int
     143            1 : main (int argc,
     144              :       const char *const argv[])
     145              : {
     146            1 :   struct GNUNET_SQ_ExecuteStatement es[] = {
     147            1 :     GNUNET_SQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS test_sq ("
     148              :                             " hamount_val INT8 NOT NULL"
     149              :                             ",hamount_frac INT8 NOT NULL"
     150              :                             ",namount_val INT8 NOT NULL"
     151              :                             ",namount_frac INT8 NOT NULL"
     152              :                             ",json VARCHAR NOT NULL"
     153              :                             ")"),
     154              :     GNUNET_SQ_EXECUTE_STATEMENT_END
     155              :   };
     156              :   sqlite3 *db;
     157              :   int ret;
     158              : 
     159              :   (void) argc;
     160              :   (void) argv;
     161            1 :   GNUNET_log_setup ("test-pq",
     162              :                     "WARNING",
     163              :                     NULL);
     164              : 
     165            1 :   if (SQLITE_OK != sqlite3_open ("talercheck.db",
     166              :                                  &db))
     167              :   {
     168            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     169              :                 "Failed to open SQLite3 database\n");
     170            0 :     return 77;
     171              :   }
     172              : 
     173            1 :   if (GNUNET_OK != GNUNET_SQ_exec_statements (db,
     174              :                                               es))
     175              :   {
     176            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     177              :                 "Failed to create new table\n");
     178            0 :     if ((SQLITE_OK != sqlite3_close (db)) ||
     179            0 :         (0 != unlink ("talercheck.db")))
     180              :     {
     181            0 :       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     182              :                   "Failed to close db or unlink\n");
     183              :     }
     184            0 :     return 1;
     185              :   }
     186              : 
     187            1 :   ret = run_queries (db);
     188              : 
     189            1 :   if (SQLITE_OK !=
     190            1 :       sqlite3_exec (db,
     191              :                     "DROP TABLE test_sq",
     192              :                     NULL, NULL, NULL))
     193              :   {
     194            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     195              :                 "Failed to drop table\n");
     196            0 :     ret = 1;
     197              :   }
     198              : 
     199            1 :   if (SQLITE_OK != sqlite3_close (db))
     200              :   {
     201            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     202              :                 "Failed to close database\n");
     203            0 :     ret = 1;
     204              :   }
     205            1 :   if (0 != unlink ("talercheck.db"))
     206              :   {
     207            0 :     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
     208              :                 "Failed to unlink test database file\n");
     209            0 :     ret = 1;
     210              :   }
     211            1 :   return ret;
     212              : }
     213              : 
     214              : 
     215              : /* end of sq/test_sq.c */
        

Generated by: LCOV version 2.0-1