
com.tinkerpop.gremlin.structure.VertexPropertyTest Maven / Gradle / Ivy
The newest version!
package com.tinkerpop.gremlin.structure;
import com.tinkerpop.gremlin.AbstractGremlinTest;
import com.tinkerpop.gremlin.ExceptionCoverage;
import com.tinkerpop.gremlin.FeatureRequirement;
import com.tinkerpop.gremlin.FeatureRequirementSet;
import com.tinkerpop.gremlin.process.T;
import com.tinkerpop.gremlin.util.function.FunctionUtils;
import com.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.*;
/**
* @author Marko A. Rodriguez (http://markorodriguez.com)
*/
@ExceptionCoverage(exceptionClass = Vertex.Exceptions.class, methods = {
"multiplePropertiesExistForProvidedKey",
})
@RunWith(Enclosed.class)
public class VertexPropertyTest extends AbstractGremlinTest {
public static class VertexPropertyAddition extends AbstractGremlinTest {
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_INTEGER_VALUES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_USER_SUPPLIED_IDS)
public void shouldAllowIdAssignment() {
final Vertex v = g.addVertex();
final Object id = graphProvider.convertId(123131231l);
v.property("name", "stephen", T.id, id);
tryCommit(g, g -> assertEquals(id, v.property("name").id()));
}
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
public void shouldSetLabelOfVertexPropertyToKeyOfOwningProperty() {
final Vertex v = g.addVertex("name", "stephen");
tryCommit(g, g -> assertEquals("name", v.property("name").label()));
}
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_INTEGER_VALUES)
public void shouldAddMultiProperties() {
final Vertex v = g.addVertex("name", "marko", "age", 34);
tryCommit(g, g -> {
assertEquals("marko", v.property("name").value());
assertEquals("marko", v.value("name"));
assertEquals(34, v.property("age").value());
assertEquals(34, v.value("age").intValue());
assertEquals(1, IteratorUtils.count(v.iterators().propertyIterator("name")));
assertEquals(2, IteratorUtils.count(v.iterators().propertyIterator()));
assertVertexEdgeCounts(1, 0);
});
final VertexProperty property = v.property("name", "marko a. rodriguez");
tryCommit(g, g -> assertEquals(v, property.element()));
try {
v.property("name");
fail("This should throw a: " + Vertex.Exceptions.multiplePropertiesExistForProvidedKey("name"));
} catch (final Exception e) {
validateException(Vertex.Exceptions.multiplePropertiesExistForProvidedKey("name"), e);
}
assertTrue(v.valueMap().next().get("name").contains("marko"));
assertTrue(v.valueMap().next().get("name").contains("marko a. rodriguez"));
assertEquals(3, IteratorUtils.count(v.iterators().propertyIterator()));
assertEquals(2, IteratorUtils.count(v.iterators().propertyIterator("name")));
assertVertexEdgeCounts(1, 0);
assertEquals(v, v.property("name", "mrodriguez").element());
tryCommit(g, g -> {
assertEquals(3, IteratorUtils.count(v.iterators().propertyIterator("name")));
assertEquals(4, IteratorUtils.count(v.iterators().propertyIterator()));
assertVertexEdgeCounts(1, 0);
});
v.properties("name").sideEffect(meta -> {
meta.get().property("counter", meta.get().value().length());
}).iterate();
tryCommit(g, g -> {
v.properties().forEachRemaining(meta -> {
assertEquals(meta.key(), meta.label());
assertTrue(meta.isPresent());
assertEquals(v, meta.element());
if (meta.key().equals("age")) {
assertEquals(meta.value(), 34);
assertEquals(0, IteratorUtils.count(meta.iterators().propertyIterator()));
}
if (meta.key().equals("name")) {
assertEquals(((String) meta.value()).length(), meta.value("counter").intValue());
assertEquals(1, IteratorUtils.count(meta.iterators().propertyIterator()));
assertEquals(1, meta.keys().size());
assertTrue(meta.keys().contains("counter"));
}
});
assertVertexEdgeCounts(1, 0);
});
}
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_INTEGER_VALUES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_VERTICES)
public void shouldHandleSingleVertexProperties() {
final Vertex v = g.addVertex("name", "marko", "name", "marko a. rodriguez", "name", "marko rodriguez");
tryCommit(g, g -> {
assertEquals(3, IteratorUtils.count(v.iterators().propertyIterator()));
assertEquals(3, IteratorUtils.count(v.iterators().propertyIterator("name")));
final List values = IteratorUtils.list(v.iterators().valueIterator("name"));
assertTrue(values.contains("marko"));
assertTrue(values.contains("marko a. rodriguez"));
assertTrue(values.contains("marko rodriguez"));
});
v.properties("name").remove();
tryCommit(g, g -> {
assertEquals(0, IteratorUtils.count(v.iterators().propertyIterator()));
assertEquals(0, IteratorUtils.count(v.iterators().propertyIterator("name")));
});
v.property("name", "marko");
v.property("name", "marko a. rodriguez");
v.property("name", "marko rodriguez");
tryCommit(g, g -> {
assertEquals(3, IteratorUtils.count(v.iterators().propertyIterator()));
assertEquals(3, IteratorUtils.count(v.iterators().propertyIterator("name")));
final List values = IteratorUtils.list(v.iterators().valueIterator("name"));
assertTrue(values.contains("marko"));
assertTrue(values.contains("marko a. rodriguez"));
assertTrue(values.contains("marko rodriguez"));
});
v.singleProperty("name", "okram", "acl", "private", "date", 2014);
tryCommit(g, g -> {
assertEquals(1, IteratorUtils.count(v.iterators().propertyIterator("name")));
assertEquals(1, IteratorUtils.count(v.iterators().propertyIterator()));
assertEquals(2, IteratorUtils.count(v.iterators().propertyIterator("name").next().iterators().propertyIterator()));
assertEquals("private", v.property("name").valueMap().next().get("acl"));
assertEquals(2014, v.property("name").valueMap().next().get("date"));
});
v.remove();
tryCommit(g, g -> {
assertVertexEdgeCounts(0, 0);
});
final Vertex u = g.addVertex("name", "marko", "name", "marko a. rodriguez", "name", "marko rodriguez");
tryCommit(g);
u.properties().remove();
u.singleProperty("name", "okram", "acl", "private", "date", 2014);
tryCommit(g, g -> {
assertEquals(1, IteratorUtils.count(u.iterators().propertyIterator("name")));
assertEquals(1, IteratorUtils.count(u.iterators().propertyIterator()));
assertEquals(2, IteratorUtils.count(u.iterators().propertyIterator("name").next().iterators().propertyIterator()));
assertEquals("private", u.property("name").valueMap().next().get("acl"));
assertEquals(2014, u.property("name").valueMap().next().get("date"));
});
}
@Test
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_META_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_STRING_VALUES)
@FeatureRequirement(featureClass = Graph.Features.VertexPropertyFeatures.class, feature = Graph.Features.VertexPropertyFeatures.FEATURE_BOOLEAN_VALUES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES)
@FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES)
public void shouldRespectWhatAreEdgesAndWhatArePropertiesInMultiProperties() {
final Vertex marko = g.addVertex("name", "marko");
final Vertex stephen = g.addVertex("name", "stephen");
marko.addEdge("knows", stephen);
final VertexProperty santaFe = marko.property("location", "santa fe", "visible", false);
final VertexProperty newMexico = marko.property("location", "new mexico", "visible", true);
assertEquals(1, IteratorUtils.count(marko.iterators().edgeIterator(Direction.OUT)));
assertEquals(1, IteratorUtils.count(marko.iterators().edgeIterator(Direction.OUT, "knows")));
assertEquals(3, IteratorUtils.count(marko.iterators().propertyIterator()));
assertEquals(2, IteratorUtils.count(marko.iterators().propertyIterator("location")));
assertEquals(1, IteratorUtils.count(marko.iterators().propertyIterator("name")));
assertEquals(1, IteratorUtils.count(stephen.iterators().edgeIterator(Direction.IN)));
assertEquals(1, IteratorUtils.count(stephen.iterators().edgeIterator(Direction.IN, "knows")));
assertEquals(1, IteratorUtils.count(stephen.iterators().propertyIterator()));
assertEquals(1, IteratorUtils.count(stephen.iterators().propertyIterator("name")));
assertEquals(1, IteratorUtils.count(santaFe.iterators().propertyIterator()));
assertEquals(1, IteratorUtils.count(santaFe.iterators().propertyIterator("visible")));
assertEquals(0, IteratorUtils.count(santaFe.iterators().propertyIterator(T.key.getAccessor())));
assertEquals(0, IteratorUtils.count(santaFe.iterators().propertyIterator(T.value.getAccessor())));
assertEquals(1, IteratorUtils.count(newMexico.iterators().propertyIterator()));
assertEquals(1, IteratorUtils.count(newMexico.iterators().propertyIterator("visible")));
assertEquals(0, IteratorUtils.count(newMexico.iterators().propertyIterator(T.key.getAccessor())));
assertEquals(0, IteratorUtils.count(newMexico.iterators().propertyIterator(T.value.getAccessor())));
}
}
public static class VertexPropertyRemoval extends AbstractGremlinTest {
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_PROPERTY)
public void shouldSupportIdempotentVertexPropertyRemoval() {
final Vertex a = g.addVertex("name", "marko");
final Vertex b = g.addVertex("name", "daniel", "name", "kuppitz");
a.property("name").remove();
a.property("name").remove();
a.property("name").remove();
b.properties("name").remove();
b.property("name").remove();
b.properties("name").remove();
b.property("name").remove();
b.properties("name").remove();
b.property("name").remove();
}
@Test
@FeatureRequirementSet(FeatureRequirementSet.Package.VERTICES_ONLY)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_MULTI_PROPERTIES)
@FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_REMOVE_PROPERTY)
public void shouldAllowIteratingAndRemovingVertexPropertyProperties() {
final Vertex daniel = g.addVertex("name", "daniel", "name", "kuppitz", "name", "big d", "name", "the german");
daniel.properties("name")
.sideEffect(vp -> vp.get().
© 2015 - 2025 Weber Informatics LLC | Privacy Policy