From 50cca731012956a57c7285aa98aaefe874597e4c Mon Sep 17 00:00:00 2001 From: Harald Date: Thu, 11 May 2023 12:00:11 +0200 Subject: [PATCH] Format all the code (#12) --- include/sl3/columns.hpp | 5 +- include/sl3/command.hpp | 43 +- include/sl3/container.hpp | 15 +- include/sl3/database.hpp | 2 +- include/sl3/dataset.hpp | 15 +- include/sl3/dbvalue.hpp | 7 +- include/sl3/dbvalues.hpp | 2 +- include/sl3/error.hpp | 2 +- include/sl3/rowcallback.hpp | 10 +- include/sl3/types.hpp | 4 +- include/sl3/value.hpp | 28 +- src/sl3/columns.cpp | 2 +- src/sl3/command.cpp | 2 +- src/sl3/config.cpp | 14 +- src/sl3/connection.hpp | 2 +- src/sl3/database.cpp | 47 ++- src/sl3/dataset.cpp | 17 +- src/sl3/dbvalue.cpp | 55 ++- src/sl3/dbvalues.cpp | 7 +- src/sl3/error.cpp | 2 +- src/sl3/rowcallback.cpp | 2 +- src/sl3/types.cpp | 2 +- src/sl3/utils.hpp | 28 +- src/sl3/value.cpp | 2 +- tests/commands/commandstest.cpp | 223 +++++----- tests/database/dbtest.cpp | 539 +++++++++++-------------- tests/dataset/datasettest.cpp | 297 ++++++-------- tests/dbvalue/dbvaluetest.cpp | 6 +- tests/rowcallback/rowcallbacktest.cpp | 506 +++++++++++------------ tests/sample/main.cpp | 32 +- tests/sample/main1.cpp | 37 +- tests/sample/main2.cpp | 23 +- tests/sample/main3.cpp | 64 +-- tests/sample/main4.cpp | 23 +- tests/sample/main5.cpp | 35 +- tests/sample/main6.cpp | 34 +- tests/test_main.cpp | 47 ++- tests/testing.hpp | 3 +- tests/typenames/typenamestest.cpp | 84 ++-- tests/value/valuetest.cpp | 559 +++++++++++++------------- tests/version/versiontest.cpp | 16 +- 41 files changed, 1351 insertions(+), 1492 deletions(-) diff --git a/include/sl3/columns.hpp b/include/sl3/columns.hpp index 842fd94..2bc3df6 100644 --- a/include/sl3/columns.hpp +++ b/include/sl3/columns.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -114,7 +114,6 @@ namespace sl3 */ DbValue getValue (int idx, Type type) const; - /** * \brief Get all columns at once * @@ -151,7 +150,7 @@ namespace sl3 /** * \brief Get the size of a column - * + * * The size sqlite3 uses to store the value of the given field. * * \param idx wanted index diff --git a/include/sl3/command.hpp b/include/sl3/command.hpp index 745bb84..201ec48 100644 --- a/include/sl3/command.hpp +++ b/include/sl3/command.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -47,10 +47,10 @@ namespace sl3 const std::string& sql, DbValues parameters); - Command () = delete; - Command (const Command&) = delete; + Command () = delete; + Command (const Command&) = delete; Command operator= (const Command&) = delete; - Command operator= (Command&&) = delete; + Command operator= (Command&&) = delete; public: /** @@ -120,7 +120,7 @@ namespace sl3 * \return false if processing the query result shall stop * \n true otherwise */ - using Callback = std::function; + using Callback = std::function; /** * \brief Execute the command @@ -131,7 +131,6 @@ namespace sl3 */ void execute (); - /** * \brief Execute the command * @@ -143,25 +142,25 @@ namespace sl3 void execute (const DbValues& parameters); /** - * \brief Execute the command applying given callback - * - * Applies given parameters and run the current command. - * - * \throw sl3::ErrTypeMisMatch given parameters are of the wrong size. - * \param cb a callback - * \param parameters a list of parameters - */ + * \brief Execute the command applying given callback + * + * Applies given parameters and run the current command. + * + * \throw sl3::ErrTypeMisMatch given parameters are of the wrong size. + * \param cb a callback + * \param parameters a list of parameters + */ void execute (RowCallback& cb, const DbValues& parameters = {}); /** - * \brief Execute the command applying given callback - * - * Applies given parameters and run the current command. - * - * \throw sl3::ErrTypeMisMatch given parameters are of the wrong size. - * \param cb a callback - * \param parameters a list of parameters - */ + * \brief Execute the command applying given callback + * + * Applies given parameters and run the current command. + * + * \throw sl3::ErrTypeMisMatch given parameters are of the wrong size. + * \param cb a callback + * \param parameters a list of parameters + */ void execute (Callback cb, const DbValues& parameters = {}); /** diff --git a/include/sl3/container.hpp b/include/sl3/container.hpp index 440c126..1315c5c 100644 --- a/include/sl3/container.hpp +++ b/include/sl3/container.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -212,13 +212,22 @@ namespace sl3 * behaves undefined if given index is invalid * \return reference to element at requested index */ - reference operator[] (size_t i) { return _cont[i]; } + reference + operator[] (size_t i) + { + return _cont[i]; + } /**\brief unchecked random access * \param i index * behaves undefined if given index is invalid * \return reference to element at requested index */ - const_reference operator[] (size_t i) const { return _cont[i]; } + const_reference + operator[] (size_t i) const + { + return _cont[i]; + } + protected: /// Container T ContainerType _cont; diff --git a/include/sl3/database.hpp b/include/sl3/database.hpp index 6f7084f..1cd01f2 100644 --- a/include/sl3/database.hpp +++ b/include/sl3/database.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/include/sl3/dataset.hpp b/include/sl3/dataset.hpp index e46b99e..5442c9a 100644 --- a/include/sl3/dataset.hpp +++ b/include/sl3/dataset.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -80,8 +80,8 @@ namespace sl3 Dataset (Dataset&&) noexcept ( std::is_nothrow_move_constructible>::value&& std::is_nothrow_move_constructible::value&& - std::is_nothrow_move_constructible>:: - value); + std::is_nothrow_move_constructible< + std::vector>::value); // = default; no mscv does not like it /** @@ -160,9 +160,10 @@ namespace sl3 * * \see Dataset::sort */ - typedef bool (*DbValueSort)(const DbValue&, const DbValue&); + typedef bool (*DbValueSort) (const DbValue&, const DbValue&); - //using DbValueSort = std::function ; + // using DbValueSort = std::function + // ; /** * \brief Sort the Dataset @@ -174,9 +175,7 @@ namespace sl3 * \param idxs list of field indexes * \param cmp pointer to a less than compare function, default dbval_lt */ - void sort (const std::vector& idxs, - DbValueSort cmp = &dbval_lt - ); + void sort (const std::vector& idxs, DbValueSort cmp = &dbval_lt); private: Types _fieldtypes; diff --git a/include/sl3/dbvalue.hpp b/include/sl3/dbvalue.hpp index fc62860..f6d7b96 100644 --- a/include/sl3/dbvalue.hpp +++ b/include/sl3/dbvalue.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -16,7 +16,6 @@ #include #include - // todo after a wile, type and storage type become confusing // find better name, type should be value type, // storage type something else, find better name! @@ -319,7 +318,6 @@ namespace sl3 */ Type dbtype () const; - /** * \brief Returns the type of the underlying Value * @@ -331,7 +329,6 @@ namespace sl3 */ Type type () const; - /** * \brief Check if assignment would be OK * @@ -358,7 +355,6 @@ namespace sl3 friend class DbValues; void assign (const DbValue& other); - }; /** @@ -426,7 +422,6 @@ namespace sl3 */ bool dbval_lt (const DbValue& a, const DbValue& b) noexcept; - } #endif /* DbValue_HPP_ */ diff --git a/include/sl3/dbvalues.hpp b/include/sl3/dbvalues.hpp index b8c0ccb..40a0dce 100644 --- a/include/sl3/dbvalues.hpp +++ b/include/sl3/dbvalues.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/include/sl3/error.hpp b/include/sl3/error.hpp index 69d1e95..e552aae 100644 --- a/include/sl3/error.hpp +++ b/include/sl3/error.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/include/sl3/rowcallback.hpp b/include/sl3/rowcallback.hpp index cef5b1b..9dc1f96 100644 --- a/include/sl3/rowcallback.hpp +++ b/include/sl3/rowcallback.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -59,10 +59,10 @@ namespace sl3 virtual void onStart (); /** - * \brief Called after a query result has been processed - * - * The default implementation does nothing. - */ + * \brief Called after a query result has been processed + * + * The default implementation does nothing. + */ virtual void onEnd (); }; } diff --git a/include/sl3/types.hpp b/include/sl3/types.hpp index 726f3ce..8797a3a 100644 --- a/include/sl3/types.hpp +++ b/include/sl3/types.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -69,7 +69,7 @@ namespace sl3 using reference = conatiner_type::reference; using const_reference = conatiner_type::const_reference; using size_type = conatiner_type::size_type; -//@} + //@} #ifndef _MSC_VER using Container::Container; diff --git a/include/sl3/value.hpp b/include/sl3/value.hpp index ad4b54a..c88459e 100644 --- a/include/sl3/value.hpp +++ b/include/sl3/value.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -15,11 +15,9 @@ #include #include - namespace sl3 { - /** * \brief * @@ -38,7 +36,7 @@ namespace sl3 * * Creates a Null Value */ - Value () noexcept ; + Value () noexcept; /** \brief Constructor * @@ -226,11 +224,10 @@ namespace sl3 */ Type getType () const noexcept; -// friend bool operator== (const Value& a, const Value& b) noexcept; -// friend bool operator< (const Value& a, const Value& b) noexcept; + // friend bool operator== (const Value& a, const Value& b) noexcept; + // friend bool operator< (const Value& a, const Value& b) noexcept; friend std::ostream& operator<< (std::ostream& stm, const sl3::Value& v); - friend bool value_type_eq (const Value& a, const Value& b) noexcept; friend bool value_type_lt (const Value& a, const Value& b) noexcept; @@ -244,8 +241,7 @@ namespace sl3 * * \param other value to swap with */ - void swap(Value& other) noexcept; - + void swap (Value& other) noexcept; private: Type _type{Type::Null}; @@ -285,8 +281,6 @@ namespace sl3 */ bool value_type_eq (const Value& a, const Value& b) noexcept; - - /** * \brief less than, including type info * @@ -309,7 +303,6 @@ namespace sl3 */ bool value_type_lt (const Value& a, const Value& b) noexcept; - /** * \brief equality, ignoring type info * @@ -343,16 +336,17 @@ namespace sl3 void swap (Value& a, Value& b) noexcept; /// Define a constant for a Value that is null - //static const Value NullValue{}; + // static const Value NullValue{}; } namespace std -{ // only allowed to extend namespace std with specializations +{ // only allowed to extend namespace std with specializations - template<> // specialization - inline void swap(sl3::Value& lhs, sl3::Value& rhs) + template <> // specialization + inline void + swap (sl3::Value& lhs, sl3::Value& rhs) { - sl3::swap(lhs, rhs) ; + sl3::swap (lhs, rhs); } } diff --git a/src/sl3/columns.cpp b/src/sl3/columns.cpp index dad4dbd..b717fba 100644 --- a/src/sl3/columns.cpp +++ b/src/sl3/columns.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/src/sl3/command.cpp b/src/sl3/command.cpp index 7ef8667..d53d758 100644 --- a/src/sl3/command.cpp +++ b/src/sl3/command.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/src/sl3/config.cpp b/src/sl3/config.cpp index 75f63f6..ede24c9 100644 --- a/src/sl3/config.cpp +++ b/src/sl3/config.cpp @@ -1,10 +1,10 @@ - /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- - ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ - ---- This Source Code Form is subject to the terms of the Mozilla Public ----- - ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- - ---------- file, You can obtain one at http://mozilla.org/MPL/2.0/. ---------- - ******************************************************************************/ +/****************************************************************************** +------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- +---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ +---- This Source Code Form is subject to the terms of the Mozilla Public ----- +---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- +---------- file, You can obtain one at http://mozilla.org/MPL/2.0/. ---------- +******************************************************************************/ #include diff --git a/src/sl3/connection.hpp b/src/sl3/connection.hpp index bdf1750..b96f804 100644 --- a/src/sl3/connection.hpp +++ b/src/sl3/connection.hpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/src/sl3/database.cpp b/src/sl3/database.cpp index e39a455..5027fba 100644 --- a/src/sl3/database.cpp +++ b/src/sl3/database.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -12,10 +12,10 @@ #include "connection.hpp" - namespace { - sqlite3* opendb(const std::string& name, int openFlags) + sqlite3* + opendb (const std::string& name, int openFlags) { if (openFlags == 0) { @@ -27,16 +27,16 @@ namespace if (sl3rc != SQLITE_OK) { - using scope_guard = std::unique_ptr; + using scope_guard + = std::unique_ptr; scope_guard guard{db, &sqlite3_close}; throw sl3::SQLite3Error{sl3rc, sqlite3_errmsg (db)}; } - return db ; + return db; } } - namespace sl3 { @@ -46,18 +46,17 @@ namespace sl3 return std::string (sqlite3_errstr (errcode)); } - Database::Database (const std::string& name, int flags) - : _connection {new internal::Connection{opendb(name, flags)}} + : _connection{new internal::Connection{opendb (name, flags)}} { sqlite3_extended_result_codes (_connection->db (), true); } Database::Database (Database&& other) noexcept - : _connection (std::move (other._connection)) + : _connection (std::move (other._connection)) { // always have a connection, but an invalid one - other._connection.reset(new internal::Connection{nullptr}); + other._connection.reset (new internal::Connection{nullptr}); } Database::~Database () noexcept @@ -89,14 +88,14 @@ namespace sl3 void Database::execute (const char* sql) { - _connection->ensureValid() ; + _connection->ensureValid (); char* dbMsg = nullptr; int rc = sqlite3_exec (_connection->db (), sql, nullptr, nullptr, &dbMsg); if (rc != SQLITE_OK) { - using scope_guard = std::unique_ptr; + using scope_guard = std::unique_ptr; scope_guard guard (dbMsg, &sqlite3_free); throw SQLite3Error{rc, dbMsg}; } @@ -111,7 +110,7 @@ namespace sl3 void Database::execute (const std::string& sql, Callback cb) { - prepare (sql).execute (std::move(cb)); + prepare (sql).execute (std::move (cb)); } Dataset @@ -131,7 +130,7 @@ namespace sl3 { DbValue retVal (Type::Variant); - Callback cb = [&retVal](Columns cols) -> bool { + Callback cb = [&retVal] (Columns cols) -> bool { retVal = cols.getValue (0); return false; // exit after first row }; @@ -147,7 +146,7 @@ namespace sl3 { DbValue retVal{type}; - Callback cb = [&retVal, type](Columns cols) -> bool { + Callback cb = [&retVal, type] (Columns cols) -> bool { retVal = cols.getValue (0, type); return false; // exit after first row }; @@ -161,36 +160,36 @@ namespace sl3 int Database::getMostRecentErrCode () { - _connection->ensureValid() ; + _connection->ensureValid (); return sqlite3_extended_errcode (_connection->db ()); } std::string Database::getMostRecentErrMsg () { - _connection->ensureValid() ; + _connection->ensureValid (); return std::string (sqlite3_errmsg (_connection->db ())); } - std::size_t Database::getTotalChanges () { - _connection->ensureValid() ; - return static_cast(sqlite3_total_changes (_connection->db ())); + _connection->ensureValid (); + return static_cast ( + sqlite3_total_changes (_connection->db ())); } std::size_t Database::getRecentlyChanged () { - _connection->ensureValid() ; - return static_cast(sqlite3_changes (_connection->db ())); + _connection->ensureValid (); + return static_cast (sqlite3_changes (_connection->db ())); } int64_t Database::getLastInsertRowid () { - _connection->ensureValid() ; + _connection->ensureValid (); return sqlite3_last_insert_rowid (_connection->db ()); } @@ -227,7 +226,7 @@ namespace sl3 void Database::Transaction::commit () { - if(_db) + if (_db) { _db->execute ("COMMIT TRANSACTION"); _db = nullptr; diff --git a/src/sl3/dataset.cpp b/src/sl3/dataset.cpp index 2d566cf..c12908d 100644 --- a/src/sl3/dataset.cpp +++ b/src/sl3/dataset.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -19,14 +19,17 @@ namespace sl3 { - Dataset::Dataset () noexcept : _fieldtypes (), _names () {} + Dataset::Dataset () noexcept + : _fieldtypes () + , _names () + { + } Dataset::Dataset (Types types) : _fieldtypes (std::move (types)) , _names () { } - Dataset::Dataset (Dataset&& other) noexcept ( std::is_nothrow_move_constructible>::value&& std::is_nothrow_move_constructible::value&& std:: @@ -105,20 +108,20 @@ namespace sl3 if (pos == _names.end ()) throw ErrOutOfRange ("Field name " + name + " not found"); - return as_size_t(distance (_names.begin (), pos)); + return as_size_t (distance (_names.begin (), pos)); } void Dataset::sort (const std::vector& idxs, DbValueSort cmp) { - ASSERT_EXCEPT(cmp, ErrNullValueAccess) ; + ASSERT_EXCEPT (cmp, ErrNullValueAccess); - auto lessValues = [&](const DbValues& a, const DbValues& b) -> bool { + auto lessValues = [&] (const DbValues& a, const DbValues& b) -> bool { for (auto cur : idxs) { if (cmp (a.at (cur), b.at (cur))) return true; - else if (cmp (b.at (cur),a.at (cur))) + else if (cmp (b.at (cur), a.at (cur))) return false; } return false; diff --git a/src/sl3/dbvalue.cpp b/src/sl3/dbvalue.cpp index 7545017..07d4733 100644 --- a/src/sl3/dbvalue.cpp +++ b/src/sl3/dbvalue.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -21,8 +21,6 @@ namespace sl3 { - - namespace { template @@ -52,7 +50,9 @@ namespace sl3 return type != Type::Null; } //---------------------------------------------------------------------- - template bool oneOf (ARGS... args) + template + bool + oneOf (ARGS... args) { return oneof (type, args...); @@ -84,7 +84,9 @@ namespace sl3 return *this; } //---------------------------------------------------------------------- - template ensure& oneOf (ARGS... args) + template + ensure& + oneOf (ARGS... args) { if (!oneof (type, args...)) throw ErrTypeMisMatch (typeName (type) @@ -106,19 +108,14 @@ namespace sl3 Type type; }; - } //-------------------------------------------------------------------------- - - - - bool dbval_type_eq (const DbValue& a, const DbValue& b) noexcept { - if (a.dbtype() == b.dbtype()) + if (a.dbtype () == b.dbtype ()) { - return value_type_eq(a.getValue(), b.getValue()) ; + return value_type_eq (a.getValue (), b.getValue ()); } return false; @@ -127,12 +124,12 @@ namespace sl3 bool dbval_type_lt (const DbValue& a, const DbValue& b) noexcept { - if (value_type_lt(a.getValue (), b.getValue ())) + if (value_type_lt (a.getValue (), b.getValue ())) { return true; } - if (value_type_eq(a.getValue(), b.getValue())) + if (value_type_eq (a.getValue (), b.getValue ())) { // a variant is bigger return a.dbtype () < b.dbtype (); } @@ -155,7 +152,7 @@ namespace sl3 } DbValue::DbValue (Type type) noexcept - : _type (type == Type::Null ? Type::Variant : type) + : _type (type == Type::Null ? Type::Variant : type) { } @@ -199,12 +196,12 @@ namespace sl3 { if (!canAssign (other)) { - throw ErrTypeMisMatch ( - typeName (_type) + "=" - + (other._type == Type::Variant - ? typeName (other._type) + " with storage type" - + typeName (other.type ()) - : typeName (other._type))); + throw ErrTypeMisMatch (typeName (_type) + "=" + + (other._type == Type::Variant + ? typeName (other._type) + + " with storage type" + + typeName (other.type ()) + : typeName (other._type))); } assign (other); @@ -216,12 +213,12 @@ namespace sl3 { if (!canAssign (other)) { - throw ErrTypeMisMatch ( - typeName (_type) + "=" - + (other._type == Type::Variant - ? typeName (other._type) + " with storage type" - + typeName (other.type ()) - : typeName (other._type))); + throw ErrTypeMisMatch (typeName (_type) + "=" + + (other._type == Type::Variant + ? typeName (other._type) + + " with storage type" + + typeName (other.type ()) + : typeName (other._type))); } _value = std::move (other._value); @@ -404,7 +401,7 @@ namespace sl3 } std::string // TODO consider change to reference, with warning in the doc - DbValue::get (const std::string& defval) const + DbValue::get (const std::string& defval) const { if (_value.getType () != Type::Text) return defval; @@ -413,7 +410,7 @@ namespace sl3 } Blob // TODO consider change to reference, with warning in the doc - DbValue::get (const Blob& defval) const + DbValue::get (const Blob& defval) const { if (_value.getType () != Type::Blob) return defval; diff --git a/src/sl3/dbvalues.cpp b/src/sl3/dbvalues.cpp index 71f2080..c946e60 100644 --- a/src/sl3/dbvalues.cpp +++ b/src/sl3/dbvalues.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- @@ -26,7 +26,10 @@ namespace sl3 } #endif - DbValues::DbValues () noexcept : Container () {} + DbValues::DbValues () noexcept + : Container () + { + } DbValues::DbValues (const DbValues& row) : Container (row) { diff --git a/src/sl3/error.cpp b/src/sl3/error.cpp index a01086e..77c1efb 100644 --- a/src/sl3/error.cpp +++ b/src/sl3/error.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/src/sl3/rowcallback.cpp b/src/sl3/rowcallback.cpp index c55baa8..c69924b 100644 --- a/src/sl3/rowcallback.cpp +++ b/src/sl3/rowcallback.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/src/sl3/types.cpp b/src/sl3/types.cpp index 27884c7..f94b246 100644 --- a/src/sl3/types.cpp +++ b/src/sl3/types.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/src/sl3/utils.hpp b/src/sl3/utils.hpp index 56fa83a..7fe6c7e 100644 --- a/src/sl3/utils.hpp +++ b/src/sl3/utils.hpp @@ -46,40 +46,36 @@ namespace sl3 bool is_less (const T1& a, const T2& b) { - using common_type = std::common_type_t ; - return std::less{}( - static_cast (a), - static_cast (b)); + using common_type = std::common_type_t; + return std::less{}(static_cast (a), + static_cast (b)); } template bool is_greater (const T1& a, const T2& b) { - using common_type = std::common_type_t ; - return std::greater{}( - static_cast (a), - static_cast (b)); + using common_type = std::common_type_t; + return std::greater{}(static_cast (a), + static_cast (b)); } template bool is_less_equal (const T1& a, const T2& b) { - using common_type = std::common_type_t ; - return std::less_equal{}( - static_cast (a), - static_cast (b)); + using common_type = std::common_type_t; + return std::less_equal{}(static_cast (a), + static_cast (b)); } template bool is_equal (const T1& a, const T2& b) { - using common_type = std::common_type_t ; - return std::equal_to{}( - static_cast (a), - static_cast (b)); + using common_type = std::common_type_t; + return std::equal_to{}(static_cast (a), + static_cast (b)); } } diff --git a/src/sl3/value.cpp b/src/sl3/value.cpp index 98a6584..0fa92e1 100644 --- a/src/sl3/value.cpp +++ b/src/sl3/value.cpp @@ -1,5 +1,5 @@ /****************************************************************************** - ------------- Copyright (c) 2009-2017 H a r a l d A c h i t z --------------- + ------------- Copyright (c) 2009-2023 H a r a l d A c h i t z --------------- ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------ ---- This Source Code Form is subject to the terms of the Mozilla Public ----- ---- License, v. 2.0. If a copy of the MPL was not distributed with this ----- diff --git a/tests/commands/commandstest.cpp b/tests/commands/commandstest.cpp index 0fe855f..9b30eee 100644 --- a/tests/commands/commandstest.cpp +++ b/tests/commands/commandstest.cpp @@ -3,256 +3,247 @@ #include -SCENARIO("using precompiled commands") +SCENARIO ("using precompiled commands") { - using namespace sl3 ; + using namespace sl3; GIVEN ("cmake genertated config") { Database db{":memory:"}; - db.execute( "CREATE TABLE tbl (fld1 , fld2 , fld3 );" ); + db.execute ("CREATE TABLE tbl (fld1 , fld2 , fld3 );"); WHEN ("creating a simple compiled command") { - auto cmd = db.prepare ( "insert into tbl (fld1 , fld2 , fld3)" - " VALUES (? , ? , ? ); "); + auto cmd = db.prepare ("insert into tbl (fld1 , fld2 , fld3)" + " VALUES (? , ? , ? ); "); THEN ("parameters are detected and set to Null") { - CHECK (cmd.getParameters().size () == 3) ; + CHECK (cmd.getParameters ().size () == 3); } - THEN ("it can be executed") { - CHECK (db.getTotalChanges () == 0) ; - cmd.execute (parameters(1,2,3)) ; - CHECK (db.getTotalChanges () == 1) ; - cmd.execute () ; // same are applied again - auto val = db.selectValue("SELECT COUNT(*) FROM tbl WHERE fld1 = 1 ;") ; - CHECK (val.getInt () == 2) ; + CHECK (db.getTotalChanges () == 0); + cmd.execute (parameters (1, 2, 3)); + CHECK (db.getTotalChanges () == 1); + cmd.execute (); // same are applied again + auto val + = db.selectValue ("SELECT COUNT(*) FROM tbl WHERE fld1 = 1 ;"); + CHECK (val.getInt () == 2); } } WHEN ("creating a command via invalid sql") { - THEN("this throws en error") + THEN ("this throws en error") { - REQUIRE_THROWS_AS(db.prepare("SELECT * FROM FOOOOO;"), - SQLite3Error); + REQUIRE_THROWS_AS (db.prepare ("SELECT * FROM FOOOOO;"), SQLite3Error); } } WHEN ("creating a command with parameters") { - auto cmd = db.prepare ( "insert into tbl (fld1 , fld2 , fld3)" - " VALUES (? , ? , ? ); ", - parameters(1,2,3)); + auto cmd = db.prepare ("insert into tbl (fld1 , fld2 , fld3)" + " VALUES (? , ? , ? ); ", + parameters (1, 2, 3)); THEN ("the parameters are set") { - CHECK (cmd.getParameters().size () == 3) ; - cmd.execute () ; - CHECK (db.getTotalChanges () == 1) ; - cmd.execute () ; - auto val = db.selectValue("SELECT COUNT(*) FROM tbl WHERE fld1 = 1 ;") ; - CHECK (val.getInt () == 2) ; + CHECK (cmd.getParameters ().size () == 3); + cmd.execute (); + CHECK (db.getTotalChanges () == 1); + cmd.execute (); + auto val + = db.selectValue ("SELECT COUNT(*) FROM tbl WHERE fld1 = 1 ;"); + CHECK (val.getInt () == 2); } THEN ("the parameters can be re-set") { - cmd.resetParameters(parameters(6,7,8)) ; - cmd.execute () ; - auto val = db.selectValue("SELECT COUNT(*) FROM tbl WHERE fld1 = 6 ;") ; - CHECK (val.getInt () == 1) ; + cmd.resetParameters (parameters (6, 7, 8)); + cmd.execute (); + auto val + = db.selectValue ("SELECT COUNT(*) FROM tbl WHERE fld1 = 6 ;"); + CHECK (val.getInt () == 1); } - } WHEN ("applying wrong numbers of parameters") { - auto sql = "insert into tbl (fld1 , fld2 , fld3)" - " VALUES (? , ? , ? ); " ; - auto param2 = parameters(1,2) ; - auto param4 = parameters(1,2,3,4) ; + auto sql = "insert into tbl (fld1 , fld2 , fld3)" + " VALUES (? , ? , ? ); "; + auto param2 = parameters (1, 2); + auto param4 = parameters (1, 2, 3, 4); THEN ("this will throw a type miss match") { - CHECK_THROWS_AS ((void)db.prepare (sql, param2), ErrTypeMisMatch) ; - CHECK_THROWS_AS ((void)db.prepare (sql, param4), ErrTypeMisMatch) ; - auto cmd = db.prepare(sql) ; - CHECK_THROWS_AS (cmd.resetParameters(parameters(1,2)), ErrTypeMisMatch); - CHECK_THROWS_AS (cmd.resetParameters(param4), ErrTypeMisMatch) ; - - CHECK_THROWS_AS (cmd.setParameters(parameters(1,2)), ErrTypeMisMatch); - CHECK_THROWS_AS (cmd.setParameters(param4), ErrTypeMisMatch) ; - } + CHECK_THROWS_AS ((void)db.prepare (sql, param2), ErrTypeMisMatch); + CHECK_THROWS_AS ((void)db.prepare (sql, param4), ErrTypeMisMatch); + auto cmd = db.prepare (sql); + CHECK_THROWS_AS (cmd.resetParameters (parameters (1, 2)), + ErrTypeMisMatch); + CHECK_THROWS_AS (cmd.resetParameters (param4), ErrTypeMisMatch); + + CHECK_THROWS_AS (cmd.setParameters (parameters (1, 2)), + ErrTypeMisMatch); + CHECK_THROWS_AS (cmd.setParameters (param4), ErrTypeMisMatch); + } } - WHEN ("creating a command with names parameters") { auto sql1 = "insert into tbl (fld1 , fld2 , fld3)" - " VALUES (:eins , @zwei , $drei); " ; + " VALUES (:eins , @zwei , $drei); "; auto sql2 = "insert into tbl (fld1 , fld2 , fld3)" - " VALUES (?1 , ?2 , ?1); " ; + " VALUES (?1 , ?2 , ?1); "; THEN ("the parameter names can be queried") { - auto cmd1 = db.prepare(sql1) ; - auto names1 = cmd1.getParameterNames () ; - CHECK(names1.size() == 3) ; + auto cmd1 = db.prepare (sql1); + auto names1 = cmd1.getParameterNames (); + CHECK (names1.size () == 3); CHECK_EQ (names1.at (0), ":eins"); CHECK_EQ (names1.at (1), "@zwei"); CHECK_EQ (names1.at (2), "$drei"); - REQUIRE_THROWS_AS (auto _ = names1.at (100), std::out_of_range) ; + REQUIRE_THROWS_AS (auto _ = names1.at (100), std::out_of_range); - auto cmd2 = db.prepare(sql2) ; - auto names2 = cmd2.getParameterNames () ; - CHECK(names2.size() == 2) ; + auto cmd2 = db.prepare (sql2); + auto names2 = cmd2.getParameterNames (); + CHECK (names2.size () == 2); CHECK_EQ (names2.at (0), "?1"); CHECK_EQ (names2.at (1), "?2"); } } - WHEN ("creating a command typed parameters") { auto sql1 = "insert into tbl (fld1 , fld2 , fld3)" - " VALUES (:eins , @zwei , $drei); " ; + " VALUES (:eins , @zwei , $drei); "; THEN ("the command is movable") { - auto cmd1 = db.prepare(sql1) ; - cmd1.getParameter(0).set(100) ; - cmd1.getParameter(1).set("hello") ; - const Command cmd2 {std::move(cmd1)} ; - auto names = cmd2.getParameterNames () ; - CHECK(names.size() == 3) ; + auto cmd1 = db.prepare (sql1); + cmd1.getParameter (0).set (100); + cmd1.getParameter (1).set ("hello"); + const Command cmd2{std::move (cmd1)}; + auto names = cmd2.getParameterNames (); + CHECK (names.size () == 3); CHECK_EQ (names.at (0), ":eins"); CHECK_EQ (names.at (1), "@zwei"); CHECK_EQ (names.at (2), "$drei"); - CHECK_EQ (cmd2.getParameters().size(), 3); - CHECK_EQ (cmd2.getParameter(0).getInt(), 100); - CHECK_EQ (cmd2.getParameter(1).getText(), "hello"); - + CHECK_EQ (cmd2.getParameters ().size (), 3); + CHECK_EQ (cmd2.getParameter (0).getInt (), 100); + CHECK_EQ (cmd2.getParameter (1).getText (), "hello"); } } - - } + } } - SCENARIO ("executing with callback and parameters") { - using namespace sl3 ; + using namespace sl3; GIVEN ("a database with some test data and a RowCallBack ") { Database db{":memory:"}; - db.execute( "CREATE TABLE t (f);" - "INSERT INTO t VALUES (1);" - "INSERT INTO t VALUES (2);" - "INSERT INTO t VALUES (3);"); - + db.execute ("CREATE TABLE t (f);" + "INSERT INTO t VALUES (1);" + "INSERT INTO t VALUES (2);" + "INSERT INTO t VALUES (3);"); struct : RowCallback { size_t counter{0}; - bool onRow(sl3::Columns) + bool + onRow (sl3::Columns) { ++counter; - return true ; + return true; } } cb; WHEN ("executing a command with parameters that give me 1 record") { auto cmd = db.prepare ("SELECT * FROM t WHERE f = ?"); - cmd.execute (cb, parameters(2)) ; - THEN ("then I got the one expected record") - { - CHECK (cb.counter == 1) ; - } + cmd.execute (cb, parameters (2)); + THEN ("then I got the one expected record") { CHECK (cb.counter == 1); } } } } - - SCENARIO ("handling constraint violations") { - using namespace sl3 ; + using namespace sl3; GIVEN ("a database with a table that requires unique values") { Database db{":memory:"}; - db.execute( "CREATE TABLE t (f UNIQUE);"); + db.execute ("CREATE TABLE t (f UNIQUE);"); WHEN ("having a value") { auto cmd = db.prepare ("INSERT INTO t VALUES(?);"); - cmd.execute (parameters(1)) ; + cmd.execute (parameters (1)); THEN ("then inserting a value again is a exception") { - CHECK_THROWS_AS (cmd.execute (), SQLite3Error) ; + CHECK_THROWS_AS (cmd.execute (), SQLite3Error); } } } } - SCENARIO ("binding values of all types and using select to check the result") { - using namespace sl3 ; + using namespace sl3; GIVEN ("a database with a table that can take all types and") { Database db{":memory:"}; - db.execute( "CREATE TABLE t (fi, fr, fs, fb, fn);"); + db.execute ("CREATE TABLE t (fi, fr, fs, fb, fn);"); auto sqin = "INSERT INTO t VALUES (?,?,?,?,?);"; - auto b = [](int v) -> std::byte { return std::byte(v); }; + auto b = [] (int v) -> std::byte { return std::byte (v); }; - DbValues params = { - DbValue{1} , - DbValue {2.2}, - DbValue {"drei"}, - DbValue {Blob{b(1) , b(2) , b(3) , b(4)}}, - DbValue{Type::Variant} - } ; + DbValues params = {DbValue{1}, + DbValue{2.2}, + DbValue{"drei"}, + DbValue{Blob{b (1), b (2), b (3), b (4)}}, + DbValue{Type::Variant}}; - auto cmd = db.prepare (sqin) ; + auto cmd = db.prepare (sqin); WHEN ("inserting values of all types") { - CHECK_NOTHROW (cmd.execute (params)) ; + CHECK_NOTHROW (cmd.execute (params)); THEN ("selecting all data returns the inserted data") { - auto ds = db.select ("SELECT * FROM t;") ; + auto ds = db.select ("SELECT * FROM t;"); REQUIRE_EQ (ds.size (), 1); - REQUIRE_EQ (ds[0].size () , params.size ()) ; - REQUIRE_EQ (ds[0][0].getInt (), params[0].getInt()); - REQUIRE_EQ (ds[0][1].getReal (), params[1].getReal()); - REQUIRE_EQ (ds[0][2].getText (), params[2].getText()); - REQUIRE_EQ (ds[0][3].getBlob (), params[3].getBlob()); - REQUIRE (ds[0][4].isNull ()) ; + REQUIRE_EQ (ds[0].size (), params.size ()); + REQUIRE_EQ (ds[0][0].getInt (), params[0].getInt ()); + REQUIRE_EQ (ds[0][1].getReal (), params[1].getReal ()); + REQUIRE_EQ (ds[0][2].getText (), params[2].getText ()); + REQUIRE_EQ (ds[0][3].getBlob (), params[3].getBlob ()); + REQUIRE (ds[0][4].isNull ()); } THEN ("selecting all data with concrete types will retun typed info") { - auto types = Types {Type::Int, Type::Real, Type::Text, Type::Blob, Type::Variant} ; - auto ds = db.select ("SELECT * FROM t;", types) ; + auto types = Types{ + Type::Int, Type::Real, Type::Text, Type::Blob, Type::Variant}; + auto ds = db.select ("SELECT * FROM t;", types); REQUIRE_EQ (ds.size (), 1); REQUIRE_EQ (ds[0].size (), types.size ()); for (size_t i = 0; i < ds[0].size (); ++i) - REQUIRE_EQ (ds[0][i].dbtype () , types[i]) ; + REQUIRE_EQ (ds[0][i].dbtype (), types[i]); } THEN ("selecting all data with wrong types will throw") { - auto types = Types {Type::Int, Type::Int, Type::Int, Type::Int, Type::Int} ; - REQUIRE_THROWS_AS ((void)db.select ("SELECT * FROM t;", types), ErrTypeMisMatch) ; - + auto types + = Types{Type::Int, Type::Int, Type::Int, Type::Int, Type::Int}; + REQUIRE_THROWS_AS ((void)db.select ("SELECT * FROM t;", types), + ErrTypeMisMatch); } THEN ("selecting all data with wrong numver of types will throw") { - auto types = Types {Type::Int, Type::Real, Type::Text, Type::Blob} ; - REQUIRE_THROWS_AS ((void)db.select ("SELECT * FROM t;", types), ErrTypeMisMatch) ; - + auto types = Types{Type::Int, Type::Real, Type::Text, Type::Blob}; + REQUIRE_THROWS_AS ((void)db.select ("SELECT * FROM t;", types), + ErrTypeMisMatch); } } } diff --git a/tests/database/dbtest.cpp b/tests/database/dbtest.cpp index 3962ce4..a660f94 100644 --- a/tests/database/dbtest.cpp +++ b/tests/database/dbtest.cpp @@ -6,18 +6,18 @@ #include #include -SCENARIO("creating a database") +SCENARIO ("creating a database") { GIVEN ("a valid and an invalid database name/path") { - auto good = ":memory:" ; - auto bad = "/this/does/not/exist/123" ; + auto good = ":memory:"; + auto bad = "/this/does/not/exist/123"; WHEN ("using the valid name/path") { THEN ("constructing the db works") { - CHECK_NOTHROW(sl3::Database{good}); + CHECK_NOTHROW (sl3::Database{good}); } } @@ -25,15 +25,13 @@ SCENARIO("creating a database") { THEN ("constructing the db works") { - CHECK_THROWS_AS(sl3::Database{bad}, sl3::SQLite3Error) ; + CHECK_THROWS_AS (sl3::Database{bad}, sl3::SQLite3Error); } } } } - - -SCENARIO("creating some test data") +SCENARIO ("creating some test data") { GIVEN ("an im memory database") { @@ -41,12 +39,11 @@ SCENARIO("creating some test data") WHEN ("executing invalid SQL") { - auto wrong_sql = "CRATE NABLE foo (f1, f2);" ; + auto wrong_sql = "CRATE NABLE foo (f1, f2);"; THEN ("execute triggers an exception") { - CHECK_THROWS_AS (db.execute (wrong_sql), sl3::SQLite3Error) ; + CHECK_THROWS_AS (db.execute (wrong_sql), sl3::SQLite3Error); } - } WHEN ("creating a table and some test data") @@ -55,216 +52,204 @@ SCENARIO("creating some test data") "CREATE TABLE tbltest (intFld INTEGER,txtFld TEXT, dblFld real );" "INSERT INTO tbltest VALUES (1, 'eins', 1.111) ;" "INSERT INTO tbltest VALUES (2, 'zwei', 2.22) ;" - "INSERT INTO tbltest VALUES (3, NULL, NULL) ;" - ); + "INSERT INTO tbltest VALUES (3, NULL, NULL) ;"); THEN ("I can veryfy that the table and some test data exists") { - CHECK (db.getTotalChanges () == 3) ; - CHECK_NOTHROW (db.execute (std::string{"DELETE FROM tbltest;"})) ; - CHECK (db.getTotalChanges () == 6) ; - CHECK (db.getRecentlyChanged () == 3) ; + CHECK (db.getTotalChanges () == 3); + CHECK_NOTHROW (db.execute (std::string{"DELETE FROM tbltest;"})); + CHECK (db.getTotalChanges () == 6); + CHECK (db.getRecentlyChanged () == 3); } - } } } - -SCENARIO("move construct a database") +SCENARIO ("move construct a database") { - GIVEN("a db with a table") + GIVEN ("a db with a table") { sl3::Database db{":memory:"}; db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;"); + "INSERT INTO tbltest VALUES (1) ;"); - CHECK_NOTHROW (db.execute ("SELECT COUNT(*) FROM tbltest;")) ; + CHECK_NOTHROW (db.execute ("SELECT COUNT(*) FROM tbltest;")); WHEN ("Moveconstruct a new db from an existing one") { - sl3::Database db1{std::move(db)} ; - THEN("the new constructed db has data") + sl3::Database db1{std::move (db)}; + THEN ("the new constructed db has data") { - CHECK_NOTHROW (db1.execute ("SELECT COUNT(*) FROM tbltest;")) ; + CHECK_NOTHROW (db1.execute ("SELECT COUNT(*) FROM tbltest;")); } - AND_THEN("the moved db is disconnected") + AND_THEN ("the moved db is disconnected") { CHECK_THROWS_AS (db.execute ("SELECT COUNT(*) FROM tbltest;"), - sl3::ErrNoConnection) ; - -// those are protected, does this make sense? -// adding a derivating from a database -// CHECK(db.db() == nullptr) ; -// CHECK(db1.db() != nullptr) ; + sl3::ErrNoConnection); + // those are protected, does this make sense? + // adding a derivating from a database + // CHECK(db.db() == nullptr) ; + // CHECK(db1.db() != nullptr) ; } } } } - -SCENARIO("check the connections and if derivate works") +SCENARIO ("check the connections and if derivate works") { - GIVEN("a derivated db that let me access db property") + GIVEN ("a derivated db that let me access db property") { struct MyDb : public sl3::Database { - using sl3::Database::Database ; + using sl3::Database::Database; using sl3::Database::db; }; MyDb db{":memory:"}; db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;"); + "INSERT INTO tbltest VALUES (1) ;"); - CHECK_NOTHROW (db.execute ("SELECT COUNT(*) FROM tbltest;")) ; + CHECK_NOTHROW (db.execute ("SELECT COUNT(*) FROM tbltest;")); WHEN ("Moveconstruct a new db from an existing one") { - MyDb db1{std::move(db)} ; - THEN("the new constructed db has data") + MyDb db1{std::move (db)}; + THEN ("the new constructed db has data") { - CHECK_NOTHROW (db1.execute ("SELECT COUNT(*) FROM tbltest;")) ; + CHECK_NOTHROW (db1.execute ("SELECT COUNT(*) FROM tbltest;")); } - AND_THEN("the moved db is disconnected") + AND_THEN ("the moved db is disconnected") { CHECK_THROWS_AS (db.execute ("SELECT COUNT(*) FROM tbltest;"), - sl3::ErrNoConnection) ; + sl3::ErrNoConnection); - CHECK(db.db() == nullptr) ; - CHECK(db1.db() != nullptr) ; + CHECK (db.db () == nullptr); + CHECK (db1.db () != nullptr); } } } } - - -SCENARIO("prepare commands") +SCENARIO ("prepare commands") { - GIVEN("a db with a table") + GIVEN ("a db with a table") { sl3::Database db{":memory:"}; db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;"); + "INSERT INTO tbltest VALUES (1) ;"); - REQUIRE_NOTHROW (db.execute ("SELECT COUNT(*) FROM tbltest;")) ; + REQUIRE_NOTHROW (db.execute ("SELECT COUNT(*) FROM tbltest;")); WHEN ("compiling a valid sql statement") { - auto sql = "SELECT * FROM tbltest;" ; + auto sql = "SELECT * FROM tbltest;"; - THEN("a new command is created") + THEN ("a new command is created") { - CHECK_NOTHROW( - auto cmd = db.prepare (sql) ; - (void)cmd; - ); + CHECK_NOTHROW (auto cmd = db.prepare (sql); (void)cmd;); } } WHEN ("compiling an invalid sql statement") { - auto sql = "SELECT_X * X_FROM tbltest;" ; + auto sql = "SELECT_X * X_FROM tbltest;"; - THEN("creating a command throws") + THEN ("creating a command throws") { - CHECK_THROWS_AS((void)db.prepare (sql), sl3::SQLite3Error); + CHECK_THROWS_AS ((void)db.prepare (sql), sl3::SQLite3Error); } } WHEN ("compiling on a disconnected db") { - auto db1 = std::move (db) ; - auto sql = "SELECT * FROM tbltest;" ; - auto sql1 = "INSERT INTO tbltest VALUES (?);" ; + auto db1 = std::move (db); + auto sql = "SELECT * FROM tbltest;"; + auto sql1 = "INSERT INTO tbltest VALUES (?);"; - THEN("creating a command throws") + THEN ("creating a command throws") { - CHECK_THROWS_AS((void)db.prepare (sql), sl3::ErrNoConnection); + CHECK_THROWS_AS ((void)db.prepare (sql), sl3::ErrNoConnection); - sl3::DbValues vals{ sl3::DbValue{1} } ; - CHECK_THROWS_AS((void)db.prepare (sql1, vals), sl3::ErrNoConnection); + sl3::DbValues vals{sl3::DbValue{1}}; + CHECK_THROWS_AS ((void)db.prepare (sql1, vals), sl3::ErrNoConnection); } } WHEN ("compiling sql with the correct number of parameters") { - auto sql = "INSERT INTO tbltest VALUES (?);" ; - THEN("creating a command succeeds") + auto sql = "INSERT INTO tbltest VALUES (?);"; + THEN ("creating a command succeeds") { - sl3::DbValues vals{ sl3::DbValue{1} } ; - CHECK_NOTHROW((void)db.prepare (sql,vals)); + sl3::DbValues vals{sl3::DbValue{1}}; + CHECK_NOTHROW ((void)db.prepare (sql, vals)); } } WHEN ("compiling sql with a wrong number of parameters") { - auto sql = "INSERT INTO tbltest VALUES (?);" ; + auto sql = "INSERT INTO tbltest VALUES (?);"; - THEN("creating a command throws") + THEN ("creating a command throws") { - sl3::DbValues vals{ sl3::DbValue{1}, sl3::DbValue{2} } ; - CHECK_THROWS((void)db.prepare (sql,sl3::DbValues{})); - CHECK_THROWS((void)db.prepare (sql,vals)); + sl3::DbValues vals{sl3::DbValue{1}, sl3::DbValue{2}}; + CHECK_THROWS ((void)db.prepare (sql, sl3::DbValues{})); + CHECK_THROWS ((void)db.prepare (sql, vals)); // Details about the exception in the command tests } } } } -SCENARIO("using execute with RowCallback") +SCENARIO ("using execute with RowCallback") { - - GIVEN("a db with some data and the most simple RowCallback ever") + GIVEN ("a db with some data and the most simple RowCallback ever") { sl3::Database db{":memory:"}; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;") - ); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;")); struct : sl3::RowCallback { - bool onRow(sl3::Columns /*unusedForNow*/) + bool + onRow (sl3::Columns /*unusedForNow*/) { - return true ; + return true; } } cb; - WHEN("executed with valid sql") + WHEN ("executed with valid sql") { - auto sql = "SELECT * FROM tbltest;" ; - THEN("it works without an error") + auto sql = "SELECT * FROM tbltest;"; + THEN ("it works without an error") { - CHECK_NOTHROW(db.execute(sql, cb)) ; + CHECK_NOTHROW (db.execute (sql, cb)); } } - WHEN("executed with incorrect sql") + WHEN ("executed with incorrect sql") { - auto sql = "SELECT_X * X_FROM tbltest;" ; - THEN("a SQLite3Error is thrown") + auto sql = "SELECT_X * X_FROM tbltest;"; + THEN ("a SQLite3Error is thrown") { - CHECK_THROWS_AS(db.execute(sql, cb), sl3::SQLite3Error) ; + CHECK_THROWS_AS (db.execute (sql, cb), sl3::SQLite3Error); } } - WHEN("executed on a disconnected database") + WHEN ("executed on a disconnected database") { - sl3::Database db1{std::move(db)} ; - auto sql = "SELECT * FROM tbltest;" ; - THEN("a ErrNoConnection is thrown") + sl3::Database db1{std::move (db)}; + auto sql = "SELECT * FROM tbltest;"; + THEN ("a ErrNoConnection is thrown") { - CHECK_THROWS_AS(db.execute(sql, cb), sl3::ErrNoConnection) ; - CHECK_NOTHROW(db1.execute(sql, cb)) ; + CHECK_THROWS_AS (db.execute (sql, cb), sl3::ErrNoConnection); + CHECK_NOTHROW (db1.execute (sql, cb)); } } } @@ -272,52 +257,44 @@ SCENARIO("using execute with RowCallback") // for details about how to use RowCallback, see (row)callback tests } - - - -SCENARIO("using execute with Callback") +SCENARIO ("using execute with Callback") { - - GIVEN("a db with some test data and the most simple Callback ") + GIVEN ("a db with some test data and the most simple Callback ") { sl3::Database db{":memory:"}; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;") - ); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;")); - sl3::Database::Callback cb = [](sl3::Columns /*ignoreForNow*/) - { - return true ; - }; + sl3::Database::Callback cb + = [] (sl3::Columns /*ignoreForNow*/) { return true; }; - WHEN("executed with valid SQL") + WHEN ("executed with valid SQL") { - auto sql = "SELECT * FROM tbltest;" ; - THEN("it works without an error") + auto sql = "SELECT * FROM tbltest;"; + THEN ("it works without an error") { - CHECK_NOTHROW(db.execute(sql, cb)) ; + CHECK_NOTHROW (db.execute (sql, cb)); } } - WHEN("executed with incorrect sql") + WHEN ("executed with incorrect sql") { - auto sql = "SELECT_X * X_FROM tbltest;" ; - THEN("a SQLite3Error is thrown") + auto sql = "SELECT_X * X_FROM tbltest;"; + THEN ("a SQLite3Error is thrown") { - CHECK_THROWS_AS(db.execute(sql, cb), sl3::SQLite3Error) ; + CHECK_THROWS_AS (db.execute (sql, cb), sl3::SQLite3Error); } } - WHEN("executed on a disconnected database") + WHEN ("executed on a disconnected database") { - auto db1 = std::move(db) ; - auto sql = "SELECT * FROM tbltest;" ; - THEN("a ErrNoConnection is thrown") + auto db1 = std::move (db); + auto sql = "SELECT * FROM tbltest;"; + THEN ("a ErrNoConnection is thrown") { - CHECK_THROWS_AS(db.execute(sql, cb), sl3::ErrNoConnection) ; - CHECK_NOTHROW(db1.execute(sql, cb)) ; + CHECK_THROWS_AS (db.execute (sql, cb), sl3::ErrNoConnection); + CHECK_NOTHROW (db1.execute (sql, cb)); } } } @@ -325,54 +302,49 @@ SCENARIO("using execute with Callback") // for details about how to use callbacks,see (row)callback tests } - - - - -SCENARIO("selecting datasets") +SCENARIO ("selecting datasets") { - - GIVEN("a db with some test data") + GIVEN ("a db with some test data") { sl3::Database db{":memory:"}; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;") - ); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;")); using namespace sl3; - WHEN("select with valid SQL") + WHEN ("select with valid SQL") { - auto sql = "SELECT * FROM tbltest;" ; - THEN("it works without an error") + auto sql = "SELECT * FROM tbltest;"; + THEN ("it works without an error") { - CHECK_NOTHROW((void)db.select(sql)) ; - CHECK_NOTHROW((void)db.select(sql, {Type::Variant})) ; + CHECK_NOTHROW ((void)db.select (sql)); + CHECK_NOTHROW ((void)db.select (sql, {Type::Variant})); } } - WHEN("executed with incorrect sql") + WHEN ("executed with incorrect sql") { - auto sql = "SELECT_X * X_FROM tbltest;" ; - THEN("a SQLite3Error is thrown") + auto sql = "SELECT_X * X_FROM tbltest;"; + THEN ("a SQLite3Error is thrown") { - CHECK_THROWS_AS((void)db.select(sql), sl3::SQLite3Error) ; - CHECK_THROWS_AS((void)db.select(sql,{Type::Variant}), sl3::SQLite3Error) ; + CHECK_THROWS_AS ((void)db.select (sql), sl3::SQLite3Error); + CHECK_THROWS_AS ((void)db.select (sql, {Type::Variant}), + sl3::SQLite3Error); } } - WHEN("executed on a disconnected database") + WHEN ("executed on a disconnected database") { - auto db1 = std::move(db) ; - auto sql = "SELECT * FROM tbltest;" ; - THEN("a ErrNoConnection is thrown") + auto db1 = std::move (db); + auto sql = "SELECT * FROM tbltest;"; + THEN ("a ErrNoConnection is thrown") { - CHECK_THROWS_AS((void)db.select(sql), sl3::ErrNoConnection) ; - CHECK_NOTHROW((void)db1.select(sql)) ; - CHECK_THROWS_AS((void)db.select(sql,{Type::Variant}), sl3::ErrNoConnection) ; - CHECK_NOTHROW((void)db1.select(sql,{Type::Variant})) ; + CHECK_THROWS_AS ((void)db.select (sql), sl3::ErrNoConnection); + CHECK_NOTHROW ((void)db1.select (sql)); + CHECK_THROWS_AS ((void)db.select (sql, {Type::Variant}), + sl3::ErrNoConnection); + CHECK_NOTHROW ((void)db1.select (sql, {Type::Variant})); } } } @@ -380,79 +352,70 @@ SCENARIO("selecting datasets") // for details about how to use datasets,see dataset tests } - -SCENARIO("selecting values") +SCENARIO ("selecting values") { - - GIVEN("a db") + GIVEN ("a db") { sl3::Database db{":memory:"}; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;") - ); - - - using namespace sl3 ;; + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;")); + using namespace sl3; + ; - WHEN("select a value with valid SQL") + WHEN ("select a value with valid SQL") { - auto sql = "SELECT 1;" ; - THEN("it works without an error") + auto sql = "SELECT 1;"; + THEN ("it works without an error") { - CHECK_NOTHROW((void)db.selectValue(sql)) ; - CHECK_NOTHROW((void)db.selectValue(sql, Type::Int)) ; + CHECK_NOTHROW ((void)db.selectValue (sql)); + CHECK_NOTHROW ((void)db.selectValue (sql, Type::Int)); } } - WHEN("executed with incorrect sql") + WHEN ("executed with incorrect sql") { - auto sql = "SELECT_X 1;" ; - THEN("a SQLite3Error is thrown") + auto sql = "SELECT_X 1;"; + THEN ("a SQLite3Error is thrown") { - CHECK_THROWS_AS((void)db.selectValue(sql), SQLite3Error) ; - CHECK_THROWS_AS((void)db.selectValue(sql, Type::Int), SQLite3Error) ; + CHECK_THROWS_AS ((void)db.selectValue (sql), SQLite3Error); + CHECK_THROWS_AS ((void)db.selectValue (sql, Type::Int), SQLite3Error); } } - WHEN("executed on a disconnected database") + WHEN ("executed on a disconnected database") { - auto db1 = std::move(db) ; - auto sql = "SELECT 1;" ; - THEN("a ErrNoConnection is thrown") + auto db1 = std::move (db); + auto sql = "SELECT 1;"; + THEN ("a ErrNoConnection is thrown") { - CHECK_THROWS_AS((void)db.selectValue(sql), ErrNoConnection) ; - CHECK_NOTHROW((void)db1.selectValue(sql)) ; - - CHECK_THROWS_AS((void)db.selectValue(sql, Type::Int), ErrNoConnection) ; - CHECK_NOTHROW((void)db1.selectValue(sql, Type::Int)) ; + CHECK_THROWS_AS ((void)db.selectValue (sql), ErrNoConnection); + CHECK_NOTHROW ((void)db1.selectValue (sql)); + CHECK_THROWS_AS ((void)db.selectValue (sql, Type::Int), + ErrNoConnection); + CHECK_NOTHROW ((void)db1.selectValue (sql, Type::Int)); } } - WHEN("select a value with the wrong type") + WHEN ("select a value with the wrong type") { // this is actually a Columns test, but I let it here for now - auto sql = "SELECT 1;" ; - THEN("a type mis match is thrown") + auto sql = "SELECT 1;"; + THEN ("a type mis match is thrown") { - CHECK_THROWS_AS((void)db.selectValue(sql, Type::Text), ErrTypeMisMatch); + CHECK_THROWS_AS ((void)db.selectValue (sql, Type::Text), + ErrTypeMisMatch); } } - } // for details about how to use dbvalues,see dbvalues tests } - - - -SCENARIO("accessing database change properties") +SCENARIO ("accessing database change properties") { - - GIVEN("an database with an empty table") + GIVEN ("an database with an empty table") { sl3::Database db{":memory:"}; @@ -460,56 +423,51 @@ SCENARIO("accessing database change properties") { THEN ("no changes have been counted") { - CHECK(db.getTotalChanges() == 0) ; - CHECK(db.getRecentlyChanged() == 0) ; - CHECK(db.getLastInsertRowid() == 0) ; + CHECK (db.getTotalChanges () == 0); + CHECK (db.getRecentlyChanged () == 0); + CHECK (db.getLastInsertRowid () == 0); } } WHEN ("inserting 2 rows") { - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (1) ;")); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (1) ;")); THEN ("changes have been counted") { - CHECK(db.getTotalChanges() == 2) ; - CHECK(db.getRecentlyChanged() == 1) ; - CHECK(db.getLastInsertRowid() == 2) ; + CHECK (db.getTotalChanges () == 2); + CHECK (db.getRecentlyChanged () == 1); + CHECK (db.getLastInsertRowid () == 2); } } WHEN ("accessing the properties on a moved from db") { - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (1) ;")); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (1) ;")); - sl3::Database db1 {std::move(db)} ; + sl3::Database db1{std::move (db)}; THEN ("no undefined behavior but exceptions are thorwn") { + CHECK_THROWS_AS ((void)db.getTotalChanges (), sl3::ErrNoConnection); + CHECK_THROWS_AS ((void)db.getRecentlyChanged (), sl3::ErrNoConnection); + CHECK_THROWS_AS ((void)db.getLastInsertRowid (), sl3::ErrNoConnection); - CHECK_THROWS_AS((void)db.getTotalChanges(), sl3::ErrNoConnection) ; - CHECK_THROWS_AS((void)db.getRecentlyChanged(), sl3::ErrNoConnection) ; - CHECK_THROWS_AS((void)db.getLastInsertRowid(), sl3::ErrNoConnection) ; - - CHECK(db1.getTotalChanges() == 2) ; - CHECK(db1.getRecentlyChanged() == 1) ; - CHECK(db1.getLastInsertRowid() == 2) ; + CHECK (db1.getTotalChanges () == 2); + CHECK (db1.getRecentlyChanged () == 1); + CHECK (db1.getLastInsertRowid () == 2); } } } } - -SCENARIO("accessing database error properties") +SCENARIO ("accessing database error properties") { - - GIVEN("an database an empty datbase") + GIVEN ("an database an empty datbase") { sl3::Database db{":memory:"}; @@ -517,130 +475,119 @@ SCENARIO("accessing database error properties") { THEN ("no errors are reported") { - CHECK(db.getMostRecentErrCode() == 0) ; - CHECK(db.getMostRecentErrMsg() == sl3::getErrStr(0)) ; + CHECK (db.getMostRecentErrCode () == 0); + CHECK (db.getMostRecentErrMsg () == sl3::getErrStr (0)); } } WHEN ("something has been done wrong") { - REQUIRE_THROWS(db.execute ("CREATE SCHNALBE tbltest (f INTEGER);")); - THEN ( "errors are reported") + REQUIRE_THROWS (db.execute ("CREATE SCHNALBE tbltest (f INTEGER);")); + THEN ("errors are reported") { - CHECK(db.getMostRecentErrCode() != 0) ; - CHECK(db.getMostRecentErrMsg() != sl3::getErrStr(0)) ; + CHECK (db.getMostRecentErrCode () != 0); + CHECK (db.getMostRecentErrMsg () != sl3::getErrStr (0)); } } WHEN ("something has been done wrong, and than something that worked") { - REQUIRE_THROWS(db.execute ("CREATE SCHNALBE tbltest (f INTEGER);")); - REQUIRE_NOTHROW(db.execute ("CREATE TABLE tbltest (f INTEGER);")); - THEN ( "errors are cleared") + REQUIRE_THROWS (db.execute ("CREATE SCHNALBE tbltest (f INTEGER);")); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);")); + THEN ("errors are cleared") { - CHECK(db.getMostRecentErrCode() == 0) ; - CHECK(db.getMostRecentErrMsg() == sl3::getErrStr(0)) ; + CHECK (db.getMostRecentErrCode () == 0); + CHECK (db.getMostRecentErrMsg () == sl3::getErrStr (0)); } } WHEN ("accessing the properties on a moved from db") { - REQUIRE_THROWS(db.execute ("CREATE SCHNALBE tbltest (f INTEGER);")); + REQUIRE_THROWS (db.execute ("CREATE SCHNALBE tbltest (f INTEGER);")); - sl3::Database db1 {std::move(db)} ; + sl3::Database db1{std::move (db)}; THEN ("exceptions are thrown, but not fot ") { - CHECK_THROWS_AS((void)db.getMostRecentErrCode(), sl3::ErrNoConnection) ; - CHECK_THROWS_AS((void)db.getMostRecentErrMsg(), sl3::ErrNoConnection) ; - - CHECK(db1.getMostRecentErrCode() != 0) ; - CHECK(db1.getMostRecentErrMsg() != sl3::getErrStr(0)) ; + CHECK_THROWS_AS ((void)db.getMostRecentErrCode (), + sl3::ErrNoConnection); + CHECK_THROWS_AS ((void)db.getMostRecentErrMsg (), + sl3::ErrNoConnection); + CHECK (db1.getMostRecentErrCode () != 0); + CHECK (db1.getMostRecentErrMsg () != sl3::getErrStr (0)); } } } } - -SCENARIO("using transactions") +SCENARIO ("using transactions") { - - GIVEN("an empty database ") + GIVEN ("an empty database ") { sl3::Database db{":memory:"}; WHEN ("using a transaction to create table and data but do not commit") { { - auto trans = db.beginTransaction() ; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (1) ;")); + auto trans = db.beginTransaction (); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (1) ;")); } THEN ("no changes have been made and the table does not exist") { - REQUIRE_THROWS(db.execute ("SELECT * FROM tbltest;")); + REQUIRE_THROWS (db.execute ("SELECT * FROM tbltest;")); } } WHEN ("using a transaction to create table and data and call commit") { { - auto trans = db.beginTransaction() ; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (1) ;")); - trans.commit() ; + auto trans = db.beginTransaction (); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (1) ;")); + trans.commit (); } THEN ("changes have been made and the table exists") { - REQUIRE_NOTHROW(db.execute ("SELECT * FROM tbltest;")); + REQUIRE_NOTHROW (db.execute ("SELECT * FROM tbltest;")); } } - WHEN ("moving a transaction and commit on the moved from trans") - { - { - auto trans = db.beginTransaction() ; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (1) ;")); - sl3::Database::Transaction trans1 = std::move(trans) ; - - trans.commit() ; - } - THEN ("commit on the moved from object does not effect the database") - { - REQUIRE_THROWS(db.execute ("SELECT * FROM tbltest;")); - } - - } + { + { + auto trans = db.beginTransaction (); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (1) ;")); + sl3::Database::Transaction trans1 = std::move (trans); + + trans.commit (); + } + THEN ("commit on the moved from object does not effect the database") + { + REQUIRE_THROWS (db.execute ("SELECT * FROM tbltest;")); + } + } WHEN ("moving a transaction and commit") - { - { - auto trans = db.beginTransaction() ; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (1) ;")); - sl3::Database::Transaction trans1 = std::move(trans) ; - - trans1.commit() ; - } - THEN ("commit did effect the database") - { - REQUIRE_NOTHROW(db.execute ("SELECT * FROM tbltest;")); - } - - } + { + { + auto trans = db.beginTransaction (); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (1) ;")); + sl3::Database::Transaction trans1 = std::move (trans); + trans1.commit (); + } + THEN ("commit did effect the database") + { + REQUIRE_NOTHROW (db.execute ("SELECT * FROM tbltest;")); + } + } } } - - diff --git a/tests/dataset/datasettest.cpp b/tests/dataset/datasettest.cpp index fe8f60d..83fcdba 100644 --- a/tests/dataset/datasettest.cpp +++ b/tests/dataset/datasettest.cpp @@ -3,134 +3,121 @@ #include -SCENARIO("dataset creation and defautl operatores") +SCENARIO ("dataset creation and defautl operatores") { - using namespace sl3 ; + using namespace sl3; GIVEN ("a database, a table and knowen data") { + Database db{":memory:"}; + db.execute ("CREATE TABLE t (int INTEGER,txt TEXT, dbl real );" + "INSERT INTO t VALUES (1, 'eins', 1.111) ;" + "INSERT INTO t VALUES (2, 'zwei', 2.22) ;" + "INSERT INTO t VALUES (3, NULL, NULL) ;"); - Database db{ ":memory:" }; - db.execute ( - "CREATE TABLE t (int INTEGER,txt TEXT, dbl real );" - "INSERT INTO t VALUES (1, 'eins', 1.111) ;" - "INSERT INTO t VALUES (2, 'zwei', 2.22) ;" - "INSERT INTO t VALUES (3, NULL, NULL) ;" - ); - - const Types types{Type::Int, Type::Text, Type::Real} ; + const Types types{Type::Int, Type::Text, Type::Real}; WHEN ("having an empty dataset") { - Dataset ds ; + Dataset ds; THEN ("assinging new dataset is possible without problems") { - CHECK_NOTHROW(ds = db.select("SELECT * FROM t;")); - CHECK_EQ(ds.getIndex("int"), 0) ; - CHECK_EQ(ds.getIndex("txt"), 1) ; - CHECK_EQ(ds.getIndex("dbl"), 2) ; - CHECK_THROWS_AS(ds.getIndex("abc"), ErrOutOfRange); - - CHECK_EQ(ds.size (), 3) ; - CHECK_EQ(ds.at(0).size (), 3) ; - + CHECK_NOTHROW (ds = db.select ("SELECT * FROM t;")); + CHECK_EQ (ds.getIndex ("int"), 0); + CHECK_EQ (ds.getIndex ("txt"), 1); + CHECK_EQ (ds.getIndex ("dbl"), 2); + CHECK_THROWS_AS (ds.getIndex ("abc"), ErrOutOfRange); + + CHECK_EQ (ds.size (), 3); + CHECK_EQ (ds.at (0).size (), 3); } } - WHEN ("having a typed dataset") { - auto ds = db.select("SELECT * FROM t;", types) ; + auto ds = db.select ("SELECT * FROM t;", types); THEN ("the fieds are not variants") { - for(const auto &row : ds) + for (const auto& row : ds) { - REQUIRE(row.size() == types.size()) ; - REQUIRE(row.at(0).dbtype() == types[0]) ; - REQUIRE(row.at(1).dbtype() == types[1]) ; - REQUIRE(row.at(2).dbtype() == types[2]) ; + REQUIRE (row.size () == types.size ()); + REQUIRE (row.at (0).dbtype () == types[0]); + REQUIRE (row.at (1).dbtype () == types[1]); + REQUIRE (row.at (2).dbtype () == types[2]); } } } - WHEN ("creating a dataset via a rvalue") { - auto dsm = db.select("SELECT * FROM t;") ; - CHECK_NOTHROW(Dataset ds{std::move(dsm)}); + auto dsm = db.select ("SELECT * FROM t;"); + CHECK_NOTHROW (Dataset ds{std::move (dsm)}); THEN ("the ds include all data and names") { - dsm = db.select("SELECT * FROM t;"); - Dataset ds{std::move(dsm)} ; + dsm = db.select ("SELECT * FROM t;"); + Dataset ds{std::move (dsm)}; - CHECK_EQ(ds.getIndex("int"), 0) ; - CHECK_EQ(ds.getIndex("txt"), 1) ; - CHECK_EQ(ds.getIndex("dbl"), 2) ; + CHECK_EQ (ds.getIndex ("int"), 0); + CHECK_EQ (ds.getIndex ("txt"), 1); + CHECK_EQ (ds.getIndex ("dbl"), 2); } } - } } - -SCENARIO("merging datasets") +SCENARIO ("merging datasets") { - using namespace sl3 ; - GIVEN ("a database and a typed empty dataset") - { - - Database db{ ":memory:" }; - Dataset ds({Type::Int,Type::Text, Type::Real}) ; + using namespace sl3; + GIVEN ("a database and a typed empty dataset") + { + Database db{":memory:"}; + Dataset ds ({Type::Int, Type::Text, Type::Real}); WHEN ("having a type compatible dataset") { - const Types types = {Type::Int, Type::Text, Type::Real} ; - auto ds1 =db.select("SELECT 1,'hello', 2.2;", types ); + const Types types = {Type::Int, Type::Text, Type::Real}; + auto ds1 = db.select ("SELECT 1,'hello', 2.2;", types); - THEN ("merging works") - { - CHECK_NOTHROW(ds.merge (ds1)); - } + THEN ("merging works") { CHECK_NOTHROW (ds.merge (ds1)); } AND_THEN ("merging compatible DbValues works") { - - CHECK_NOTHROW(ds.merge(ds1.at(0))); + CHECK_NOTHROW (ds.merge (ds1.at (0))); } } WHEN ("merge a dataset with different field count") { - const auto dsdifferent = db.select("SELECT 1,'hello';", - {Type::Int, Type::Text}); + const auto dsdifferent + = db.select ("SELECT 1,'hello';", {Type::Int, Type::Text}); THEN ("merging this dataset throws") { - CHECK_THROWS_AS(ds.merge (dsdifferent), ErrTypeMisMatch); + CHECK_THROWS_AS (ds.merge (dsdifferent), ErrTypeMisMatch); } AND_THEN ("merging just a row of this dataset also throws") { - CHECK_THROWS_AS(ds.merge (dsdifferent.at(0)), ErrTypeMisMatch); + CHECK_THROWS_AS (ds.merge (dsdifferent.at (0)), ErrTypeMisMatch); } } WHEN ("having a dataset with incompatible types") { - const auto dsdifferent = db.select("SELECT 'he', 'll', 'o';") ; + const auto dsdifferent = db.select ("SELECT 'he', 'll', 'o';"); THEN ("merging this ds throws") { - CHECK_THROWS_AS(ds.merge(dsdifferent), ErrTypeMisMatch); + CHECK_THROWS_AS (ds.merge (dsdifferent), ErrTypeMisMatch); } THEN ("merging a row of this ds throws") { - CHECK_THROWS_AS(ds.merge(dsdifferent.at(0)), ErrTypeMisMatch); + CHECK_THROWS_AS (ds.merge (dsdifferent.at (0)), ErrTypeMisMatch); } AND_WHEN ("reset the fields but not the types") { THEN ("merging this ds still thorws") { - ds.reset() ; - CHECK_THROWS_AS(ds.merge(dsdifferent), ErrTypeMisMatch); + ds.reset (); + CHECK_THROWS_AS (ds.merge (dsdifferent), ErrTypeMisMatch); } } @@ -138,150 +125,130 @@ SCENARIO("merging datasets") { THEN ("merging works") { - ds.reset({Type::Variant,Type::Variant, Type::Variant}) ; - CHECK_NOTHROW(ds.merge(dsdifferent)); - CHECK(ds.size () == 1); + ds.reset ({Type::Variant, Type::Variant, Type::Variant}); + CHECK_NOTHROW (ds.merge (dsdifferent)); + CHECK (ds.size () == 1); } } } WHEN ("having an dataset with names fields") { - Dataset dsf = db.select("SELECT 1 as int, 'eins' as txt, 2.2 as dbl;"); - CHECK_EQ(dsf.getIndex("int"), 0) ; - CHECK_EQ(dsf.getIndex("txt"), 1) ; - CHECK_EQ(dsf.getIndex("dbl"), 2) ; + Dataset dsf = db.select ("SELECT 1 as int, 'eins' as txt, 2.2 as dbl;"); + CHECK_EQ (dsf.getIndex ("int"), 0); + CHECK_EQ (dsf.getIndex ("txt"), 1); + CHECK_EQ (dsf.getIndex ("dbl"), 2); THEN ("merging a dataset with the same fieldnames work") { - const auto orig_cout = dsf.size() ; - auto ds1 = db.select("SELECT 1 as int, 'eins' as txt, 2.2 as dbl;"); - CHECK_NOTHROW(dsf.merge(ds1)) ; - CHECK (dsf.size() == orig_cout+1); + const auto orig_cout = dsf.size (); + auto ds1 = db.select ("SELECT 1 as int, 'eins' as txt, 2.2 as dbl;"); + CHECK_NOTHROW (dsf.merge (ds1)); + CHECK (dsf.size () == orig_cout + 1); } AND_WHEN ("merging a dataset with different fieldnames throws") { - auto ds1 = db.select("SELECT 1 as foo, 'eins' as bar, 2.2 as bas;"); + auto ds1 = db.select ("SELECT 1 as foo, 'eins' as bar, 2.2 as bas;"); THEN ("merging throws") { - CHECK_THROWS_AS(ds.merge(ds1), ErrTypeMisMatch) ; + CHECK_THROWS_AS (ds.merge (ds1), ErrTypeMisMatch); } } - } - } } - - - -SCENARIO("sorting a dataset") +SCENARIO ("sorting a dataset") { - using namespace sl3 ; + using namespace sl3; GIVEN ("a database, a table and known data") { - - Database db{ ":memory:" }; - db.execute ( - "CREATE TABLE t (int INTEGER,txt TEXT, dbl real );" - "INSERT INTO t VALUES (1, 1.1, 'b') ;" - "INSERT INTO t VALUES (2, 0.11, 'c') ;" - "INSERT INTO t VALUES (1, 0.2, 'a') ;" - ); - + Database db{":memory:"}; + db.execute ("CREATE TABLE t (int INTEGER,txt TEXT, dbl real );" + "INSERT INTO t VALUES (1, 1.1, 'b') ;" + "INSERT INTO t VALUES (2, 0.11, 'c') ;" + "INSERT INTO t VALUES (1, 0.2, 'a') ;"); WHEN ("having dataset with known order") { - Dataset ds = db.select("SELECT * FROM t;"); - CHECK_EQ(ds[0][2].getText(), "b") ; - CHECK_EQ(ds[1][2].getText(), "c") ; - CHECK_EQ(ds[2][2].getText(), "a") ; + Dataset ds = db.select ("SELECT * FROM t;"); + CHECK_EQ (ds[0][2].getText (), "b"); + CHECK_EQ (ds[1][2].getText (), "c"); + CHECK_EQ (ds[2][2].getText (), "a"); THEN ("sorting with one index sorts as expected") { - ds.sort({0}) ; - CHECK_EQ(ds[0][2].getText(), "b") ; - CHECK_EQ(ds[1][2].getText(), "a") ; - CHECK_EQ(ds[2][2].getText(), "c") ; + ds.sort ({0}); + CHECK_EQ (ds[0][2].getText (), "b"); + CHECK_EQ (ds[1][2].getText (), "a"); + CHECK_EQ (ds[2][2].getText (), "c"); } THEN ("sorting with 2 indexes sorts as expected") { - ds.sort({0, 1}) ; - CHECK_EQ(ds[0][2].getText(), "a") ; - CHECK_EQ(ds[1][2].getText(), "b") ; - CHECK_EQ(ds[2][2].getText(), "c") ; + ds.sort ({0, 1}); + CHECK_EQ (ds[0][2].getText (), "a"); + CHECK_EQ (ds[1][2].getText (), "b"); + CHECK_EQ (ds[2][2].getText (), "c"); } - } } } - - - -SCENARIO("doing some things via dbvalues on rows of datasets") +SCENARIO ("doing some things via dbvalues on rows of datasets") { - using namespace sl3 ; - GIVEN ("a database and a typed dataset with knowen content") - { - Database db{ ":memory:" }; - Types types = {Type::Int,Type::Text, Type::Real} ; - Dataset ds = db.select("SELECT 1,'hello', 1.1 as dbl;", types); - WHEN ("having a dataset with different field count") - { - auto ds1 = db.select("SELECT 1,'hello';", - {Type::Int, Type::Text}); - THEN ("swapping rows will work !") - { // this might be wanted or not - CHECK_NOTHROW(ds.at(0).swap(ds1.at(0))); - CHECK_NOTHROW(std::swap(ds.at(0),ds1.at(0))); - CHECK_NOTHROW(sl3::swap(ds.at(0),ds1.at(0))); - } - } - - WHEN ("having a dataset with different field count") - { - auto ds1 = db.select("SELECT 1,'hello';", - {Type::Int, Type::Text}); - THEN ("assign a different rows will not work !") - { - CHECK_THROWS_AS(ds.at(0) = ds1.at(0), ErrTypeMisMatch); - CHECK_THROWS_AS(ds.at(0) = std::move(ds1.at(0)), ErrTypeMisMatch); - } - } - - WHEN ("having a dataset with same field count but incampatible types") - { - auto ds1 = db.select("SELECT 1,'hello', 'hello';", - {Type::Int, Type::Text, Type::Text}); + using namespace sl3; + GIVEN ("a database and a typed dataset with knowen content") + { + Database db{":memory:"}; + Types types = {Type::Int, Type::Text, Type::Real}; + Dataset ds = db.select ("SELECT 1,'hello', 1.1 as dbl;", types); + WHEN ("having a dataset with different field count") + { + auto ds1 = db.select ("SELECT 1,'hello';", {Type::Int, Type::Text}); + THEN ("swapping rows will work !") + { // this might be wanted or not + CHECK_NOTHROW (ds.at (0).swap (ds1.at (0))); + CHECK_NOTHROW (std::swap (ds.at (0), ds1.at (0))); + CHECK_NOTHROW (sl3::swap (ds.at (0), ds1.at (0))); + } + } - THEN ("assign a row will not work !") - { - CHECK_THROWS_AS(ds.at(0) = ds1.at(0), ErrTypeMisMatch); - CHECK_THROWS_AS(ds.at(0) = std::move(ds1.at(0)), ErrTypeMisMatch); - } + WHEN ("having a dataset with different field count") + { + auto ds1 = db.select ("SELECT 1,'hello';", {Type::Int, Type::Text}); + THEN ("assign a different rows will not work !") + { + CHECK_THROWS_AS (ds.at (0) = ds1.at (0), ErrTypeMisMatch); + CHECK_THROWS_AS (ds.at (0) = std::move (ds1.at (0)), ErrTypeMisMatch); + } + } - } + WHEN ("having a dataset with same field count but incampatible types") + { + auto ds1 = db.select ("SELECT 1,'hello', 'hello';", + {Type::Int, Type::Text, Type::Text}); - WHEN ("having an ohter , compatible dataset") - { - auto ds1 = db.select("SELECT 2,'world', 3.3;", - {Type::Int, Type::Text, Type::Real}); - auto ds2 = db.select("SELECT 3,'world', 3.3;", - {Type::Int, Type::Text, Type::Real}); - THEN ("assign a row will work !") - { - CHECK_NOTHROW(ds.at(0) = ds1.at(0)); - CHECK (ds[0][0].getInt() == 2) ; - CHECK_NOTHROW(ds.at(0) = std::move(ds2.at(0))); - CHECK (ds[0][0].getInt() == 3) ; - } + THEN ("assign a row will not work !") + { + CHECK_THROWS_AS (ds.at (0) = ds1.at (0), ErrTypeMisMatch); + CHECK_THROWS_AS (ds.at (0) = std::move (ds1.at (0)), ErrTypeMisMatch); + } + } - } - } + WHEN ("having an ohter , compatible dataset") + { + auto ds1 = db.select ("SELECT 2,'world', 3.3;", + {Type::Int, Type::Text, Type::Real}); + auto ds2 = db.select ("SELECT 3,'world', 3.3;", + {Type::Int, Type::Text, Type::Real}); + THEN ("assign a row will work !") + { + CHECK_NOTHROW (ds.at (0) = ds1.at (0)); + CHECK (ds[0][0].getInt () == 2); + CHECK_NOTHROW (ds.at (0) = std::move (ds2.at (0))); + CHECK (ds[0][0].getInt () == 3); + } + } + } } - - - diff --git a/tests/dbvalue/dbvaluetest.cpp b/tests/dbvalue/dbvaluetest.cpp index 55bb261..1313137 100644 --- a/tests/dbvalue/dbvaluetest.cpp +++ b/tests/dbvalue/dbvaluetest.cpp @@ -222,9 +222,9 @@ SCENARIO ("using value, basics") { std::vector otherTypes; std::copy_if (std::begin (types), - std::end (types), - std::back_inserter (otherTypes), - [&dbval] (Type t) { return t != dbval.dbtype (); }); + std::end (types), + std::back_inserter (otherTypes), + [&dbval] (Type t) { return t != dbval.dbtype (); }); THEN ("getting defaults of other types throws") { diff --git a/tests/rowcallback/rowcallbacktest.cpp b/tests/rowcallback/rowcallbacktest.cpp index 3bdfa1c..7085af2 100644 --- a/tests/rowcallback/rowcallbacktest.cpp +++ b/tests/rowcallback/rowcallbacktest.cpp @@ -5,116 +5,104 @@ #include -SCENARIO("Check RowCallback and Callback return values") +SCENARIO ("Check RowCallback and Callback return values") { - sl3::Database db{":memory:"}; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (2) ;" - "INSERT INTO tbltest VALUES (3) ;") - ); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (2) ;" + "INSERT INTO tbltest VALUES (3) ;")); - GIVEN("a RowCallback that counts all calls") + GIVEN ("a RowCallback that counts all calls") { struct CB : sl3::RowCallback { size_t counter{0}; - bool onRow(sl3::Columns /*unusedForNow*/) + bool + onRow (sl3::Columns /*unusedForNow*/) { ++counter; - return true ; + return true; } }; CB cb; - WHEN("executed for the test data") + WHEN ("executed for the test data") { - db.execute ("SELECT * FROM tbltest;", cb) ; - THEN("it was called for each of the 3 records") + db.execute ("SELECT * FROM tbltest;", cb); + THEN ("it was called for each of the 3 records") { - CHECK (cb.counter == 3) ; + CHECK (cb.counter == 3); } } } - GIVEN("a RowCallback that returns false after the second call") + GIVEN ("a RowCallback that returns false after the second call") { - struct : sl3::RowCallback + struct : sl3::RowCallback { size_t counter{0}; - bool onRow(sl3::Columns /*unusedForNow*/) + bool + onRow (sl3::Columns /*unusedForNow*/) { ++counter; - if(counter == 2) return false ; - return true ; + if (counter == 2) + return false; + return true; } } cb; - WHEN("executed for the test data") + WHEN ("executed for the test data") { - db.execute ("SELECT * FROM tbltest;", cb) ; - THEN("it was called 2 time") - { - CHECK (cb.counter == 2) ; - } + db.execute ("SELECT * FROM tbltest;", cb); + THEN ("it was called 2 time") { CHECK (cb.counter == 2); } } } - GIVEN("a Callback that counts all calls") + GIVEN ("a Callback that counts all calls") { - size_t counter {0} ; - auto cb = [&counter](sl3::Columns /*ignoreForNow*/) ->bool - { + size_t counter{0}; + auto cb = [&counter] (sl3::Columns /*ignoreForNow*/) -> bool { ++counter; - return true ; + return true; }; - WHEN("executed for the test data") + WHEN ("executed for the test data") { - db.execute ("SELECT * FROM tbltest;", cb) ; - THEN("it was called for each of the 3 records") + db.execute ("SELECT * FROM tbltest;", cb); + THEN ("it was called for each of the 3 records") { - CHECK (counter == 3) ; + CHECK (counter == 3); } } } - GIVEN("a Callback that returns false after the second call") + GIVEN ("a Callback that returns false after the second call") { - size_t counter {0} ; - auto cb = [&counter](sl3::Columns /*ignoreForNow*/) ->bool - { + size_t counter{0}; + auto cb = [&counter] (sl3::Columns /*ignoreForNow*/) -> bool { ++counter; - return counter == 2 ? false : true ; + return counter == 2 ? false : true; }; - WHEN("executed for the test data") + WHEN ("executed for the test data") { - db.execute ("SELECT * FROM tbltest;", cb) ; - THEN("it was called 2 time") - { - CHECK (counter == 2) ; - } + db.execute ("SELECT * FROM tbltest;", cb); + THEN ("it was called 2 time") { CHECK (counter == 2); } } } } - -SCENARIO("Check RowCallback start and end calls") +SCENARIO ("Check RowCallback start and end calls") { - sl3::Database db{":memory:"}; - REQUIRE_NOTHROW( - db.execute ("CREATE TABLE tbltest (f INTEGER);" - "INSERT INTO tbltest VALUES (1) ;" - "INSERT INTO tbltest VALUES (2) ;" - "INSERT INTO tbltest VALUES (3) ;") - ); + REQUIRE_NOTHROW (db.execute ("CREATE TABLE tbltest (f INTEGER);" + "INSERT INTO tbltest VALUES (1) ;" + "INSERT INTO tbltest VALUES (2) ;" + "INSERT INTO tbltest VALUES (3) ;")); - GIVEN("a RowCallback that counts all calls and fantasi on start/end counts") + GIVEN ("a RowCallback that counts all calls and fantasi on start/end counts") { struct CB : sl3::RowCallback { @@ -122,94 +110,96 @@ SCENARIO("Check RowCallback start and end calls") size_t onstart{10000}; size_t onend{10000}; - void onStart () + void + onStart () { - onstart = counter ; + onstart = counter; } - void onEnd () + void + onEnd () { - onend = counter ; + onend = counter; } - bool onRow(sl3::Columns /*unusedForNow*/) + bool + onRow (sl3::Columns /*unusedForNow*/) { ++counter; - return true ; + return true; } }; CB cb; - WHEN("executed for the test data") + WHEN ("executed for the test data") { - db.execute ("SELECT * FROM tbltest;", cb) ; - THEN("on start/end counts have logical values") + db.execute ("SELECT * FROM tbltest;", cb); + THEN ("on start/end counts have logical values") { - CHECK (cb.counter == 3) ; - CHECK (cb.onstart == 0) ; - CHECK (cb.onstart == 0) ; - CHECK (cb.onend == 3) ; + CHECK (cb.counter == 3); + CHECK (cb.onstart == 0); + CHECK (cb.onstart == 0); + CHECK (cb.onend == 3); } } } } - -SCENARIO("testing column names and index properties") +SCENARIO ("testing column names and index properties") { - sl3::Database db{":memory:"}; - GIVEN("a statement that returns 2 columns with known names") + GIVEN ("a statement that returns 2 columns with known names") { auto sql = "SELECT 1 AS first, 'hello' AS second;"; - REQUIRE_NOTHROW (db.execute(sql)) ; + REQUIRE_NOTHROW (db.execute (sql)); WHEN ("accessing columns via index") { - THEN("valid indexes will not throw") + THEN ("valid indexes will not throw") { - db.execute(sql, [](sl3::Columns cols){ - REQUIRE(cols.count () == 2) ; - CHECK_NOTHROW((void)cols.getValue (0)) ; - CHECK_NOTHROW((void)cols.getValue (0, sl3::Type::Int)) ; - CHECK_NOTHROW((void)cols.getValue (1)) ; - CHECK_NOTHROW((void)cols.getValue (1, sl3::Type::Text)) ; + db.execute (sql, [] (sl3::Columns cols) { + REQUIRE (cols.count () == 2); + CHECK_NOTHROW ((void)cols.getValue (0)); + CHECK_NOTHROW ((void)cols.getValue (0, sl3::Type::Int)); + CHECK_NOTHROW ((void)cols.getValue (1)); + CHECK_NOTHROW ((void)cols.getValue (1, sl3::Type::Text)); return false; }); } - THEN("invalid indexes will throw throw out of range") + THEN ("invalid indexes will throw throw out of range") { - db.execute(sql, [](sl3::Columns cols){ - REQUIRE(cols.count () == 2) ; - CHECK_THROWS_AS(cols.getValue (2), sl3::ErrOutOfRange) ; - CHECK_THROWS_AS(cols.getValue (2, sl3::Type::Variant), sl3::ErrOutOfRange); + db.execute (sql, [] (sl3::Columns cols) { + REQUIRE (cols.count () == 2); + CHECK_THROWS_AS (cols.getValue (2), sl3::ErrOutOfRange); + CHECK_THROWS_AS (cols.getValue (2, sl3::Type::Variant), + sl3::ErrOutOfRange); return false; }); } } - WHEN("accessing the names via index") + WHEN ("accessing the names via index") { THEN ("valid indexes return expected results") { - db.execute(sql, [](sl3::Columns cols){ - REQUIRE(cols.count () == 2) ; - CHECK(cols.getName (0) == "first") ; - CHECK(cols.getName (1) == "second") ; - auto names = cols.getNames(); - CHECK(cols.getName (0) == names.at (0)) ; - CHECK(cols.getName (1) == names.at (1)) ; + db.execute (sql, [] (sl3::Columns cols) { + REQUIRE (cols.count () == 2); + CHECK (cols.getName (0) == "first"); + CHECK (cols.getName (1) == "second"); + auto names = cols.getNames (); + CHECK (cols.getName (0) == names.at (0)); + CHECK (cols.getName (1) == names.at (1)); return false; }); } THEN ("invalid indexes throw out of range") { - db.execute(sql, [](sl3::Columns cols){ - REQUIRE(cols.count () == 2) ; - CHECK_THROWS_AS(cols.getName (2), sl3::ErrOutOfRange) ; + db.execute (sql, [] (sl3::Columns cols) { + REQUIRE (cols.count () == 2); + CHECK_THROWS_AS (cols.getName (2), sl3::ErrOutOfRange); return false; }); } @@ -217,107 +207,101 @@ SCENARIO("testing column names and index properties") } } - - SCENARIO ("all index accesses throw out of range for an invalud index") { - - GIVEN("a record with some fields") + GIVEN ("a record with some fields") { sl3::Database db{":memory:"}; - auto sql = "SELECT 1 as int, " - " 2.3 as real, " - " 'hello' as text, " - " x'1F' as blob, " - " NULL as noval; "; - - REQUIRE_NOTHROW(db.execute(sql)) ; - WHEN("access any property with an invalid index") + auto sql = "SELECT 1 as int, " + " 2.3 as real, " + " 'hello' as text, " + " x'1F' as blob, " + " NULL as noval; "; + + REQUIRE_NOTHROW (db.execute (sql)); + WHEN ("access any property with an invalid index") { THEN ("an ErrOutOfRange exception is thrown") { - using namespace sl3 ; - db.execute(sql, [](Columns cols){ - auto badIdx = cols.count ();// or bigge - - CHECK_THROWS_AS((void)cols.getType (badIdx), ErrOutOfRange); - CHECK_THROWS_AS((void)cols.getValue (badIdx), ErrOutOfRange) ; - CHECK_THROWS_AS((void)cols.getValue (badIdx, Type::Variant), ErrOutOfRange); - CHECK_THROWS_AS((void)cols.getName (badIdx), ErrOutOfRange) ; - CHECK_THROWS_AS((void)cols.getSize (badIdx), ErrOutOfRange); - CHECK_THROWS_AS((void)cols.getInt (badIdx), ErrOutOfRange); - CHECK_THROWS_AS((void)cols.getInt64 (badIdx), ErrOutOfRange); - CHECK_THROWS_AS((void)cols.getReal (badIdx), ErrOutOfRange); - CHECK_THROWS_AS((void)cols.getText (badIdx), ErrOutOfRange); - CHECK_THROWS_AS((void)cols.getBlob (badIdx), ErrOutOfRange); + using namespace sl3; + db.execute (sql, [] (Columns cols) { + auto badIdx = cols.count (); // or bigge + + CHECK_THROWS_AS ((void)cols.getType (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getValue (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getValue (badIdx, Type::Variant), + ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getName (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getSize (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getInt (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getInt64 (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getReal (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getText (badIdx), ErrOutOfRange); + CHECK_THROWS_AS ((void)cols.getBlob (badIdx), ErrOutOfRange); return false; }); } - } } } - - - -SCENARIO("getting automatic type info") +SCENARIO ("getting automatic type info") { - GIVEN("a test db") + GIVEN ("a test db") { sl3::Database db{":memory:"}; WHEN ("selecting an integer value") { - auto sql = "SELECT 1 ;" ; + auto sql = "SELECT 1 ;"; THEN ("columns will auto detect int") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getType (0) == sl3::Type::Int) ; + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getType (0) == sl3::Type::Int); return false; }); } } WHEN ("selecting a real value") { - auto sql = "SELECT 1.23 ;" ; + auto sql = "SELECT 1.23 ;"; THEN ("columns will auto detect real") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getType (0) == sl3::Type::Real) ; + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getType (0) == sl3::Type::Real); return false; }); } } WHEN ("selecting a text value") { - auto sql = "SELECT 'hello' ;" ; + auto sql = "SELECT 'hello' ;"; THEN ("columns will auto detect text") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getType (0) == sl3::Type::Text) ; + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getType (0) == sl3::Type::Text); return false; }); } } WHEN ("selecting a blob value") { - auto sql = "SELECT x'12345678ABCDEF';" ; + auto sql = "SELECT x'12345678ABCDEF';"; THEN ("columns will auto detect blob") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getType (0) == sl3::Type::Blob) ; + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getType (0) == sl3::Type::Blob); return false; }); } } WHEN ("selecting a NULL") { - auto sql = "SELECT NULL ;" ; + auto sql = "SELECT NULL ;"; THEN ("columns will auto detect NULL") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getType (0) == sl3::Type::Null) ; + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getType (0) == sl3::Type::Null); return false; }); } @@ -325,64 +309,60 @@ SCENARIO("getting automatic type info") AND_WHEN ("accessing the type out of index") { - auto sql = "SELECT NULL ;" ; + auto sql = "SELECT NULL ;"; THEN ("an out of range exception is thrown") { - db.execute(sql, [](sl3::Columns cols){ - CHECK_THROWS_AS(cols.getType (1), sl3::ErrOutOfRange) ; + db.execute (sql, [] (sl3::Columns cols) { + CHECK_THROWS_AS (cols.getType (1), sl3::ErrOutOfRange); return false; }); } } - } - } -SCENARIO("getting DbValues from columns") +SCENARIO ("getting DbValues from columns") { - GIVEN("a record with known data and known types") + GIVEN ("a record with known data and known types") { sl3::Database db{":memory:"}; - auto sql = "SELECT 1 as int, " - " 2.3 as real, " - " 'hello' as text, " - " x'1F' as blob, " - " NULL as noval; "; + auto sql = "SELECT 1 as int, " + " 2.3 as real, " + " 'hello' as text, " + " x'1F' as blob, " + " NULL as noval; "; - WHEN("verify the test data") + WHEN ("verify the test data") { THEN ("fields and types are as expected") { - REQUIRE_NOTHROW(db.execute(sql)) ; - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getType (0) == sl3::Type::Int) ; - CHECK(cols.getType (1) == sl3::Type::Real) ; - CHECK(cols.getType (2) == sl3::Type::Text) ; - CHECK(cols.getType (3) == sl3::Type::Blob) ; - CHECK(cols.getType (4) == sl3::Type::Null) ; + REQUIRE_NOTHROW (db.execute (sql)); + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getType (0) == sl3::Type::Int); + CHECK (cols.getType (1) == sl3::Type::Real); + CHECK (cols.getType (2) == sl3::Type::Text); + CHECK (cols.getType (3) == sl3::Type::Blob); + CHECK (cols.getType (4) == sl3::Type::Null); return false; }); } - } - WHEN ("accessing values as they are") { THEN ("value type properties are set correct") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getValue (0).dbtype () == sl3::Type::Variant) ; - CHECK(cols.getValue (0).type () == sl3::Type::Int) ; - CHECK(cols.getValue (1).dbtype () == sl3::Type::Variant) ; - CHECK(cols.getValue (1).type () == sl3::Type::Real) ; - CHECK(cols.getValue (2).dbtype () == sl3::Type::Variant) ; - CHECK(cols.getValue (2).type () == sl3::Type::Text) ; - CHECK(cols.getValue (3).dbtype () == sl3::Type::Variant) ; - CHECK(cols.getValue (3).type () == sl3::Type::Blob) ; - CHECK(cols.getValue (4).dbtype () == sl3::Type::Variant) ; - CHECK(cols.getValue (4).type () == sl3::Type::Null) ; + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getValue (0).dbtype () == sl3::Type::Variant); + CHECK (cols.getValue (0).type () == sl3::Type::Int); + CHECK (cols.getValue (1).dbtype () == sl3::Type::Variant); + CHECK (cols.getValue (1).type () == sl3::Type::Real); + CHECK (cols.getValue (2).dbtype () == sl3::Type::Variant); + CHECK (cols.getValue (2).type () == sl3::Type::Text); + CHECK (cols.getValue (3).dbtype () == sl3::Type::Variant); + CHECK (cols.getValue (3).type () == sl3::Type::Blob); + CHECK (cols.getValue (4).dbtype () == sl3::Type::Variant); + CHECK (cols.getValue (4).type () == sl3::Type::Null); return true; }); } @@ -392,21 +372,22 @@ SCENARIO("getting DbValues from columns") { THEN ("requesting correct types will work") { - using namespace sl3 ; - db.execute(sql, [](sl3::Columns cols){ - auto t = {Type::Int, Type::Real, Type::Text, Type::Blob, Type::Variant}; - sl3::Types expectedTypes{t} ; - REQUIRE (expectedTypes.size () == cols.count ()) ; + using namespace sl3; + db.execute (sql, [] (sl3::Columns cols) { + auto t + = {Type::Int, Type::Real, Type::Text, Type::Blob, Type::Variant}; + sl3::Types expectedTypes{t}; + REQUIRE (expectedTypes.size () == cols.count ()); for (int i = 0; i < cols.count (); ++i) - { - auto type = expectedTypes[size_t(i)]; - auto val = cols.getValue (i, type) ; - CHECK(val.dbtype () == type) ; - if (i == cols.count () -1 ) - CHECK(val.type () == Type::Null) ; - else - CHECK(val.type () == type) ; - } + { + auto type = expectedTypes[size_t (i)]; + auto val = cols.getValue (i, type); + CHECK (val.dbtype () == type); + if (i == cols.count () - 1) + CHECK (val.type () == Type::Null); + else + CHECK (val.type () == type); + } return true; }); @@ -414,119 +395,121 @@ SCENARIO("getting DbValues from columns") THEN ("requesting incorrect types will always throw") { - using namespace sl3 ; - db.execute(sql, [](sl3::Columns cols){ - CHECK_THROWS_AS((void)cols.getValue (0, Type::Real), ErrTypeMisMatch); - CHECK_THROWS_AS((void)cols.getValue (1, Type::Int), ErrTypeMisMatch); - CHECK_THROWS_AS((void)cols.getValue (2, Type::Int), ErrTypeMisMatch); - CHECK_THROWS_AS((void)cols.getValue (3, Type::Int), ErrTypeMisMatch); + using namespace sl3; + db.execute (sql, [] (sl3::Columns cols) { + CHECK_THROWS_AS ((void)cols.getValue (0, Type::Real), + ErrTypeMisMatch); + CHECK_THROWS_AS ((void)cols.getValue (1, Type::Int), + ErrTypeMisMatch); + CHECK_THROWS_AS ((void)cols.getValue (2, Type::Int), + ErrTypeMisMatch); + CHECK_THROWS_AS ((void)cols.getValue (3, Type::Int), + ErrTypeMisMatch); return true; }); } - } - } } - - -SCENARIO("getting rows from columns") +SCENARIO ("getting rows from columns") { - GIVEN("a record with known data and known types") + GIVEN ("a record with known data and known types") { sl3::Database db{":memory:"}; - auto sql = "SELECT 1 as int, " - " 2.3 as real, " - " 'hello' as text, " - " x'1F' as blob, " - " NULL as noval; "; + auto sql = "SELECT 1 as int, " + " 2.3 as real, " + " 'hello' as text, " + " x'1F' as blob, " + " NULL as noval; "; - WHEN("requesting just a row") + WHEN ("requesting just a row") { THEN ("fields and types are as expected") { - REQUIRE_NOTHROW(db.execute(sql)) ; - db.execute(sql, [](sl3::Columns cols){ + REQUIRE_NOTHROW (db.execute (sql)); + db.execute (sql, [] (sl3::Columns cols) { auto row = cols.getRow (); - REQUIRE (row.size () == cols.count ()) ; - CHECK(row.at (0).dbtype () == sl3::Type::Variant) ; - CHECK(row.at (0).type () == sl3::Type::Int) ; - CHECK(row.at (1).dbtype () == sl3::Type::Variant) ; - CHECK(row.at (1).type () == sl3::Type::Real) ; - CHECK(row.at (2).dbtype () == sl3::Type::Variant) ; - CHECK(row.at (2).type () == sl3::Type::Text) ; - CHECK(row.at (3).dbtype () == sl3::Type::Variant) ; - CHECK(row.at (3).type () == sl3::Type::Blob) ; - CHECK(row.at (4).dbtype () == sl3::Type::Variant) ; - CHECK(row.at (4).type () == sl3::Type::Null) ; + REQUIRE (row.size () == cols.count ()); + CHECK (row.at (0).dbtype () == sl3::Type::Variant); + CHECK (row.at (0).type () == sl3::Type::Int); + CHECK (row.at (1).dbtype () == sl3::Type::Variant); + CHECK (row.at (1).type () == sl3::Type::Real); + CHECK (row.at (2).dbtype () == sl3::Type::Variant); + CHECK (row.at (2).type () == sl3::Type::Text); + CHECK (row.at (3).dbtype () == sl3::Type::Variant); + CHECK (row.at (3).type () == sl3::Type::Blob); + CHECK (row.at (4).dbtype () == sl3::Type::Variant); + CHECK (row.at (4).type () == sl3::Type::Null); return false; }); } } - - WHEN("request a row with wrong count of types") + WHEN ("request a row with wrong count of types") { THEN ("exceptions are the consequenze") { - REQUIRE_NOTHROW(db.execute(sql)) ; - db.execute(sql, [](sl3::Columns cols){ - using namespace sl3 ; - auto toless = {Type::Variant,Type::Variant,Type::Variant,Type::Variant}; - auto tomany = {Type::Variant,Type::Variant,Type::Variant,Type::Variant,Type::Variant,Type::Variant}; - CHECK_THROWS_AS (cols.getRow(toless), ErrTypeMisMatch) ; - CHECK_THROWS_AS (cols.getRow(tomany), ErrTypeMisMatch) ; + REQUIRE_NOTHROW (db.execute (sql)); + db.execute (sql, [] (sl3::Columns cols) { + using namespace sl3; + auto toless + = {Type::Variant, Type::Variant, Type::Variant, Type::Variant}; + auto tomany = {Type::Variant, + Type::Variant, + Type::Variant, + Type::Variant, + Type::Variant, + Type::Variant}; + CHECK_THROWS_AS (cols.getRow (toless), ErrTypeMisMatch); + CHECK_THROWS_AS (cols.getRow (tomany), ErrTypeMisMatch); return false; }); } } - WHEN("request a row with spezific correct types") + WHEN ("request a row with spezific correct types") { THEN ("a row with those types is retunred") { - REQUIRE_NOTHROW(db.execute(sql)) ; - db.execute(sql, [](sl3::Columns cols){ - using namespace sl3 ; - auto types = {Type::Int,Type::Real,Type::Text,Type::Blob,Type::Variant}; + REQUIRE_NOTHROW (db.execute (sql)); + db.execute (sql, [] (sl3::Columns cols) { + using namespace sl3; + auto types + = {Type::Int, Type::Real, Type::Text, Type::Blob, Type::Variant}; auto row = cols.getRow (types); - CHECK(row.at (0).dbtype () == sl3::Type::Int) ; - CHECK(row.at (0).type () == sl3::Type::Int) ; - CHECK(row.at (1).dbtype () == sl3::Type::Real) ; - CHECK(row.at (1).type () == sl3::Type::Real) ; - CHECK(row.at (2).dbtype () == sl3::Type::Text) ; - CHECK(row.at (2).type () == sl3::Type::Text) ; - CHECK(row.at (3).dbtype () == sl3::Type::Blob) ; - CHECK(row.at (3).type () == sl3::Type::Blob) ; - CHECK(row.at (4).dbtype () == sl3::Type::Variant) ; - CHECK(row.at (4).type () == sl3::Type::Null) ; + CHECK (row.at (0).dbtype () == sl3::Type::Int); + CHECK (row.at (0).type () == sl3::Type::Int); + CHECK (row.at (1).dbtype () == sl3::Type::Real); + CHECK (row.at (1).type () == sl3::Type::Real); + CHECK (row.at (2).dbtype () == sl3::Type::Text); + CHECK (row.at (2).type () == sl3::Type::Text); + CHECK (row.at (3).dbtype () == sl3::Type::Blob); + CHECK (row.at (3).type () == sl3::Type::Blob); + CHECK (row.at (4).dbtype () == sl3::Type::Variant); + CHECK (row.at (4).type () == sl3::Type::Null); return false; }); } } - } } - - SCENARIO ("for coverage") { - - GIVEN("a record with known data and known types") + GIVEN ("a record with known data and known types") { sl3::Database db{":memory:"}; - auto sql = "SELECT 'b' as byte, NULL as noval; "; - REQUIRE_NOTHROW (db.execute(sql)); + auto sql = "SELECT 'b' as byte, NULL as noval; "; + REQUIRE_NOTHROW (db.execute (sql)); WHEN ("asking for the used storegae space") { THEN ("some int will use some space, and null non space") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getSize (0) == 1); - CHECK(cols.getSize (1) == 0); + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getSize (0) == 1); + CHECK (cols.getSize (1) == 0); return true; }); } @@ -535,8 +518,8 @@ SCENARIO ("for coverage") { THEN ("sqlite will convert it to 0") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.getInt (1) == 0); + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.getInt (1) == 0); return true; }); } @@ -546,14 +529,11 @@ SCENARIO ("for coverage") { THEN ("this is possible") { - db.execute(sql, [](sl3::Columns cols){ - CHECK(cols.get_stmt () != nullptr); + db.execute (sql, [] (sl3::Columns cols) { + CHECK (cols.get_stmt () != nullptr); return true; }); } } } } - - - diff --git a/tests/sample/main.cpp b/tests/sample/main.cpp index d768970..ec69895 100644 --- a/tests/sample/main.cpp +++ b/tests/sample/main.cpp @@ -3,28 +3,24 @@ #include #include -int main() +int +main () { using namespace sl3; // define a db - Database db(":memory:"); + Database db (":memory:"); // run commands against the db - db.execute("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);" - "INSERT INTO tbl (f1, f2, f3) VALUES (1, 'one', 1.1);" - "INSERT INTO tbl (f1, f2, f3) VALUES (2, 'two', 2.2)" - ); - - - db.execute ("SELECT f1, f2 FROM tbl;", [](Columns cols) - { - assert (cols.count() == 2) ; - assert (cols.getType(0) == Type::Int) ; - assert (cols.getType(1) == Type::Text) ; - std::cout << std::to_string(cols.getInt(0)) - << "_" << cols.getText(1) - << std::endl; - return true ; - }); + db.execute ("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);" + "INSERT INTO tbl (f1, f2, f3) VALUES (1, 'one', 1.1);" + "INSERT INTO tbl (f1, f2, f3) VALUES (2, 'two', 2.2)"); + db.execute ("SELECT f1, f2 FROM tbl;", [] (Columns cols) { + assert (cols.count () == 2); + assert (cols.getType (0) == Type::Int); + assert (cols.getType (1) == Type::Text); + std::cout << std::to_string (cols.getInt (0)) << "_" << cols.getText (1) + << std::endl; + return true; + }); } diff --git a/tests/sample/main1.cpp b/tests/sample/main1.cpp index 492baac..cab0d4c 100644 --- a/tests/sample/main1.cpp +++ b/tests/sample/main1.cpp @@ -2,38 +2,41 @@ #include #include -int main() +int +main () { using namespace sl3; // define a db - Database db(":memory:"); + Database db (":memory:"); // run commands against the db - db.execute("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); + db.execute ("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); // create a command with parameters - auto cmd = db.prepare("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);"); + auto cmd = db.prepare ("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);"); - //add some data + // add some data cmd.execute (parameters (1, "one", 1.1)); cmd.execute (parameters (2, "two", 2.1)); // access the data - Dataset ds = db.select("SELECT * FROM tbl;"); + Dataset ds = db.select ("SELECT * FROM tbl;"); // Dataset is a container - assert(ds.size()==2); + assert (ds.size () == 2); // Dataset row is a container - auto row = ds[0] ; - assert(row.size()==3); - assert ( row[0].type() == Type::Int ) ; - assert ( row[1].type() == Type::Text ) ; - assert ( row[2].type() == Type::Real ) ; + auto row = ds[0]; + assert (row.size () == 3); + assert (row[0].type () == Type::Int); + assert (row[1].type () == Type::Text); + assert (row[2].type () == Type::Real); // of course there is also iterator access - for(auto&& row :ds) { - for (auto&& field : row) { - std::cout << field << " " ; - } + for (auto&& row : ds) + { + for (auto&& field : row) + { + std::cout << field << " "; + } std::cout << std::endl; - } + } } diff --git a/tests/sample/main2.cpp b/tests/sample/main2.cpp index a4d6e0a..dede7e6 100644 --- a/tests/sample/main2.cpp +++ b/tests/sample/main2.cpp @@ -3,29 +3,28 @@ #include -int main() +int +main () { using namespace sl3; // define a db - Database db(":memory:"); + Database db (":memory:"); // run commands against the db - db.execute("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); + db.execute ("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); // create a command with parameters - auto cmd = db.prepare("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);", - DbValues({Type::Int,Type::Text, Type::Real})) ; + auto cmd = db.prepare ("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);", + DbValues ({Type::Int, Type::Text, Type::Real})); - //this will work, + // this will work, cmd.execute (parameters (1, "one", 1.1)); // this will throw since "2" is a wrong type try - { + { cmd.execute (parameters ("2", "two", 2.2)); - } + } catch (const Error& e) - { + { std::cout << e << std::endl; - } - - + } } diff --git a/tests/sample/main3.cpp b/tests/sample/main3.cpp index 1754f60..15ef9a7 100644 --- a/tests/sample/main3.cpp +++ b/tests/sample/main3.cpp @@ -3,41 +3,43 @@ #include -int main() +int +main () { using namespace sl3; - // define a db - Database db(":memory:"); - // run commands against the db - db.execute("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); - // create a command with parameters - auto cmd = db.prepare("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);"); - // no types so we use variants - //so this will work - cmd.execute (parameters (1, "one", 1.1)); - //and this will also work - cmd.execute (parameters ("some text", "two", 2.1)); + // define a db + Database db (":memory:"); + // run commands against the db + db.execute ("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); + // create a command with parameters + auto cmd = db.prepare ("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);"); + // no types so we use variants + // so this will work + cmd.execute (parameters (1, "one", 1.1)); + // and this will also work + cmd.execute (parameters ("some text", "two", 2.1)); - // access the data - Dataset ds = db.select("SELECT * FROM tbl;"); + // access the data + Dataset ds = db.select ("SELECT * FROM tbl;"); - assert (ds.size () == 2); // 2 records - assert (ds[0].size() == 3); // 3 fields + assert (ds.size () == 2); // 2 records + assert (ds[0].size () == 3); // 3 fields - // first row first field is a integer, as inserted - assert (ds[0][0].dbtype() == Type::Variant) ; - assert (ds[0][0].type() == Type::Int) ; - // second row first field is text, as inserted - assert (ds[1][0].dbtype() == Type::Variant) ; - assert (ds[1][0].type() == Type::Text) ; + // first row first field is a integer, as inserted + assert (ds[0][0].dbtype () == Type::Variant); + assert (ds[0][0].type () == Type::Int); + // second row first field is text, as inserted + assert (ds[1][0].dbtype () == Type::Variant); + assert (ds[1][0].type () == Type::Text); - // of course we can work wit the values - for(const auto& row :ds) { - for (const auto& field : row) { - std::cout << typeName (field.dbtype()) << "/" - << typeName (field.type()) << ": " << field << ", "; - } - std::cout << std::endl; - } + // of course we can work wit the values + for (const auto& row : ds) + { + for (const auto& field : row) + { + std::cout << typeName (field.dbtype ()) << "/" + << typeName (field.type ()) << ": " << field << ", "; + } + std::cout << std::endl; + } } - diff --git a/tests/sample/main4.cpp b/tests/sample/main4.cpp index a3bc4a6..005e76f 100644 --- a/tests/sample/main4.cpp +++ b/tests/sample/main4.cpp @@ -1,25 +1,26 @@ #include #include #include -int main() +int +main () { using namespace sl3; DbValue val (Type::Variant); - assert (val.isNull ()) ; - assert (val.get ("foo") == "foo") ; // get with default never throws - val = 2 ; + assert (val.isNull ()); + assert (val.get ("foo") == "foo"); // get with default never throws + val = 2; std::cout << val << std::endl; // access the integer property try - { // not possible since this variant holds an integer value - val.getText() ; - } + { // not possible since this variant holds an integer value + val.getText (); + } catch (const Error& e) - { + { std::cout << e << std::endl; - } - assert (val.get ("foo") == "foo") ; // get with default never throws + } + assert (val.get ("foo") == "foo"); // get with default never throws val = "example"; // now it is possible to access the text property - std::cout << val.getText() << std::endl; + std::cout << val.getText () << std::endl; } diff --git a/tests/sample/main5.cpp b/tests/sample/main5.cpp index f5d70c4..b6fa253 100644 --- a/tests/sample/main5.cpp +++ b/tests/sample/main5.cpp @@ -1,31 +1,30 @@ #include #include -int main() +int +main () { using namespace sl3; - // define a db - Database db(":memory:"); - // run commands against the db - db.execute("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); - // create a command with parameters - auto cmd = db.prepare("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);"); - // no types so we use variants - // insert Int, Text, Real - cmd.execute(parameters(1, "one", 1.1)); - // insert Text, Text, Real - cmd.execute(parameters("some text", "two", 2.1)); + // define a db + Database db (":memory:"); + // run commands against the db + db.execute ("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);"); + // create a command with parameters + auto cmd = db.prepare ("INSERT INTO tbl (f1, f2, f3) VALUES (?,?,?);"); + // no types so we use variants + // insert Int, Text, Real + cmd.execute (parameters (1, "one", 1.1)); + // insert Text, Text, Real + cmd.execute (parameters ("some text", "two", 2.1)); - // this will throw since types in column0 are different - try + // this will throw since types in column0 are different + try { Dataset ds = db.select ("SELECT * FROM tbl;", - { Type::Int, Type::Text, Type::Real }); - + {Type::Int, Type::Text, Type::Real}); } catch (const Error& e) { - std::cout << e << std::endl ; + std::cout << e << std::endl; } - } diff --git a/tests/sample/main6.cpp b/tests/sample/main6.cpp index 6c7904c..4cbe24b 100644 --- a/tests/sample/main6.cpp +++ b/tests/sample/main6.cpp @@ -1,29 +1,25 @@ #include #include -#include #include -int main() +#include +int +main () { using namespace sl3; // define a db - Database db(":memory:"); + Database db (":memory:"); // run commands against the db - db.execute("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);" - "INSERT INTO tbl (f1, f2, f3) VALUES (1, 'one', 1.1);" - "INSERT INTO tbl (f1, f2, f3) VALUES (2, 'two', 2.2)" - ); - - - db.execute ("SELECT f1, f2 FROM tbl;", [](Columns cols) - { - assert (cols.count() == 2) ; - assert (cols.getType(0) == Type::Int) ; - assert (cols.getType(1) == Type::Text) ; - std::cout << std::to_string(cols.getInt(0)) - << "_" << cols.getText(1) - << std::endl; - return true ; - }); + db.execute ("CREATE TABLE tbl(f1 INTEGER, f2 TEXT, f3 REAL);" + "INSERT INTO tbl (f1, f2, f3) VALUES (1, 'one', 1.1);" + "INSERT INTO tbl (f1, f2, f3) VALUES (2, 'two', 2.2)"); + db.execute ("SELECT f1, f2 FROM tbl;", [] (Columns cols) { + assert (cols.count () == 2); + assert (cols.getType (0) == Type::Int); + assert (cols.getType (1) == Type::Text); + std::cout << std::to_string (cols.getInt (0)) << "_" << cols.getText (1) + << std::endl; + return true; + }); } diff --git a/tests/test_main.cpp b/tests/test_main.cpp index f096fb1..d021b50 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -1,36 +1,39 @@ -//#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN -//#include "doctest.h" - - +// #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +// #include "doctest.h" #define DOCTEST_CONFIG_IMPLEMENT #include "doctest/doctest.h" -int main(int argc, char** argv) { - // environment setup give me the code that should stand here +int +main (int argc, char** argv) +{ + // environment setup give me the code that should stand here doctest::Context context; // initialize - // defaults -// context.addFilter("test-case-exclude", "*math*"); // exclude test cases with "math" in their name - context.setOption("no-breaks", true); // don't break in the debugger when assertions fail + // defaults + // context.addFilter("test-case-exclude", "*math*"); // exclude test cases + // with "math" in their name + context.setOption ("no-breaks", + true); // don't break in the debugger when assertions fail - context.applyCommandLine(argc, argv); + context.applyCommandLine (argc, argv); - // overrides - context.setOption("abort-after", 5); // stop test execution after 5 failed assertions - // context.setOption("sort", "name"); // sort the test cases by their name + // overrides + context.setOption ("abort-after", + 5); // stop test execution after 5 failed assertions + // context.setOption("sort", "name"); // sort the test cases by their name - int res = context.run(); // run + int res = context.run (); // run - if(context.shouldExit()) // important - query flags (and --exit) rely on the user doing this - return res; // propagate the result of the tests + if (context.shouldExit ()) // important - query flags (and --exit) rely on + // the user doing this + return res; // propagate the result of the tests - int client_stuff_return_code = 0; - // your program - if the testing framework is integrated in your production code + int client_stuff_return_code = 0; + // your program - if the testing framework is integrated in your production + // code - return res + client_stuff_return_code; // the result from doctest is propagated here as well + return res + client_stuff_return_code; // the result from doctest is + // propagated here as well } - - - diff --git a/tests/testing.hpp b/tests/testing.hpp index de9e3a0..b0f01d2 100644 --- a/tests/testing.hpp +++ b/tests/testing.hpp @@ -1,6 +1,5 @@ #pragma once #include "doctest/doctest.h" -#include #include - +#include diff --git a/tests/typenames/typenamestest.cpp b/tests/typenames/typenamestest.cpp index d44195c..e0d9497 100644 --- a/tests/typenames/typenamestest.cpp +++ b/tests/typenames/typenamestest.cpp @@ -5,67 +5,65 @@ #include -SCENARIO("check typenames") +SCENARIO ("check typenames") { - using namespace sl3 ; + using namespace sl3; GIVEN ("the sqlite version libsl3 was comiled with") { - std::string libUsedVersion = sl3::sqliteCompiledVersion () ; + std::string libUsedVersion = sl3::sqliteCompiledVersion (); WHEN ("using the typename function") { - std::string appUsedVersion = sl3::sqliteRuntimeVersion () ; + std::string appUsedVersion = sl3::sqliteRuntimeVersion (); THEN ("the short enum name is returned") { - CHECK_EQ (typeName (Type::Int), "Int") ; - CHECK_EQ (typeName (Type::Real), "Real") ; - CHECK_EQ (typeName (Type::Text), "Text") ; - CHECK_EQ (typeName (Type::Blob), "Blob") ; - CHECK_EQ (typeName (Type::Null), "Null") ; - CHECK_EQ (typeName (Type::Variant), "Variant") ; + CHECK_EQ (typeName (Type::Int), "Int"); + CHECK_EQ (typeName (Type::Real), "Real"); + CHECK_EQ (typeName (Type::Text), "Text"); + CHECK_EQ (typeName (Type::Blob), "Blob"); + CHECK_EQ (typeName (Type::Null), "Null"); + CHECK_EQ (typeName (Type::Variant), "Variant"); } } WHEN ("getting typnmes in a stream") { - std::string appUsedVersion = sl3::sqliteRuntimeVersion () ; + std::string appUsedVersion = sl3::sqliteRuntimeVersion (); THEN ("the full enum name in in the stream") { - { - std::stringstream ss; - ss << Type::Int ; - CHECK_EQ (ss.str(), "Type::Int"); - } - { - std::stringstream ss; - ss << Type::Real ; - CHECK_EQ (ss.str(), "Type::Real"); - } - { - std::stringstream ss; - ss << Type::Text ; - CHECK_EQ (ss.str(), "Type::Text"); - } - { - std::stringstream ss; - ss << Type::Blob ; - CHECK_EQ (ss.str(), "Type::Blob"); - } - { - std::stringstream ss; - ss << Type::Variant ; - CHECK_EQ (ss.str(), "Type::Variant"); - } - { - std::stringstream ss; - ss << Type::Null ; - CHECK_EQ (ss.str(), "Type::Null"); - } + { + std::stringstream ss; + ss << Type::Int; + CHECK_EQ (ss.str (), "Type::Int"); + } + { + std::stringstream ss; + ss << Type::Real; + CHECK_EQ (ss.str (), "Type::Real"); + } + { + std::stringstream ss; + ss << Type::Text; + CHECK_EQ (ss.str (), "Type::Text"); + } + { + std::stringstream ss; + ss << Type::Blob; + CHECK_EQ (ss.str (), "Type::Blob"); + } + { + std::stringstream ss; + ss << Type::Variant; + CHECK_EQ (ss.str (), "Type::Variant"); + } + { + std::stringstream ss; + ss << Type::Null; + CHECK_EQ (ss.str (), "Type::Null"); + } } } - } } - diff --git a/tests/value/valuetest.cpp b/tests/value/valuetest.cpp index c5d6a59..c584963 100644 --- a/tests/value/valuetest.cpp +++ b/tests/value/valuetest.cpp @@ -1,168 +1,163 @@ #include "../testing.hpp" #include -#include -#include #include #include +#include +#include -using ValueList = std::vector ; +using ValueList = std::vector; template -//std::initializer_list // crash on gcc 5.3 * +// std::initializer_list // crash on gcc 5.3 * ValueList typed_values (VALS&&... vals) { return {sl3::Value{vals}...}; } - namespace sl3 { // if ever wanted, implement < and == operators yourself like this - inline - bool + inline bool operator== (const sl3::Value& a, const sl3::Value& b) noexcept { - return sl3::value_type_eq(a, b) ; + return sl3::value_type_eq (a, b); } - inline - bool + inline bool operator< (const sl3::Value& a, const sl3::Value& b) noexcept { - return sl3::value_type_lt(a, b) ; + return sl3::value_type_lt (a, b); } } // * interesting, does not happen on 6.3 // on 5.3 there is a Value::Value (const Value& other) noexcept // call where oter has some type - -SCENARIO("create, assign, copy and moveing values") +SCENARIO ("create, assign, copy and moveing values") { using namespace sl3; GIVEN ("some values of each supported type") { - using b = std::byte; - const int ival = 2 ; - const double dval = 2.3 ; - const std::string txt = "hello" ; - const Blob blob{b{'A'},b{'B'}}; + using b = std::byte; + const int ival = 2; + const double dval = 2.3; + const std::string txt = "hello"; + const Blob blob{b{'A'}, b{'B'}}; WHEN ("creating a values vector via the helper function") { - ValueList vals {typed_values(ival, dval,txt, blob)} ; + ValueList vals{typed_values (ival, dval, txt, blob)}; THEN ("the expected values with expected types have been created") { - CHECK(vals.at(0).getType() == Type::Int) ; - CHECK(vals.at(0).int64() == ival) ; - CHECK(vals.at(1).getType() == Type::Real) ; - CHECK(vals.at(1).real() == dval) ; - CHECK(vals.at(2).getType() == Type::Text) ; - CHECK(vals.at(2).text() == txt) ; - CHECK(vals.at(3).getType() == Type::Blob) ; - CHECK(vals.at(3).blob() == blob) ; - for (const auto& val : vals) - CHECK_FALSE(val.isNull()); - - AND_WHEN("adding a null value to the vector") - { - vals.emplace_back(Value{}) ; - - THEN ("the last element is a null value") - { - CHECK(vals.back().isNull()) ; - } - } + CHECK (vals.at (0).getType () == Type::Int); + CHECK (vals.at (0).int64 () == ival); + CHECK (vals.at (1).getType () == Type::Real); + CHECK (vals.at (1).real () == dval); + CHECK (vals.at (2).getType () == Type::Text); + CHECK (vals.at (2).text () == txt); + CHECK (vals.at (3).getType () == Type::Blob); + CHECK (vals.at (3).blob () == blob); + for (const auto& val : vals) + CHECK_FALSE (val.isNull ()); + + AND_WHEN ("adding a null value to the vector") + { + vals.emplace_back (Value{}); + + THEN ("the last element is a null value") + { + CHECK (vals.back ().isNull ()); + } + } } } WHEN ("creating values of each type") { - ValueList vals {typed_values(ival, dval,txt, blob)} ; - vals.emplace_back(Value{}) ; + ValueList vals{typed_values (ival, dval, txt, blob)}; + vals.emplace_back (Value{}); THEN ("every value can be copied to an other value") { - ValueList vals_m {typed_values(ival, dval,txt, blob)} ; - vals_m.emplace_back(Value{}) ; + ValueList vals_m{typed_values (ival, dval, txt, blob)}; + vals_m.emplace_back (Value{}); for (auto val : vals_m) { - CHECK_NOTHROW(val = vals.at(0)); - CHECK(val.getType() == Type::Int) ; - CHECK(val.int64() == ival) ; - CHECK_NOTHROW(val = vals.at(1)); - CHECK(val.getType() == Type::Real) ; - CHECK(val.real() == dval) ; - CHECK_NOTHROW(val = vals.at(2)); - CHECK(val.getType() == Type::Text) ; - CHECK(val.text() == txt) ; - CHECK_NOTHROW(val = vals.at(3)); - CHECK(val.getType() == Type::Blob) ; - CHECK(val.blob() == blob) ; - CHECK_NOTHROW(val = vals.at(4)); - CHECK(val.isNull()) ; + CHECK_NOTHROW (val = vals.at (0)); + CHECK (val.getType () == Type::Int); + CHECK (val.int64 () == ival); + CHECK_NOTHROW (val = vals.at (1)); + CHECK (val.getType () == Type::Real); + CHECK (val.real () == dval); + CHECK_NOTHROW (val = vals.at (2)); + CHECK (val.getType () == Type::Text); + CHECK (val.text () == txt); + CHECK_NOTHROW (val = vals.at (3)); + CHECK (val.getType () == Type::Blob); + CHECK (val.blob () == blob); + CHECK_NOTHROW (val = vals.at (4)); + CHECK (val.isNull ()); } } } WHEN ("creating values of each type") { - ValueList vals {typed_values(ival, dval,txt, blob)} ; - vals.emplace_back(Value{}) ; + ValueList vals{typed_values (ival, dval, txt, blob)}; + vals.emplace_back (Value{}); THEN ("every value can be move assinged to an other value") { for (auto val : vals) { - ValueList vals_m {typed_values(ival, dval,txt, blob)} ; - vals_m.emplace_back(Value{}) ; - - CHECK_NOTHROW(val = std::move(vals_m.at(0))); - CHECK(val.getType() == Type::Int) ; - CHECK(val.int64() == ival) ; - CHECK_NOTHROW(val = std::move(vals_m.at(1))); - CHECK(val.getType() == Type::Real) ; - CHECK(val.real() == dval) ; - CHECK_NOTHROW(val = std::move(vals_m.at(2))); - CHECK(val.getType() == Type::Text) ; - CHECK(val.text() == txt) ; - CHECK_NOTHROW(val = std::move(vals_m.at(3))); - CHECK(val.getType() == Type::Blob) ; - CHECK(val.blob() == blob) ; - CHECK_NOTHROW(val = std::move(vals_m.at(4))); - CHECK(val.isNull()) ; + ValueList vals_m{typed_values (ival, dval, txt, blob)}; + vals_m.emplace_back (Value{}); + + CHECK_NOTHROW (val = std::move (vals_m.at (0))); + CHECK (val.getType () == Type::Int); + CHECK (val.int64 () == ival); + CHECK_NOTHROW (val = std::move (vals_m.at (1))); + CHECK (val.getType () == Type::Real); + CHECK (val.real () == dval); + CHECK_NOTHROW (val = std::move (vals_m.at (2))); + CHECK (val.getType () == Type::Text); + CHECK (val.text () == txt); + CHECK_NOTHROW (val = std::move (vals_m.at (3))); + CHECK (val.getType () == Type::Blob); + CHECK (val.blob () == blob); + CHECK_NOTHROW (val = std::move (vals_m.at (4))); + CHECK (val.isNull ()); } } } WHEN ("creating values of each type") { - ValueList vals {typed_values(int64_t{ival}, dval,txt, blob)} ; - vals.emplace_back(Value{}) ; + ValueList vals{typed_values (int64_t{ival}, dval, txt, blob)}; + vals.emplace_back (Value{}); THEN ("every value can be assinged to any other value") { for (auto val : vals) { - - ValueList vals_m {typed_values(ival, dval,txt, blob)} ; - vals_m.emplace_back(Value{}) ; + ValueList vals_m{typed_values (ival, dval, txt, blob)}; + vals_m.emplace_back (Value{}); const Value val_cp = val; - for(auto val_m : vals_m) + for (auto val_m : vals_m) { - CHECK(val_cp.getType() == val.getType()) ; - CHECK(val_cp == val) ; - const Value val_m_cp = val_m ; - val = std::move(val_m) ; - CHECK(val_m.isNull()) ; - CHECK(val.getType() == val_m_cp.getType()) ; - CHECK(val == val_m_cp) ; - val = val_cp ; // reset - CHECK(val == val_cp) ; + CHECK (val_cp.getType () == val.getType ()); + CHECK (val_cp == val); + const Value val_m_cp = val_m; + val = std::move (val_m); + CHECK (val_m.isNull ()); + CHECK (val.getType () == val_m_cp.getType ()); + CHECK (val == val_m_cp); + val = val_cp; // reset + CHECK (val == val_cp); } } } @@ -170,148 +165,151 @@ SCENARIO("create, assign, copy and moveing values") WHEN ("constructing a value from const char") { - Value cval{"hello"} ; + Value cval{"hello"}; THEN ("it is the same as constructing from std::string") { - Value sval{std::string{"hello"}} ; - CHECK (sval == cval) ; + Value sval{std::string{"hello"}}; + CHECK (sval == cval); } } - } } -SCENARIO("null value values access") +SCENARIO ("null value values access") { GIVEN ("a check throw on invalid value access function") { - sl3::Value val ; + sl3::Value val; WHEN ("trying to access any value") { - CHECK(val.isNull()) ; + CHECK (val.isNull ()); THEN ("err null values access will be throws") { - CHECK_THROWS_AS((void)val.int64(), sl3::ErrNullValueAccess) ; - CHECK_THROWS_AS((void)static_cast(val), sl3::ErrNullValueAccess) ; - CHECK_THROWS_AS((void)val.real(), sl3::ErrNullValueAccess) ; - CHECK_THROWS_AS((void)static_cast(val), sl3::ErrNullValueAccess) ; - CHECK_THROWS_AS((void)val.text(), sl3::ErrNullValueAccess) ; - CHECK_THROWS_AS((void)static_cast(val), sl3::ErrNullValueAccess) ; - CHECK_THROWS_AS((void)val.blob(), sl3::ErrNullValueAccess) ; + CHECK_THROWS_AS ((void)val.int64 (), sl3::ErrNullValueAccess); + CHECK_THROWS_AS ((void)static_cast (val), + sl3::ErrNullValueAccess); + CHECK_THROWS_AS ((void)val.real (), sl3::ErrNullValueAccess); + CHECK_THROWS_AS ((void)static_cast (val), + sl3::ErrNullValueAccess); + CHECK_THROWS_AS ((void)val.text (), sl3::ErrNullValueAccess); + CHECK_THROWS_AS ((void)static_cast (val), + sl3::ErrNullValueAccess); + CHECK_THROWS_AS ((void)val.blob (), sl3::ErrNullValueAccess); // TODO, this raises an error on gcc 13 release build // interestingly, not for the string. - // add further investigation if I do the case operators as they should be or, if they should be removed - // CHECK_THROWS_AS((void)static_cast(val), sl3::ErrNullValueAccess) ; - CHECK_THROWS_AS((void)static_cast(val), sl3::ErrNullValueAccess) ; + // add further investigation if I do the case operators as they should + // be or, if they should be removed + // CHECK_THROWS_AS((void)static_cast(val), + // sl3::ErrNullValueAccess) ; + CHECK_THROWS_AS ((void)static_cast (val), + sl3::ErrNullValueAccess); } } } } - -SCENARIO("invalid type access") +SCENARIO ("invalid type access") { using namespace sl3; GIVEN ("a check throw on invalid value access function") { - const int ival = 2 ; - const double dval = 2.3 ; - const std::string txt = "hello" ; - const Blob blob{std::byte{'A'},std::byte{'B'}}; + const int ival = 2; + const double dval = 2.3; + const std::string txt = "hello"; + const Blob blob{std::byte{'A'}, std::byte{'B'}}; - auto check_invariants = [&](const Value val) - { - if(val.getType() != Type::Int) + auto check_invariants = [&] (const Value val) { + if (val.getType () != Type::Int) { - CHECK_THROWS_AS((void)val.int64(), ErrTypeMisMatch) ; - CHECK_THROWS_AS((void)static_cast(val), ErrTypeMisMatch) ; + CHECK_THROWS_AS ((void)val.int64 (), ErrTypeMisMatch); + CHECK_THROWS_AS ((void)static_cast (val), ErrTypeMisMatch); } else { - CHECK_EQ(val.int64(), ival) ; - CHECK_EQ(static_cast(val), ival) ; + CHECK_EQ (val.int64 (), ival); + CHECK_EQ (static_cast (val), ival); } // casting int to real works , other way not - if(val.getType() != Type::Real && val.getType() != Type::Int) + if (val.getType () != Type::Real && val.getType () != Type::Int) { - CHECK_THROWS_AS((void)val.real(), ErrTypeMisMatch) ; - CHECK_THROWS_AS((void)static_cast(val), ErrTypeMisMatch) ; + CHECK_THROWS_AS ((void)val.real (), ErrTypeMisMatch); + CHECK_THROWS_AS ((void)static_cast (val), ErrTypeMisMatch); } - else if(val.getType() == Type::Int) + else if (val.getType () == Type::Int) { - CHECK_EQ(val.int64(), ival) ; - CHECK_EQ(static_cast(val), ival) ; + CHECK_EQ (val.int64 (), ival); + CHECK_EQ (static_cast (val), ival); } - else if(val.getType() == Type::Real) + else if (val.getType () == Type::Real) { - CHECK_EQ(val.real(),dval) ; - CHECK_EQ(static_cast(val), dval) ; + CHECK_EQ (val.real (), dval); + CHECK_EQ (static_cast (val), dval); } - - if(val.getType() != Type::Text) + if (val.getType () != Type::Text) { - CHECK_THROWS_AS((void)val.text(), ErrTypeMisMatch) ; - CHECK_THROWS_AS((void)static_cast(val), ErrTypeMisMatch) ; + CHECK_THROWS_AS ((void)val.text (), ErrTypeMisMatch); + CHECK_THROWS_AS ((void)static_cast (val), + ErrTypeMisMatch); } else { - CHECK_EQ(val.text(),txt) ; - CHECK_EQ(static_cast(val), txt) ; + CHECK_EQ (val.text (), txt); + CHECK_EQ (static_cast (val), txt); } - if(val.getType() != Type::Blob) + if (val.getType () != Type::Blob) { - CHECK_THROWS_AS((void)val.blob(), ErrTypeMisMatch) ; - CHECK_THROWS_AS((void)static_cast(val), ErrTypeMisMatch) ; + CHECK_THROWS_AS ((void)val.blob (), ErrTypeMisMatch); + CHECK_THROWS_AS ((void)static_cast (val), + ErrTypeMisMatch); } else { - CHECK_EQ(val.blob(),blob) ; - CHECK_EQ(static_cast(val), blob) ; + CHECK_EQ (val.blob (), blob); + CHECK_EQ (static_cast (val), blob); } }; - WHEN ("having values of each type") { - ValueList vals {typed_values(ival, dval,txt, blob)} ; + ValueList vals{typed_values (ival, dval, txt, blob)}; THEN ("running check_throw on each fo this value will work") { for (auto v : vals) - check_invariants(v) ; + check_invariants (v); } - THEN("assinging the values to something otherworks") + THEN ("assinging the values to something otherworks") { - vals.emplace_back(Value{}) ; + vals.emplace_back (Value{}); for (auto v : vals) { - v = ival ; - check_invariants(v) ; + v = ival; + check_invariants (v); } for (auto v : vals) - { - v = int64_t{ival}; - check_invariants(v) ; - } + { + v = int64_t{ival}; + check_invariants (v); + } for (auto v : vals) - { - v = dval ; - check_invariants(v) ; - } + { + v = dval; + check_invariants (v); + } for (auto v : vals) - { - v = txt; - check_invariants(v) ; - } + { + v = txt; + check_invariants (v); + } for (auto v : vals) - { - v = blob ; - check_invariants(v) ; - } + { + v = blob; + check_invariants (v); + } } } } @@ -319,17 +317,17 @@ SCENARIO("invalid type access") SCENARIO ("number special") { - using namespace sl3 ; + using namespace sl3; // do the int stuff extra here GIVEN ("a null value") { - Value v ; + Value v; WHEN ("assign an integer") { - v = 100 ; + v = 100; THEN ("accessing that integer is not a problem") { - CHECK_EQ(static_cast(v), 100) ; + CHECK_EQ (static_cast (v), 100); } } @@ -337,54 +335,54 @@ SCENARIO ("number special") { THEN ("a null value access happens") { - CHECK_THROWS_AS((void)static_cast(v), ErrNullValueAccess) ; + CHECK_THROWS_AS ((void)static_cast (v), ErrNullValueAccess); } } WHEN ("assign an int value bigger than max int") { - v= std::numeric_limits::max() ; + v = std::numeric_limits::max (); THEN ("converting to an integer will might throw") { - CHECK_THROWS_AS((void)static_cast(v), ErrOutOfRange) ; + CHECK_THROWS_AS ((void)static_cast (v), ErrOutOfRange); } } WHEN ("assign a real value that is int") { - v = 1.00 ; + v = 1.00; THEN ("converting to an integer will work") { - CHECK_EQ(static_cast(v), 1) ; + CHECK_EQ (static_cast (v), 1); } } WHEN ("assign a real value that is not an int") { - v = 1.11 ; + v = 1.11; THEN ("converting to an integer will throw") { - CHECK_THROWS_AS((void)static_cast(v), ErrTypeMisMatch) ; + CHECK_THROWS_AS ((void)static_cast (v), ErrTypeMisMatch); } } WHEN ("assign an integer") { - v = 100 ; + v = 100; THEN ("converting to a real just works") { - CHECK_EQ(static_cast(v), 100.0) ; + CHECK_EQ (static_cast (v), 100.0); } } WHEN ("assign a real value bigger than is to big") { - v = std::numeric_limits::max() ; + v = std::numeric_limits::max (); THEN ("converting to an integer will throw") { - CHECK_THROWS_AS((void)static_cast(v), ErrOutOfRange) ; - CHECK_THROWS_AS((void)static_cast(v), ErrOutOfRange) ; + CHECK_THROWS_AS ((void)static_cast (v), ErrOutOfRange); + CHECK_THROWS_AS ((void)static_cast (v), ErrOutOfRange); } } @@ -393,36 +391,36 @@ SCENARIO ("number special") v = "hello"; THEN ("converting to an integer will throw") { - CHECK_THROWS_AS((void)static_cast(v), ErrTypeMisMatch) ; + CHECK_THROWS_AS ((void)static_cast (v), ErrTypeMisMatch); } } } } -SCENARIO("eject values") +SCENARIO ("eject values") { - using namespace sl3 ; + using namespace sl3; GIVEN ("a blob an a string value with knowen value") { - const std::string txt = "hello" ; - const Blob blob{std::byte{'A'},std::byte{'B'}}; + const std::string txt = "hello"; + const Blob blob{std::byte{'A'}, std::byte{'B'}}; - sl3::Value sval{txt} ; - sl3::Value bval{blob} ; + sl3::Value sval{txt}; + sl3::Value bval{blob}; WHEN ("ejecting the blob / string value from the right type") { - auto etxt = sval.ejectText(); - auto eblob = bval.ejectBlob(); + auto etxt = sval.ejectText (); + auto eblob = bval.ejectBlob (); THEN ("the ejected values are expected") { - CHECK_EQ(txt, etxt); - CHECK_EQ(blob, eblob); + CHECK_EQ (txt, etxt); + CHECK_EQ (blob, eblob); - AND_THEN("the values are resteded to Null") + AND_THEN ("the values are resteded to Null") { - CHECK(sval.isNull()); - CHECK(bval.isNull()); + CHECK (sval.isNull ()); + CHECK (bval.isNull ()); } } } @@ -430,198 +428,185 @@ SCENARIO("eject values") { THEN ("exceptions are throws") { - CHECK_THROWS_AS(sval.ejectBlob(), ErrTypeMisMatch); - CHECK_THROWS_AS(bval.ejectText(), ErrTypeMisMatch); + CHECK_THROWS_AS (sval.ejectBlob (), ErrTypeMisMatch); + CHECK_THROWS_AS (bval.ejectText (), ErrTypeMisMatch); } } WHEN ("ejecting from Null values") { THEN ("exceptions are throws") { - Value val ; - CHECK_THROWS_AS(val.ejectBlob(), ErrNullValueAccess); - CHECK_THROWS_AS(val.ejectText(), ErrNullValueAccess); + Value val; + CHECK_THROWS_AS (val.ejectBlob (), ErrNullValueAccess); + CHECK_THROWS_AS (val.ejectText (), ErrNullValueAccess); } } } } - -SCENARIO("compare values") +SCENARIO ("compare values") { using namespace sl3; GIVEN ("a check smaller function") { - auto check_smaller= [](const Value& val, const ValueList& smaller) - { - CHECK_FALSE(value_lt(val, val)) ; - CHECK_FALSE(value_type_lt(val, val)) ; + auto check_smaller = [] (const Value& val, const ValueList& smaller) { + CHECK_FALSE (value_lt (val, val)); + CHECK_FALSE (value_type_lt (val, val)); for (const auto& v : smaller) { - CHECK(value_lt(v, val)) ; - CHECK(value_type_lt(v, val)) ; - CHECK_FALSE(value_lt(val, v)) ; - CHECK_FALSE(value_type_lt(val, v)) ; + CHECK (value_lt (v, val)); + CHECK (value_type_lt (v, val)); + CHECK_FALSE (value_lt (val, v)); + CHECK_FALSE (value_type_lt (val, v)); } }; WHEN ("checking values with expected smaller values") { - Value v{0} ; - ValueList smaller = {Value{-1}, Value{}, Value{-1.0} }; + Value v{0}; + ValueList smaller = {Value{-1}, Value{}, Value{-1.0}}; THEN ("all check succeed") { - check_smaller(Value{0}, smaller); - check_smaller(Value{1.0}, smaller); - smaller.push_back(Value{"aaa"}); - check_smaller(Value{"bbb"}, smaller); - smaller.push_back(Value{Blob{std::byte{'A'}}}); - check_smaller(Value{Blob{std::byte{'A'}, std::byte{'B'}}}, smaller); + check_smaller (Value{0}, smaller); + check_smaller (Value{1.0}, smaller); + smaller.push_back (Value{"aaa"}); + check_smaller (Value{"bbb"}, smaller); + smaller.push_back (Value{Blob{std::byte{'A'}}}); + check_smaller (Value{Blob{std::byte{'A'}, std::byte{'B'}}}, smaller); } } } - GIVEN("a function that checks equalities") + GIVEN ("a function that checks equalities") { - auto check_eq= [](const Value& val, const ValueList& different) - { - CHECK(value_eq(val, val)) ; - CHECK(value_type_eq(val, val)) ; + auto check_eq = [] (const Value& val, const ValueList& different) { + CHECK (value_eq (val, val)); + CHECK (value_type_eq (val, val)); for (const auto& v : different) { - CHECK_FALSE(value_eq(val, v)) ; - CHECK_FALSE(value_type_eq(val, v)) ; + CHECK_FALSE (value_eq (val, v)); + CHECK_FALSE (value_type_eq (val, v)); } }; WHEN ("callin check_eq with knowen values") { - THEN("all tests succeed") + THEN ("all tests succeed") { - check_eq(Value{}, - {Value{1}, Value{2.0}, Value{"foo"}, Value{Blob{'A'}}}); + check_eq (Value{}, + {Value{1}, Value{2.0}, Value{"foo"}, Value{Blob{'A'}}}); - check_eq(Value{1}, - {Value{},Value{2.0}, Value{"foo"}, Value{Blob{'A'}}}); + check_eq (Value{1}, + {Value{}, Value{2.0}, Value{"foo"}, Value{Blob{'A'}}}); - check_eq(Value{2.0}, - {Value{},Value{1}, Value{"foo"}, Value{Blob{'A'}}}); + check_eq (Value{2.0}, + {Value{}, Value{1}, Value{"foo"}, Value{Blob{'A'}}}); - check_eq(Value{"foo"}, - {Value{},Value{1}, Value{2.0}, Value{Blob{'A'}}}); - - check_eq(Value{Blob{'A'}}, - {Value{},Value{1}, Value{2.0}, Value{"foo"}}); + check_eq (Value{"foo"}, + {Value{}, Value{1}, Value{2.0}, Value{Blob{'A'}}}); + check_eq (Value{Blob{'A'}}, + {Value{}, Value{1}, Value{2.0}, Value{"foo"}}); } } } - GIVEN("int and a real types with same values") + GIVEN ("int and a real types with same values") { - Value ival{1} ; - Value rval{1.0} ; + Value ival{1}; + Value rval{1.0}; WHEN ("comparing with them self") { - THEN("value_eq is true and value_type_eq is true") + THEN ("value_eq is true and value_type_eq is true") { - CHECK(value_eq(ival, ival)) ; - CHECK(value_type_eq(ival, ival)) ; - CHECK(value_eq(rval, rval)) ; - CHECK(value_type_eq(rval, rval)) ; + CHECK (value_eq (ival, ival)); + CHECK (value_type_eq (ival, ival)); + CHECK (value_eq (rval, rval)); + CHECK (value_type_eq (rval, rval)); } } WHEN ("comparing both with each other") { - THEN("value_eq is true and value_type_eq is false") + THEN ("value_eq is true and value_type_eq is false") { - CHECK(value_eq(ival, rval)) ; - CHECK_FALSE(value_type_eq(ival, rval)) ; + CHECK (value_eq (ival, rval)); + CHECK_FALSE (value_type_eq (ival, rval)); } } } } - -SCENARIO("swapping values") +SCENARIO ("swapping values") { using namespace sl3; - GIVEN("2 different values") + GIVEN ("2 different values") { Value a{1}; Value b{"a longer text with no small string opt"}; - WHEN("swapping those values") + WHEN ("swapping those values") { - std::swap(a,b); - THEN("the values are swapped") + std::swap (a, b); + THEN ("the values are swapped") { - CHECK(a.getType() == Type::Text); - CHECK(a.text() == "a longer text with no small string opt"); + CHECK (a.getType () == Type::Text); + CHECK (a.text () == "a longer text with no small string opt"); - CHECK(b.getType() == Type::Int); - CHECK(b.int64() == 1); + CHECK (b.getType () == Type::Int); + CHECK (b.int64 () == 1); } } } } - -SCENARIO("stringing values") +SCENARIO ("stringing values") { using namespace sl3; - GIVEN("a string stream") + GIVEN ("a string stream") { - std::stringstream ss ; - WHEN("stringing a null value") + std::stringstream ss; + WHEN ("stringing a null value") { - ss << Value{} ; - THEN("the stingstream contains ") + ss << Value{}; + THEN ("the stingstream contains ") { - CHECK(ss.str() == ""); + CHECK (ss.str () == ""); } } - WHEN("stringing an int value") + WHEN ("stringing an int value") { - ss << Value{1} ; - THEN("the stingstream contains the int") - { - CHECK(ss.str() == "1"); - } + ss << Value{1}; + THEN ("the stingstream contains the int") { CHECK (ss.str () == "1"); } } - WHEN("stringing a real value") + WHEN ("stringing a real value") { - ss << Value{1.1} ; - THEN("the stingstream contains the real") + ss << Value{1.1}; + THEN ("the stingstream contains the real") { - CHECK(ss.str() == "1.1"); + CHECK (ss.str () == "1.1"); } } - WHEN("stringing a text value") + WHEN ("stringing a text value") { - ss << Value{"hello"} ; - THEN("the stingstream contains the text") + ss << Value{"hello"}; + THEN ("the stingstream contains the text") { - CHECK(ss.str() == "hello"); + CHECK (ss.str () == "hello"); } } - WHEN("stringing a blob value") + WHEN ("stringing a blob value") { - ss << Value{Blob{}} ; - THEN("the stingstream contains ") + ss << Value{Blob{}}; + THEN ("the stingstream contains ") { - CHECK(ss.str() == ""); + CHECK (ss.str () == ""); } } - } } - - - diff --git a/tests/version/versiontest.cpp b/tests/version/versiontest.cpp index 169cebb..4eb39af 100644 --- a/tests/version/versiontest.cpp +++ b/tests/version/versiontest.cpp @@ -3,7 +3,7 @@ #include -SCENARIO("check sqlite versions for library and app") +SCENARIO ("check sqlite versions for library and app") { // this can be used as an example to add into your project // assure the libsl3 was compiled using the same header as you app @@ -14,8 +14,8 @@ SCENARIO("check sqlite versions for library and app") { THEN ("these version are the same") { - std::string compiledVersion = sl3::sqliteCompiledVersion() ; - CHECK(compiledVersion == sl3::sqliteRuntimeVersion()); + std::string compiledVersion = sl3::sqliteCompiledVersion (); + CHECK (compiledVersion == sl3::sqliteRuntimeVersion ()); } } @@ -23,8 +23,8 @@ SCENARIO("check sqlite versions for library and app") { THEN ("they are the same") { - CHECK_EQ ( sl3::sqliteCompiledVersionNumber(), - sl3::sqliteRuntimeVersionNumber()); + CHECK_EQ (sl3::sqliteCompiledVersionNumber (), + sl3::sqliteRuntimeVersionNumber ()); } } @@ -32,9 +32,9 @@ SCENARIO("check sqlite versions for library and app") { THEN ("it should always pass") { // if this is ever important, just use as needed - auto tsco = sl3::sqliteThreadSafeCompileOption () ; - auto ts = tsco == 1 || tsco == 2 || tsco == 3; - CHECK (ts) ; + auto tsco = sl3::sqliteThreadSafeCompileOption (); + auto ts = tsco == 1 || tsco == 2 || tsco == 3; + CHECK (ts); } } }