IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 4383


Ignore:
Timestamp:
Jun 24, 2005, 11:31:13 AM (21 years ago)
Author:
evanalst
Message:

Update test case for invalid arguments for psDBUpdateRows.

Location:
trunk/psLib/test/dataIO
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/test/dataIO/tst_psDB.c

    r4374 r4383  
    99 *  @author Aaron Culliney, MHPCC
    1010 *
    11  *  @version $Revision: 1.26 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2005-06-24 02:11:59 $
     11 *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2005-06-24 21:31:13 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    7272static psMetadata *_get_invalid_row( void );
    7373static psMetadata *_get_where( void );
     74static psMetadata *_get_invalid_where( void );
     75static psMetadata *_get_where_bad_value( void );
    7476static psMetadata *_get_update_values(const char *val0, psS32 val1, psF32 val2, psF64 val3, psBool val4);
    75 static void _print_row( psMetadata *row );
    7677static bool _check_row(psMetadata* row);
     78static bool _check_row_update(psMetadata* row);
    7779static bool _check_const_row1(psMetadata* row);
    7880static bool _check_const_row2(psMetadata* row);
     
    106108                              {TPDBDumpRows,       850,  "dbDumpRows",        0, false},
    107109                              {TPDBDumpCols,       851,  "dbDumpCols",        0, false},
    108                               {TPDBUpdateRows,      -12, "dbUpdateRows",      0, false},
     110                              {TPDBUpdateRows,     852, "dbUpdateRows",      0, false},
    109111                              {TPDBDeleteRows,      -13, "dbDeleteRows",      0, false},
    110112                              //{TPDBCreate,        -14, "dbCreate",          0, false},
     
    280282}
    281283
     284psMetadata *_get_invalid_where( void )
     285{
     286    psMetadata *md = NULL;
     287    psMetadataItem *s32=NULL;
     288
     289    md = psMetadataAlloc();
     290    if (md == NULL) {
     291        psError(PS_ERR_UNKNOWN, true, "could not create psMetadata in %s!", __func__ );
     292        return NULL;
     293    }
     294    s32 = psMetadataItemAllocS32("col999", "", S32_1);
     295    if (s32 == NULL) {
     296        psError(PS_ERR_UNKNOWN, true, "could not create psMetadataItem in %s!", __func__ );
     297        return NULL;
     298    }
     299    if (!psMetadataAddItem(md, s32, 0, 0)) {
     300        psError(PS_ERR_UNKNOWN, true, "could not add psMetadataItem(s) to psMetadata in %s!", __func__ );
     301        return NULL;
     302    }
     303
     304    psFree(s32);
     305    return md;
     306}
     307
     308psMetadata *_get_where_bad_value( void )
     309{
     310    psMetadata *md = NULL;
     311    psMetadataItem *s32=NULL;
     312
     313    md = psMetadataAlloc();
     314    if (md == NULL) {
     315        psError(PS_ERR_UNKNOWN, true, "could not create psMetadata in %s!", __func__ );
     316        return NULL;
     317    }
     318    s32 = psMetadataItemAllocS32(TAB_COL_1_NAME, "", S32_1 + 1);
     319    if (s32 == NULL) {
     320        psError(PS_ERR_UNKNOWN, true, "could not create psMetadataItem in %s!", __func__ );
     321        return NULL;
     322    }
     323    if (!psMetadataAddItem(md, s32, 0, 0)) {
     324        psError(PS_ERR_UNKNOWN, true, "could not add psMetadataItem(s) to psMetadata in %s!", __func__ );
     325        return NULL;
     326    }
     327
     328    psFree(s32);
     329    return md;
     330}
     331
    282332psMetadata *_get_update_values(const char *val0, psS32 val1, psF32 val2, psF64 val3, psBool val4)
    283333{
     
    328378}
    329379
     380bool _check_row_update(psMetadata* row)
     381{
     382    bool              returnValue = true;
     383    psMetadataItem*   item        = NULL;
     384
     385    item = psMetadataLookup(row,TAB_COL_0_NAME);
     386    if((item==NULL) || (strcmp(item->data.V,STR_2)!=0)) {
     387        psError(PS_ERR_UNKNOWN,true,"Column key_string item %s not as expected %s",
     388                item->data.V,STR_2);
     389        return false;
     390    }
     391
     392    item = psMetadataLookup(row,TAB_COL_1_NAME);
     393    if((item==NULL) || (item->data.S32 != S32_2)) {
     394        psError(PS_ERR_UNKNOWN,true,"Column key_s32 item %d not as expected %d",
     395                item->data.S32, S32_2);
     396        return false;
     397    }
     398
     399    item = psMetadataLookup(row,TAB_COL_2_NAME);
     400    if((item==NULL) || (fabs(item->data.F32-F32_2)>ERROR_TOL)) {
     401        psError(PS_ERR_UNKNOWN,true,"Column key_f32 item %f not as expected %f",
     402                item->data.F32, F32_2);
     403        return false;
     404    }
     405
     406    item = psMetadataLookup(row,TAB_COL_3_NAME);
     407    if((item==NULL) || (fabs(item->data.F64-F64_2)>ERROR_TOL)) {
     408        psError(PS_ERR_UNKNOWN,true,"Column key_f64 item %lf not as expected %lf",
     409                item->data.F64,F64_2);
     410        return false;
     411    }
     412
     413    item = psMetadataLookup(row,TAB_COL_4_NAME);
     414    if((item==NULL) || (item->data.B != B_2)) {
     415        psError(PS_ERR_UNKNOWN,true,"Column key_bool item %d not as expected %d",
     416                item->data.B, B_2);
     417        return false;
     418    }
     419
     420    return returnValue;
     421}
     422
    330423bool _check_const_row1(psMetadata* row)
    331424{
     
    412505
    413506    return returnValue;
    414 }
    415 
    416 void _print_row( psMetadata *row )
    417 {
    418     psMetadataIterator *iter=NULL;
    419     psMetadataItem *item=NULL;
    420 
    421     iter = psMetadataIteratorAlloc(row, 0, NULL);
    422     while ((item = psMetadataGetAndIncrement(iter)) != NULL) {
    423         if (item->type == PS_META_STR) {
    424             psLogMsg( __func__, PS_LOG_INFO, "[%s]=[%s]\n", item->name, item->data.V );
    425         } else if (item->type == PS_META_S32) {
    426             psLogMsg( __func__, PS_LOG_INFO, "[%s]=[%d]\n", item->name, item->data.S32 );
    427         } else if (item->type == PS_META_F32) {
    428             psLogMsg( __func__, PS_LOG_INFO, "[%s]=[%g]\n", item->name, item->data.F32 );
    429         } else if (item->type == PS_META_F64) {
    430             psLogMsg( __func__, PS_LOG_INFO, "[%s]=[%g]\n", item->name, item->data.F64 );
    431         } else if (item->type == PS_META_BOOL) {
    432             psLogMsg( __func__, PS_LOG_INFO, "[%s]=[%s]\n", item->name, item->data.B ? "Yay" : "Nay" );
    433         }
    434     }
    435 
    436     psFree(iter);
    437507}
    438508
     
    17521822}
    17531823
    1754 // Testpoint #XXX, TPDBUpdateRows shall update rows in a test table ...
     1824// Testpoint #852, TPDBUpdateRows shall update rows in a test table ...
    17551825psS32 TPDBUpdateRows( void )
    17561826{
    1757     psS32 failed = 0;
    1758     psDB *dbh = NULL;
    1759     const char* table = "table10";
    1760     psArray *ary=NULL, *rowSet=NULL;
    1761     psMetadata *row=NULL, *where=NULL, *updates=NULL, *md = _get_CreateTableMetadata();
    1762     psMetadataItem *item=NULL;
    1763 
     1827    psDB*            dbh       = NULL;
     1828    const char*      table     = "table10";
     1829    psArray*         ary       = NULL;
     1830    psArray*         rowSet    = NULL;
     1831    psMetadata*      row       = NULL;
     1832    psMetadata*      where     = NULL;
     1833    psMetadata*      updates   = NULL;
     1834    psMetadata*      md        = NULL;
     1835    int              chgRows   = 0;
     1836
     1837    // Create database table definition
     1838    md = _get_CreateTableMetadata();
     1839
     1840    // Initialize database connection
    17641841    dbh = _init_psDB();
    17651842    if (dbh == NULL) {
     1843        psError(PS_ERR_UNKNOWN,true,"Did not expect return NULL when initializing database");
    17661844        return 1;
    17671845    }
    17681846
    1769     psLogMsg( __func__, PS_LOG_INFO, "psDBUpdateRows shall update rows in a test table.\n" );
    1770 
    1771     failed = ! psDBCreateTable(dbh, table, md);
    1772     if (!failed) {
    1773         rowSet = psArrayAlloc(3);
    1774         rowSet->n = 0;
    1775 
    1776         row = _get_const_row1();
    1777         psArrayAdd(rowSet, 0, row);
    1778         psFree(row);
    1779 
    1780         row = _get_const_row2();
    1781         psArrayAdd(rowSet, 0, row);
    1782         psFree(row);
    1783 
    1784         row = _get_row();
    1785         psArrayAdd(rowSet, 0, row);
    1786         psFree(row);
    1787 
    1788         failed = ! psDBInsertRows(dbh, table, rowSet);
    1789         if (!failed) {
    1790             where = _get_where();
    1791             updates = _get_update_values(STR_2, S32_2, F32_2, F64_2, B_2);
    1792             failed = (psDBUpdateRows(dbh, table, where, updates) < 0);
    1793 
    1794             ary = psDBDumpRows(dbh, table);
    1795             if (ary == NULL) {
    1796                 failed = 1;
    1797             } else {
    1798 
    1799                 row = (psMetadata*)psArrayGet(ary, 0);
    1800                 _print_row(row);
    1801 
    1802                 item = psMetadataLookup(row, TAB_COL_1_NAME);
    1803                 if ((item == NULL) || (item->data.S32 != S32_2)) {
    1804                     psLogMsg( __func__, PS_LOG_INFO, "oops, did not find expected output!\n" );
    1805                     failed = 1;
    1806                 } else {
    1807                     psLogMsg( __func__, PS_LOG_INFO, "found expected output.\n" );
    1808                 }
    1809 
    1810                 item = psMetadataLookup(row, TAB_COL_4_NAME);
    1811                 if ((item == NULL) || (item->data.B != B_2)) {
    1812                     psLogMsg( __func__, PS_LOG_INFO, "oops, did not find expected output!\n" );
    1813                     failed = 1;
    1814                 } else {
    1815                     psLogMsg( __func__, PS_LOG_INFO, "found expected output.\n" );
    1816                 }
    1817 
    1818                 psFree(ary);
    1819             }
    1820 
    1821             psFree(updates);
    1822             psFree(where);
    1823         }
    1824         psFree(rowSet);
    1825         psDBDropTable(dbh, table);
    1826     }
     1847    // Create test table
     1848    if(!psDBCreateTable(dbh, table, md)) {
     1849        psError(PS_ERR_UNKNOWN,true,"Did not expect return false to create test table");
     1850        psDBCleanup(dbh);
     1851        return 2;
     1852    }
     1853
     1854    // Create array to hold rows to be added to test table
     1855    rowSet = psArrayAlloc(3);
     1856    rowSet->n = 0;
     1857
     1858    row = _get_const_row1();
     1859    psArrayAdd(rowSet, 0, row);
     1860    psFree(row);
     1861
     1862    row = _get_const_row2();
     1863    psArrayAdd(rowSet, 0, row);
     1864    psFree(row);
     1865
     1866    row = _get_row();
     1867    psArrayAdd(rowSet, 0, row);
     1868    psFree(row);
     1869
     1870    // Insert rows into test table
     1871    if(!psDBInsertRows(dbh, table, rowSet)) {
     1872        psError(PS_ERR_UNKNOWN,true,"Did not expect return false for inserting rows");
     1873        psDBDropTable(dbh,table);
     1874        psDBCleanup(dbh);
     1875        return 3;
     1876    }
     1877
     1878    // Create where metadata to specify update - only one row
     1879    where = _get_where();
     1880
     1881    // Create update metadata to specify values
     1882    updates = _get_update_values(STR_2, S32_2, F32_2, F64_2, B_2);
     1883
     1884    // Perform database update with valid parameters
     1885    chgRows = psDBUpdateRows(dbh,table,where,updates);
     1886    if(chgRows != 1) {
     1887        psError(PS_ERR_UNKNOWN,true,"Did not expect return other than 1 for valid arguments");
     1888        psDBDropTable(dbh,table);
     1889        psDBCleanup(dbh);
     1890        return 4;
     1891    }
     1892    // Verify row contents after update
     1893    ary = psDBDumpRows(dbh, table);
     1894    if (ary == NULL) {
     1895        psError(PS_ERR_UNKNOWN,true,"Did not expect NULL return from dump rows");
     1896        psDBDropTable(dbh,table);
     1897        psDBCleanup(dbh);
     1898        return 5;
     1899    }
     1900    row = (psMetadata*)psArrayGet(ary, 0);
     1901    if(!_check_row_update(row)) {
     1902        psError(PS_ERR_UNKNOWN,true,"Update row not verified");
     1903        psDBDropTable(dbh,table);
     1904        psDBCleanup(dbh);
     1905        return 6;
     1906    }
     1907    psFree(ary);
     1908
     1909    // Attempt to update rows with NULL database
     1910    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for NULL database");
     1911    chgRows = psDBUpdateRows(NULL,table,where,updates);
     1912    if(chgRows != -1) {
     1913        psError(PS_ERR_UNKNOWN,true,"Updated rows %ld not as expected %ld",
     1914                chgRows,-1);
     1915        psDBDropTable(dbh,table);
     1916        psDBCleanup(dbh);
     1917        return 7;
     1918    }
     1919
     1920    // Attempt to update rows with invalid table
     1921    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for invalid table");
     1922    chgRows = psDBUpdateRows(dbh,"table999",where,updates);
     1923    if(chgRows != -1) {
     1924        psError(PS_ERR_UNKNOWN,true,"Updated rows %ld not as expected %ld",
     1925                chgRows,-1);
     1926        psDBDropTable(dbh,table);
     1927        psDBCleanup(dbh);
     1928        return 8;
     1929    }
     1930
     1931    // Attempt to update rows with NULL updates
     1932    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for NULL updates");
     1933    chgRows = psDBUpdateRows(dbh,table,where,NULL);
     1934    if(chgRows != -1) {
     1935        psError(PS_ERR_UNKNOWN,true,"Updated rows %ld not as expected %ld",
     1936                chgRows,-1);
     1937        psDBDropTable(dbh,table);
     1938        psDBCleanup(dbh);
     1939        return 8;
     1940    }
     1941
     1942    // Attempt to update rows with invalid where specifying non-existent column
     1943    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message for invalid where");
     1944    psFree(where);
     1945    where = _get_invalid_where();
     1946    chgRows = psDBUpdateRows(dbh,table,where,updates);
     1947    if(chgRows != -1) {
     1948        psError(PS_ERR_UNKNOWN,true,"Updated rows %ld not as expected %ld",
     1949                chgRows,-1);
     1950        psDBDropTable(dbh,table);
     1951        psDBCleanup(dbh);
     1952        return 9;
     1953    }
     1954
     1955    // Attempt to update rows with bad value in where statement
     1956    psFree(where);
     1957    where = _get_where_bad_value();
     1958    chgRows = psDBUpdateRows(dbh,table,where,updates);
     1959    if(chgRows != 0) {
     1960        psError(PS_ERR_UNKNOWN,true,"Updated rows %ld not as expected %ld",
     1961                chgRows,0);
     1962        psDBDropTable(dbh,table);
     1963        psDBCleanup(dbh);
     1964        return 10;
     1965    }
     1966    // Verify row contents after update - no change
     1967    ary = psDBDumpRows(dbh, table);
     1968    if (ary == NULL) {
     1969        psError(PS_ERR_UNKNOWN,true,"Did not expect NULL return from dump rows");
     1970        psDBDropTable(dbh,table);
     1971        psDBCleanup(dbh);
     1972        return 11;
     1973    }
     1974    row = (psMetadata*)psArrayGet(ary, 0);
     1975    if(!_check_row_update(row)) {
     1976        psError(PS_ERR_UNKNOWN,true,"Update row not verified");
     1977        psDBDropTable(dbh,table);
     1978        psDBCleanup(dbh);
     1979        return 12;
     1980    }
     1981    psFree(ary);
     1982
     1983    psFree(updates);
     1984    psFree(where);
     1985    psFree(rowSet);
     1986    psDBDropTable(dbh, table);
    18271987
    18281988    psFree(md);
    18291989    psDBCleanup(dbh);
    18301990
    1831     return failed;
     1991    return 0;
    18321992}
    18331993
  • trunk/psLib/test/dataIO/verified/tst_psDB.stderr

    r4374 r4383  
    281281
    282282<DATE><TIME>|<HOST>|I|TPDBUpdateRows
    283     psDBUpdateRows shall update rows in a test table.
    284 <DATE><TIME>|<HOST>|I|_print_row
    285     [key_string]=[foobar]
    286 <DATE><TIME>|<HOST>|I|_print_row
    287     [key_s32]=[-18]
    288 <DATE><TIME>|<HOST>|I|_print_row
    289     [key_f32]=[3.33]
    290 <DATE><TIME>|<HOST>|I|_print_row
    291     [key_f64]=[1.23457]
    292 <DATE><TIME>|<HOST>|I|_print_row
    293     [key_bool]=[Nay]
    294 <DATE><TIME>|<HOST>|I|_print_row
    295     [key_s32_0]=[3]
     283    Following should generate an error message for NULL database
     284<DATE><TIME>|<HOST>|E|psDBUpdateRows (FILE:LINENO)
     285    Invalid psDB has been specified.
    296286<DATE><TIME>|<HOST>|I|TPDBUpdateRows
    297     found expected output.
     287    Following should generate an error message for invalid table
     288<DATE><TIME>|<HOST>|E|psDBUpdateRows (FILE:LINENO)
     289    Failed to prepare query.  Error: Table 'test.table999' doesn't exist
    298290<DATE><TIME>|<HOST>|I|TPDBUpdateRows
    299     found expected output.
     291    Following should generate an error message for NULL updates
     292<DATE><TIME>|<HOST>|E|psDBGenerateUpdateRowSQL (FILE:LINENO)
     293    Update row SQL generate fail: values and where params may not be NULL.
     294<DATE><TIME>|<HOST>|E|psDBUpdateRows (FILE:LINENO)
     295    Query generation failed.
     296<DATE><TIME>|<HOST>|I|TPDBUpdateRows
     297    Following should generate an error message for invalid where
     298<DATE><TIME>|<HOST>|E|psDBUpdateRows (FILE:LINENO)
     299    Failed to prepare query.  Error: Unknown column 'col999' in 'where clause'
    300300
    301301---> TESTPOINT PASSED (psDB{dbUpdateRows} | tst_psDB.c)
Note: See TracChangeset for help on using the changeset viewer.