/*
* Licensed to CRATE Technology GmbH ("Crate") under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership. Crate licenses
* this file to you 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.
*
* However, if you have executed another commercial license agreement
* with Crate these terms will supersede the license and you may use the
* software solely pursuant to the terms of the relevant commercial agreement.
*/
package io.crate.analyze;
import com.google.common.collect.ImmutableList;
import io.crate.metadata.ColumnIdent;
import org.apache.lucene.util.BytesRef;
import org.junit.Test;
import java.util.ArrayList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
public class IdTest {
@Test
public void testAutoGenerated() throws Exception {
Id id = new Id(ImmutableList.of(new ColumnIdent("_id")), ImmutableList.<BytesRef>of(), new ColumnIdent("_id"));
assertThat(id.values().size(), is(1));
assertThat(id.stringValue(), is(id.values().get(0).utf8ToString()));
}
@Test
public void testAutoGeneratedWithRouting() throws Exception {
Id id = new Id(ImmutableList.of(new ColumnIdent("_id")), ImmutableList.<BytesRef>of(), new ColumnIdent("foo"));
assertThat(id.values().size(), is(1));
assertThat(id.stringValue(), is(id.values().get(0).utf8ToString()));
}
@Test
public void testSinglePrimaryKey() throws Exception {
Id id = new Id(ImmutableList.of(new ColumnIdent("id")), ImmutableList.of(new BytesRef("1")), new ColumnIdent("id"));
assertThat(id.values().size(), is(1));
assertThat(id.stringValue(), is(id.values().get(0).utf8ToString()));
}
@Test (expected = UnsupportedOperationException.class)
public void testSinglePrimaryKeyWithoutValue() throws Exception {
Id id = new Id(ImmutableList.of(new ColumnIdent("id")), ImmutableList.<BytesRef>of(), new ColumnIdent("id"));
}
@Test
public void testMultiplePrimaryKey() throws Exception {
Id id = new Id(ImmutableList.of(new ColumnIdent("id"), new ColumnIdent("name")), ImmutableList.of(new BytesRef("1"), new BytesRef("foo")), null);
assertThat(id.values().size(), is(2));
assertEquals(ImmutableList.of(new BytesRef("1"), new BytesRef("foo")), id.values());
Id id1 = Id.fromString(id.stringValue());
assertEquals(id.values(), id1.values());
}
@Test
public void testMultiplePrimaryKeyWithClusteredBy() throws Exception {
Id id = new Id(ImmutableList.of(new ColumnIdent("id"), new ColumnIdent("name")), ImmutableList.of(new BytesRef("1"), new BytesRef("foo")), new ColumnIdent("name"));
assertThat(id.values().size(), is(2));
assertEquals(ImmutableList.of(new BytesRef("foo"), new BytesRef("1")), id.values());
Id id1 = Id.fromString(id.stringValue());
assertEquals(id.values(), id1.values());
}
@Test
public void testNull() throws Exception {
Id id = new Id(ImmutableList.of(new ColumnIdent("id")), new ArrayList<BytesRef>(){{add(null);}}, new ColumnIdent("id"));
assertThat(id.values().size(), is(0));
assertEquals(null, id.stringValue());
}
}