Mercurial > malikania
changeset 103:41e5722da2ae
Tests: begin of database tests, #682
line wrap: on
line diff
--- a/CMakeLists.txt Mon Aug 21 18:33:26 2017 +0200 +++ b/CMakeLists.txt Mon Aug 21 20:59:18 2017 +0200 @@ -62,6 +62,7 @@ add_subdirectory(libclient) add_subdirectory(libclient-js) add_subdirectory(libserver) +add_subdirectory(libserver-test) add_subdirectory(client) add_subdirectory(server)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/CMakeLists.txt Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,44 @@ +# +# CMakeLists.txt -- CMake build system for malikania +# +# Copyright (c) 2013-2017 Malikania Authors +# +# 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. +# + +project(libmlk-server-test) + +set( + HEADERS + ${libmlk-server-test_SOURCE_DIR}/malikania/server/account_dao_test.hpp + ${libmlk-server-test_SOURCE_DIR}/malikania/server/character_dao_test.hpp + ${libmlk-server-test_SOURCE_DIR}/malikania/server/database_test.hpp + ${libmlk-server-test_SOURCE_DIR}/malikania/server/spell_dao_test.hpp +) + +set( + SOURCES + ${libmlk-server-test_SOURCE_DIR}/malikania/server/account_dao_test.cpp + ${libmlk-server-test_SOURCE_DIR}/malikania/server/character_dao_test.cpp + ${libmlk-server-test_SOURCE_DIR}/malikania/server/database_test.cpp + ${libmlk-server-test_SOURCE_DIR}/malikania/server/spell_dao_test.cpp +) + +malikania_define_library( + TARGET libmlk-server-test + SOURCES ${HEADERS} ${SOURCES} + LIBRARIES + libmlk-server + PUBLIC_INCLUDES + ${libmlk-server-test_SOURCE_DIR} +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/account_dao_test.cpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,56 @@ +/* + * account_dao_test.cpp -- database account interface test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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 <malikania/server/db/account.hpp> + +#include "account_dao_test.hpp" + +namespace mlk { + +namespace server { + +std::shared_ptr<account> account_dao_test::authenticate(const std::string& login, + const std::string& password) +{ + return nullptr; +} + +void account_dao_test::set_email(account& ac, const std::string& email) +{ + +} + +void account_dao_test::set_firstname(account& ac, const std::string& name) +{ + +} + +void account_dao_test::set_lastname(account& ac, const std::string& name) +{ + +} + +void account_dao_test::save(account& ac) +{ + ac.set_id(id_++); + accounts_.push_back(ac.shared_from_this()); +} + +} // !server + +} // !mlk
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/account_dao_test.hpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,49 @@ +/* + * account_dao_test.hpp -- database account interface test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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. + */ + +#ifndef MALIKANIA_SERVER_ACCOUNT_DAO_TEST_HPP +#define MALIKANIA_SERVER_ACCOUNT_DAO_TEST_HPP + +#include <vector> + +#include <malikania/server/db/account_dao.hpp> + +namespace mlk { + +namespace server { + +class account_dao_test : public account_dao { +private: + std::uint64_t id_{0}; + std::vector<std::shared_ptr<account>> accounts_; + +public: + std::shared_ptr<account> authenticate(const std::string& login, + const std::string& password) override; + + void set_email(account& ac, const std::string& email) override; + void set_firstname(account& ac, const std::string& name) override; + void set_lastname(account& ac, const std::string& name) override; + void save(account& ac) override; +}; + +} // !server + +} // !mlk + +#endif // !MALIKANIA_SERVER_ACCOUNT_DAO_TEST_HPP
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/character_dao_test.cpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,44 @@ +/* + * character_dao_test.cpp -- database character interface test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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 <malikania/server/db/character.hpp> + +#include "character_dao_test.hpp" + +namespace mlk { + +namespace server { + +void character_dao_test::save(character&) +{ + +} + +void character_dao_test::remove(character&) +{ + +} + +void character_dao_test::set_name(character&, const std::string&) +{ + +} + +} // !server + +} // !mlk
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/character_dao_test.hpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,47 @@ +/* + * character_dao_test.hpp -- database character interface test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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. + */ + +#ifndef MALIKANIA_SERVER_CHARACTER_DAO_TEST_HPP +#define MALIKANIA_SERVER_CHARACTER_DAO_TEST_HPP + +#include <cstdint> +#include <memory> +#include <vector> + +#include <malikania/server/db/character_dao.hpp> + +namespace mlk { + +namespace server { + +class character_dao_test : public character_dao { +private: + std::uint64_t id_{0}; + std::vector<std::shared_ptr<character>> characters_; + +public: + void save(character&) override; + void remove(character&) override; + void set_name(character&, const std::string&) override; +}; + +} // !server + +} // !mlk + +#endif // !MALIKANIA_SERVER_CHARACTER_DAO_TEST_HPP
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/database_test.cpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,45 @@ +/* + * database_test.cpp -- database test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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 "database_test.hpp" +#include "account_dao_test.hpp" +#include "character_dao_test.hpp" +#include "spell_dao_test.hpp" + +namespace mlk { + +namespace server { + +std::unique_ptr<class account_dao> database_test::account_dao() +{ + return std::make_unique<account_dao_test>(); +} + +std::unique_ptr<class character_dao> database_test::character_dao() +{ + return nullptr; +} + +std::unique_ptr<class spell_dao> database_test::spell_dao() +{ + return nullptr; +} + +} // !server + +} // !mlk
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/database_test.hpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,39 @@ +/* + * database_test.hpp -- database test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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. + */ + +#ifndef MALIKANIA_SERVER_DATABASE_TEST_HPP +#define MALIKANIA_SERVER_DATABASE_TEST_HPP + +#include <malikania/server/db/database.hpp> + +namespace mlk { + +namespace server { + +class database_test : public database { +public: + std::unique_ptr<class account_dao> account_dao() override; + std::unique_ptr<class character_dao> character_dao() override; + std::unique_ptr<class spell_dao> spell_dao() override; +}; + +} // !server + +} // !mlk + +#endif // !MALIKANIA_SERVER_DATABASE_TEST_HPP
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/spell_dao_test.cpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,44 @@ +/* + * spell_dao_test.cpp -- database spell interface test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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 <malikania/server/db/spell.hpp> + +#include "spell_dao_test.hpp" + +namespace mlk { + +namespace server { + +void spell_dao_test::save(spell&, character&) +{ + +} + +void spell_dao_test::set_level(spell&, std::uint8_t) +{ + +} + +void spell_dao_test::remove(spell&) +{ + +} + +} // !server + +} // !mlk
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/spell_dao_test.hpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,47 @@ +/* + * spell_dao_test.hpp -- database spell interface test + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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. + */ + +#ifndef MALIKANIA_SERVER_SPELL_DAO_TEST_HPP +#define MALIKANIA_SERVER_SPELL_DAO_TEST_HPP + +#include <memory> +#include <string> +#include <vector> + +#include <malikania/server/db/spell_dao.hpp> + +namespace mlk { + +namespace server { + +class spell_dao_test : public spell_dao { +private: + std::uint64_t id_{0}; + std::vector<std::shared_ptr<spell>> spells; + +public: + void save(spell&, character&) override; + void set_level(spell&, std::uint8_t) override; + void remove(spell&) override; +}; + +} // !server + +} // !mlk + +#endif // !MALIKANIA_SERVER_SPELL_DAO_TEST_HPP
--- a/libserver/malikania/server/db/account.cpp Mon Aug 21 18:33:26 2017 +0200 +++ b/libserver/malikania/server/db/account.cpp Mon Aug 21 20:59:18 2017 +0200 @@ -48,6 +48,12 @@ lastname_ = std::move(name); } +void account::save() +{ + if (is_draft()) + db_->account_dao()->save(*this); +} + } // !server } // !mlk
--- a/libserver/malikania/server/db/account.hpp Mon Aug 21 18:33:26 2017 +0200 +++ b/libserver/malikania/server/db/account.hpp Mon Aug 21 20:59:18 2017 +0200 @@ -39,11 +39,9 @@ public: inline account(std::shared_ptr<database> db, - std::string login, - std::shared_ptr<class character> ch) + std::string login) : model(std::move(db)) , login_(std::move(login)) - , character_(std::move(ch)) { } @@ -77,6 +75,13 @@ { return character_; } + + void save(); + + std::string table() const override + { + return "account"; + } }; } // !server
--- a/libserver/malikania/server/db/account_dao.hpp Mon Aug 21 18:33:26 2017 +0200 +++ b/libserver/malikania/server/db/account_dao.hpp Mon Aug 21 20:59:18 2017 +0200 @@ -36,6 +36,7 @@ virtual void set_email(account& ac, const std::string& email) = 0; virtual void set_firstname(account& ac, const std::string& name) = 0; virtual void set_lastname(account& ac, const std::string& name) = 0; + virtual void save(account& ac) = 0; }; } // !server
--- a/libserver/malikania/server/db/character_dao.hpp Mon Aug 21 18:33:26 2017 +0200 +++ b/libserver/malikania/server/db/character_dao.hpp Mon Aug 21 20:59:18 2017 +0200 @@ -19,6 +19,8 @@ #ifndef MALIKANIA_SERVER_DB_CHARACTER_DAO_HPP #define MALIKANIA_SERVER_DB_CHARACTER_DAO_HPP +#include <string> + namespace mlk { namespace server {
--- a/tests/libserver/CMakeLists.txt Mon Aug 21 18:33:26 2017 +0200 +++ b/tests/libserver/CMakeLists.txt Mon Aug 21 20:59:18 2017 +0200 @@ -17,4 +17,5 @@ # add_subdirectory(id) +add_subdirectory(db/account) # add_subdirectory(dao-account)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libserver/db/account/CMakeLists.txt Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,23 @@ +# +# CMakeLists.txt -- CMake build system for malikania +# +# Copyright (c) 2013-2017 Malikania Authors +# +# 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. +# + +malikania_create_test( + NAME db-account + LIBRARIES libmlk-server-test + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp +)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/libserver/db/account/main.cpp Mon Aug 21 20:59:18 2017 +0200 @@ -0,0 +1,47 @@ +/* + * main.cpp -- unit test for database + * + * Copyright (c) 2013-2017 Alexis Dörr <nanahara@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. + */ + +#define BOOST_TEST_MODULE "database account" +#include <boost/test/unit_test.hpp> + +#include <malikania/server/database_test.hpp> +#include <malikania/server/account_dao_test.hpp> +#include <malikania/server/db/account.hpp> + +namespace mlk { + +namespace server { + +BOOST_AUTO_TEST_SUITE(basic) + +BOOST_AUTO_TEST_CASE(set_email) +{ + auto db = std::make_shared<database_test>(); + auto acc = std::make_shared<account>(db, "nanahara"); + + BOOST_TEST(acc->is_draft()); + acc->set_email("nanahara@malikania.fr"); + BOOST_TEST(acc->is_draft()); + BOOST_TEST(acc->email() == "nanahara@malikania.fr"); + acc->save(); + BOOST_TEST((acc->is_published() && !acc->is_draft())); +} + +BOOST_AUTO_TEST_SUITE_END() +} // !server +} // !mlk