PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/t/lib/Mango/Test.pm

https://github.com/gitpan/Mango
Perl | 230 lines | 191 code | 36 blank | 3 comment | 15 complexity | 4e2c2865a5cb592ffa49cdce18af6a7f MD5 | raw file
Possible License(s): AGPL-3.0
  1. # $Id: /local/CPAN/Mango/t/lib/Mango/Test.pm 1644 2008-06-02T01:46:53.055259Z claco $
  2. package Mango::Test;
  3. use strict;
  4. use warnings;
  5. BEGIN {
  6. use Test::More;
  7. use Catalyst::Utils ();
  8. use Catalyst::Helper::Mango ();
  9. use Cwd ();
  10. use DateTime ();
  11. use File::Temp ();
  12. use Path::Class ();
  13. use YAML ();
  14. };
  15. ## cribbed and modified from DBICTest in DBIx::Class tests
  16. sub init_schema {
  17. my ($self, %args) = @_;
  18. my $namespace = $args{'namespace'} || 'Mango::TestSchema';
  19. eval 'use DBD::SQLite';
  20. if ($@) {
  21. BAIL_OUT('DBD::SQLite not installed');
  22. return;
  23. };
  24. eval 'use Mango::Test::Schema';
  25. if ($@) {
  26. BAIL_OUT("Could not load Mango::Test::Schema: $@");
  27. return;
  28. };
  29. my $temp = File::Temp->new(
  30. SUFFIX => '.db',
  31. EXLOCK => 0,
  32. UNLINK => 0
  33. );
  34. my $dsn = "dbi:SQLite:$temp";undef $temp;
  35. my $schema = Mango::Test::Schema->compose_namespace($namespace)->connect(
  36. $dsn, undef, undef, {AutoCommit => 1}
  37. );
  38. $schema->storage->on_connect_do([
  39. 'PRAGMA synchronous = OFF',
  40. 'PRAGMA temp_store = MEMORY'
  41. ]);
  42. __PACKAGE__->deploy_schema($schema, %args);
  43. __PACKAGE__->populate_schema($schema, %args) unless $args{'no_populate'};
  44. return $schema;
  45. };
  46. sub deploy_schema {
  47. my ($self, $schema, %options) = @_;
  48. my $eval = $options{'eval_deploy'};
  49. eval 'use SQL::Translator';
  50. if (!$@ && !$options{'no_deploy'}) {
  51. eval {
  52. $schema->deploy();
  53. };
  54. if ($@ && !$eval) {
  55. die $@;
  56. };
  57. } else {
  58. open IN, Path::Class::file('t', 'sql', 'test.sqlite.sql');
  59. my $sql;
  60. { local $/ = undef; $sql = <IN>; }
  61. close IN;
  62. eval {
  63. ($schema->storage->dbh->do($_) || print "Error on SQL: $_\n") for split(/;\n/, $sql);
  64. };
  65. if ($@ && !$eval) {
  66. die $@;
  67. };
  68. };
  69. };
  70. sub clear_schema {
  71. my ($self, $schema, %options) = @_;
  72. foreach my $source ($schema->sources) {
  73. $schema->resultset($source)->delete_all;
  74. };
  75. };
  76. sub populate_schema {
  77. my ($self, $schema, %options) = @_;
  78. my $date = '2004-07-04 12:00:00';
  79. if ($options{'clear'}) {
  80. $self->clear_schema($schema, %options);
  81. };
  82. $schema->populate('Users', [
  83. [ qw/id username password created updated/ ],
  84. [1,'test1','password1',$date,$date],
  85. [2,'test2','password2',$date,$date],
  86. [3,'test3','password3',$date,$date],
  87. ]);
  88. $schema->populate('Roles', [
  89. [ qw/id name description created updated/ ],
  90. [1,'role1','Role1',$date,$date],
  91. [2,'role2','Role2',$date,$date],
  92. ]);
  93. $schema->populate('UsersRoles', [
  94. [ qw/user_id role_id/ ],
  95. [1,1],
  96. [1,2],
  97. [2,1],
  98. ]);
  99. $schema->populate('Profiles', [
  100. [ qw/id user_id first_name last_name email created updated/ ],
  101. [1,1,'First1', 'Last1', 'email1@example.com', $date,$date],
  102. [2,2,'First2', 'Last2', 'email2@example.com', $date,$date],
  103. ]);
  104. $schema->populate('Carts', [
  105. [ qw/id user_id created updated/ ],
  106. [1,1,$date,$date],
  107. [2,undef,$date,$date],
  108. ]);
  109. $schema->populate('CartItems', [
  110. [ qw/id cart_id sku quantity price description created updated/ ],
  111. [1,1,'ABC-123',1,1.11,'SKU1',$date,$date],
  112. [2,1,'DEF-345',2,2.22,'SKU2',$date,$date],
  113. [3,2,'GHI-678',3,3.33,'SKU3',$date,$date],
  114. ]);
  115. $schema->populate('Wishlists', [
  116. [ qw/id user_id name description created updated/ ],
  117. [1,1,'Wishlist1','First Wishlist',$date,$date],
  118. [2,1,'Wishlist2','Second Wishlist',$date,$date],
  119. [3,2,'Wishlist3','Third Wishlist',$date,$date],
  120. ]);
  121. $schema->populate('WishlistItems', [
  122. [ qw/id wishlist_id sku quantity description created updated/ ],
  123. [1,1,'WABC-123',1,'WSKU1',$date,$date],
  124. [2,1,'WDEF-345',2,'WSKU2',$date,$date],
  125. [3,2,'WGHI-678',3,'WSKU3',$date,$date],
  126. ]);
  127. $schema->populate('Orders', [
  128. [ qw/id user_id type billtofirstname billtolastname billtoaddress1 billtoaddress2 billtoaddress3 billtocity billtostate billtozip billtocountry billtodayphone billtonightphone billtofax billtoemail comments created handling number shipmethod shipping shiptosameasbillto shiptofirstname shiptolastname shiptoaddress1 shiptoaddress2 shiptoaddress3 shiptocity shiptostate shiptozip shiptocountry shiptodayphone shiptonightphone shiptofax shiptoemail subtotal total updated tax/ ],
  129. [1,1,0,'Christopher','Laco','BillToAddress1','BillToAddress2','BillToAddress3','BillToCity','BillToState','BillToZip','BillToCountry','1-111-111-1111','2-222-222-2222','3-333-333-3333','mendlefarg@gmail.com','Comments',$date,8.95,'O123456789','UPS Ground',23.95,0,'Christopher','Laco','ShipToAddress1','ShipToAddress2','ShipToAddress3','ShipToCity','ShipToState','ShipToZip','ShipToCountry','4-444-444-4444','5-555-555-5555','6-666-666-6666','chrislaco@hotmail.com',5.55,37.95,$date, 6.66],
  130. [2,1,1,'Christopher','Laco','BillToAddress1','BillToAddress2','BillToAddress3','BillToCity','BillToState','BillToZip','BillToCountry','1-111-111-1111','2-222-222-2222','3-333-333-3333','mendlefarg@gmail.com','Comments',$date,8.95,'O123456789','UPS Ground',23.95,0,'Christopher','Laco','ShipToAddress1','ShipToAddress2','ShipToAddress3','ShipToCity','ShipToState','ShipToZip','ShipToCountry','4-444-444-4444','5-555-555-5555','6-666-666-6666','chrislaco@hotmail.com',5.55,37.95,$date, 6.66],
  131. [3,2,1,'Christopher','Laco','BillToAddress1','BillToAddress2','BillToAddress3','BillToCity','BillToState','BillToZip','BillToCountry','1-111-111-1111','2-222-222-2222','3-333-333-3333','mendlefarg@gmail.com','Comments',$date,8.95,'O123456789','UPS Ground',23.95,0,'Christopher','Laco','ShipToAddress1','ShipToAddress2','ShipToAddress3','ShipToCity','ShipToState','ShipToZip','ShipToCountry','4-444-444-4444','5-555-555-5555','6-666-666-6666','chrislaco@hotmail.com',5.55,37.95,$date, 6.66]
  132. ]);
  133. $schema->populate('OrderItems', [
  134. [ qw/id order_id sku quantity price total description created updated/ ],
  135. [1,1,'SKU1111',1,1.11,0,'Line Item SKU 1',$date,$date],
  136. [2,1,'SKU2222',2,2.22,0,'Line Item SKU 2',$date,$date],
  137. [3,2,'SKU3333',3,3.33,0,'Line Item SKU 3',$date,$date],
  138. [4,3,'SKU4444',4,4.44,0,'Line Item SKU 4',$date,$date],
  139. [5,3,'SKU1111',5,5.55,0,'Line Item SKU 5',$date,$date]
  140. ]);
  141. $schema->populate('Products', [
  142. [ qw/id sku name description price created updated/ ],
  143. [1,'SKU1111','SKU 1','My SKU 1',1.11,$date,$date],
  144. [2,'SKU2222','SKU 2','My SKU 2',2.22,$date,$date],
  145. [3,'SKU3333','SKU 3','My SKU 3',3.33,$date,$date],
  146. ]);
  147. $schema->populate('ProductAttributes', [
  148. [ qw/id product_id name value created updated/ ],
  149. [1,1,'Attribute1','Value1',$date,$date],
  150. [2,1,'Attribute2','Value2',$date,$date],
  151. [3,3,'Attribute3','Value3',$date,$date],
  152. ]);
  153. $schema->populate('Tags', [
  154. [ qw/id name created updated/ ],
  155. [1,'Tag1',$date,$date],
  156. [2,'Tag2',$date,$date],
  157. [3,'Tag3',$date,$date],
  158. [4,'Tag4',$date,$date],
  159. ]);
  160. $schema->populate('ProductTags', [
  161. [ qw/product_id tag_id/ ],
  162. [1,1],
  163. [1,2],
  164. [3,3],
  165. ]);
  166. };
  167. ## create test catalyst app
  168. sub mk_app {
  169. my $class = $_[0];
  170. my $app = $_[1] || 'TestApp';
  171. my $config = $_[2] || {};
  172. my $prefix = Catalyst::Utils::appprefix($app);
  173. my $cwd = Cwd::cwd;
  174. my $temp = File::Temp::tempdir(
  175. undef,
  176. EXLOCK => 0,
  177. UNLINK => 0,
  178. CLEANUP => 0
  179. );
  180. my @dir = split(/\:\:/, $app);
  181. my $lib = Path::Class::dir($temp, @dir, 'lib');
  182. my $helper = Catalyst::Helper::Mango->new;
  183. chdir $temp;
  184. $helper->mk_app($app);
  185. my $config_file = YAML::LoadFile(
  186. Path::Class::file($app, "$prefix.yml")
  187. );
  188. $config_file->{'connection_info'}->[0] = 'dbi:SQLite:' . Path::Class::file($temp, @dir, 'data', 'mango.db');
  189. my $merged = Catalyst::Utils::merge_hashes($config_file, $config);
  190. YAML::DumpFile(Path::Class::file($app, "$prefix.yml"), $merged);
  191. chdir($cwd);
  192. return Path::Class::dir($temp, @dir);
  193. };
  194. 1;