t/unit/MGH_Biostat/TravEpi/SimpleRulesBase/Countries.t


#!/usr/bin/env perl
use Modern::Perl '2013';
use Test2::V0;
use Test2::Tools::Spec;

use Set::Tiny 'set';
use MGH_Biostat::TravEpi::SimpleRulesBase::Countries;

# @TODO add a test that pulls the CDC's page of mapping, and compares it to the current one

describe 'load' => sub {
    tests 'loaded' => sub {
        ref_ok( $MGH_Biostat::TravEpi::SimpleRulesBase::Countries::countries, 'HASH', 'country map exists' );
    };

    tests 'subset' => sub {
        my $prior = set( @{ [ keys %{$MGH_Biostat::TravEpi::SimpleRulesBase::Countries::countries} ] } );
        my $after = set( @{ [ values %{$MGH_Biostat::TravEpi::SimpleRulesBase::Countries::countries} ] } );

        is( $after->is_subset($prior), T(), 'mapped countries are a subset of valid countries' );
    };
};

describe 'valid_countries()' => sub {
    tests 'returns the correct set of countries' => sub {
        my $ret    = MGH_Biostat::TravEpi::SimpleRulesBase::Countries::valid_countries();
        my $proper = set( 'afghanistan', 'albania', 'algeria', 'american-samoa', 'andorra', 'anegada', 'angola', 'anguilla', 'antarctica', 'antigua-and-barbuda', 'argentina', 'armenia', 'aruba', 'austral-islands', 'australia', 'austria', 'azerbaijan', 'azores', 'bahrain', 'bangladesh', 'barbados', 'barbuda', 'belarus', 'belgium', 'belize', 'benin', 'bermuda', 'bhutan', 'bolivia', 'bonaire', 'bora-bora', 'bosnia-and-herzegovina', 'botswana', 'brazil', 'british-indian-ocean-territory', 'british-virgin-islands', 'brunei', 'bulgaria', 'burkina-faso', 'burma', 'burundi', 'caicos-islands', 'cambodia', 'cameroon', 'canada', 'canary-islands', 'cape-verde', 'cayman-islands', 'central-african-republic', 'chad', 'chile', 'china', 'christmas-island', 'cocos-islands', 'colombia', 'comoros', 'congo', 'cook-islands', 'costa-rica', 'croatia', 'cuba', 'curacao', 'cyprus', 'czech-republic', 'democratic-republic-of-congo', 'denmark', 'djibouti', 'dominica', 'dominican-republic', 'dubai', 'east-timor', 'easter-island', 'ecuador', 'egypt', 'el-salvador', 'england', 'equatorial-guinea', 'eritrea', 'estonia', 'ethiopia', 'falkland-islands', 'faroe-island', 'fiji', 'finland', 'france', 'french-guiana', 'french-polynesia', 'gabon', 'galapagos-islands', 'georgia', 'germany', 'ghana', 'gibraltar', 'greece', 'greenland', 'grenada', 'grenadines', 'guadeloupe', 'guam', 'guatemala', 'guernsey', 'guinea-bissau', 'guinea', 'guyana', 'haiti', 'holy-see', 'honduras', 'hong-kong-sar', 'hungary', 'iceland', 'india', 'indonesia', 'iran', 'iraq', 'ireland', 'isle-of-man', 'israel', 'italy', 'ivory-coast', 'jamaica', 'japan', 'jersey', 'jordan', 'jost-van-dyke', 'kazakhstan', 'kenya', 'kiribati', 'kosovo', 'kuwait', 'kyrgyzstan', 'laos', 'latvia', 'lebanon', 'lesotho', 'liberia', 'libya', 'liechtenstein', 'lithuania', 'luxembourg', 'macau-sar', 'macedonia', 'madagascar', 'maderia-islands', 'malawi', 'malaysia', 'maldives', 'mali', 'malta', 'marquesas-islands', 'marshall-islands', 'martinique', 'mauritania', 'mauritius', 'mayotte', 'mexico', 'micronesia', 'moldova', 'monaco', 'mongolia', 'montenegro', 'montserrat', 'moorea', 'morocco', 'mozambique', 'namibia', 'nauru', 'nepal', 'netherlands', 'new-caledonia', 'new-zealand', 'nicaragua', 'niger', 'nigeria', 'niue', 'norfolk-island', 'north-korea', 'northern-ireland', 'northern-mariana-islands', 'norway', 'oman', 'pakistan', 'palau', 'panama', 'papua-new-guinea', 'paraguay', 'peru', 'philippines', 'pitcairn-islands', 'poland', 'portugal', 'puerto-rico', 'qatar', 'reunion', 'romania', 'rota', 'rurutu', 'russia', 'rwanda', 'saba', 'saint-barthelemy', 'saint-croix', 'saint-helena', 'saint-john', 'saint-lucia', 'saint-martin', 'saint-pierre-and-miquelon', 'saint-thomas', 'saint-vincent-and-the-grenadines', 'saipan', 'samoa', 'san-marino', 'sao-tome-and-principe', 'saudi-arabia', 'scotland', 'senegal', 'serbia', 'seychelles', 'sierra-leone', 'singapore', 'sint-eustatius', 'sint-maarten', 'slovakia', 'slovenia', 'society-islands', 'solomon-islands', 'somalia', 'south-africa', 'south-georgia-south-sandwich-islands', 'south-korea', 'south-sandwich-islands', 'south-sudan', 'spain', 'sri-lanka', 'st-kitts-and-nevis', 'sudan', 'suriname', 'swaziland', 'sweden', 'switzerland', 'syria', 'tahiti', 'taiwan', 'tajikistan', 'tanzania', 'thailand', 'the-bahamas', 'the-gambia', 'tinian', 'tobago', 'togo', 'tokelau', 'tonga', 'tortola', 'trinidad-and-tobago', 'tubuai', 'tunisia', 'turkey', 'turkmenistan', 'turks-and-caicos', 'tuvalu', 'uganda', 'ukraine', 'united-arab-emirates', 'united-kingdom', 'united-states', 'uruguay', 'usvirgin-islands', 'uzbekistan', 'vanuatu', 'vatican-city', 'venezuela', 'vietnam', 'virgin-gorda', 'wake-island', 'wales', 'western-sahara', 'yemen', 'zambia', 'zanzibar', 'zimbabwe' );
        isa_ok( $ret, [ 'Set::Tiny' ], 'returns a set of the countries' );
        ok( $ret->is_equal($proper), 'which is the correct set' );
    };
};

describe 'mapped_countries()' => sub {
    my $func = \&MGH_Biostat::TravEpi::SimpleRulesBase::Countries::mapped_countries;

    tests 'returns set' => sub {
        my $res = $func->();
        is( $res, object { prop blessed => 'Set::Tiny'; }, 'returns a set' );
    };

    describe 'parameter types' => sub {
        tests 'accepts set' => sub {
            my $res = $func->( set('united-states') );
            is( $res, set('united-states'), 'accepts a set' );
        };
        tests 'accepts arrayref' => sub {
            my $res = $func->( [ 'united-states' ] );
            is( $res, set('united-states'), 'accepts an array ref' );
        };
        tests 'accepts array' => sub {
            my $res = $func->('united-states');
            is( $res, set('united-states'), 'accepts an array' );
        };
    };

    tests 'drops invalid countries' => sub {
        my $res = $func->(qw<foo bar united-states>);
        is( $res, set('united-states'), 'properly drops invalid countries' );
    };

    tests 'remaps countries correctly' => sub {
        my $res = $func->(qw<anegada dubai france holy-see isle-of-man jersey united-states>);
        is( $res, set(qw<british-virgin-islands united-arab-emirates france italy united-kingdom united-states>), 'remaps countries' );
    };
};

done_testing();