Mercurial > malikania
diff tests/libserver/dao-account/main.cpp @ 29:99792c6c8b06
Server: add initial postgresql account management, #475
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 26 May 2016 07:32:05 +0200 |
parents | |
children | a1e80d991968 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libserver/dao-account/main.cpp Thu May 26 07:32:05 2016 +0200 @@ -0,0 +1,105 @@ +/* + * main.cpp -- test AccountDao + * + * Copyright (c) 2013-2016 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <cassert> + +#include <gtest/gtest.h> + +#include <sysconfig-tests.h> + +#include <malikania/account.h> +#include <malikania/dao-account.h> +#include <malikania/database.h> + +using namespace malikania; + +class TestAccountDao : public testing::Test { +protected: + Database m_database; + AccountDao m_dao; + +public: + TestAccountDao() + : m_database(DRIVERDIR "pgsql.so", { + { "host", WITH_TEST_PGSQL_HOST }, + { "port", WITH_TEST_PGSQL_PORT }, + { "user", WITH_TEST_PGSQL_USER }, + { "database", WITH_TEST_PGSQL_DATABASE } + }) + , m_dao(m_database) + { + m_dao.clear(); + + assert(m_dao.count() == 0U); + } +}; + +TEST_F(TestAccountDao, create) +{ + try { + Account ac; + + ac.setName("jean"); + ac.setEmail("jean@christophe.fr"); + ac.setFirstName("Jean"); + ac.setLastName("Christophe"); + ac.setPassword("raw"); + + m_dao.create(ac); + + ASSERT_EQ(1U, m_dao.count()); + ASSERT_EQ(ac, m_dao.list()[0]); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +TEST_F(TestAccountDao, update) +{ + try { + Account ac; + + ac.setName("jean"); + ac.setEmail("jean@christophe.fr"); + ac.setFirstName("Jean"); + ac.setLastName("Christophe"); + ac.setPassword("raw"); + + m_dao.create(ac); + + ac.setEmail("benoit@christophe.fr"); + ac.setFirstName("Benoit"); + + m_dao.update(ac); + + Account ac2 = m_dao.get(ac.id()); + + ASSERT_EQ("jean", ac2.name()); + ASSERT_EQ("benoit@christophe.fr", ac2.email()); + ASSERT_EQ("Benoit", ac2.firstName()); + } catch (const std::exception &ex) { + FAIL() << ex.what(); + } +} + +int main(int argc, char **argv) +{ + testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +}