File: 01-mappings.t

package info (click to toggle)
libtangram-perl 2.10-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,184 kB
  • ctags: 702
  • sloc: perl: 9,665; makefile: 9
file content (178 lines) | stat: -rw-r--r-- 3,908 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#  -*- cperl-mode -*-

use strict;

use lib 't/springfield';

use Springfield;

use Test::More tests => 70;

package Vehicle;

sub new
  {
	my $self = bless { }, shift;
  }

sub make
  {
	my $class = shift;
	my $self = bless { }, $class;
	@$self{ $self->fields } = @_;
	return $self;
  }

sub state
  {
	my $self = shift;
	join ' ', ref($self), @$self{ $self->fields };
  }

package Boat;
use base qw( Vehicle );

sub fields { qw( name knots ) }

package Plane;
use base qw( Vehicle );

sub fields { qw( name altitude ) }

package HydroPlane;
use base qw( Boat Plane );

sub fields { qw( name knots altitude whatever ) }

package main;

sub check
  {
	my ($storage, $test_name, $class, @states) = @_;
	my @objs;
	eval {
	    @objs = $storage->select($class);
	};
	is($@, "", "$test_name: selecting $class objects doesn't die");
	is(@objs, @states, "$test_name: correct # of $class objects");

	if (@objs == @states) {
	  my %states;
	  @states{ @states } = ();
	  delete @states{ map { $_->state } @objs };
	  is(keys %states, 0, "$test_name: objects correspond exactly");
	} else {
	SKIP:{
		skip("$test_name: carried error", 1);
	    }
	}
  }

sub test_mapping
  {
	my ($v, $b, $p, $h) = @_;

	my $test_name = "$v$b$p$h";
	
	my $schema = Tangram::Relational->schema
	    ( {
	       control => 'Vehicles',

	       classes =>
	       [
		Vehicle =>
		{
		 table => $v,
		 abstract => 1,
		 fields => { string => [ 'name' ] }
		},

		Boat =>
		{
		 table => $b,
		 bases => [ qw( Vehicle ) ],
		 fields => { int => [ 'knots' ] },
		},

		Plane =>
		{
		 table => $p,
		 bases => [ qw( Vehicle ) ],
		 fields => { int => [ 'altitude' ] },
		},

		HydroPlane =>
		{
		 table => $h,
		 bases => [ qw( Boat Plane ) ],
		 fields => { string => [ 'whatever' ] },
		},
	       ] } );

	use YAML;
	#diag(Dump $schema);

    SKIP: {
	    my $dbh = DBI->connect($Springfield::cs, $Springfield::user,
				   $Springfield::passwd, { PrintError => 0 });

	    # $Tangram::TRACE = \*STDOUT;
	    eval { $Springfield::dialect->retreat($schema, $dbh) };

	    eval { $Springfield::dialect->deploy($schema, $dbh); };
	    is($@, "", "$test_name: deploy succeeded")
		or skip "$test_name: deploy failed", 13;
	    $dbh->disconnect();

	    my $storage = Springfield::connect($schema);

	    # use Data::Dumper;	print Dumper $storage->{engine}->get_polymorphic_select($schema->classdef('Boat'));	die;
	    # my $t = HydroPlane->make(qw(Hydro 5 200 foo)); print Dumper $t; die;

	    eval {
		$storage->insert( Boat->make(qw( Erika 2 )),
				  Plane->make(qw( AF-1 20000 )),
				  HydroPlane->make(qw(Hydro 5 200 foo)) );
	    };
	    is($@, "", "$test_name: Inserting objects doesn't die");

	    check($storage, $test_name,
		  'Boat', 'Boat Erika 2', 'HydroPlane Hydro 5 200 foo');
	    check($storage, $test_name,
		  'Plane', 'Plane AF-1 20000', 'HydroPlane Hydro 5 200 foo');
	    check($storage, $test_name,
		  'HydroPlane', 'HydroPlane Hydro 5 200 foo');
	    check($storage, $test_name,
		  'Vehicle', 'Boat Erika 2', 'Plane AF-1 20000',
		  'HydroPlane Hydro 5 200 foo');

	    $storage->disconnect();
	}
  }

test_mapping('V', 'V', 'V', 'V');
test_mapping('V', 'V', 'V', 'H');
test_mapping('V', 'B', 'V', 'V');
test_mapping('V', 'V', 'P', 'V');
test_mapping('V', 'B', 'P', 'V');
__END__
{
	my $schema = $dialect
	  ->schema( {
				 control => 'Mappings',
				 classes =>
				 [
				  Fruit => { abstract => 1 },
				  Apple => { bases => [ 'Fruit' ] },
				  AppleTree => { fields => { iset => { fruits => 'Apple' } } }
				 ] } );

	$Tangram::TRACE = \*STDOUT;
	$dialect->retreat($schema, $cs, $user, $passwd, { PrintError => 0 });
	$dialect->deploy($schema, $cs, $user, $passwd, { PrintError => 0 });
							  
	my $storage = $dialect->connect($schema, $cs, $user, $passwd);
	$storage->insert( bless { fruits => Set::Object->new( bless { }, 'Apple' ) }, 'AppleTree' );
	$storage->disconnect();
														  
}