<commit_before>/*
 * Copyright (c) 2015 IBM Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package com.cloudant.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.DesignDocumentManager;
import com.cloudant.client.api.Search;
import com.cloudant.client.api.model.DesignDocument;
import com.cloudant.client.api.model.SearchResult;
import com.cloudant.client.api.model.SearchResult.SearchResultRow;
import com.cloudant.client.org.lightcouch.internal.URIBuilder;
import com.cloudant.test.main.RequiresCloudant;
import com.cloudant.tests.util.CloudantClientResource;
import com.cloudant.tests.util.DatabaseResource;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.RuleChain;

import java.io.File;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Category(RequiresCloudant.class)
public class SearchTests {

    public static CloudantClientResource clientResource = new CloudantClientResource();
    public static DatabaseResource dbResource = new DatabaseResource(clientResource);

    @ClassRule
    public static RuleChain chain = RuleChain.outerRule(clientResource).around(dbResource);

    private static Database db;
    private static CloudantClient account;

    @BeforeClass
    public static void setUp() throws Exception {
        account = clientResource.get();
        db = dbResource.get();

        // replciate the animals db for search tests
        com.cloudant.client.api.Replication r = account.replication();
        r.source("https://clientlibs-test.cloudant.com/animaldb");
        r.createTarget(true);
        r.target(dbResource.getDbURIWithUserInfo());
        r.trigger();

        // sync the design doc for faceted search
        File designDocViews101 =
                new File(String.format("%s/views101_design_doc.js", new File(System.getProperty(
                        "user.dir") + "/src/test/resources/design-docs")));
        DesignDocument designDoc = DesignDocumentManager.fromFile(designDocViews101);
        db.getDesignDocumentManager().put(designDoc);
    }

    @Test
    public void searchCountsTest() {

        // do a faceted search for counts
        Search srch = db.search("views101/animals");
        SearchResult<Animal> rslt = srch.limit(10)
                .includeDocs(true)
                .counts(new String[]{"class", "diet"})
                .querySearchResult("l*", Animal.class);
        assertNotNull(rslt);
        assertNotNull(rslt.getCounts());
        assert (rslt.getCounts().keySet().size() == 2);
        assertEquals(rslt.getCounts().get("class").keySet().size(), 1);<commit_msg>Fixed SearchTests design doc file path<commit_after>/*
 * Copyright (c) 2015 IBM Corp. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package com.cloudant.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import com.cloudant.client.api.CloudantClient;
import com.cloudant.client.api.Database;
import com.cloudant.client.api.DesignDocumentManager;
import com.cloudant.client.api.Search;
import com.cloudant.client.api.model.DesignDocument;
import com.cloudant.client.api.model.SearchResult;
import com.cloudant.client.api.model.SearchResult.SearchResultRow;
import com.cloudant.client.org.lightcouch.internal.URIBuilder;
import com.cloudant.test.main.RequiresCloudant;
import com.cloudant.tests.util.CloudantClientResource;
import com.cloudant.tests.util.DatabaseResource;

import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.RuleChain;

import java.io.File;
import java.net.URI;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

@Category(RequiresCloudant.class)
public class SearchTests {

    public static CloudantClientResource clientResource = new CloudantClientResource();
    public static DatabaseResource dbResource = new DatabaseResource(clientResource);

    @ClassRule
    public static RuleChain chain = RuleChain.outerRule(clientResource).around(dbResource);

    private static Database db;
    private static CloudantClient account;

    @BeforeClass
    public static void setUp() throws Exception {
        account = clientResource.get();
        db = dbResource.get();

        // replciate the animals db for search tests
        com.cloudant.client.api.Replication r = account.replication();
        r.source("https://clientlibs-test.cloudant.com/animaldb");
        r.createTarget(true);
        r.target(dbResource.getDbURIWithUserInfo());
        r.trigger();

        // sync the design doc for faceted search
        File designDocViews101 =
                new File(String.format("%s/views101_design_doc.js", new File(System.getProperty(
                        "user.dir") + "/src/test/resources/design-files")));
        DesignDocument designDoc = DesignDocumentManager.fromFile(designDocViews101);
        db.getDesignDocumentManager().put(designDoc);
    }

    @Test
    public void searchCountsTest() {

        // do a faceted search for counts
        Search srch = db.search("views101/animals");
        SearchResult<Animal> rslt = srch.limit(10)
                .includeDocs(true)
                .counts(new String[]{"class", "diet"})
                .querySearchResult("l*", Animal.class);
        assertNotNull(rslt);
        assertNotNull(rslt.getCounts());
        assert (rslt.getCounts().keySet().size() == 2);
        assertEquals(rslt.getCounts().get("class").keySet().size(), 1);
{'fraction_non_alphanumeric': 0.10481997677119628, 'fraction_numerical': 0.006968641114982578, 'mean_word_length': 3.6547297297297296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3011970', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1876, 'n_words': 488}