<commit_before>/*
 * Copyright 2011-2016 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.index;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.util.Collection;

import org.junit.Test;

import com.b2international.index.Fixtures.Data;
import com.b2international.index.Fixtures.PartialData;
import com.b2international.index.mapping.DocumentMapping;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;

public class PartialDocumentLoadingTest extends BaseIndexTest {

	@Override
	protected Collection<Class<?>> getTypes() {
		return ImmutableSet.<Class<?>>of(Data.class);
	}
	
	@Test
	public void loadSingleFieldFromDocumentWithSelect() throws Exception {
		indexDocument(KEY, new Data("field1", "field2"));
		indexDocument(KEY2, new Data("field11", "field21"));
		
		final Iterable<PartialData> hits = search(Query
				.selectPartial(PartialData.class, Data.class)
				.where(Expressions.matchAny(DocumentMapping._ID, ImmutableSet.of(KEY, KEY2)))
				.build());
		
		assertEquals(2, Iterables.size(hits));
		assertEquals(PartialData.class, Iterables.get(hits, 0).getClass());
		assertEquals(PartialData.class, Iterables.get(hits, 1).getClass());
	}
	
	@Test
	public void selectPartialWithFields() throws Exception {
		final Data data1 = new Data("key1Field1", "key1Field2");
		indexDocument(KEY, data1);
		final Data data2 = new Data("key2Field1", "key2Field2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "field1")
			.where(Expressions.matchAll())
			.build();
		final Hits<Data> hits = search(query);
		assertThat(hits).hasSize(2);
		
		Data partialData1 = Iterables.get(hits, 0);
		Data partialData2 = Iterables.get(hits, 1);
		assertEquals("key1Field1", partialData1.getField1());
		assertEquals("key2Field1", partialData2.getField1());
		assertNull(partialData1.getField2());
		assertNull(partialData2.getField2());
	}

}<commit_msg>SO-2138 Add test cases for partial field loading<commit_after>/*
 * Copyright 2011-2017 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.index;

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

import java.math.BigDecimal;
import java.util.Collection;

import org.junit.Test;

import com.b2international.index.Fixtures.Data;
import com.b2international.index.Fixtures.PartialData;
import com.b2international.index.query.Expressions;
import com.b2international.index.query.Query;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;

public class PartialDocumentLoadingTest extends BaseIndexTest {

	/**
	 * @see com.b2international.index.query.DefaultQueryBuilder
	 */
	private static final int DEFAULT_LIMIT = 50;

	@Override
	protected Collection<Class<?>> getTypes() {
		return ImmutableSet.<Class<?>>of(Data.class);
	}
	
	private void checkHits(final Hits<?> hits, int offset, int limit, int total, int returned) {
		assertEquals(offset, hits.getOffset());
		assertEquals(limit, hits.getLimit());
		assertEquals(total, hits.getTotal());
		assertEquals(returned, Iterables.size(hits));
		assertEquals(returned, hits.getHits().size());
	}

	@Test
	public void selectPartialWithClass() throws Exception {
		final Data data1 = new Data();
		data1.setField1("field1_1"); 
		data1.setField2("field2_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setField2("field1_2"); 
		data2.setField2("field2_2");
		indexDocument(KEY2, data2);
		
		final Query<PartialData> query = Query.selectPartial(PartialData.class, Data.class)
				.where(Expressions.matchAll())
				.build();

		final Hits<PartialData> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(data1.getField1(), hits.getHits().get(0).getField1());
		assertEquals(data2.getField1(), hits.getHits().get(1).getField1());
	}

	@Test
	public void selectPartialStringField() throws Exception {
		final Data data1 = new Data();
		data1.setField1("field1_1"); 
		data1.setField2("field2_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setField1("field1_2"); 
		data2.setField2("field2_2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "field1")
				.where(Expressions.matchAll())
				.build();
		
		final Hits<Data> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(data1.getField1(), hits.getHits().get(0).getField1());
		assertEquals(data2.getField1(), hits.getHits().get(1).getField1());
		
		assertNull(hits.getHits().get(0).getField2());
		assertNull(hits.getHits().get(1).getField2());
	}
	
	@Test
	public void selectPartialAnalyzedField() throws Exception {
		final Data data1 = new Data();
		data1.setAnalyzedField("analyzedField_1"); 
		data1.setField1("field1_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setAnalyzedField("analyzedField_2"); 
		data2.setField1("field1_2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "analyzedField")
				.where(Expressions.matchAll())
				.build();
		
		final Hits<Data> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(data1.getAnalyzedField(), hits.getHits().get(0).getAnalyzedField());
		assertEquals(data2.getAnalyzedField(), hits.getHits().get(1).getAnalyzedField());
		
		assertNull(hits.getHits().get(0).getField1());
		assertNull(hits.getHits().get(1).getField1());
	}
	
	@Test
	public void selectPartialBigDecimalField() throws Exception {
		final Data data1 = new Data();
		data1.setBigDecimalField(new BigDecimal("100.987654321")); 
		data1.setField1("field1_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setBigDecimalField(new BigDecimal("200.123456789")); 
		data2.setField1("field1_2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "bigDecimalField")
				.where(Expressions.matchAll())
				.build();
		
		final Hits<Data> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(data1.getBigDecimalField(), hits.getHits().get(0).getBigDecimalField());
		assertEquals(data2.getBigDecimalField(), hits.getHits().get(1).getBigDecimalField());
		
		assertNull(hits.getHits().get(0).getField1());
		assertNull(hits.getHits().get(1).getField1());
	}
	
	@Test
	public void selectPartialFloatFields() throws Exception {
		final Data data1 = new Data();
		data1.setFloatField(64.0f); 
		data1.setFloatWrapper(32.0f); 
		data1.setField1("field1_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setFloatField(16.0f); 
		data2.setFloatWrapper(8.0f); 
		data2.setField1("field1_2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "floatField", "floatWrapper")
				.where(Expressions.matchAll())
				.build();
		
		final Hits<Data> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(Float.floatToRawIntBits(data1.getFloatField()), Float.floatToRawIntBits(hits.getHits().get(0).getFloatField()));
		assertEquals(Float.floatToRawIntBits(data2.getFloatField()), Float.floatToRawIntBits(hits.getHits().get(1).getFloatField()));
		assertEquals(data1.getFloatWrapper(), hits.getHits().get(0).getFloatWrapper()); // Float is already doing bitwise comparison in equals
		assertEquals(data2.getFloatWrapper(), hits.getHits().get(1).getFloatWrapper());
		
		assertNull(hits.getHits().get(0).getField1());
		assertNull(hits.getHits().get(1).getField1());
	}
	
	@Test
	public void selectPartialLongFields() throws Exception {
		final Data data1 = new Data();
		data1.setLongField(64L); 
		data1.setLongWrapper(32L); 
		data1.setField1("field1_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setLongField(16L); 
		data2.setLongWrapper(8L); 
		data2.setField1("field1_2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "longField", "longWrapper")
				.where(Expressions.matchAll())
				.build();
		
		final Hits<Data> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(data1.getLongField(), hits.getHits().get(0).getLongField());
		assertEquals(data2.getLongField(), hits.getHits().get(1).getLongField());
		assertEquals(data1.getLongWrapper(), hits.getHits().get(0).getLongWrapper());
		assertEquals(data2.getLongWrapper(), hits.getHits().get(1).getLongWrapper());
		
		assertNull(hits.getHits().get(0).getField1());
		assertNull(hits.getHits().get(1).getField1());
	}
	
	@Test
	public void selectPartialIntFields() throws Exception {
		final Data data1 = new Data();
		data1.setIntField(64); 
		data1.setIntWrapper(32); 
		data1.setField1("field1_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setIntField(16); 
		data2.setIntWrapper(8); 
		data2.setField1("field1_2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "intField", "intWrapper")
				.where(Expressions.matchAll())
				.build();
		
		final Hits<Data> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(data1.getIntField(), hits.getHits().get(0).getIntField());
		assertEquals(data2.getIntField(), hits.getHits().get(1).getIntField());
		assertEquals(data1.getIntWrapper(), hits.getHits().get(0).getIntWrapper());
		assertEquals(data2.getIntWrapper(), hits.getHits().get(1).getIntWrapper());
		
		assertNull(hits.getHits().get(0).getField1());
		assertNull(hits.getHits().get(1).getField1());
	}
	
	@Test
	public void selectPartialShortFields() throws Exception {
		final Data data1 = new Data();
		data1.setShortField((short) 64); 
		data1.setShortWrapper((short) 32); 
		data1.setField1("field1_1");
		indexDocument(KEY1, data1);
		
		final Data data2 = new Data();
		data2.setShortField((short) 16); 
		data2.setShortWrapper((short) 8); 
		data2.setField1("field1_2");
		indexDocument(KEY2, data2);
		
		final Query<Data> query = Query.selectPartial(Data.class, "shortField", "shortWrapper")
				.where(Expressions.matchAll())
				.build();
		
		final Hits<Data> hits = search(query);
		
		checkHits(hits, 0, DEFAULT_LIMIT, 2, 2);
		assertEquals(data1.getShortField(), hits.getHits().get(0).getShortField());
		assertEquals(data2.getShortField(), hits.getHits().get(1).getShortField());
		assertEquals(data1.getShortWrapper(), hits.getHits().get(0).getShortWrapper());
		assertEquals(data2.getShortWrapper(), hits.getHits().get(1).getShortWrapper());
		
		assertNull(hits.getHits().get(0).getField1());
		assertNull(hits.getHits().get(1).getField1());
	}

}
{'fraction_non_alphanumeric': 0.14496375906023495, 'fraction_numerical': 0.033574939598433724, 'mean_word_length': 5.181256436663234, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, 'https://': 0, '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': '6083128', 'n_tokens_mistral': 4353, 'n_tokens_neox': 3875, 'n_words': 775}