| 78 | | # TODO: loop the tests for these three test loads |
|---|
| 79 | | ## Test loading object using ID |
|---|
| 80 | | $tmp = Foo->load($foo[0]->id); |
|---|
| 81 | | isa_ok($tmp, 'Foo', 'Loaded Foo #1'); |
|---|
| 82 | | is($tmp->id, $foo[0]->id, 'Loaded Foo #1 has 1 for an id'); |
|---|
| 83 | | is($tmp->name, $foo[0]->name, 'Loaded Foo #1 has the same name as the saved Foo'); |
|---|
| 84 | | is($tmp->text, $foo[0]->text, 'Loaded Foo #1 has the same text as the saved Foo'); |
|---|
| 85 | | is($tmp->status, $foo[0]->status, 'Loaded Foo #1 has the same status as the saved Foo'); |
|---|
| 86 | | is($tmp->created_on, $foo[0]->created_on, 'Loaded Foo #1 has the same created_on as the saved Foo'); |
|---|
| 87 | | is(length($tmp->created_on), 14, "Loaded Foo's created_on is 14 characters"); |
|---|
| 88 | | |
|---|
| 89 | | ## Test loading object using ID in a hash (new in MT 3.0) |
|---|
| 90 | | $tmp = Foo->load({id => $foo[0]->id}); |
|---|
| 91 | | |
|---|
| 92 | | isa_ok($tmp, 'Foo'); |
|---|
| 93 | | is($tmp->id, $foo[0]->id, 'id'); |
|---|
| 94 | | is($tmp->name, $foo[0]->name, 'name'); |
|---|
| 95 | | is($tmp->text, $foo[0]->text, 'text'); |
|---|
| 96 | | is($tmp->status, $foo[0]->status, 'status'); |
|---|
| 97 | | is($tmp->created_on, $foo[0]->created_on, 'created_on'); |
|---|
| 98 | | is(length($tmp->created_on), 14, 'length is 14'); |
|---|
| 99 | | |
|---|
| 100 | | ## Test loading object using ID in a hash, w/other params |
|---|
| 101 | | $tmp = Foo->load({id => $foo[0]->id, name => $foo[0]->name}); |
|---|
| 102 | | isa_ok($tmp, 'Foo'); |
|---|
| 103 | | is($tmp->id, $foo[0]->id, 'id'); |
|---|
| 104 | | is($tmp->name, $foo[0]->name, 'name'); |
|---|
| 105 | | is($tmp->text, $foo[0]->text, 'text'); |
|---|
| 106 | | is($tmp->status, $foo[0]->status, 'status'); |
|---|
| 107 | | is($tmp->created_on, $foo[0]->created_on, 'created_on'); |
|---|
| 108 | | is(length($tmp->created_on), 14, 'length is 14'); |
|---|
| 109 | | |
|---|
| 110 | | ## Test loading object using indexes |
|---|
| 111 | | $tmp = Foo->load({ name => $foo[0]->name }); |
|---|
| 112 | | isa_ok($tmp, 'Foo'); |
|---|
| 113 | | is($tmp->id, $foo[0]->id, 'id'); |
|---|
| 114 | | $foo[1] = Foo->load({ created_on => $foo[0]->created_on }); |
|---|
| 115 | | isa_ok($tmp, 'Foo'); |
|---|
| 116 | | is($tmp->id, $foo[0]->id, 'id'); |
|---|
| 117 | | $tmp = Foo->load({ status => $foo[0]->status }); |
|---|
| 118 | | isa_ok($tmp, 'Foo'); |
|---|
| 119 | | is($tmp->id, $foo[0]->id, 'id'); |
|---|
| | 78 | sub is_first_foo { |
|---|
| | 79 | my ($obj, $name) = @_; |
|---|
| | 80 | |
|---|
| | 81 | isa_ok($obj, 'Foo', $name); |
|---|
| | 82 | isnt($obj, $foo[0], "$name is not the same instance as the first Foo"); |
|---|
| | 83 | |
|---|
| | 84 | # Ignore object columns that have undefined values. |
|---|
| | 85 | # TODO: test that the objects are unequal unless we ignore undef members |
|---|
| | 86 | my %obj_values; |
|---|
| | 87 | while (my ($field, $value) = each %{ $obj->{column_values} }) { |
|---|
| | 88 | $obj_values{$field} = $value if defined $value; |
|---|
| | 89 | } |
|---|
| | 90 | |
|---|
| | 91 | is_deeply(\%obj_values, $foo[0]->{column_values}, "$name is equivalent to the first Foo"); |
|---|
| | 92 | } |
|---|
| | 93 | |
|---|
| | 94 | is_first_foo(scalar Foo->load($foo[0]->id), 'Foo #1 by id'); |
|---|
| | 95 | is_first_foo(scalar Foo->load({ id => $foo[0]->id }), 'Foo #1 by id hash'); |
|---|
| | 96 | is_first_foo(scalar Foo->load({ id => $foo[0]->id, name => $foo[0]->name }), 'Foo #1 by id-name hash'); |
|---|
| | 97 | is_first_foo(scalar Foo->load({ name => $foo[0]->name }), 'Foo #1 by name hash'); |
|---|
| | 98 | is_first_foo(scalar Foo->load({ created_on => $foo[0]->created_on }), 'Foo #1 by created_on hash'); |
|---|
| | 99 | is_first_foo(scalar Foo->load({ status => $foo[0]->status }), 'Foo #1 by status hash'); |
|---|