view tests/libserver/dao-account/main.cpp @ 33:d4f5f7231b84

Misc: switch to .hpp, dos2unix everything while here, #478
author David Demelier <markand@malikania.fr>
date Fri, 17 Jun 2016 13:12:35 +0200
parents a1e80d991968
children 56ab2f0c90dc
line wrap: on
line source

/*
 * 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.hpp>
#include <malikania/dao-account.hpp>
#include <malikania/database.hpp>

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();
}