<commit_before>/* This file is part of the YAZ toolkit.
 * Copyright (C) 1995-2011 Index Data
 * See the file LICENSE for details.
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include <yaz/test.h>
#include <yaz/log.h>
#include <yaz/rpn2solr.h>
#include <yaz/wrbuf.h>
#include <yaz/pquery.h>

static int compare(solr_transform_t ct, const char *pqf, const char *solr)
{
    int ret = 0;
    ODR odr = odr_createmem(ODR_ENCODE);
    WRBUF w = wrbuf_alloc();
    Z_RPNQuery *q = p_query_rpn(odr, pqf);

    if (q)
    {
        int r = solr_transform_rpn2solr_wrbuf(ct, w, q);

        if (r != 0)
        {
            /* transform error */
            yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r);
            if (!solr) /* also expected error? */
                ret = 1;
        }
        else if (r == 0)
        {
            yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
            if (solr && !strcmp(wrbuf_cstr(w), solr))
            {
                ret = 1;
            }
        }
    }
    wrbuf_destroy(w);
    odr_destroy(odr);
    return ret;
}

static void tst1(void)
{
    solr_transform_t ct = solr_transform_create();

    YAZ_CHECK(compare(ct, "abc", "abc"));
    YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
#if 0
/* Invalid PQF, so this will never work */
    YAZ_CHECK(compare(ct, "a b", "a b"));
#endif
    YAZ_CHECK(compare(ct, "@not a b", "a AND NOT b"));
    YAZ_CHECK(compare(ct, "@and @or a b c", "(a OR b) AND c"));
    YAZ_CHECK(compare(ct, "@and a b", "a AND b"));
    YAZ_CHECK(compare(ct, "@or a b", "a OR b"));
    YAZ_CHECK(compare(ct, "@attr 1=field abc", "field:abc"));
    YAZ_CHECK(compare(ct, "@attr 1=field \"a b c\"", "field:\"a b c\""));
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */

    solr_transform_define_pattern(ct, "index.title", "1=4");
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));

    solr_transform_define_pattern(ct, "index.foo", "1=bar");
    YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));

    /* Truncation */
    YAZ_CHECK(compare(ct, "@attr 5=1 water", "water*"));
    YAZ_CHECK(compare(ct, "@attr 5=r water", "water*"));
/*    YAZ_CHECK(compare(ct, "@attr t=r water", "water*")); */

    YAZ_CHECK(compare(ct, "@attr 5=2 water", "*water"));

    YAZ_CHECK(compare(ct, "@attr 5=3 water", "*water*"));

    /*
    YAZ_CHECK(compare(ct, "@or @attr 1=1016 water @attr 7=1 @attr 1=4 0", "any:water rank:??");
     */

    solr_transform_close(ct);
}

static void tst2(void)
{
    WRBUF w = wrbuf_alloc();
    solr_transform_t ct = 0;
    const char *srcdir = getenv("srcdir");
    if (srcdir)
    {
        wrbuf_puts(w, srcdir);
        wrbuf_puts(w, "/");
    }
    wrbuf_puts(w, "../etc/pqf.properties");
    
    ct = solr_transform_open_fname(wrbuf_cstr(w));
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
#if 0
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
#endif

    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));<commit_msg>Remove empty lines and out-commented irrelevant test<commit_after>/* This file is part of the YAZ toolkit.
 * Copyright (C) 1995-2011 Index Data
 * See the file LICENSE for details.
 */
#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>

#include <yaz/test.h>
#include <yaz/log.h>
#include <yaz/rpn2solr.h>
#include <yaz/wrbuf.h>
#include <yaz/pquery.h>

static int compare(solr_transform_t ct, const char *pqf, const char *solr)
{
    int ret = 0;
    ODR odr = odr_createmem(ODR_ENCODE);
    WRBUF w = wrbuf_alloc();
    Z_RPNQuery *q = p_query_rpn(odr, pqf);

    if (q)
    {
        int r = solr_transform_rpn2solr_wrbuf(ct, w, q);

        if (r != 0)
        {
            /* transform error */
            yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r);
            if (!solr) /* also expected error? */
                ret = 1;
        }
        else if (r == 0)
        {
            yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
            if (solr && !strcmp(wrbuf_cstr(w), solr))
            {
                ret = 1;
            }
        }
    }
    wrbuf_destroy(w);
    odr_destroy(odr);
    return ret;
}

static void tst1(void)
{
    solr_transform_t ct = solr_transform_create();

    YAZ_CHECK(compare(ct, "abc", "abc"));
    YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
#if 0
/* Invalid PQF, so this will never work */
    YAZ_CHECK(compare(ct, "a b", "a b"));
#endif
    YAZ_CHECK(compare(ct, "@not a b", "a AND NOT b"));
    YAZ_CHECK(compare(ct, "@and @or a b c", "(a OR b) AND c"));
    YAZ_CHECK(compare(ct, "@and a b", "a AND b"));
    YAZ_CHECK(compare(ct, "@or a b", "a OR b"));
    YAZ_CHECK(compare(ct, "@attr 1=field abc", "field:abc"));
    YAZ_CHECK(compare(ct, "@attr 1=field \"a b c\"", "field:\"a b c\""));
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */

    solr_transform_define_pattern(ct, "index.title", "1=4");
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));

    solr_transform_define_pattern(ct, "index.foo", "1=bar");
    YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));

    /* Truncation */
    YAZ_CHECK(compare(ct, "@attr 5=1 water", "water*"));
    YAZ_CHECK(compare(ct, "@attr 5=2 water", "*water"));
    YAZ_CHECK(compare(ct, "@attr 5=3 water", "*water*"));

    /*
    YAZ_CHECK(compare(ct, "@or @attr 1=1016 water @attr 7=1 @attr 1=4 0", "any:water rank:??");
     */

    solr_transform_close(ct);
}

static void tst2(void)
{
    WRBUF w = wrbuf_alloc();
    solr_transform_t ct = 0;
    const char *srcdir = getenv("srcdir");
    if (srcdir)
    {
        wrbuf_puts(w, srcdir);
        wrbuf_puts(w, "/");
    }
    wrbuf_puts(w, "../etc/pqf.properties");
    
    ct = solr_transform_open_fname(wrbuf_cstr(w));
    YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
#if 0
    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
#endif

    YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
{'fraction_non_alphanumeric': 0.185842175066313, 'fraction_numerical': 0.020391246684350134, 'mean_word_length': 2.8549520766773164, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2601861', 'n_tokens_mistral': 2545, 'n_tokens_neox': 2405, 'n_words': 579}