]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/tests/naco_normalize.t
Wrap upgrade script for 0840 circulation column picker.
[working/Evergreen.git] / Open-ILS / tests / naco_normalize.t
1 use strict;
2 use warnings;
3 use utf8;
4
5 use Test::More tests => 50;
6 use Unicode::Normalize;
7 use DBI;
8
9 use OpenILS::Utils::Normalize qw( naco_normalize );
10
11 # This could be made better in at least one of two ways (or both);
12 # 1. put PL/Perl code that doesn't require a database into external
13 #    modules so that test frameworks can get at it more easily
14 # 2. Build a test harness that knows how to find an Evergreen
15 #    database to use for non-destructive testing.  Of course, there
16 #    can be a chicken-and-egg problem here; also, a complete test
17 #    suite would need to be able to do *destructive* testing, from
18 #    which we'd presumably want to protect production databases.
19
20 # Database connection parameters
21 my $db_driver = 'Pg';
22 my $db_host   = 'evergreen';
23 my $db_port   = '5432';
24 my $db_name   = 'evergreen';
25 my $db_user   = 'evergreen';
26 my $db_pw     = 'evergreen';
27 my $dsn       = "dbi:" . $db_driver . ":dbname=" . $db_name .';host=' . $db_host . ';port=' . $db_port;
28
29 binmode STDOUT, ':utf8';
30 binmode STDERR, ':utf8';
31
32 my @test_cases = (
33     [ 'abc', 'abc', 'regular text' ],
34     [ 'ABC', 'abc', 'regular text' ],
35     [ 'åbçdéñœöîøæÇıÂÅÍÎÏÔÔÒÚÆŒè', 'abcdenoeoioaeciaaiiiooouaeoee', 'European diacritics' ],
36     [ '“‘„«quotes»’”', 'quotes', 'special quotes' ],
37     [ '\98abc\9c def', 'def', 'special non-filing characters designation' ],
38     [ '\9cabcdef', 'abcdef', 'unpaired start of string' ],
39     [ 'ß', 'ss', 'sharp S (eszett)' ],
40     [ 'flfiff', 'flfiff', 'ligatures' ],
41     [ 'ƠơƯư²IJij', 'oouu2ijij', 'NFKD applied correctly' ],
42     [ 'ÆØÞæðøþĐđıŁłŒœʻʼℓ', 'aeothaedothddilloeoel', 'part 3.6' ],
43     [ 'Ð', 'd', 'uppercase eth (missing from 3.6?)' ],
44     [ 'ıİ', 'ii', 'Turkish I' ],
45     [ '[book\'s cover]', 'books cover', 'square brackets and apostrophe' ],
46     [ '  grue   food ', 'grue food', 'trim spaces' ],
47     # note addition of NFKD() to transform expected output
48     [ '한국어 조선말', NFKD('한국어 조선말'), 'Korean text' ],
49     [ '普通話 / 普通话', '普通話 普通话', 'Chinese text' ],
50     [ 'العربية', 'العربية', 'Arabic text' ],
51     [ 'ქართული ენა', 'ქართული ენა', 'Georgian text' ],
52     [ 'русский язык', 'русскии язык', 'Russian text' ],
53     [ "\r\npa\tper\f", 'paper', 'other whitespace' ],
54     [ '#1: ∃ C++, @ home & abroad', '#1 c++ @ home & abroad', 'other punctuation' ],
55     [ '٠١٢٣٤٥', '012345', 'other decimal digits' ],
56     [ '²³¹', '231', 'superscript numbers' ],
57     [ '♭©®♯', '♭ ♯', 'other symbols' ],
58 );
59
60 # test copy of naco_normalize in OpenILS::Utils::Normalize
61 foreach my $case (@test_cases) {
62     is(naco_normalize($case->[0]), $case->[1], $case->[2] . ' (Normalize.pm)');
63 }
64 is(naco_normalize('Smith, Jane. Poet, painter, and author', 'a'),
65     'smith, jane poet painter and author',
66     'retain first comma (Normalize.pm)');
67
68 SKIP: {
69     my $dbh = DBI->connect($dsn, $db_user, $db_pw, {AutoCommit => 1, pg_enable_utf8 => 1, PrintError => 0});
70     skip "Failed to connect to database: $DBI::errstr", 25 if (!defined($dbh));
71
72     # test stored procedure
73     my $sth1 = $dbh->prepare_cached('SELECT public.naco_normalize(?)');
74     my $sth2 = $dbh->prepare_cached('SELECT public.naco_normalize(?, ?)');
75     sub naco_normalize_wrapper {
76         my ($str, $sf) = @_;
77         if (defined $sf) {
78             $sth2->execute($str, $sf);
79             return $sth2->fetchrow_array;
80         } else {
81             $sth1->execute($str);
82             return $sth1->fetchrow_array;
83         }
84     }
85
86     foreach my $case (@test_cases) {
87         is(naco_normalize_wrapper($case->[0]), $case->[1], $case->[2] . ' (stored procedure)');
88     }
89     is(naco_normalize_wrapper('Smith, Jane. Poet, painter, and author', 'a'), 'smith, jane poet painter and author',
90         'retain first comma (stored procedure)');
91 }