LCOV - code coverage report
Current view: top level - src/plugins/csvstorage - testmod_csvstorage.c (source / functions) Hit Total Coverage
Test: coverage-filtered.info Lines: 141 141 100.0 %
Date: 2019-09-12 12:28:41 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file
       3             :  *
       4             :  * @brief Tests for csvstorage plugin
       5             :  *
       6             :  * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
       7             :  *
       8             :  */
       9             : 
      10             : #include <stdlib.h>
      11             : #include <string.h>
      12             : 
      13             : #include <kdbease.h>
      14             : #include <kdbhelper.h>
      15             : 
      16             : #include <tests_plugin.h>
      17             : 
      18           4 : static void testread (const char * file)
      19             : {
      20           4 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
      21           4 :         KeySet * conf = ksNew (20, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END),
      22             :                                keyNew ("system/header", KEY_VALUE, "colname", KEY_END), KS_END);
      23           4 :         PLUGIN_OPEN ("csvstorage");
      24           4 :         KeySet * ks = ksNew (0, KS_END);
      25           4 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
      26             :         Key * key;
      27           4 :         key = ksLookupByName (ks, "user/tests/csvstorage/#1/col1", 0);
      28           4 :         exit_if_fail (key, "key not found");
      29           4 :         succeed_if (strcmp (keyString (key), "l1c1") == 0, "wrong key");
      30           4 :         key = ksLookupByName (ks, "user/tests/csvstorage/#2/col2", 0);
      31           4 :         exit_if_fail (key, "key not found");
      32           4 :         succeed_if (strcmp (keyString (key), "l2c2") == 0, "wrong key");
      33             : 
      34           4 :         ksDel (ks);
      35           4 :         keyDel (parentKey);
      36             : 
      37           4 :         PLUGIN_CLOSE ();
      38           4 : }
      39           2 : static void testreadfixcolcount (const char * file)
      40             : {
      41           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
      42           2 :         KeySet * conf =
      43           2 :                 ksNew (20, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END), keyNew ("system/header", KEY_VALUE, "colname", KEY_END),
      44             :                        keyNew ("system/columns", KEY_VALUE, "4", KEY_END), KS_END);
      45           2 :         PLUGIN_OPEN ("csvstorage");
      46           2 :         KeySet * ks = ksNew (0, KS_END);
      47           2 :         int ret = plugin->kdbGet (plugin, ks, parentKey);
      48           2 :         succeed_if (ret == (-1), "call to kdbGet was successful but shouldn't have been");
      49           2 :         ksDel (ks);
      50           2 :         keyDel (parentKey);
      51             : 
      52           2 :         PLUGIN_CLOSE ();
      53           2 : }
      54           2 : static void testreadwriteinvalid (const char * file)
      55             : {
      56           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
      57           2 :         KeySet * conf = ksNew (10, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END), KS_END);
      58           2 :         KeySet * ks = ksNew (0, KS_END);
      59           2 :         PLUGIN_OPEN ("csvstorage");
      60           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) > 0, "call to kdbGet was not successful");
      61           2 :         succeed_if (!output_warnings (parentKey), "no warnings in kdbGet");
      62           2 :         keySetString (parentKey, elektraFilename ());
      63           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "error: wrote invalid data");
      64           2 :         ksDel (ks);
      65           2 :         keyDel (parentKey);
      66           2 :         PLUGIN_CLOSE ();
      67           2 : }
      68             : 
      69             : 
      70           2 : static void testwriteinvalidheader (const char * file)
      71             : {
      72             : 
      73           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
      74           2 :         KeySet * conf = ksNew (20, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END),
      75             :                                keyNew ("system/header", KEY_VALUE, "colname", KEY_END), KS_END);
      76             : 
      77           2 :         KeySet * ks = ksNew (0, KS_END);
      78           2 :         PLUGIN_OPEN ("csvstorage");
      79           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) > 0, "call to kdbGet was not successful");
      80           2 :         succeed_if (!output_warnings (parentKey), "no warnings in kdbGet");
      81           2 :         keySetString (parentKey, elektraFilename ());
      82           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) == (-1), "error: wrote invalid data");
      83           2 :         ksDel (ks);
      84           2 :         keyDel (parentKey);
      85           2 :         PLUGIN_CLOSE ();
      86           2 : }
      87             : 
      88           2 : static void testwritevalidemptycol (const char * file)
      89             : {
      90             : 
      91           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
      92           2 :         KeySet * conf = ksNew (20, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END),
      93             :                                keyNew ("system/header", KEY_VALUE, "colname", KEY_END), KS_END);
      94             : 
      95           2 :         KeySet * ks = ksNew (0, KS_END);
      96           2 :         PLUGIN_OPEN ("csvstorage");
      97           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) > 0, "call to kdbGet was not successful");
      98           2 :         keySetString (parentKey, elektraFilename ());
      99           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 0, "error: couldn't write data");
     100           2 :         ksDel (ks);
     101           2 :         keyDel (parentKey);
     102           2 :         PLUGIN_CLOSE ();
     103           2 : }
     104           2 : static void testSetColnames (const char * file)
     105             : {
     106           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
     107           2 :         KeySet * conf =
     108           2 :                 ksNew (20, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END), keyNew ("system/header", KEY_VALUE, "colname", KEY_END),
     109             :                        keyNew ("system/columns", KEY_VALUE, "2", KEY_END), keyNew ("system/columns/names", KEY_VALUE, "", KEY_END),
     110             :                        keyNew ("system/columns/names/#0", KEY_VALUE, "col0Name", KEY_END),
     111             :                        keyNew ("system/columns/names/#1", KEY_VALUE, "col1Name", KEY_END), KS_END);
     112           2 :         PLUGIN_OPEN ("csvstorage");
     113           2 :         KeySet * ks = ksNew (0, KS_END);
     114           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
     115             :         Key * key;
     116           2 :         key = ksLookupByName (ks, "user/tests/csvstorage/#1/col0Name", 0);
     117           2 :         exit_if_fail (key, "key not found");
     118           2 :         succeed_if (strcmp (keyString (key), "l1c1") == 0, "wrong key");
     119           2 :         key = ksLookupByName (ks, "user/tests/csvstorage/#2/col1Name", 0);
     120           2 :         exit_if_fail (key, "key not found");
     121           2 :         succeed_if (strcmp (keyString (key), "l2c2") == 0, "wrong key");
     122             : 
     123           2 :         ksDel (ks);
     124           2 :         keyDel (parentKey);
     125             : 
     126           2 :         PLUGIN_CLOSE ();
     127           2 : }
     128             : 
     129           2 : static void testreadwritecomplicated (const char * file)
     130             : {
     131           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
     132           2 :         KeySet * conf = ksNew (10, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END),
     133             :                                keyNew ("system/header", KEY_VALUE, "colname", KEY_END), KS_END);
     134           2 :         KeySet * ks = ksNew (0, KS_END);
     135           2 :         PLUGIN_OPEN ("csvstorage");
     136           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) > 0, "call to kdbGet was not successful");
     137           2 :         succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/csvstorage/#1/col3", KDB_O_NONE)), "l1;c3"),
     138             :                     "key value doesn't match expected value");
     139           2 :         succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/csvstorage/#1/col4", KDB_O_NONE)), "l1\"\"c4"),
     140             :                     "key value doesn't match expected value");
     141           2 :         succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/csvstorage/#2/col3", KDB_O_NONE)), "l2\nc3"),
     142             :                     "key value doesn't match expected value");
     143           2 :         succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/csvstorage/#4/col4", KDB_O_NONE)), "l4\"\"c4"),
     144             :                     "key value doesn't match expected value");
     145           2 :         succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/csvstorage/#5/col3", KDB_O_NONE)), "l5\"\"\nc3"),
     146             :                     "key value doesn't match expected value");
     147             : 
     148           2 :         keySetString (parentKey, elektraFilename ());
     149           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 0, "error: wrote invalid data");
     150           2 :         succeed_if (compare_line_files (srcdir_file (file), keyString (parentKey)), "files do not match as expected");
     151             : 
     152           2 :         ksDel (ks);
     153           2 :         keyDel (parentKey);
     154           2 :         PLUGIN_CLOSE ();
     155           2 : }
     156             : 
     157           2 : static void testreadunescapedDQuote (const char * file)
     158             : {
     159           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, srcdir_file (file), KEY_END);
     160           2 :         KeySet * conf = ksNew (10, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END),
     161             :                                keyNew ("system/header", KEY_VALUE, "colname", KEY_END), KS_END);
     162           2 :         KeySet * ks = ksNew (0, KS_END);
     163           2 :         PLUGIN_OPEN ("csvstorage");
     164           2 :         succeed_if (plugin->kdbGet (plugin, ks, parentKey) > 0, "call to kdbGet was not successful");
     165           2 :         succeed_if (!output_warnings (parentKey), "no warnings in kdbGet");
     166           2 :         succeed_if (!strcmp (keyString (ksLookupByName (ks, "user/tests/csvstorage/#3/col3", KDB_O_NONE)), "l3\"c3"),
     167             :                     "key value doesn't match expected value");
     168             : 
     169           2 :         ksDel (ks);
     170           2 :         keyDel (parentKey);
     171           2 :         PLUGIN_CLOSE ();
     172           2 : }
     173             : 
     174           2 : static void testexportmissing (const char * file)
     175             : {
     176           2 :         Key * parentKey = keyNew ("user/tests/csvstorage", KEY_VALUE, elektraFilename (), KEY_END);
     177           2 :         KeySet * conf = ksNew (10, keyNew ("system/delimiter", KEY_VALUE, ";", KEY_END), keyNew ("system/export", KEY_VALUE, "", KEY_END),
     178             :                                keyNew ("system/export/a", KEY_VALUE, "", KEY_END), KS_END);
     179           2 :         KeySet * ks = ksNew (10, keyNew ("user/tests/csvstorage/#0", KEY_VALUE, "", KEY_END),
     180             :                              keyNew ("user/tests/csvstorage/#0/a", KEY_VALUE, "a1", KEY_END),
     181             :                              keyNew ("user/tests/csvstorage/#0/b", KEY_VALUE, "b", KEY_END),
     182             :                              keyNew ("user/tests/csvstorage/#1", KEY_VALUE, "", KEY_END),
     183             :                              keyNew ("user/tests/csvstorage/#1/a", KEY_VALUE, "a2", KEY_END), KS_END);
     184           2 :         PLUGIN_OPEN ("csvstorage");
     185           2 :         succeed_if (plugin->kdbSet (plugin, ks, parentKey) >= 0, "call to kdbSet was not successful");
     186           2 :         succeed_if (compare_line_files (srcdir_file (file), keyString (parentKey)), "files do not match as expected");
     187             : 
     188           2 :         ksDel (ks);
     189           2 :         keyDel (parentKey);
     190           2 :         PLUGIN_CLOSE ();
     191           2 : }
     192             : 
     193           2 : int main (int argc, char ** argv)
     194             : {
     195           2 :         printf ("CSVSTORAGE     TESTS\n");
     196           2 :         printf ("====================\n\n");
     197           2 :         printf ("Some tests report warnings because of invalid CSV files\n");
     198             : 
     199           2 :         init (argc, argv);
     200             : 
     201           2 :         testread ("csvstorage/valid.csv");
     202           2 :         testread ("csvstorage/validDos.csv");
     203           2 :         testreadfixcolcount ("csvstorage/valid.csv");
     204           2 :         testreadwriteinvalid ("csvstorage/invalid_columns.csv");
     205           2 :         testwriteinvalidheader ("csvstorage/invalid_columns_header2.csv");
     206           2 :         testwritevalidemptycol ("csvstorage/valid_empty_col.csv");
     207           2 :         testSetColnames ("csvstorage/valid.csv");
     208           2 :         testreadwritecomplicated ("csvstorage/complicated.csv");
     209           2 :         testreadunescapedDQuote ("csvstorage/unescapedQuote.csv");
     210           2 :         testexportmissing ("csvstorage/exporttest.csv");
     211           2 :         print_result ("testmod_csvstorage");
     212             : 
     213           2 :         return nbError;
     214             : }

Generated by: LCOV version 1.13