view UnitTestsSources/UnitTestsMain.cpp @ 151:a4a532a60838

sync, support linux standard base
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 03 Jan 2018 16:58:59 +0100 (2018-01-03)
parents 5611e6b1ec14
children
line wrap: on
line source
/**
 * Orthanc - A Lightweight, RESTful DICOM Store
 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
 * Department, University Hospital of Liege, Belgium
 * Copyright (C) 2017-2018 Osimis S.A., Belgium
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 **/


#include "../Core/PostgreSQLConnection.h"

#include <gtest/gtest.h>
#include <memory>
#include <boost/lexical_cast.hpp>

static int argc_;
static char** argv_;

OrthancPlugins::PostgreSQLConnection* CreateTestConnection(bool clearAll)
{
  std::auto_ptr<OrthancPlugins::PostgreSQLConnection> pg(new OrthancPlugins::PostgreSQLConnection);

  pg->SetHost(argv_[1]);
  pg->SetPortNumber(boost::lexical_cast<uint16_t>(argv_[2]));
  pg->SetUsername(argv_[3]);
  pg->SetPassword(argv_[4]);
  pg->SetDatabase(argv_[5]);
  
  pg->Open();

  if (clearAll)
  {
    pg->ClearAll();
  }

  return pg.release();
}


TEST(PostgreSQLConnection, GetConnectionUri)
{
  OrthancPlugins::PostgreSQLConnection c;
  c.SetDatabase("world");
  ASSERT_EQ("postgresql://postgres:postgres@localhost:5432/world", c.GetConnectionUri());

  c.ResetDatabase();
  ASSERT_EQ("postgresql://postgres:postgres@localhost:5432/", c.GetConnectionUri());

  c.SetDatabase("hello");
  ASSERT_EQ("postgresql://postgres:postgres@localhost:5432/hello", c.GetConnectionUri());

  c.SetHost("server");
  ASSERT_EQ("postgresql://postgres:postgres@server:5432/hello", c.GetConnectionUri());

  c.SetPortNumber(1234);
  ASSERT_EQ("postgresql://postgres:postgres@server:1234/hello", c.GetConnectionUri());

  c.SetPortNumber(0);
  ASSERT_EQ("postgresql://postgres:postgres@server/hello", c.GetConnectionUri());

  c.SetPortNumber(5432);
  ASSERT_EQ("postgresql://postgres:postgres@server:5432/hello", c.GetConnectionUri());

  c.SetUsername("user");
  c.SetPassword("pass");
  ASSERT_EQ("postgresql://user:pass@server:5432/hello", c.GetConnectionUri());

  c.SetPassword("");
  ASSERT_EQ("postgresql://user@server:5432/hello", c.GetConnectionUri());

  c.SetUsername("");
  c.SetPassword("pass");
  ASSERT_EQ("postgresql://server:5432/hello", c.GetConnectionUri());

  c.SetUsername("");
  c.SetPassword("");
  ASSERT_EQ("postgresql://server:5432/hello", c.GetConnectionUri());

  c.SetConnectionUri("hello://world");
  ASSERT_EQ("hello://world", c.GetConnectionUri());
}


int main(int argc, char **argv)
{
  if (argc < 6)
  {
    std::cerr << "Usage: " << argv[0] << " <host> <port> <username> <password> <database>"
              << std::endl << std::endl
              << "Example: " << argv[0] << " localhost 5432 postgres postgres orthanctest"
              << std::endl << std::endl;
    return -1;
  }

  argc_ = argc;
  argv_ = argv;  

  ::testing::InitGoogleTest(&argc, argv);

  return RUN_ALL_TESTS();
}