added test for table sequence
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
#ifndef MATADOR_SEQUENCE_FIXTURE_HPP
|
||||
#define MATADOR_SEQUENCE_FIXTURE_HPP
|
||||
|
||||
#include "matador/query/schema.hpp"
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
#include <stack>
|
||||
@@ -10,18 +8,18 @@
|
||||
namespace matador::test {
|
||||
class SequenceFixture {
|
||||
public:
|
||||
SequenceFixture();
|
||||
~SequenceFixture();
|
||||
SequenceFixture();
|
||||
~SequenceFixture();
|
||||
|
||||
void check_sequence_exists(const std::string &sequence_name) const;
|
||||
void check_sequence_not_exists(const std::string &sequence_name) const;
|
||||
void check_sequence_exists(const std::string &sequence_name) const;
|
||||
void check_sequence_not_exists(const std::string &sequence_name) const;
|
||||
|
||||
protected:
|
||||
sql::connection db;
|
||||
std::stack <std::string> sequences_to_drop;
|
||||
sql::connection db;
|
||||
std::stack <std::string> sequences_to_drop;
|
||||
|
||||
private:
|
||||
void drop_sequence_if_exists(const std::string &sequence_name) const;
|
||||
void drop_sequence_if_exists(const std::string &sequence_name) const;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
#include "TableSequenceFixture.hpp"
|
||||
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/builder.hpp"
|
||||
|
||||
#include "connection.hpp"
|
||||
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
TableSequenceFixture::TableSequenceFixture()
|
||||
: db(connection::dns) {
|
||||
REQUIRE(db.open());
|
||||
|
||||
REQUIRE(query::query::create()
|
||||
.table(sequence_table_name)
|
||||
.columns({
|
||||
query::column("name", utils::basic_type::Varchar, 255),
|
||||
query::column("next_id", utils::basic_type::Int64)
|
||||
})
|
||||
.execute(db));
|
||||
}
|
||||
|
||||
TableSequenceFixture::~TableSequenceFixture() {
|
||||
REQUIRE(query::query::drop()
|
||||
.table(sequence_table_name)
|
||||
.execute(db));
|
||||
REQUIRE(db.close());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#ifndef MATADOR_TABLE_SEQUENCE_FIXTURE_HPP
|
||||
#define MATADOR_TABLE_SEQUENCE_FIXTURE_HPP
|
||||
|
||||
#include "matador/sql/connection.hpp"
|
||||
|
||||
namespace matador::test {
|
||||
class TableSequenceFixture {
|
||||
public:
|
||||
TableSequenceFixture();
|
||||
~TableSequenceFixture();
|
||||
|
||||
protected:
|
||||
sql::connection db;
|
||||
std::string sequence_table_name{"test_seq_table"};
|
||||
};
|
||||
}
|
||||
|
||||
#endif //MATADOR_TABLE_SEQUENCE_FIXTURE_HPP
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "catch2/catch_test_macros.hpp"
|
||||
|
||||
#include "matador/query/criteria.hpp"
|
||||
#include "matador/query/expression/expression_operators.hpp"
|
||||
#include "matador/query/query.hpp"
|
||||
#include "matador/query/table_column.hpp"
|
||||
|
||||
#include "TableSequenceFixture.hpp"
|
||||
|
||||
using namespace matador::query;
|
||||
using namespace matador::test;
|
||||
|
||||
TEST_CASE_METHOD(TableSequenceFixture, "test create and drop table sequence", "[table_sequence][create][drop]") {
|
||||
const auto next_id_col = "next_id"_col;
|
||||
auto result = query::query::insert()
|
||||
.into(sequence_table_name, { "name", next_id_col})
|
||||
.values({ "test_seq", 1 })
|
||||
.execute(db);
|
||||
REQUIRE(result);
|
||||
REQUIRE(result->affected_rows == 1);
|
||||
|
||||
table_column exp(next_id_col - 1);
|
||||
auto id_result = query::query::update(sequence_table_name)
|
||||
.set(next_id_col, next_id_col + 1)
|
||||
.where("name"_col == "test_seq")
|
||||
.returning((next_id_col - 1).as("id"))
|
||||
.fetch_value<int64_t>(db);
|
||||
|
||||
REQUIRE(id_result);
|
||||
REQUIRE(id_result->has_value());
|
||||
REQUIRE(id_result->value() == 1);
|
||||
|
||||
id_result = query::query::select({next_id_col})
|
||||
.from(sequence_table_name)
|
||||
.where("name"_col == "test_seq")
|
||||
.fetch_value<int64_t>(db);
|
||||
REQUIRE(id_result);
|
||||
REQUIRE(id_result->has_value());
|
||||
REQUIRE(id_result->value() == 2);
|
||||
}
|
||||
Reference in New Issue
Block a user