From ede5a8c6364051fd866b699d3328870218d3564e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sascha=20K=C3=BChl?= Date: Mon, 24 Nov 2025 16:26:52 +0100 Subject: [PATCH] object generator progress --- include/matador/object/attribute.hpp | 2 + .../matador/object/attribute_generator.hpp | 26 ++++--- include/matador/object/constraint.hpp | 2 + .../matador/object/constraints_generator.hpp | 76 +++++++++++++++++++ include/matador/object/object.hpp | 51 +++++++++++++ include/matador/object/object_generator.hpp | 67 ---------------- include/matador/object/repository.hpp | 1 + include/matador/object/repository_node.hpp | 7 +- .../query_create_intermediate.hpp | 4 - .../matador/utils/primary_key_attribute.hpp | 70 +++++++++-------- source/core/CMakeLists.txt | 5 +- source/core/object/attribute_generator.cpp | 3 +- source/core/object/constraints_generator.cpp | 48 ++++++++++++ source/core/object/object.cpp | 55 ++++++++++++++ test/core/CMakeLists.txt | 19 ++--- ...torTest.cpp => AttributeGeneratorTest.cpp} | 7 +- test/core/object/ObjectTest.cpp | 20 +++++ 17 files changed, 329 insertions(+), 134 deletions(-) create mode 100644 include/matador/object/constraints_generator.hpp create mode 100644 include/matador/object/object.hpp delete mode 100644 include/matador/object/object_generator.hpp create mode 100644 source/core/object/constraints_generator.cpp create mode 100644 source/core/object/object.cpp rename test/core/object/{AttributeDefinitionGeneratorTest.cpp => AttributeGeneratorTest.cpp} (95%) create mode 100644 test/core/object/ObjectTest.cpp diff --git a/include/matador/object/attribute.hpp b/include/matador/object/attribute.hpp index 5f0bec4..0be13a3 100644 --- a/include/matador/object/attribute.hpp +++ b/include/matador/object/attribute.hpp @@ -19,6 +19,7 @@ struct attribute_options { int index{-1}; }; class object; +class attribute_generator; class attribute { public: @@ -87,6 +88,7 @@ public: private: friend class object; + friend class attribute_generator; std::string name_; object *owner_{nullptr}; diff --git a/include/matador/object/attribute_generator.hpp b/include/matador/object/attribute_generator.hpp index 03843d6..2392015 100644 --- a/include/matador/object/attribute_generator.hpp +++ b/include/matador/object/attribute_generator.hpp @@ -54,25 +54,25 @@ private: class attribute_generator final { private: - attribute_generator(std::vector &columns, const repository &repo); + attribute_generator(std::vector &columns, const repository &repo, object &obj); public: ~attribute_generator() = default; template < class Type > - static std::vector generate(const repository &repo) { + static std::vector generate(const repository &repo, object &obj) { std::vector columns; - attribute_generator gen(columns, repo); - Type obj; - access::process(gen, obj); + attribute_generator gen(columns, repo, obj); + Type t; + access::process(gen, t); return columns; } template < class Type > - static std::vector generate(const Type& obj, const repository &repo) { + static std::vector generate(const Type& t, const repository &repo, object &obj) { std::vector columns; - attribute_generator gen(columns, repo); - access::process(gen, obj); + attribute_generator gen(columns, repo, obj); + access::process(gen, t); return columns; } @@ -127,6 +127,7 @@ private: size_t index_ = 0; std::vector &columns_; const repository &repo_; + object &obj_; fk_attribute_generator fk_column_generator_; }; @@ -138,13 +139,14 @@ void attribute_generator::on_primary_key(const char *id, ValueType &x, const uti template void attribute_generator::on_attribute(const char *id, Type &/*x*/, const utils::field_attributes &attr) { - columns_.emplace_back(id, utils::data_type_traits::type(attr.size()), attr, null_option_type::NOT_NULL); + auto &ref = columns_.emplace_back(id, utils::data_type_traits::type(attr.size()), attr, null_option_type::NOT_NULL); + ref.owner_ = &obj_; } template -void attribute_generator::on_attribute(const char *id, std::optional & /*x*/, const utils::field_attributes &attr) -{ - columns_.emplace_back(id, utils::data_type_traits::type(attr.size()), attr, null_option_type::NULLABLE); +void attribute_generator::on_attribute(const char *id, std::optional & /*x*/, const utils::field_attributes &attr) { + auto &ref = columns_.emplace_back(id, utils::data_type_traits::type(attr.size()), attr, null_option_type::NULLABLE); + ref.owner_ = &obj_; } } diff --git a/include/matador/object/constraint.hpp b/include/matador/object/constraint.hpp index fae2a17..9f1055f 100644 --- a/include/matador/object/constraint.hpp +++ b/include/matador/object/constraint.hpp @@ -9,6 +9,7 @@ namespace matador::object { class attribute; class constraint_builder; +class constraint_generator; class object; class constraint { @@ -20,6 +21,7 @@ public: private: friend class constraint_builder; + friend class constraints_generator; friend class object; std::string name_; diff --git a/include/matador/object/constraints_generator.hpp b/include/matador/object/constraints_generator.hpp new file mode 100644 index 0000000..463e2c9 --- /dev/null +++ b/include/matador/object/constraints_generator.hpp @@ -0,0 +1,76 @@ +#ifndef MATADOR_CONSTRAINTS_GENERATOR_HPP +#define MATADOR_CONSTRAINTS_GENERATOR_HPP + +#include "matador/object/constraint.hpp" + +#include "matador/utils/access.hpp" +#include "matador/utils/field_attributes.hpp" +#include "matador/utils/primary_key_attribute.hpp" + +#include + +namespace matador::object { +class object; +class basic_object_info; +class repository; + +class constraints_generator final { +private: + constraints_generator(std::vector &constraints, const repository &repo, object &obj); + +public: + constraints_generator() = delete; + + template < typename Type > + static std::vector generate(const repository &repo, object &obj) { + std::vector constraints; + constraints_generator gen(constraints, repo, obj); + Type t; + access::process(gen, t); + return constraints; + } + + template < class Type > + void on_primary_key(const char *id, Type &/*x*/, const utils::primary_key_attribute& attr = utils::default_pk_attributes) { + create_pk_constraint(id); + } + static void on_revision(const char * /*id*/, uint64_t &/*rev*/) {} + template + void on_attribute(const char *id, Type &x, const utils::field_attributes &attr = utils::null_attributes) { + if (utils::is_constraint_set(attr.options() ,utils::constraints::Unique)) { + create_unique_constraint(id); + } + } + + template + void on_belongs_to(const char *id, Pointer &x, const utils::foreign_attributes &/*attr*/) { + const auto result = repo_.info(); + if (!result) { + return; + } + create_fk_constraint(id, *result); + } + template + static void on_has_one(const char * /*id*/, Pointer &/*x*/, const utils::foreign_attributes &/*attr*/) {} + template + static void on_has_many(const char * /*id*/, ContainerType &, const char *, const utils::foreign_attributes &/*attr*/) {} + template + static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const char * /*join_column*/, const char * /*inverse_join_column*/, const utils::foreign_attributes &/*attr*/) {} + template + static void on_has_many_to_many(const char * /*id*/, ContainerType & /*cont*/, const utils::foreign_attributes &/*attr*/) {} + +private: + void create_pk_constraint(const std::string& name) const; + void create_fk_constraint(const std::string& name, const basic_object_info& info) const; + void create_unique_constraint(const std::string& name) const; + + [[nodiscard]] std::vector::iterator find_attribute_by_name(const std::string &name) const; + +private: + std::vector &constraints_; + const repository &repo_; + object &obj_; +}; +} + +#endif //MATADOR_CONSTRAINTS_GENERATOR_HPP \ No newline at end of file diff --git a/include/matador/object/object.hpp b/include/matador/object/object.hpp new file mode 100644 index 0000000..b33c91f --- /dev/null +++ b/include/matador/object/object.hpp @@ -0,0 +1,51 @@ +#ifndef MATADOR_OBJECT_GENERATOR_HPP +#define MATADOR_OBJECT_GENERATOR_HPP + +#include "matador/object/attribute.hpp" +#include "matador/object/attribute_generator.hpp" +#include "matador/object/constraint.hpp" +#include "matador/object/constraints_generator.hpp" + +namespace matador::object { + +class repository; + +class object { +public: + explicit object(std::string name, std::string alias = ""); + + template + static std::unique_ptr generate(const repository& repo, std::string name, std::string alias = "") { + auto obj = std::make_unique(std::move(name), std::move(alias)); + obj->attributes_ = std::move(attribute_generator::generate(repo, *obj)); + obj->constraints_ = std::move(constraints_generator::generate(repo, *obj)); + return std::move(obj); + } + + static const attribute& create_attribute(std::string name, object& obj); + + void add_attribute(attribute attr); + void add_constraint(class constraint c); + + [[nodiscard]] const std::string& name() const; + [[nodiscard]] const std::string& alias() const; + + [[nodiscard]] bool has_attributes() const; + [[nodiscard]] size_t attribute_count() const; + [[nodiscard]] const std::vector& attributes() const; + + [[nodiscard]] bool has_constraints() const; + [[nodiscard]] size_t constraint_count() const; + [[nodiscard]] const std::vector& constraints() const; + +private: + friend class constraints_generator; + + std::string name_; + std::string alias_; + + std::vector attributes_; + std::vector constraints_; +}; +} +#endif //MATADOR_OBJECT_GENERATOR_HPP \ No newline at end of file diff --git a/include/matador/object/object_generator.hpp b/include/matador/object/object_generator.hpp deleted file mode 100644 index 61e0cce..0000000 --- a/include/matador/object/object_generator.hpp +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef MATADOR_OBJECT_GENERATOR_HPP -#define MATADOR_OBJECT_GENERATOR_HPP - -#include "matador/object/attribute.hpp" -#include "matador/object/attribute_generator.hpp" -#include "matador/object/constraint.hpp" - -namespace matador::object { - -class object_generator; - -class object { -public: - explicit object(std::string name, std::string alias = ""); - - void add_attribute(attribute attr) { - auto &ref = attributes_.emplace_back(std::move(attr)); - ref.owner_ = this; - } - - static const attribute& create_attribute(std::string name, object& obj) { - attribute attr{std::move(name)}; - attr.owner_ = &obj; - return obj.attributes_.emplace_back(std::move(attr)); - } - - void add_constraint(class constraint c) { - auto &ref = constraints_.emplace_back(std::move(c)); - ref.owner_ = this; - } - - [[nodiscard]] const std::string& name() const { return name_; } - [[nodiscard]] const std::string& alias() const { return alias_; } - - [[nodiscard]] bool has_attributes() const { return attributes_.empty(); } - [[nodiscard]] size_t attribute_count() const { return attributes_.size(); } - [[nodiscard]] const std::vector& attributes() const { return attributes_; } - - [[nodiscard]] bool has_constraints() const { return constraints_.empty(); } - [[nodiscard]] size_t constraint_count() const { return constraints_.size(); } - [[nodiscard]] const std::vector& constraints() const { return constraints_; } - -private: - friend class object_generator; - - std::string name_; - std::string alias_; - - std::vector attributes_; - std::vector constraints_; -}; - -class object_generator { -public: - template - object generate(std::string name, std::string alias = "") { - object obj{std::move(name), std::move(alias)}; - attribute_generator::generate(*this); - Type obj2; - access::process(gen, obj2); - return obj; - } - -private: -}; -} -#endif //MATADOR_OBJECT_GENERATOR_HPP \ No newline at end of file diff --git a/include/matador/object/repository.hpp b/include/matador/object/repository.hpp index 4d9329c..ff5487d 100644 --- a/include/matador/object/repository.hpp +++ b/include/matador/object/repository.hpp @@ -5,6 +5,7 @@ #include "matador/object/error_code.hpp" #include "matador/object/foreign_node_completer.hpp" +#include "matador/object/object_info.hpp" #include "matador/object/relation_completer.hpp" #include "matador/object/repository_node.hpp" #include "matador/object/repository_node_iterator.hpp" diff --git a/include/matador/object/repository_node.hpp b/include/matador/object/repository_node.hpp index f10d6fe..aedd289 100644 --- a/include/matador/object/repository_node.hpp +++ b/include/matador/object/repository_node.hpp @@ -2,6 +2,7 @@ #define SCHEMA_NODE_HPP #include "matador/object/attribute_generator.hpp" +#include "matador/object/object.hpp" #include "matador/object/object_info.hpp" #include "matador/object/primary_key_resolver.hpp" @@ -22,8 +23,9 @@ public: primary_key_resolver resolver; auto pk_info = resolver.resolve(); + auto obj = object::generate(repo, name); auto ref_column = determine_reference_column(typeid(Type), name, pk_info, repo); - const auto attributes = attribute_generator::generate(repo); + const auto attributes = attribute_generator::generate(repo, *obj); auto info = std::make_unique>( node, attributes, @@ -45,9 +47,10 @@ public: } auto obj = creator(); + object o(name); auto info = std::make_unique>( result.value(), - attribute_generator::generate(*obj, repo), + attribute_generator::generate(*obj, repo, o), std::forward(creator) ); result.value()->info_ = std::move(info); diff --git a/include/matador/query/intermediates/query_create_intermediate.hpp b/include/matador/query/intermediates/query_create_intermediate.hpp index 3824369..f4ab9ed 100644 --- a/include/matador/query/intermediates/query_create_intermediate.hpp +++ b/include/matador/query/intermediates/query_create_intermediate.hpp @@ -15,10 +15,6 @@ public: executable_query table(const table &tab, std::initializer_list columns); executable_query table(const query::table &tab, const std::vector &columns); - template - executable_query table(const matador::query::table &tab, const object::repository &schema) { - return this->table(tab, object::attribute_generator::generate(schema)); - } executable_query schema(const std::string &schema_name); }; diff --git a/include/matador/utils/primary_key_attribute.hpp b/include/matador/utils/primary_key_attribute.hpp index 58a2b5e..4bd845e 100644 --- a/include/matador/utils/primary_key_attribute.hpp +++ b/include/matador/utils/primary_key_attribute.hpp @@ -8,45 +8,43 @@ namespace matador::utils { class primary_key_attribute final { public: - /** - * Creates a primary_key_attribute instance - * with size 0 (zero). - */ - primary_key_attribute() = default; - /** - * Creates primary_key_attribute instance - * with given size. - * - * @param size Size of the attribute - */ - primary_key_attribute(size_t size); // NOLINT(*-explicit-constructor) - primary_key_attribute(generator_type generator); // NOLINT(*-explicit-constructor) - primary_key_attribute(size_t size, generator_type generator); - ~primary_key_attribute() = default; - primary_key_attribute(const primary_key_attribute &) = default; - primary_key_attribute(primary_key_attribute &&) = default; - primary_key_attribute &operator=(const primary_key_attribute &) = default; - primary_key_attribute &operator=(primary_key_attribute &&) = default; - + /** + * Creates a primary_key_attribute instance + * with size 0 (zero). + */ + primary_key_attribute() = default; + /** + * Creates primary_key_attribute instance + * with given size. + * + * @param size Size of the attribute + */ + primary_key_attribute(size_t size); // NOLINT(*-explicit-constructor) + primary_key_attribute(generator_type generator); // NOLINT(*-explicit-constructor) + primary_key_attribute(size_t size, generator_type generator); + ~primary_key_attribute() = default; + primary_key_attribute(const primary_key_attribute &) = default; + primary_key_attribute(primary_key_attribute &&) = default; + primary_key_attribute &operator=(const primary_key_attribute &) = default; + primary_key_attribute &operator=(primary_key_attribute &&) = default; primary_key_attribute& operator=(size_t size); - primary_key_attribute& operator=(generator_type generator); + primary_key_attribute& operator=(generator_type generator); - /** - * Returns the size of the field - * - * @return Size of the field - */ - [[nodiscard]] size_t size() const; - - /** - * Returns the generator type of the field - * - * @return Generator type of the field - */ - [[nodiscard]] generator_type generator() const; + /** + * Returns the size of the field + * + * @return Size of the field + */ + [[nodiscard]] size_t size() const; + /** + * Returns the generator type of the field + * + * @return Generator type of the field + */ + [[nodiscard]] generator_type generator() const; private: - size_t size_ = 0; - generator_type generator_ = generator_type::MANUALLY; + size_t size_ = 0; + generator_type generator_ = generator_type::MANUALLY; }; const primary_key_attribute default_pk_attributes {}; diff --git a/source/core/CMakeLists.txt b/source/core/CMakeLists.txt index c6fb4ff..fd464b4 100644 --- a/source/core/CMakeLists.txt +++ b/source/core/CMakeLists.txt @@ -17,11 +17,12 @@ add_library(matador-core STATIC ../../include/matador/object/attribute_generator.hpp ../../include/matador/object/basic_object_info.hpp ../../include/matador/object/constraint.hpp + ../../include/matador/object/constraints_generator.hpp ../../include/matador/object/error_code.hpp ../../include/matador/object/foreign_node_completer.hpp ../../include/matador/object/internal/shadow_repository.hpp ../../include/matador/object/many_to_many_relation.hpp - ../../include/matador/object/object_generator.hpp + ../../include/matador/object/object.hpp ../../include/matador/object/object_info.hpp ../../include/matador/object/object_proxy.hpp ../../include/matador/object/object_ptr.hpp @@ -80,6 +81,7 @@ add_library(matador-core STATIC object/attribute_generator.cpp object/basic_object_info.cpp object/constraint.cpp + object/constraints_generator.cpp object/error_code.cpp object/foreign_node_completer.cpp object/internal/shadow_repository.cpp @@ -105,6 +107,7 @@ add_library(matador-core STATIC utils/uuid.cpp utils/value.cpp utils/version.cpp + object/object.cpp ) target_link_libraries(matador-core ${CMAKE_DL_LIBS}) diff --git a/source/core/object/attribute_generator.cpp b/source/core/object/attribute_generator.cpp index d7dd5e1..c411a56 100644 --- a/source/core/object/attribute_generator.cpp +++ b/source/core/object/attribute_generator.cpp @@ -3,9 +3,10 @@ namespace matador::object { -attribute_generator::attribute_generator(std::vector &columns, const repository &repo) +attribute_generator::attribute_generator(std::vector &columns, const repository &repo, object &obj) : columns_(columns) , repo_(repo) +, obj_(obj) {} void attribute_generator::on_revision(const char *id, uint64_t &rev) { diff --git a/source/core/object/constraints_generator.cpp b/source/core/object/constraints_generator.cpp new file mode 100644 index 0000000..3d13c5a --- /dev/null +++ b/source/core/object/constraints_generator.cpp @@ -0,0 +1,48 @@ +#include "matador/object/constraints_generator.hpp" +#include "matador/object/object.hpp" +#include "matador/object/repository.hpp" + +namespace matador::object { +constraints_generator::constraints_generator(std::vector &constraints, const repository& repo, object &obj) +: constraints_(constraints) +, repo_(repo) +, obj_(obj) {} + +void constraints_generator::create_pk_constraint(const std::string& name) const { + class constraint pk_constraint("PK_" + obj_.name()); + pk_constraint.options_ |= utils::constraints::PrimaryKey; + if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) { + pk_constraint.attr_ = &*pk_attr; + } + pk_constraint.owner_ = &obj_; + constraints_.emplace_back(std::move(pk_constraint)); +} + +void constraints_generator::create_fk_constraint(const std::string& name, const basic_object_info& info) const { + const auto pk_attribute = info.reference_column(); + class constraint pk_constraint("FK_" + obj_.name() + "_" + name); + pk_constraint.options_ |= utils::constraints::ForeignKey; + if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) { + pk_constraint.attr_ = &*pk_attr; + } + pk_constraint.owner_ = &obj_; + pk_constraint.ref_column_name_ = pk_attribute->name(); + pk_constraint.ref_table_name_ = pk_attribute->table_name(); + constraints_.emplace_back(std::move(pk_constraint)); +} + +void constraints_generator::create_unique_constraint(const std::string& name) const { + class constraint pk_constraint("UK_" + obj_.name() + "_" + name); + pk_constraint.options_ |= utils::constraints::Unique; + if (const auto pk_attr = find_attribute_by_name(name); pk_attr != std::end(obj_.attributes_)) { + pk_constraint.attr_ = &*pk_attr; + } + pk_constraint.owner_ = &obj_; +} + +std::vector::iterator constraints_generator::find_attribute_by_name(const std::string& name) const { + return std::find_if(std::begin(obj_.attributes_), std::end(obj_.attributes_), [&name](const attribute& elem) { + return elem.name() == name; + }); +} +} \ No newline at end of file diff --git a/source/core/object/object.cpp b/source/core/object/object.cpp new file mode 100644 index 0000000..a368343 --- /dev/null +++ b/source/core/object/object.cpp @@ -0,0 +1,55 @@ +#include "matador/object/object.hpp" + +namespace matador::object { +object::object( std::string name, std::string alias ) +: name_(std::move(name)) +, alias_(std::move(alias)) {} + +const attribute& object::create_attribute( std::string name, object& obj ) { + attribute attr{std::move(name)}; + attr.owner_ = &obj; + return obj.attributes_.emplace_back(std::move(attr)); +} + +void object::add_attribute( attribute attr ) { + auto &ref = attributes_.emplace_back(std::move(attr)); + ref.owner_ = this; +} + +void object::add_constraint( class constraint c ) { + auto &ref = constraints_.emplace_back(std::move(c)); + ref.owner_ = this; +} + +const std::string& object::name() const { + return name_; +} + +const std::string& object::alias() const { + return alias_; +} + +bool object::has_attributes() const { + return attributes_.empty(); +} + +size_t object::attribute_count() const { + return attributes_.size(); +} + +const std::vector& object::attributes() const { + return attributes_; +} + +bool object::has_constraints() const { + return constraints_.empty(); +} + +size_t object::constraint_count() const { + return constraints_.size(); +} + +const std::vector& object::constraints() const { + return constraints_; +} +} \ No newline at end of file diff --git a/test/core/CMakeLists.txt b/test/core/CMakeLists.txt index 595147d..6052fcb 100644 --- a/test/core/CMakeLists.txt +++ b/test/core/CMakeLists.txt @@ -3,22 +3,23 @@ CPMAddPackage("gh:catchorg/Catch2@3.7.1") list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras) add_executable(CoreTests + ../backends/SchemaFixture.hpp logger/LoggerTest.cpp + object/AttributeGeneratorTest.cpp + object/ObjectTest.cpp + object/PrimaryKeyResolverTest.cpp + object/SchemaTest.cpp utils/BasicTypeToVisitorTest.cpp utils/ConvertTest.cpp utils/DefaultTypeTraitsTest.cpp utils/DependencyInjectionTest.cpp - utils/IdentifierTest.cpp - utils/ResultTest.cpp utils/FieldAttributeTest.cpp - utils/VersionTest.cpp - utils/ThreadPoolTest.cpp - utils/StringTest.cpp - object/AttributeDefinitionGeneratorTest.cpp - object/PrimaryKeyResolverTest.cpp - object/SchemaTest.cpp + utils/IdentifierTest.cpp utils/MessageBusTest.cpp - ../backends/SchemaFixture.hpp + utils/ResultTest.cpp + utils/StringTest.cpp + utils/ThreadPoolTest.cpp + utils/VersionTest.cpp ) target_link_libraries(CoreTests matador-core Catch2::Catch2WithMain) diff --git a/test/core/object/AttributeDefinitionGeneratorTest.cpp b/test/core/object/AttributeGeneratorTest.cpp similarity index 95% rename from test/core/object/AttributeDefinitionGeneratorTest.cpp rename to test/core/object/AttributeGeneratorTest.cpp index 1e10790..a476faa 100644 --- a/test/core/object/AttributeDefinitionGeneratorTest.cpp +++ b/test/core/object/AttributeGeneratorTest.cpp @@ -1,6 +1,7 @@ #include #include "matador/object/attribute_generator.hpp" +#include "matador/object/object.hpp" #include "matador/object/repository.hpp" #include "../test/models/product.hpp" @@ -17,7 +18,8 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener .and_then([&repo] { return repo.attach("categories"); }); REQUIRE(result); - auto columns = attribute_generator::generate(repo); + object obj("products"); + auto columns = attribute_generator::generate(repo, obj); const std::vector expected_columns = { attribute{"product_name", basic_type::type_varchar, constraints::PrimaryKey, null_option_type::NOT_NULL }, @@ -43,7 +45,8 @@ TEST_CASE("Generate column definitions from object", "[column][definition][gener TEST_CASE("Generate columns from object with nullable columns", "[column generator]") { repository repo("main"); - auto columns = attribute_generator::generate(repo); + object obj("optionals"); + auto columns = attribute_generator::generate(repo, obj); const std::vector expected_columns = { attribute{"id", basic_type::type_uint32, constraints::PrimaryKey, null_option_type::NOT_NULL }, diff --git a/test/core/object/ObjectTest.cpp b/test/core/object/ObjectTest.cpp new file mode 100644 index 0000000..ac66eb0 --- /dev/null +++ b/test/core/object/ObjectTest.cpp @@ -0,0 +1,20 @@ +#include + +#include "matador/object/object.hpp" +#include "matador/object/repository.hpp" + +#include "../test/models/author.hpp" +#include "../test/models/book.hpp" + +TEST_CASE("Generate object from type", "[object][generate]") { + using namespace matador; + object::repository repo; + auto result = repo.attach("authors"); + REQUIRE(result); + + const auto obj = object::object::generate(repo, "books"); + REQUIRE(obj->name() == "books"); + REQUIRE(obj->alias().empty()); + REQUIRE(obj->attributes().size() == 4); + REQUIRE(obj->constraints().size() == 2); +} \ No newline at end of file