# t/synsuck.t # # Test LJ::SynSuck. # # Authors: # Afuna # # Copyright (c) 2013 by Dreamwidth Studios, LLC. # # This program is free software; you may redistribute it and/or modify it under # the same terms as Perl itself. For a copy of the license, please reference # 'perldoc perlartistic' or 'perldoc perlgpl'. # use strict; use warnings; use Test::More tests => 24; BEGIN { $LJ::_T_CONFIG = 1; require "$ENV{LJHOME}/cgi-bin/ljlib.pl"; } use LJ::SynSuck; sub err { local $Test::Builder::Level = $Test::Builder::Level + 1; my ( $content, $type, $test ) = @_; subtest "$test (expect err)" => sub { plan tests => 2; my ( $ok, $rv ) = LJ::SynSuck::parse_items_from_feed($content); ok( !$ok, "returned status is an error" ); is( $rv->{type}, $type, $rv->{message} ? "$rv->{message}" : "(no response message)" ); }; } sub success { local $Test::Builder::Level = $Test::Builder::Level + 1; my ( $content, $test, %opts ) = @_; my ( $ok, $rv ); subtest "$test (expect ok)" => sub { plan tests => 1; ( $ok, $rv ) = LJ::SynSuck::parse_items_from_feed( $content, $opts{num_items} ); ok( $ok, "returned status is ok" ); die $rv->{message} unless $ok; }; return @{ $rv->{items} }; } note("Error"); { my $content = q{ Blah </channel> </rss> }; err ( $content, "parseerror", "Mismatched tags" ); } note("No items"); { my $content = q{<?xml version="1.0" encoding="ISO-8859-1"?> <rss version="2.0"> <channel> <title>Title http://www.example.com/ Some Feed Mon, 24 Jan 2011 00:00:00 GMT }; err ( $content, "noitems", "Empty feed" ); } note("RSS pubDate - descending"); { my $content = q { Title http://www.example.com/ Some Feed Mon, 24 Jan 2011 11:06:54 GMT Item 3 http://example.com/feed/3 baz someone 3 Mon, 24 Jan 2011 03:00:00 GMT Item 2 http://example.com/feed/2 bar someone 2 Sun, 23 Jan 2011 05:30:00 GMT Item 1 http://example.com/feed/1 foo someone 1 Mon, 17 Jan 2011 20:00:00 GMT }; my @items = success( $content, "Correct order from RSS pubDate (originally descending)" ); is_deeply( [ map { $_->{id} } @items ], [ 1, 2, 3 ], "Items from feed returned in correct order (originally in descending order)" ); } note("RSS pubDate - ascending"); { my $content = q { Title http://www.example.com/ Some Feed Mon, 24 Jan 2011 11:06:54 GMT Item 1 http://example.com/feed/1 foo someone 1 Mon, 17 Jan 2011 20:00:00 GMT Item 2 http://example.com/feed/2 bar someone 2 Sun, 23 Jan 2011 05:30:00 GMT Item 3 http://example.com/feed/3 baz someone 3 Mon, 24 Jan 2011 03:00:00 GMT }; my @items = success( $content, "Correct order from RSS pubDate (originally ascending)" ); is_deeply( [ map { $_->{id} } @items ], [ 1, 2, 3 ], "Items from feed returned in correct order (originally in ascending order)" ); } note("Atom - descending"); { my $content = q{ Feed title example:atom:feed 2011-01-23T17:38:49-08:00 Item 3 3 2011-01-23T17:38:49-08:00 2011-01-23T17:38:49-08:00 someone baz Item 2 2 2011-01-23T13:59:55-08:00 2011-01-23T13:59:55-08:00 someone bar Item 1 1 2011-01-23T13:58:08-08:00 2011-01-23T13:58:08-08:00 someone foo }; my @items = success( $content, "Correct order from Atom (originally descending)" ); is_deeply( [ map { $_->{id} } @items ], [ 1, 2, 3 ], "Items from feed returned in correct order (originally in descending order)" ); } note("Atom - ascending"); { my $content = q{ Feed title example:atom:feed 2011-01-23T17:38:49-08:00 Item 1 1 2011-01-23T13:58:08-08:00 2011-01-23T13:58:08-08:00 someone foo Item 2 2 2011-01-23T13:59:55-08:00 2011-01-23T13:59:55-08:00 someone bar Item 3 3 2011-01-23T17:38:49-08:00 2011-01-23T17:38:49-08:00 someone baz }; my @items = success( $content, "Correct order from Atom (originally ascending)" ); is_deeply( [ map { $_->{id} } @items ], [ 1, 2, 3 ], "Items from feed returned in correct order (originally in ascending order)" ); } note("RSS dc:date - descending"); { my $content = q { Title http://www.example.com/ Some Feed 2011-01-24T11:06:54Z Item 3 http://example.com/feed/3 baz someone 3 2011-01-24T03:00:00Z Item 2 http://example.com/feed/2 bar someone 2 2011-01-23T05:30:00Z Item 1 http://example.com/feed/1 foo someone 1 2011-01-17T20:00:00Z }; my @items = success( $content, "Correct order from RSS dc:date (originally descending)" ); is_deeply( [ map { $_->{id} } @items ], [ 1, 2, 3 ], "Items from feed returned in correct order (originally in descending order)" ); } note("RSS dc:date - ascending"); { my $content = q { Title http://www.example.com/ Some Feed 2011-01-24T11:06:54Z Item 1 http://example.com/feed/1 foo someone 1 2011-01-17T20:00:00Z Item 2 http://example.com/feed/2 bar someone 2 2011-01-23T05:30:00Z Item 3 http://example.com/feed/3 baz someone 3 2011-01-24T03:00:00Z }; my @items = success( $content, "Correct order from RSS dc:date (originally ascending)" ); is_deeply( [ map { $_->{id} } @items ], [ 1, 2, 3 ], "Items from feed returned in correct order (originally in ascending order)" ); } note("Without datestamp - descending"); { my $content = q{ Feed title example:atom:feed 2011-01-23T17:38:49-08:00 Item 3 3 someone baz Item 2 2 someone bar Item 1 1 someone foo }; my @items = success( $content, "Correct order without datestamps (originally descending)" ); is_deeply( [ map { $_->{id} } @items ], [ 1, 2, 3 ], "Items from feed returned in correct order (originally without datestamps in descending order)" ); } note("Without datestamp - ascending"); { my $content = q{ Feed title example:atom:feed 2011-01-23T17:38:49-08:00 Item 1 1 someone foo Item 2 2 someone bar Item 3 3 someone baz }; my @items = success( $content, "Correct order without datestamps (originally ascending)" ); is_deeply( [ map { $_->{id} } @items ], [ 3, 2, 1 ], "Items from feed returned in what we guessed is the correct order (originally without datestamps in ascending order)" ); } note("Active feed - too many items - descending"); { my $content = q { Title http://www.example.com/ Some Feed Mon, 24 Jan 2011 11:06:54 GMT Item 3 http://example.com/feed/3 baz someone 3 Mon, 24 Jan 2011 03:00:00 GMT Item 2 http://example.com/feed/2 bar someone 2 Sun, 23 Jan 2011 05:30:00 GMT Item 1 http://example.com/feed/1 foo someone 1 Mon, 17 Jan 2011 20:00:00 GMT }; my @items = success( $content, "Latest two items in the feed", num_items => 2 ); is_deeply( [ map { $_->{id} } @items ], [ 2, 3 ], "Returned latest two items from feed (originally in descending order)" ); } note("Active feed - too many items - ascending"); { my $content = q { Title http://www.example.com/ Some Feed Mon, 24 Jan 2011 11:06:54 GMT Item 1 http://example.com/feed/1 foo someone 1 Mon, 17 Jan 2011 20:00:00 GMT Item 2 http://example.com/feed/2 bar someone 2 Sun, 23 Jan 2011 05:30:00 GMT Item 3 http://example.com/feed/3 baz someone 3 Mon, 24 Jan 2011 03:00:00 GMT }; my @items = success( $content, "Latest two items in the feed", num_items => 2 ); is_deeply( [ map { $_->{id} } @items ], [ 2, 3 ], "Returned latest two items from feed (originally in ascending order)" ); } note("Active feed - too many items - no datestamp ascending"); { my $content = q { Title http://www.example.com/ Some Feed Mon, 24 Jan 2011 11:06:54 GMT Item 1 http://example.com/feed/1 foo someone 1 Item 2 http://example.com/feed/2 bar someone 2 Item 3 http://example.com/feed/3 baz someone 3 }; my @items = success( $content, "Latest two items in the feed (guessed)", num_items => 2 ); is_deeply( [ map { $_->{id} } @items ], [ 2, 1 ], "Returned what we guessed are the latest two items from feed (originally without datestamps in ascending order)" ); }