Bug 40383: Add unit tests for Koha::Edifact to_json method
This patch adds unit tests for the new to_json method being introduced in Koha::Edifact module in the next patch. The tests verify: - JSON output generation and validity - Proper structure with header, messages, and trailer fields - Message array structure with correct number of messages - Individual message structure (header, segments, trailer) - Segment data integrity including raw data and elements arrays - Line ID functionality for related segments (LIN, QTY, PRI, PIA) Test plan: 1. Apply this patch and the next one 2. Run: prove t/Edifact.t 3. Verify all 63 tests pass 4. Confirm the new JSON-related tests (22 tests) execute successfully Sponsored-by: Martin Renvoize <martin.renvoize@openfifth.co.uk> Signed-off-by: Hannah Dunne-Howrie <hdunne-howrie@westminster.gov.uk> Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com> Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
This commit is contained in:
parent
11de56769b
commit
e8cdc4e9d5
61
t/Edifact.t
61
t/Edifact.t
|
|
@ -4,7 +4,8 @@ use warnings;
|
|||
use FindBin qw( $Bin );
|
||||
|
||||
use Test::NoWarnings;
|
||||
use Test::More tests => 53;
|
||||
use Test::More tests => 75;
|
||||
use JSON qw( decode_json );
|
||||
use Koha::EDI;
|
||||
|
||||
BEGIN {
|
||||
|
|
@ -254,3 +255,61 @@ is( $y, undef, 'LSQ field correctly returns undef when not present for occurrenc
|
|||
# Test LSL field when LSQ is missing
|
||||
$y = $mock_line->girfield( 'sub_location_code', 1 );
|
||||
is( $y, 'REFERENCE', 'LSL field works correctly when LSQ is missing for that occurrence' );
|
||||
|
||||
# Test JSON output functionality
|
||||
my $json_output = $quote->to_json();
|
||||
ok( $json_output, 'JSON output generated' );
|
||||
|
||||
my $parsed_json = decode_json($json_output);
|
||||
ok( $parsed_json, 'JSON output is valid JSON' );
|
||||
|
||||
# Test structure
|
||||
ok( exists $parsed_json->{header}, 'JSON has header field' );
|
||||
ok( exists $parsed_json->{messages}, 'JSON has messages field' );
|
||||
ok( exists $parsed_json->{trailer}, 'JSON has trailer field' );
|
||||
|
||||
# Test header content (should be UNB segment)
|
||||
like( $parsed_json->{header}, qr/^UNB\+/, 'Header starts with UNB' );
|
||||
|
||||
# Test messages array
|
||||
is( ref $parsed_json->{messages}, 'ARRAY', 'Messages is an array' );
|
||||
is( scalar @{ $parsed_json->{messages} }, 1, 'Correct number of messages' );
|
||||
|
||||
# Test first message structure
|
||||
my $first_msg = $parsed_json->{messages}->[0];
|
||||
ok( exists $first_msg->{header}, 'Message has header' );
|
||||
ok( exists $first_msg->{segments}, 'Message has segments' );
|
||||
ok( exists $first_msg->{trailer}, 'Message has trailer' );
|
||||
|
||||
# Test message header/trailer format
|
||||
like( $first_msg->{header}, qr/^UNH\+/, 'Message header starts with UNH' );
|
||||
like( $first_msg->{trailer}, qr/^UNT\+/, 'Message trailer starts with UNT' );
|
||||
|
||||
# Test segments array
|
||||
is( ref $first_msg->{segments}, 'ARRAY', 'Segments is an array' );
|
||||
ok( scalar @{ $first_msg->{segments} } > 0, 'Message has segments' );
|
||||
|
||||
# Find a LIN segment to test line_id functionality
|
||||
my $lin_segment;
|
||||
my $lin_related_segment;
|
||||
for my $seg ( @{ $first_msg->{segments} } ) {
|
||||
if ( $seg->{tag} eq 'LIN' ) {
|
||||
$lin_segment = $seg;
|
||||
} elsif ( $lin_segment && $seg->{tag} =~ /^(QTY|PRI|PIA)$/ && $seg->{line_id} ) {
|
||||
$lin_related_segment = $seg;
|
||||
last;
|
||||
}
|
||||
}
|
||||
|
||||
ok( $lin_segment, 'Found LIN segment in JSON' );
|
||||
if ($lin_segment) {
|
||||
ok( exists $lin_segment->{line_id}, 'LIN segment has line_id' );
|
||||
ok( exists $lin_segment->{raw}, 'LIN segment has raw data' );
|
||||
ok( exists $lin_segment->{elements}, 'LIN segment has elements array' );
|
||||
like( $lin_segment->{raw}, qr/^LIN\+/, 'LIN raw data starts correctly' );
|
||||
}
|
||||
|
||||
ok( $lin_related_segment, 'Found line-related segment with line_id' );
|
||||
if ( $lin_related_segment && $lin_segment ) {
|
||||
is( $lin_related_segment->{line_id}, $lin_segment->{line_id}, 'Line-related segment has matching line_id' );
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue