Sanify MC test and correct the plan
[dbsrgits/DBIx-Class-Historic.git] / t / 96multi_create.t
index cf77442..0ef9e80 100644 (file)
@@ -6,11 +6,13 @@ use Test::Exception;
 use lib qw(t/lib);
 use DBICTest;
 
-plan tests => 104;
+sub mc_diag { diag (@_) if $ENV{DBIC_MULTICREATE_DEBUG} };
+
+plan tests => 84;
 
 my $schema = DBICTest->init_schema();
 
-diag '* simple create + parent (the stuff $rs belongs_to)';
+mc_diag '* simple create + parent (the stuff $rs belongs_to)';
 eval {
   my $cd = $schema->resultset('CD')->create({
     artist => { 
@@ -26,7 +28,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* same as above but the child and parent have no values, except for an explicit parent pk';
+mc_diag '* same as above but the child and parent have no values, except for an explicit parent pk';
 eval {
   my $bm_rs = $schema->resultset('Bookmark');
   my $bookmark = $bm_rs->create({
@@ -48,7 +50,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )';
+mc_diag '* create over > 1 levels of has_many create (A => { has_many => { B => has_many => C } } )';
 eval {
   my $artist = $schema->resultset('Artist')->first;
   my $cd = $artist->create_related (cds => {
@@ -82,7 +84,7 @@ throws_ok (
   'create via update of multi relationships throws an exception'
 );
 
-diag '* Create m2m while originating in the linker table';
+mc_diag '* Create m2m while originating in the linker table';
 eval {
   my $artist = $schema->resultset('Artist')->first;
   my $c2p = $schema->resultset('CD_to_Producer')->create ({
@@ -91,9 +93,9 @@ eval {
       title => 'Bad investment',
       year => 2008,
       tracks => [
-        { position => 1, title => 'Just buy' },
-        { position => 2, title => 'Why did we do it' },
-        { position => 3, title => 'Burn baby burn' },
+        { title => 'Just buy' },
+        { title => 'Why did we do it' },
+        { title => 'Burn baby burn' },
       ],
     },
     producer => {
@@ -112,7 +114,7 @@ eval {
 };
 diag $@ if $@;
 
-diag (<<'DG');
+mc_diag (<<'DG');
 * Create over > 1 levels of might_have with multiple has_many and multiple m2m
 but starting at a has_many level
 
@@ -133,19 +135,17 @@ eval {
     year => 2008,
     tracks => [
       {
-        position => 1, # some day me might test this with Ordered
         title => 'Off by one again',
       },
       {
-        position => 2,
         title => 'The dereferencer',
         cd_single => {
           artist => $artist,
           year => 2008,
           title => 'Was that a null (Single)',
           tracks => [
-            { title => 'The dereferencer', position => 1 },
-            { title => 'The dereferencer II', position => 2 },
+            { title => 'The dereferencer' },
+            { title => 'The dereferencer II' },
           ],
           cd_to_producer => [
             {
@@ -188,7 +188,7 @@ eval {
 };
 diag $@ if $@;
 
-diag (<<'DG');
+mc_diag (<<'DG');
 * Same as above but starting at the might_have directly
 
 Track -> might have -> Single -> has_many -> Tracks
@@ -204,16 +204,15 @@ eval {
   my $cd = $schema->resultset('CD')->first;
   my $track = $schema->resultset('Track')->create ({
     cd => $cd,
-    position => 77,  # some day me might test this with Ordered
     title => 'Multicreate rocks',
     cd_single => {
       artist => $cd->artist,
       year => 2008,
       title => 'Disemboweling MultiCreate',
       tracks => [
-        { title => 'Why does mst write this way', position => 1 },
-        { title => 'Chainsaw celebration', position => 2 },
-        { title => 'Purl cleans up', position => 3 },
+        { title => 'Why does mst write this way' },
+        { title => 'Chainsaw celebration' },
+        { title => 'Purl cleans up' },
       ],
       cd_to_producer => [
         {
@@ -254,7 +253,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
+mc_diag '* Test might_have again but with a PK == FK in the middle (obviously not specified)';
 eval {
   my $artist = $schema->resultset('Artist')->first;
   my $cd = $schema->resultset('CD')->create ({
@@ -298,12 +297,11 @@ eval {
 };
 diag $@ if $@;
 
-diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
+mc_diag '* Test might_have again but with just a PK and FK (neither specified) in the mid-table';
 eval {
   my $cd = $schema->resultset('CD')->first;
   my $track = $schema->resultset ('Track')->create ({
     cd => $cd,
-    position => 66,
     title => 'Black',
     lyrics => {
       lyric_versions => [
@@ -344,7 +342,7 @@ eval {
 };
 diag $@ if $@;
 
-diag (<<'DG');
+mc_diag (<<'DG');
 * Test a multilevel might-have with a PK == FK in the might_have/has_many table
 
 CD -> might have -> Artwork
@@ -395,7 +393,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* Nested find_or_create';
+mc_diag '* Nested find_or_create';
 eval {
   my $newartist2 = $schema->resultset('Artist')->find_or_create({ 
     name => 'Fred 3',
@@ -410,7 +408,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* Multiple same level has_many create';
+mc_diag '* Multiple same level has_many create';
 eval {
   my $artist2 = $schema->resultset('Artist')->create({
     name => 'Fred 4',
@@ -432,7 +430,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* First create_related pass';
+mc_diag '* First create_related pass';
 eval {
        my $artist = $schema->resultset('Artist')->first;
        
@@ -441,13 +439,8 @@ eval {
                title => 'TestOneCD1',
                year => 2007,
                tracks => [
-               
-                       { position=>111,
-                         title => 'TrackOne',
-                       },
-                       { position=>112,
-                         title => 'TrackTwo',
-                       }
+                       { title => 'TrackOne' },
+                       { title => 'TrackTwo' },
                ],
 
        });
@@ -466,7 +459,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* second create_related with same arguments';
+mc_diag '* second create_related with same arguments';
 eval {
        my $artist = $schema->resultset('Artist')->first;
        
@@ -475,13 +468,8 @@ eval {
                title => 'TestOneCD2',
                year => 2007,
                tracks => [
-               
-                       { position=>111,
-                         title => 'TrackOne',
-                       },
-                       { position=>112,
-                         title => 'TrackTwo',
-                       }
+                       { title => 'TrackOne' },
+                       { title => 'TrackTwo' },
                ],
 
     liner_notes => { notes => 'I can haz liner notes?' },
@@ -503,7 +491,7 @@ eval {
 };
 diag $@ if $@;
 
-diag '* create of parents of a record linker table';
+mc_diag '* create of parents of a record linker table';
 eval {
   my $cdp = $schema->resultset('CD_to_Producer')->create({
     cd => { artist => 1, title => 'foo', year => 2000 },
@@ -513,9 +501,6 @@ eval {
 };
 diag $@ if $@;
 
-TODO: {
-local $TODO = 'Next 2 evals are NOT supposed to work, jnaps code will be torn to bits in another branch';
-#SPECIAL_CASE
 eval {
   my $kurt_cobain = { name => 'Kurt Cobain' };
 
@@ -536,7 +521,8 @@ eval {
 };
 diag $@ if $@;
 
-#SPECIAL_CASE2
+=pod
+# This test case has been moved to t/96multi_create/cd_single.t
 eval {
   my $pink_floyd = { name => 'Pink Floyd' };
 
@@ -552,10 +538,11 @@ eval {
   is($a->cds && $a->cds->first->title, 'The Wall', 'CD insertion ok');
 };
 diag $@ if $@;
-}
-
+=cut
 
-diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
+mc_diag '* Create foreign key col obj including PK (See test 20 in 66relationships.t)';
+## Create foreign key col obj including PK
+## See test 20 in 66relationships.t
 eval {
   my $new_cd_hashref = { 
     cdid => 27, 
@@ -583,7 +570,7 @@ eval {
 };
 is($@, '', 'new cd created without clash on related artist');
 
-diag '* Make sure exceptions from errors in created rels propogate';
+mc_diag '* Make sure exceptions from errors in created rels propogate';
 eval {
     my $t = $schema->resultset("Track")->new({ cd => { artist => undef } });
     #$t->cd($t->new_related('cd', { artist => undef } ) );
@@ -592,7 +579,7 @@ eval {
 };
 like($@, qr/cd.artist may not be NULL/, "Exception propogated properly");
 
-diag '* Test multi create over many_to_many';
+mc_diag '* Test multi create over many_to_many';
 eval {
   $schema->resultset('CD')->create ({
     artist => {
@@ -611,217 +598,4 @@ eval {
   is ($m2m_cd->first->producers->first->name, 'Cowboy Neal', 'Correct producer row created');
 };
 
-diag '* And the insane multicreate';
-# (should work, despite the fact that no one will probably use it this way)
-
-# first count how many rows do we initially have
-my $counts;
-$counts->{$_} = $schema->resultset($_)->count for qw/Artist CD Genre Producer Tag/;
-
-# do the crazy create
-eval {
-  $schema->resultset('CD')->create ({
-    artist => {
-      name => 'james',
-    },
-    title => 'Greatest hits 1',
-    year => '2012',
-    genre => {
-      name => '"Greatest" collections',
-    },
-    tags => [
-      { tag => 'A' },
-      { tag => 'B' },
-    ],
-    cd_to_producer => [
-      {
-        producer => {
-          name => 'bob',
-          producer_to_cd => [
-            {
-              cd => { 
-                artist => {
-                  name => 'lars',
-                  cds => [
-                    {
-                      title => 'Greatest hits 2',
-                      year => 2012,
-                      genre => {
-                        name => '"Greatest" collections',
-                      },
-                      tags => [
-                        { tag => 'A' },
-                        { tag => 'B' },
-                      ],
-                      # This cd is created via artist so it doesn't know about producers
-                      cd_to_producer => [
-                        # if we specify 'bob' here things bomb
-                        # as the producer attached to Greatest Hits 1 is
-                        # already created, but not yet inserted.
-                        # Maybe this can be fixed, but things are hairy
-                        # enough already.
-                        #
-                        #{ producer => { name => 'bob' } },
-                        { producer => { name => 'paul' } },
-                        { producer => {
-                          name => 'flemming',
-                          producer_to_cd => [
-                            { cd => {
-                              artist => {
-                                name => 'kirk',
-                                cds => [
-                                  {
-                                    title => 'Greatest hits 3',
-                                    year => 2012,
-                                    genre => {
-                                      name => '"Greatest" collections',
-                                    },
-                                    tags => [
-                                      { tag => 'A' },
-                                      { tag => 'B' },
-                                    ],
-                                  },
-                                  {
-                                    title => 'Greatest hits 4',
-                                    year => 2012,
-                                    genre => {
-                                      name => '"Greatest" collections2',
-                                    },
-                                    tags => [
-                                      { tag => 'A' },
-                                      { tag => 'B' },
-                                    ],
-                                  },
-                                ],
-                              },
-                              title => 'Greatest hits 5',
-                              year => 2013,
-                              genre => {
-                                name => '"Greatest" collections2',
-                              },
-                            }},
-                          ],
-                        }},
-                      ],
-                    },
-                  ],
-                },
-                title => 'Greatest hits 6',
-                year => 2012,
-                genre => {
-                  name => '"Greatest" collections',
-                },
-                tags => [
-                  { tag => 'A' },
-                  { tag => 'B' },
-                ],
-              },
-            },
-            {
-              cd => { 
-                artist => {
-                  name => 'lars',    # should already exist
-                  # even though the artist 'name' is not uniquely constrained
-                  # find_or_create will arguably DWIM 
-                },
-                title => 'Greatest hits 7',
-                year => 2013,
-              },
-            },
-          ],
-        },
-      },
-    ],
-  });
-
-  is ($schema->resultset ('Artist')->count, $counts->{Artist} + 3, '3 new artists created');
-  is ($schema->resultset ('Genre')->count, $counts->{Genre} + 2, '2 additional genres created');
-  is ($schema->resultset ('Producer')->count, $counts->{Producer} + 3, '3 new producer');
-  is ($schema->resultset ('CD')->count, $counts->{CD} + 7, '7 new CDs');
-  is ($schema->resultset ('Tag')->count, $counts->{Tag} + 10, '10 new Tags');
-
-  my $cd_rs = $schema->resultset ('CD')
-    ->search ({ title => { -like => 'Greatest hits %' }}, { order_by => 'title'} );
-  is ($cd_rs->count, 7, '7 greatest hits created');
-
-  my $cds_2012 = $cd_rs->search ({ year => 2012});
-  is ($cds_2012->count, 5, '5 CDs created in 2012');
-
-  is (
-    $cds_2012->search(
-      { 'tags.tag' => { -in => [qw/A B/] } },
-      { join => 'tags', group_by => 'me.cdid' }
-    ),
-    5,
-    'All 10 tags were pairwise distributed between 5 year-2012 CDs'
-  );
-
-  my $paul_prod = $cd_rs->search (
-    { 'producer.name' => 'paul'},
-    { join => { cd_to_producer => 'producer' } }
-  );
-  is ($paul_prod->count, 1, 'Paul had 1 production');
-  my $pauls_cd = $paul_prod->single;
-  is ($pauls_cd->cd_to_producer->count, 2, 'Paul had one co-producer');
-  is (
-    $pauls_cd->search_related ('cd_to_producer',
-      { 'producer.name' => 'flemming'},
-      { join => 'producer' }
-    )->count,
-    1,
-    'The second producer is flemming',
-  );
-
-  my $kirk_cds = $cd_rs->search ({ 'artist.name' => 'kirk' }, { join => 'artist' });
-  is ($kirk_cds, 3, 'Kirk had 3 CDs');
-  is (
-    $kirk_cds->search (
-      { 'cd_to_producer.cd' => { '!=', undef } },
-      { join => 'cd_to_producer' },
-    ),
-    1,
-    'Kirk had a producer only on one cd',
-  );
-
-  my $lars_cds = $cd_rs->search ({ 'artist.name' => 'lars' }, { join => 'artist' });
-  is ($lars_cds->count, 3, 'Lars had 3 CDs');
-  is (
-    $lars_cds->search (
-      { 'cd_to_producer.cd' => undef },
-      { join => 'cd_to_producer' },
-    ),
-    0,
-    'Lars always had a producer',
-  );
-  is (
-    $lars_cds->search_related ('cd_to_producer',
-      { 'producer.name' => 'flemming'},
-      { join => 'producer' }
-    )->count,
-    1,
-    'Lars produced 1 CD with flemming',
-  );
-  is (
-    $lars_cds->search_related ('cd_to_producer',
-      { 'producer.name' => 'bob'},
-      { join => 'producer' }
-    )->count,
-    2,
-    'Lars produced 2 CDs with bob',
-  );
-
-  my $bob_prod = $cd_rs->search (
-    { 'producer.name' => 'bob'},
-    { join => { cd_to_producer => 'producer' } }
-  );
-  is ($bob_prod->count, 3, 'Bob produced a total of 3 CDs');
-
-  is (
-    $bob_prod->search ({ 'artist.name' => 'james' }, { join => 'artist' })->count,
-    1,
-    "Bob produced james' only CD",
-  );
-};
-diag $@ if $@;
-
 1;