Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

In attach_story_data_to_stories(), don't overwrite existing fields #730

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions apps/common/src/perl/MediaWords/DBI/Stories.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ sub attach_story_data_to_stories
{
my ( $stories, $story_data, $list_field ) = @_;

map { $_->{ $list_field } = [] } @{ $stories } if ( $list_field );
if ( $list_field ) {
for my $story ( @{ $stories } ) {
# This subroutine could be called for multiple "story_data" chunks
# with the same "stories" list to attach the chunks to, so the list
# with "list_field" key might already exist
if ( defined $story->{ $list_field } ) {
# Validate that it's actually an arrayref
unless ( ref( $story->{ $list_field } ) eq ref( [] ) ) {
die "One or more stories already have '$list_field' set which is not an arrayref";
}
} else {
$story->{ $list_field } = [];
}
}
}

unless ( scalar @{ $story_data } )
{
Expand Down Expand Up @@ -65,7 +79,17 @@ sub attach_story_data_to_stories
my $sid = $story->{ stories_id };
if ( my $sd = $story_data_lookup->{ $sid } )
{
map { $story->{ $_ } = $sd->{ $_ } } keys( %{ $sd } );
foreach my $story_key ( keys( %{ $sd } ) ) {
if ( defined $list_field and $story_key eq $list_field ) {
$story->{ $story_key } //= [];
foreach my $story_field_value ( @{ $sd->{ $story_key } } ) {
push( @{ $story->{ $story_key } }, $story_field_value );
}
} else {
$story->{ $story_key } = $sd->{ $story_key };
}
}

TRACE "story matched: " . Dumper( $story );
}
}
Expand Down
177 changes: 177 additions & 0 deletions apps/common/tests/perl/MediaWords/DBI/Stories.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
use strict;
use warnings;

use Test::More tests => 2;
use Test::Deep;

use Data::Dumper;

use MediaWords::CommonLibs;
use MediaWords::DBI::Stories;


sub test_attach_story_data_to_stories()
{
my $stories = [
{
'stories_id' => 1,
'title' => 'Foo',
},
{
'stories_id' => 2,
'title' => 'Bar',
},
{
'stories_id' => 3,
'title' => 'Baz',
},
];

my $story_data = [
{
'stories_id' => 1,
'description' => 'foo foo foo',
},
{
'stories_id' => 2,
'description' => 'bar bar bar',
},
{
'stories_id' => 3,
'description' => 'baz baz baz',
},
];

my $got_stories = MediaWords::DBI::Stories::attach_story_data_to_stories( $stories, $story_data );

my $expected_stories = [
{
'stories_id' => 1,
'title' => 'Foo',
'description' => 'foo foo foo',
},
{
'stories_id' => 2,
'title' => 'Bar',
'description' => 'bar bar bar',
},
{
'stories_id' => 3,
'title' => 'Baz',
'description' => 'baz baz baz',
}
];

is_deeply( $got_stories, $expected_stories, "attach_story_data_to_stories()" );
}

sub test_attach_story_data_to_stories_list_field()
{
my $stories = [
{
'stories_id' => 1,
'title' => 'Foo',
},
{
'stories_id' => 2,
'title' => 'Bar',
},
{
'stories_id' => 3,
'title' => 'Baz',
},
];

# Run function with multiple inputs to confirm that existing "attached" data
# doesn't get overwritten

my $story_data_1 = [
{
'stories_id' => 1,
'description' => 'foo 1',
},
{
'stories_id' => 1,
'description' => 'foo 2',
},
{
'stories_id' => 2,
'description' => 'bar 1',
},
];
my $story_data_2 = [
{
'stories_id' => 2,
'description' => 'bar 2',
},
{
'stories_id' => 3,
'description' => 'baz 1',
},
{
'stories_id' => 3,
'description' => 'baz 2',
},
];

my $got_stories;
$got_stories = MediaWords::DBI::Stories::attach_story_data_to_stories( $stories, $story_data_1, 'attached' );
$got_stories = MediaWords::DBI::Stories::attach_story_data_to_stories( $stories, $story_data_2, 'attached' );

my $expected_stories = [
{
'stories_id' => 1,
'title' => 'Foo',
'attached' => [
{
'description' => 'foo 1',
'stories_id' => 1,
},
{
'description' => 'foo 2',
'stories_id' => 1,
}
],
},
{
'stories_id' => 2,
'title' => 'Bar',
'attached' => [
{
'stories_id' => 2,
'description' => 'bar 1',
},
{
'stories_id' => 2,
'description' => 'bar 2',
}
],
},
{
'stories_id' => 3,
'title' => 'Baz',
'attached' => [
{
'stories_id' => 3,
'description' => 'baz 1',
},
{
'stories_id' => 3,
'description' => 'baz 2',
}
],
},
];

is_deeply( $got_stories, $expected_stories, "attach_story_data_to_stories() with list_field" );
}

sub main
{
test_attach_story_data_to_stories();
test_attach_story_data_to_stories_list_field();

done_testing();
}

main();