Subject: | filter_column + 2 * find_or_create always dies if column is uniq |
I have a filter like below for some uniq column (pls see a full test in attached file)
# sub to_storage { $_[1] * 100 }
# sub from_storage { $_[1] / 100 }
$schema->resultset('Item')->find_or_create({id => 1}); $schema->resultset('Item')->find_or_create({id => 1});
That code will die at the 2'nd line because find_or_create doesn't respect a filter and generated SQL will be wrong
# must be '100', not '1'
SELECT me.id FROM items me WHERE ( me.id = ? ): '1'
INSERT INTO items ( id) VALUES ( ? ): '100'
Subject: | dbic-test.log |
SELECT me.id FROM items me WHERE ( me.id = ? ): '3'
INSERT INTO items ( id) VALUES ( ? ): '300'
SELECT me.id FROM items me WHERE ( me.id = ? ): '3'
INSERT INTO items ( id) VALUES ( ? ): '300'
# Failed test 'find_or_create must return a true value'
# at t/model/item.t line 45.
SELECT me.id FROM items me WHERE ( me.id = ? ): '3'
# Failed test 'we've just created id 3 on test above'
# at t/model/item.t line 50.
# Looks like you failed 2 tests of 3.
t/model/item.t ..
ok 1 - ID must transform twice (3->300->3)
not ok 2 - find_or_create must return a true value
not ok 3 - we've just created id 3 on test above
1..3
Dubious, test returned 2 (wstat 512, 0x200)
Failed 2/3 subtests
Subject: | dbix-bug.t |
# In Item.pm
# ...
# __PACKAGE__->load_components("FilterColumn");
# __PACKAGE__->table("items");
# __PACKAGE__->add_columns(
# "id",
# {
# data_type => "integer",
# extra => {unsigned => 1},
# is_auto_increment => 1,
# is_nullable => 0,
# },
# );
# __PACKAGE__->set_primary_key("id");
#
# __PACKAGE__->filter_column(
# id => {
# filter_to_storage => 'to_storage',
# filter_from_storage => 'from_storage',
# }
# );
#
# sub to_storage { $_[1] * 100 }
# sub from_storage { $_[1] / 100 }
# the_test.t
# ... init code is omitted
$schema->resultset('Item')->delete_all;
# 1) DOESN'T WORK
# we're going to insert id 3 and find it after that
my $id_3 = 3;
my $item = $schema->resultset('Item')->find_or_create({id => $id_3});
is $item->id, $id_3, "ID must transform twice ($id_3->300->$id_3)";
# dies !
ok eval { $schema->resultset('Item')->find_or_create({id => $id_3}) },
"find_or_create must return a true value";
CHECK_ID_3: {
my $item = $schema->resultset('Item')->find({id => $id_3});
ok $item && $item->id == $id_3, "we've just created id 3 on test above";
}
# 2) WORKS GREAT only this way
# this way it works
# subtest id_4 => sub {
# $schema->storage->debug(0);
# my $id_4 = 4;
# my $item = $schema->resultset('Item')->new({id => $id_4});
#
# $item->insert;
#
# my $check = $schema->resultset('Item')->new({});
# $check->id($id_4);
# ok $check->get_from_storage;
# };
done_testing;