File Coverage

blib/lib/Test/PONAPI/Repository/MockDB/Loader.pm
Criterion Covered Total %
statement 19 19 100.0
branch 1 2 50.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 27 29 93.1


line stmt bran cond sub pod time code
1             # ABSTRACT: mock repository loader
2             package Test::PONAPI::Repository::MockDB::Loader;
3              
4 8     8   44 use Moose;
  8         18  
  8         82  
5              
6 8     8   56511 use DBI;
  8         19  
  8         633  
7              
8             has dbd => (
9             is => 'ro',
10             isa => 'Str',
11             builder => '_build_dbd',
12             );
13              
14 8     8   6387 use File::Temp qw/tempfile/;
  8         105437  
  8         3034  
15             sub _build_dbd {
16 13     13   93 my ($fh, $path) = tempfile("MockDB.db.XXXXXXX", TMPDIR => 1, UNLINK => 1);
17 13         8655 close $fh;
18 13         660 return "DBI:SQLite:dbname=$path";
19             }
20              
21             has dbh => (
22             is => 'ro',
23             isa => 'DBI::db',
24             lazy => 1,
25             builder => '_build_dbh',
26             );
27              
28             sub _build_dbh {
29 13     13   30 my $self = shift;
30 13 50       572 DBI->connect( $self->dbd, '', '', { RaiseError => 1 } )
31             or die $DBI::errstr;
32             }
33              
34             sub load {
35 13     13 0 35 my $self = shift;
36              
37 13         828 $self->dbh->do($_) for
38             q< DROP TABLE IF EXISTS articles; >,
39             q< CREATE TABLE IF NOT EXISTS articles (
40             id INTEGER PRIMARY KEY AUTOINCREMENT,
41             title CHAR(64) NOT NULL,
42             body TEXT NOT NULL,
43             created DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
44             updated DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
45             status CHAR(10) NOT NULL DEFAULT "pending approval" ); >,
46              
47             map(qq< INSERT INTO articles (title, body, created, updated, status) VALUES $_>,
48             q<("JSON API paints my bikeshed!", "The shortest article. Ever.",
49             "2015-05-22 14:56:29", "2015-05-22 14:56:29", "ok" )>,
50             q<("A second title", "The 2nd shortest article. Ever.",
51             "2015-06-22 14:56:29", "2015-06-22 14:56:29", "ok" )>,
52             q<("a third one", "The 3rd shortest article. Ever.",
53             "2015-07-22 14:56:29", "2015-07-22 14:56:29", "pending approval" ); >),
54              
55             q< DROP TABLE IF EXISTS people; >,
56             q< CREATE TABLE IF NOT EXISTS people (
57             id INTEGER PRIMARY KEY,
58             name CHAR(64) NOT NULL DEFAULT "anonymous",
59             age INTEGER NOT NULL DEFAULT "100",
60             gender CHAR(10) NOT NULL DEFAULT "unknown" ); >,
61              
62             map(qq< INSERT INTO people (id, name, age, gender) VALUES $_>,
63             q<(42, "John", 80, "male")>,
64             q<(88, "Jimmy", 18, "male")>,
65             q<(91, "Diana", 30, "female")>),
66              
67             q< DROP TABLE IF EXISTS rel_articles_people; >,
68             q< CREATE TABLE IF NOT EXISTS rel_articles_people (
69             id_articles INTEGER NOT NULL PRIMARY KEY,
70             id_people INTEGER NOT NULL
71             ); >,
72              
73             map(qq< INSERT INTO rel_articles_people (id_articles, id_people) VALUES $_>,
74             q<(1, 42)>,
75             q<(2, 88)>,
76             q<(3, 91)>),
77              
78             q< DROP TABLE IF EXISTS comments; >,
79             q< CREATE TABLE IF NOT EXISTS comments (
80             id INTEGER PRIMARY KEY,
81             body TEXT NOT NULL DEFAULT "" ); >,
82              
83             map(qq< INSERT INTO comments (id, body) VALUES $_>,
84             q<(5, "First!")>,
85             q<(12, "I like XML better")>),
86              
87             q< DROP TABLE IF EXISTS rel_articles_comments; >,
88             q< CREATE TABLE IF NOT EXISTS rel_articles_comments (
89             id_articles INTEGER NOT NULL,
90             id_comments INTEGER UNIQUE NOT NULL ); >,
91              
92             map(qq< INSERT INTO rel_articles_comments (id_articles, id_comments) VALUES $_>,
93             q<(2, 5)>,
94             q<(2, 12)>);
95             }
96              
97             __PACKAGE__->meta->make_immutable;
98 8     8   64 no Moose; 1;
  8         17  
  8         70  
99              
100             __END__
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Test::PONAPI::Repository::MockDB::Loader - mock repository loader
109              
110             =head1 VERSION
111              
112             version 0.002006
113              
114             =head1 AUTHORS
115              
116             =over 4
117              
118             =item *
119              
120             Mickey Nasriachi <mickey@cpan.org>
121              
122             =item *
123              
124             Stevan Little <stevan@cpan.org>
125              
126             =item *
127              
128             Brian Fraser <hugmeir@cpan.org>
129              
130             =back
131              
132             =head1 COPYRIGHT AND LICENSE
133              
134             This software is copyright (c) 2016 by Mickey Nasriachi, Stevan Little, Brian Fraser.
135              
136             This is free software; you can redistribute it and/or modify it under
137             the same terms as the Perl 5 programming language system itself.
138              
139             =cut