Skip to content

Commit

Permalink
Add SplicingIdGeneratorTest class
Browse files Browse the repository at this point in the history
Change-Id: I701283da13e73df3a454c0a6eac8be355864011d
  • Loading branch information
Linary committed May 20, 2021
1 parent 558ab02 commit a9ecf55
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.baidu.hugegraph.unit.id.EdgeIdTest;
import com.baidu.hugegraph.unit.id.IdTest;
import com.baidu.hugegraph.unit.id.IdUtilTest;
import com.baidu.hugegraph.unit.id.SplicingIdGeneratorTest;
import com.baidu.hugegraph.unit.mysql.MysqlUtilTest;
import com.baidu.hugegraph.unit.mysql.WhereBuilderTest;
import com.baidu.hugegraph.unit.rocksdb.RocksDBCountersTest;
Expand Down Expand Up @@ -84,6 +85,7 @@
IdTest.class,
EdgeIdTest.class,
IdUtilTest.class,
SplicingIdGeneratorTest.class,

/* core */
LocksTableTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* 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.
*/

package com.baidu.hugegraph.unit.id;

import java.util.List;

import org.junit.Test;
import org.mockito.Mockito;

import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.id.IdGenerator;
import com.baidu.hugegraph.backend.id.SplicingIdGenerator;
import com.baidu.hugegraph.schema.PropertyKey;
import com.baidu.hugegraph.schema.VertexLabel;
import com.baidu.hugegraph.structure.HugeVertex;
import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.type.define.IdStrategy;
import com.baidu.hugegraph.unit.FakeObjects;
import com.google.common.collect.ImmutableList;

public class SplicingIdGeneratorTest {

@Test
public void testGenerate() {
FakeObjects fakeObjects = new FakeObjects();
PropertyKey name = fakeObjects.newPropertyKey(IdGenerator.of(1),
"name");
VertexLabel vertexLabel = fakeObjects.newVertexLabel(
IdGenerator.of(1L), "fake",
IdStrategy.PRIMARY_KEY, name.id());
HugeVertex vertex = Mockito.mock(HugeVertex.class);
Mockito.when(vertex.schemaLabel()).thenReturn(vertexLabel);
Mockito.when(vertex.name()).thenReturn("marko");
Id vid = SplicingIdGenerator.instance().generate(vertex);
Assert.assertEquals(IdGenerator.of("1:marko"), vid);
}

@Test
public void testConcatIds() {
Assert.assertEquals("a>b>c>d",
SplicingIdGenerator.concat("a", "b", "c", "d"));
Assert.assertEquals("a>`>>c>`>",
SplicingIdGenerator.concat("a", ">", "c", ">"));
Assert.assertEquals("a>b`>c>d",
SplicingIdGenerator.concat("a", "b>c", "d"));
}

@Test
public void testSplitIds() {
Assert.assertArrayEquals(new String[]{"a", "b", "c", "d"},
SplicingIdGenerator.split("a>b>c>d"));
Assert.assertArrayEquals(new String[]{"a", ">", "c", ">"},
SplicingIdGenerator.split("a>`>>c>`>"));
Assert.assertArrayEquals(new String[]{"a", "b>c", "d"},
SplicingIdGenerator.split("a>b`>c>d"));
}

@Test
public void testConcatValues() {
Assert.assertEquals("a!1!c!d",
SplicingIdGenerator.concatValues("a", 1, 'c', "d"));
Assert.assertEquals("a!`!!1!d",
SplicingIdGenerator.concatValues("a", "!", 1, 'd'));
Assert.assertEquals("a!b`!c!d",
SplicingIdGenerator.concatValues("a", "b!c", "d"));

List<Object> values = ImmutableList.of("a", 1, 'c', "d");
Assert.assertEquals("a!1!c!d",
SplicingIdGenerator.concatValues(values));
values = ImmutableList.of("a", "!", 1, 'd');
Assert.assertEquals("a!`!!1!d",
SplicingIdGenerator.concatValues(values));
values = ImmutableList.of("a", "b!c", "d");
Assert.assertEquals("a!b`!c!d",
SplicingIdGenerator.concatValues(values));
}

@Test
public void testSplicing() {
Assert.assertEquals(IdGenerator.of("1:marko"),
SplicingIdGenerator.splicing("1", "marko"));
Assert.assertEquals(IdGenerator.of("book:c:2020"),
SplicingIdGenerator.splicing("book", "c", "2020"));
}

@Test
public void testParse() {
Assert.assertArrayEquals(new String[]{"1", "marko"},
SplicingIdGenerator.parse(
IdGenerator.of("1:marko")));
Assert.assertArrayEquals(new String[]{"book", "c", "2020"},
SplicingIdGenerator.parse(
IdGenerator.of("book:c:2020")));
}
}

0 comments on commit a9ecf55

Please sign in to comment.