Compare commits

...

4 commits

Author SHA1 Message Date
b2650fad26 some getters added 2018-07-11 15:59:38 +02:00
4fdbc268a5 more on states 2018-07-11 15:59:08 +02:00
976dcbcbeb store sellers with state == NEW 2018-07-11 15:58:09 +02:00
ddfb9471a8 more test cases 2018-07-11 15:57:39 +02:00
7 changed files with 115 additions and 12 deletions

View file

@ -41,7 +41,7 @@ void Database::createNew()
"seller_no INTEGER, "
"first_name TEXT, "
"last_name TEXT, "
"offered_articles INTEGER, "
"num_offered_articles INTEGER, "
"UNIQUE (seller_no)"
");"};
sqlStrings.push_back(sqlCreateSellers);
@ -143,4 +143,53 @@ int Database::getVersion()
void Database::beginTransaction() { exec("BEGIN TRANSACTION"); }
void Database::endTransaction() { exec("END TRANSACTION"); }
void Database::endTransaction() { exec("END TRANSACTION"); }
unsigned int Database::storeSellers(std::vector<Seller>& sellers)
{
int retCode{};
int count{};
sqlite3_stmt* stmt;
beginTransaction();
for (auto& seller : sellers) {
if (seller.getState() == Entity::State::NEW) {
retCode = sqlite3_prepare_v2(
db_,
"INSERT INTO sellers"
" (id, seller_no, first_name, last_name, num_offered_articles)"
" VALUES (:uuid, :seller_no, :first_name, :last_name, :num_offered_articles)",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::runtime_error(sqlite3_errmsg(db_));
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":uuid"),
boost::uuids::to_string(seller.getUuid()).c_str(), -1, nullptr);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":seller_no"),
seller.getSellerNo());
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":first_name"),
seller.getFirstName().c_str(), -1, nullptr);
sqlite3_bind_text(stmt, sqlite3_bind_parameter_index(stmt, ":last_name"),
seller.getLastName().c_str(), -1, nullptr);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":num_offered_articles"),
seller.getNumberOfOfferedArticles());
retCode = sqlite3_step(stmt);
if (retCode != SQLITE_DONE) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
seller.setState(Seller::State::CLEAN);
++count;
sqlite3_finalize(stmt);
}
}
endTransaction();
return count;
}

View file

@ -1,6 +1,8 @@
#ifndef DATABASE_H
#define DATABASE_H
#include "seller.h"
#include <string>
#include <sqlite3.h>
@ -14,6 +16,7 @@ public:
Database& operator=(const Database&) = delete;
void exec(const std::string& sql);
void init();
unsigned int storeSellers(std::vector<Seller>& sellers);
private:
sqlite3 *db_;
std::string dbname_;

View file

@ -19,7 +19,7 @@ void Entity::createUuidFromString(const std::string& uuidString)
uuid_ = generator(uuidString);
}
inline Entity::State Entity::getState()
inline Entity::State Entity::getState() const
{
return state_;
}

View file

@ -4,17 +4,19 @@
#include <string>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_io.hpp>
class Entity
{
public:
enum class State { NEW, UPDATED, READ };
// Entity();
enum class State { NEW, UPDATED, CLEAN };
virtual ~Entity() = 0;
const boost::uuids::uuid& getUuid() const { return uuid_; };
void createUuid();
void createUuidFromString(const std::string& uuidString);
State getState();
virtual State getState() const;
void setState(State state) { state_ = state; }
private:
boost::uuids::uuid uuid_{};

View file

@ -12,7 +12,7 @@ Seller::Seller(const std::string& firstName, const std::string& lastName, int se
numberOfOfferedArticles_ = numberOfArticles;
}
inline void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; }
void Seller::setSellerNo(int seller_no) { sellerNo_ = seller_no; }
inline void Seller::setFirstName(const std::string& firstName) { firstName_ = firstName; }
@ -20,11 +20,15 @@ inline void Seller::setLastName(const std::string& lastName) { lastName_ = lastN
inline void Seller::setNumberOfOfferedArticles(int number) { numberOfOfferedArticles_ = number; }
inline size_t Seller::getNumberOfOfferedArticles() { return articles_.size(); }
int Seller::getNumberOfOfferedArticles() const { return static_cast<int>(articles_.size()); }
void Seller::addArticle(Article article) {
articles_.push_back(article);
}
void Seller::addArticle(Article article) { articles_.push_back(article); }
std::string Seller::getFirstName() const { return firstName_; }
std::string Seller::getLastName() const { return lastName_; }
int Seller::getSellerNo() const { return sellerNo_; }
std::vector<Article*> Seller::getArticles(bool onlySold)
{

View file

@ -22,8 +22,11 @@ class Seller : public Entity
void setNumberOfOfferedArticles(int number);
void addArticle(Article article);
std::string getFirstName() const;
std::string getLastName() const;
int getSellerNo() const;
int getNumberOfOfferedArticles() const;
std::vector<Article*> getArticles(bool onlySold = false);
size_t getNumberOfOfferedArticles();
private:
int sellerNo_{-1};

View file

@ -1,6 +1,7 @@
#define BOOST_TEST_MODULE database
#include "../src/core/database.h"
#include "../src/core/seller.h"
#include <filesystem>
@ -11,5 +12,46 @@ BOOST_AUTO_TEST_CASE(create_database)
Database db(":memory:");
BOOST_CHECK_NO_THROW(db.init());
}
BOOST_AUTO_TEST_CASE(store_seller_fail)
{
Database db(":memory:");
db.init();
std::vector<Seller> sellers;
sellers.push_back({});
sellers.push_back({});
BOOST_CHECK_THROW(db.storeSellers(sellers), std::runtime_error);
}
BOOST_AUTO_TEST_CASE(store_sellers_succ)
{
Database db(":memory:");
db.init();
std::vector<Seller> sellers;
Seller a{};
a.createUuid();
a.setSellerNo(1);
Seller b{};
b.createUuid();
b.setSellerNo(2);
BOOST_TEST(a.getUuid() != b.getUuid());
sellers.push_back(a);
sellers.push_back(b);
BOOST_CHECK_NO_THROW(db.storeSellers(sellers));
}
BOOST_AUTO_TEST_CASE(seller_states)
{
Database db(":memory:");
db.init();
std::vector<Seller> sellers;
Seller a;
a.setSellerNo(3);
a.createUuid();
sellers.push_back(a);
std::cout << "Anzahl sellers: " << sellers.size() << "\n";
BOOST_TEST((sellers.at(0).getState() == Entity::State::NEW));
BOOST_TEST(db.storeSellers(sellers) == 1);
BOOST_TEST((sellers.at(0).getState() == Entity::State::CLEAN));
BOOST_TEST(db.storeSellers(sellers) == 0);
}