<commit_before>package kbasesearchengine.test.main;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static kbasesearchengine.test.common.TestCommon.set;

import java.util.Arrays;
import java.util.Collections;

import org.junit.Test;

import kbasesearchengine.AccessFilter;
import kbasesearchengine.MatchFilter;
import kbasesearchengine.Pagination;
import kbasesearchengine.SearchObjectsInput;
import kbasesearchengine.SearchObjectsOutput;
import kbasesearchengine.authorization.AccessGroupProvider;
import kbasesearchengine.main.SearchMethods;
import kbasesearchengine.search.FoundHits;
import kbasesearchengine.search.IndexingStorage;
import kbasesearchengine.search.PostProcessing;
import kbasesearchengine.search.SortingRule;
import kbasesearchengine.system.TypeStorage;

public class SearchMethodsTest {
    
    private final static PostProcessing PP_DEFAULT = new PostProcessing();
    static {
        PP_DEFAULT.objectData = true;
        PP_DEFAULT.objectInfo = true;
        PP_DEFAULT.objectKeys = true;
    }

    //TODO TEST add a whole lot more tests. Most of SearchMethods is not covered.
    
    @Test
    public void searchObjectsExcludeSubObjects() throws Exception {
        // false cases
        searchObjectsExcludeSubObjects(
                new MatchFilter(),
                new kbasesearchengine.search.MatchFilter());
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(null),
                new kbasesearchengine.search.MatchFilter());
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(0L),
                new kbasesearchengine.search.MatchFilter());
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(2L),
                new kbasesearchengine.search.MatchFilter());
        
        //true case
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(1L),
                new kbasesearchengine.search.MatchFilter().withExcludeSubObjects(true));
    }

    private void searchObjectsExcludeSubObjects(
            final MatchFilter input,
            final kbasesearchengine.search.MatchFilter expected)
            throws Exception {
        final AccessGroupProvider agp = mock(AccessGroupProvider.class);
        final IndexingStorage is = mock(IndexingStorage.class);
        final TypeStorage ts = mock(TypeStorage.class);
        
        final SearchMethods sm = new SearchMethods(agp, is, ts, Collections.emptySet());
        
        // what's returned doesn't matter, we're just checking that indexing storage gets the
        // right message
        
        final SortingRule sr = new SortingRule();
        sr.isTimestamp = true;
        sr.ascending = true;
        
        final FoundHits fh = new FoundHits();
        fh.pagination = null;
        fh.sortingRules = Arrays.asList(sr);
        fh.total = 1;
        fh.guids = set();
        fh.objects = Collections.emptyList();
        
        when(is.searchObjects(
                "Genome",
                expected,
                null, // sort
                new kbasesearchengine.search.AccessFilter().withAccessGroups(set()),
                null, // pagination
                PP_DEFAULT))
                .thenReturn(fh);
        
        final SearchObjectsOutput res = sm.searchObjects(new SearchObjectsInput()
                .withObjectType("Genome")
                .withMatchFilter(input)
                .withAccessFilter(new AccessFilter()),
                "auser");

        assertThat("incorrect objects", res.getObjects(), is(Collections.emptyList()));
        assertThat("incorrect pagination", res.getPagination(), is((Pagination) null));
        // if we want to check search time, need to mock a Clock. Don't bother for now.
//        assertThat("incorrect objects", res.getSearchTime(), is(20));
        // don't care about the sorting rules for this test, so just check size
        assertThat("incorrect sorting rules count", res.getSortingRules().size(), is(1));
        assertThat("incorrect total", res.getTotal(), is(1L));
    }
    
}<commit_msg>Add searchTypes test for excluding subobjects<commit_after>package kbasesearchengine.test.main;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static kbasesearchengine.test.common.TestCommon.set;

import java.util.Arrays;
import java.util.Collections;

import org.junit.Test;

import kbasesearchengine.AccessFilter;
import kbasesearchengine.MatchFilter;
import kbasesearchengine.Pagination;
import kbasesearchengine.SearchObjectsInput;
import kbasesearchengine.SearchObjectsOutput;
import kbasesearchengine.SearchTypesInput;
import kbasesearchengine.SearchTypesOutput;
import kbasesearchengine.authorization.AccessGroupProvider;
import kbasesearchengine.main.SearchMethods;
import kbasesearchengine.search.FoundHits;
import kbasesearchengine.search.IndexingStorage;
import kbasesearchengine.search.PostProcessing;
import kbasesearchengine.search.SortingRule;
import kbasesearchengine.system.TypeStorage;

public class SearchMethodsTest {
    
    private final static PostProcessing PP_DEFAULT = new PostProcessing();
    static {
        PP_DEFAULT.objectData = true;
        PP_DEFAULT.objectInfo = true;
        PP_DEFAULT.objectKeys = true;
    }

    //TODO TEST add a whole lot more tests. Most of SearchMethods is not covered.
    
    @Test
    public void searchObjectsExcludeSubObjects() throws Exception {
        // false cases
        searchObjectsExcludeSubObjects(
                new MatchFilter(),
                new kbasesearchengine.search.MatchFilter());
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(null),
                new kbasesearchengine.search.MatchFilter());
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(0L),
                new kbasesearchengine.search.MatchFilter());
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(2L),
                new kbasesearchengine.search.MatchFilter());
        
        //true case
        searchObjectsExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(1L),
                new kbasesearchengine.search.MatchFilter().withExcludeSubObjects(true));
    }

    private void searchObjectsExcludeSubObjects(
            final MatchFilter input,
            final kbasesearchengine.search.MatchFilter expected)
            throws Exception {
        final AccessGroupProvider agp = mock(AccessGroupProvider.class);
        final IndexingStorage is = mock(IndexingStorage.class);
        final TypeStorage ts = mock(TypeStorage.class);
        
        final SearchMethods sm = new SearchMethods(agp, is, ts, Collections.emptySet());
        
        // what's returned doesn't matter, we're just checking that indexing storage gets the
        // right message
        
        final SortingRule sr = new SortingRule();
        sr.isTimestamp = true;
        sr.ascending = true;
        
        final FoundHits fh = new FoundHits();
        fh.pagination = null;
        fh.sortingRules = Arrays.asList(sr);
        fh.total = 1;
        fh.guids = set();
        fh.objects = Collections.emptyList();
        
        when(is.searchObjects(
                "Genome",
                expected,
                null, // sort
                new kbasesearchengine.search.AccessFilter().withAccessGroups(set()),
                null, // pagination
                PP_DEFAULT))
                .thenReturn(fh);
        
        final SearchObjectsOutput res = sm.searchObjects(new SearchObjectsInput()
                .withObjectType("Genome")
                .withMatchFilter(input)
                .withAccessFilter(new AccessFilter()),
                "auser");

        assertThat("incorrect objects", res.getObjects(), is(Collections.emptyList()));
        assertThat("incorrect pagination", res.getPagination(), is((Pagination) null));
        // if we want to check search time, need to mock a Clock. Don't bother for now.
//        assertThat("incorrect objects", res.getSearchTime(), is(20));
        // don't care about the sorting rules for this test, so just check size
        assertThat("incorrect sorting rules count", res.getSortingRules().size(), is(1));
        assertThat("incorrect total", res.getTotal(), is(1L));
    }
    
    @Test
    public void searchTypesExcludeSubObjects() throws Exception {
        // false cases
        searchTypesExcludeSubObjects(
                new MatchFilter(),
                new kbasesearchengine.search.MatchFilter());
        searchTypesExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(null),
                new kbasesearchengine.search.MatchFilter());
        searchTypesExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(0L),
                new kbasesearchengine.search.MatchFilter());
        searchTypesExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(2L),
                new kbasesearchengine.search.MatchFilter());
        
        //true case
        searchTypesExcludeSubObjects(
                new MatchFilter().withExcludeSubobjects(1L),
                new kbasesearchengine.search.MatchFilter().withExcludeSubObjects(true));
    }

    private void searchTypesExcludeSubObjects(
            final MatchFilter input,
            final kbasesearchengine.search.MatchFilter expected)
            throws Exception {
        final AccessGroupProvider agp = mock(AccessGroupProvider.class);
        final IndexingStorage is = mock(IndexingStorage.class);
        final TypeStorage ts = mock(TypeStorage.class);
        
        final SearchMethods sm = new SearchMethods(agp, is, ts, Collections.emptySet());
        
        // what's returned doesn't matter, we're just checking that indexing storage gets the
        // right message
        
        when(is.searchTypes(
                expected,
                new kbasesearchengine.search.AccessFilter().withAccessGroups(set())))
                .thenReturn(Collections.emptyMap());
        
        final SearchTypesOutput res = sm.searchTypes(new SearchTypesInput()
                .withMatchFilter(input)
                .withAccessFilter(new AccessFilter()),
                "auser");
        
        assertThat("incorrect counts", res.getTypeToCount(), is(Collections.emptyMap()));
        // if we want to check search time, need to mock a Clock. Don't bother for now.
//        assertThat("incorrect objects", res.getSearchTime(), is(20));
    }
    
}
{'fraction_non_alphanumeric': 0.0857483492296405, 'fraction_numerical': 0.001925898752751284, 'mean_word_length': 2.5824572930354797, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4625287', 'n_tokens_mistral': 2963, 'n_tokens_neox': 2818, 'n_words': 637}