1#!/usr/bin/perl -w
2
3use strict;
4use Test::More tests => 32;
5
6use lib "t/springfield";
7use Springfield;
8
9for my $fairy ("FaerieHairy", "Faerie") {
10
11#---------------------------------------------------------------------
12# Test simple insertion of an explicitly listed field.
13{
14    my $storage = Springfield::connect_empty();
15
16    my $bert = new $fairy( name => "Bert" );
17    my $bob  = new $fairy( name => "Bob"  );
18    $bert->{friends} = [ "Jesus" # everyone's friend
19		       ];
20    $bob->{friends} = { first => "Buddha" };
21    $bob->{foo} = "bar";
22
23    $storage->insert($bert, $bob);
24
25    ok($storage->id($bert), "Bert got an ID ($fairy)");
26    ok($storage->id($bob), "Bob got an ID ($fairy)");
27}
28
29is(leaked, 0, "leaktest");
30
31# test update of an explicitly listed field, that contains a reference
32# to another object.
33{
34    my $storage = Springfield::connect();
35    my $pixie = $storage->remote($fairy);
36
37    my ($bert) = $storage->select($pixie, $pixie->{name} eq "Bert");
38    ok($bert, "Fetched Bert by name");
39    is($bert->{friends}->[0], "Jesus", "Jesus still Bert's friend");
40
41    my ($bob) = $storage->select($pixie, $pixie->{name} eq "Bob");
42    ok($bob, "Fetched Bob by name");
43    is($bob->{friends}->{first}, "Buddha",
44       "The Buddha still on Bob's side");
45    is($bob->{foo},
46	 (($fairy eq "Faerie") ? "bar" : undef),
47	 "Unknown attribute saved appropriately");
48
49    push @{ $bert->{friends} }, $bob;
50    $bob->{friends}->{second} = $bert;
51
52    #local($Tangram::TRACE)=\*STDERR;
53    $storage->update($bert, $bob);
54
55    delete $bert->{friends};  # break cyclic reference...
56}
57
58is(leaked, 0, "leaktest");
59
60# test that the above worked.
61{
62    my $storage = Springfield::connect();
63    my $pixie = $storage->remote($fairy);
64
65    my ($bert) = $storage->select($pixie, $pixie->{name} eq "Bert");
66    ok($bert, "Fetched Bert by name");
67    ok($bert->{friends}->[1], "Bert has another friend now");
68    is($bert->{friends}->[1]->{name}, "Bob", "Bert's other friend is Bob");
69
70    my ($bob) = $storage->select($pixie, $pixie->{name} eq "Bob");
71    ok($bob, "Fetched Bob by name");
72    ok($bob->{friends}->{second}, "Bob's has another friend now");
73    is($bob->{friends}->{second}, $bert, "Bob's other friend is Bert");
74
75    $storage->update($bert, $bob);
76
77    delete $bert->{friends};  # break cyclic reference...
78}
79
80is(leaked, 0, "leaktest");
81
82}
83