From 2a4e0863fc908a82d7cd5ee1b69c7ec7c09a7ddf Mon Sep 17 00:00:00 2001 From: Fernando Oliveira Date: Sun, 7 Nov 2021 02:40:21 +0000 Subject: [PATCH] Fix string model but something is requiring test order and to type attributes --- lib/MetamodelX/Red/Model.pm6 | 1 - lib/Red/Attr/Relationship.pm6 | 2 +- lib/Red/Column.pm6 | 2 +- t/35-create.t | 50 +++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/lib/MetamodelX/Red/Model.pm6 b/lib/MetamodelX/Red/Model.pm6 index 3d9c6c74..37833a8e 100644 --- a/lib/MetamodelX/Red/Model.pm6 +++ b/lib/MetamodelX/Red/Model.pm6 @@ -547,7 +547,6 @@ multi method create(\mo where *.DEFINITE, *%orig-pars, :$with where not .defined $obj.^clean-up; $obj.^populate-ids; } - my %should-set = |mo.^join-on(mo.^parent).should-set.Hash if mo.HOW.?join-on: mo; my $p = mo.^parent; my %attrs = |$p.^columns.map: { .name.substr(2) => .self } diff --git a/lib/Red/Attr/Relationship.pm6 b/lib/Red/Attr/Relationship.pm6 index 8bab9d64..1152d484 100644 --- a/lib/Red/Attr/Relationship.pm6 +++ b/lib/Red/Attr/Relationship.pm6 @@ -15,7 +15,7 @@ unit role Red::Attr::Relationship[ Red::Model :$model-type, ]; -has Mu:U $!type; +#has Mu:U $!type; has Bool $.has-lazy-relationship = ?$model; diff --git a/lib/Red/Column.pm6 b/lib/Red/Column.pm6 index 4370b524..9c893ca7 100644 --- a/lib/Red/Column.pm6 +++ b/lib/Red/Column.pm6 @@ -163,7 +163,7 @@ method comment { .Str with self.attr.WHY } #| Returns a function that will return a column that is referenced by this column method references(--> Callable) is rw { - &!actual-references = do { + &!actual-references //= do { if &!references { if $!model-name { ReferencesProxy.new(:&!references, :$!model-name, :$!require, :$!model-type); diff --git a/t/35-create.t b/t/35-create.t index 99fd4475..bc54baf5 100644 --- a/t/35-create.t +++ b/t/35-create.t @@ -33,6 +33,55 @@ subtest "Create on has-one", { is $ble.bla.gist, $bla.gist; } +subtest "belogs-to using types", { + model Blo { ... } + model Bli { + has UInt $.id is serial; + has Str $.value is column; + has Blo @.blos is relationship(*.bli-id, :model(Blo)); + has Blo $.one-blo is relationship(*.bli-id, :model(Blo), :has-one); + } + + model Blo { + has UInt $.id is serial; + has Str $.value is column; + has UInt $.bli-id is referencing(*.id, :model(Bli)); + has Bli $.bli is relationship(*.bli-id, :model(Bli)); + } + + schema(Bli, Blo).create; + + my $blo = Blo.^create(:value); + my $bli = $blo.bli.^create: :value; + is $bli.blos.head.gist, $blo.gist; + is $blo.bli.gist, $bli.gist; +} + +# TODO: make this work +#subtest "belogs-to using types not using it on attrs", { +# model Blu { ... } +# model Blb { +# has UInt $.id is serial; +# has Str $.value is column; +# has @.blus is relationship(*.blb-id, :model(Blu)); +# has $.one-blu is relationship(*.blb-id, :model(Blu), :has-one); +# } +# +# model Blu { +# has UInt $.id is serial; +# has Str $.value is column; +# has UInt $.blb-id is referencing(*.id, :model(Blb)); +# has $.blb is relationship(*.blb-id, :model(Blb)); +# } +# +# schema(Blb, Blu).create; +# +# my $blu = Blu.^create(:value); +# my $blb = $blu.blb.^create: :value; +# is $blb.blus.head.gist, $blu.gist; +# is $blu.blb.gist, $blb.gist; +#} + subtest "Simple create and fk id", { my $bla = Bla.^create: :value; my $ble = Ble.^create: :value, :bla-id($bla.id); @@ -114,4 +163,5 @@ subtest "Simple create and creating by array", { isa-ok $bla.bles, Ble::ResultSeq; is $bla.bles.map(*.value), ; }; + done-testing;