
#include "stdafx.h"

#include "connection_helpers.h"

BEGIN_TEST_DATA_CLASS(module_dbc_metadata_test)
public:
  GRT grt;
END_TEST_DATA_CLASS

TEST_MODULE(module_dbc_metadata_test, "DBC: metadata tests");

TEST_FUNCTION(1)
{
  // load structs
  grt.scan_metaclasses_in("../../res/grt/");
  grt.end_loading_metaclasses();

  ensure_equals("load structs", grt.get_metaclasses().size(), 142);
}

// Test DatabaseMetaData::getCatalogs().
TEST_FUNCTION(2)
{
  db_mgmt_ConnectionRef connectionProperties(&grt);

  setup_env(&grt, connectionProperties);

  try {
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
    ensure("dm is NULL", dm != NULL);

    std::auto_ptr<sql::Connection> conn= dm->getConnection(connectionProperties);
    ensure("conn is NULL", conn.get() != NULL);

    sql::DatabaseMetaData *meta(conn->getMetaData());
    ensure("meta is not NULL", meta != NULL);

    // TODO: getCatalogs() is not implemented.
    // std::auto_ptr<sql::Statement> rset(meta->getCatalogs());
    // ensure("rset is not NULL", rset.get() != NULL);
    // while (rset->next());
  } catch (sql::SQLException &) {
    printf("ERR: Caught sql::SQLException\n");
    throw;
  } catch (...) {
    printf("ERR: Caught unknown exception\n");    
    throw;
  }
}

// Test DatabaseMetaData::getDatabaseProductName.
TEST_FUNCTION(4)
{
  db_mgmt_ConnectionRef connectionProperties(&grt);

  setup_env(&grt, connectionProperties);

  try {
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
    ensure("dm is NULL", dm != NULL);

    std::auto_ptr<sql::Connection> conn= dm->getConnection(connectionProperties);
    ensure("conn is NULL", conn.get() != NULL);

    sql::DatabaseMetaData *meta(conn->getMetaData());
    ensure("meta is not NULL", meta != NULL);

    const std::string & name = meta->getDatabaseProductName();
  } catch (sql::SQLException &) {
    printf("ERR: Caught sql::SQLException\n");
  }
}

// Test DatabaseMetaData::getDatabaseProductVersion.
TEST_FUNCTION(5)
{
  db_mgmt_ConnectionRef connectionProperties(&grt);

  setup_env(&grt, connectionProperties);

  try {
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
    ensure("dm is NULL", dm != NULL);

    std::auto_ptr<sql::Connection> conn= dm->getConnection(connectionProperties);
    ensure("conn is NULL", conn.get() != NULL);

    sql::DatabaseMetaData *meta(conn->getMetaData());
    ensure("meta is not NULL", meta != NULL);

    const std::string & name = meta->getDatabaseProductVersion();
  } catch (sql::SQLException &) {
    printf("ERR: Caught sql::SQLException\n");
    throw;
  }
}

// Test DatabaseMetaData::getDriverMajorVersion.
TEST_FUNCTION(7)
{
  db_mgmt_ConnectionRef connectionProperties(&grt);

  setup_env(&grt, connectionProperties);

  try {
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
    ensure("dm is NULL", dm != NULL);

    std::auto_ptr<sql::Connection> conn= dm->getConnection(connectionProperties);
    ensure("conn is NULL", conn.get() != NULL);

    sql::DatabaseMetaData *meta(conn->getMetaData());
    ensure("meta is not NULL", meta != NULL);

    int major = meta->getDriverMajorVersion();
  } catch (sql::SQLException &) {
    printf("ERR: Caught sql::SQLException\n");
    throw;
  }
}

// Test DatabaseMetaData::getDriverMinorVersion.
TEST_FUNCTION(8)
{
  db_mgmt_ConnectionRef connectionProperties(&grt);

  setup_env(&grt, connectionProperties);

  try {
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
    ensure("dm is NULL", dm != NULL);

    std::auto_ptr<sql::Connection> conn= dm->getConnection(connectionProperties);
    ensure("conn is NULL", conn.get() != NULL);

    sql::DatabaseMetaData *meta(conn->getMetaData());
    ensure("meta is not NULL", meta != NULL);

    int major = meta->getDriverMinorVersion();
  } catch (sql::SQLException &) {
    printf("ERR: Caught sql::SQLException\n");
    throw;
  }
}

// Test DatabaseMetaData::getDriverName.
TEST_FUNCTION(9)
{
  db_mgmt_ConnectionRef connectionProperties(&grt);

  setup_env(&grt, connectionProperties);

  try {
    sql::DriverManager *dm= sql::DriverManager::getDriverManager();
    ensure("dm is NULL", dm != NULL);

    std::auto_ptr<sql::Connection> conn= dm->getConnection(connectionProperties);
    ensure("conn is NULL", conn.get() != NULL);

    sql::DatabaseMetaData *meta(conn->getMetaData());
    ensure("meta is not NULL", meta != NULL);

    const std::string & name = meta->getDriverName();
  } catch (sql::SQLException &) {
    printf("ERR: Caught sql::SQLException\n");
    throw;
  }
}

END_TESTS
