File: 02-yaml_example.t

package info (click to toggle)
libdbix-class-schema-populatemore-perl 0.19-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 400 kB
  • sloc: perl: 3,412; makefile: 2
file content (205 lines) | stat: -rw-r--r-- 4,425 bytes parent folder | download
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use warnings;
use strict;

use Test::More tests => 31;
use DBIx::Class::Schema::PopulateMore::Test::Schema;
use YAML::Tiny;

## This is just a quick example to show what this might look like if you
## loaded from an external file, like yaml.  It's evil cut 'n paste from
## the standard test.

ok my $schema = DBIx::Class::Schema::PopulateMore::Test::Schema->connect_and_setup
=> 'Got a schema';

diag "Created Datebase with @{$schema->storage->connect_info}[0]";

ok $schema->can('populate_more')
=> 'schema has required method';

ok my @sources = sort($schema->sources)
=> 'got some sources';

is_deeply \@sources, [qw/
    Company CompanyPerson
    EmploymentHistory FriendList 
    Gender Person/]
=> 'Got expected sources';

my $string = join('', <DATA>);

ok my $yaml = YAML::Tiny->read_string( $string )
=> 'loaded yaml config';

ok my %index = $schema->populate_more($yaml->[0])
=> 'Successful populated.';

## Find some Genders

GENDER: {

    ok my $gender_rs = $schema->resultset('Gender')
    => 'Got a resultset of genders';

    is $gender_rs->count, 2
    => 'Got expected number of genders';

    ok $gender_rs->find({label=>'male'})
    => 'Found male';

    ok $gender_rs->find({label=>'female'})
    => 'Found female';

    ok ! $gender_rs->find({label=>'transgender'})
    => 'Correctly didn not find transgender';

}


## Find some People

PERSON: {

    ok my $person_rs = $schema->resultset('Person')
    => 'Got a person resultset';

    is $person_rs->count, 3
    => 'Got expected number of person_rs';

    ok my $john = $person_rs->search({name=>'john'})->first
    => 'Found John';

    is $john->age, 38
    => 'Got correct age for john';

    ok my $jane = $person_rs->search({name=>'jane'})->first
    => 'Found John';

    is $jane->age, 40
    => 'Got correct age for jane';

}

## Find some companies

COMPANY: {

    ok my $company_rs = $schema->resultset('Company')
    => 'Got a person resultset';

    is $company_rs->count, 2
    => 'Got expected number of person_rs';

    ok my $takkle = $company_rs->search({name=>'takkle'})->first
    => 'Found takkle';

    ok my $company_persons_rs = $takkle->company_persons
    => 'got company_persons';

    is $company_persons_rs->count, 2
    => 'got right number of $company_persons_rs';
    
    ok my $employees_rs = $takkle->employees
    => 'got some employees';
    
    ok my $john1 = $employees_rs->search({name=>'john'})->first
    => 'found john';
    
    is $john1->age, 38
    => 'got correct age';
    
    ok my $bms = $company_rs->search({name=>'bristol meyers squibb'})->first
    => 'Found bristol meyers squibb';
    
    is $bms->employees->count, 2
    => 'got correct count for bms employees';
    
}

## Test Friendlist

FRIENDLIST: {

    ok my $friendlist_rs = $schema->resultset('FriendList')
    => 'Got a friendlist resultset';
    
    is $friendlist_rs->count, 3
    => 'Got expected number of friendlist_rs';
    
    ok my $mike = $schema->resultset('Person')->search({name=>'mike'})->first
    => 'found mike';
    
    is $mike->friends, 2
    => 'got correct number of friends for mike';
    
}
__DATA__
---
- Gender:
    data:
      female: female
      male: male
    fields: label
- Person:
    data:
      jane:
        - jane
        - 40
        - '!Index:Gender.female'
      john:
        - john
        - 38
        - '!Index:Gender.male'
    fields:
      - name
      - age
      - gender
- Company:
    data:
      bms:
        - bristol meyers squibb
        -
          - employee: '!Index:Person.john'
          - employee: '!Index:Person.jane'
      takkle:
        - takkle
        -
          - employee: '!Index:Person.john'
            employment_history:
              started: '!Date:january 1, 2000'
    fields:
      - name
      - company_persons
- FriendList:
    data:
      john_jane:
        - '!Index:Person.john'
        - '!Index:Person.jane'
    fields:
      - befriender
      - friendee
- Person:
    data:
      mike:
        - mike
        - 25
        - '!Index:Gender.male'
        -
          - friendee: '!Index:Person.john'
          - friendee: '!Index:Person.jane'
    fields:
      - name
      - age
      - gender
      - friendlist
- CompanyPerson:
    data:
      mike_at_takkle:
        - '!Index:Person.mike'
        - '!Index:Company.takkle'
        - started: '!Date:yesterday'
    fields:
      - employee
      - company
      - employment_history