Search in sources :

Example 16 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method shouldBeAbleToImportAnonymousNodes.

@Test
public void shouldBeAbleToImportAnonymousNodes() throws Exception {
    // GIVEN
    List<String> nodeIds = asList("1", "", "", "", "3", "", "", "", "", "", "5");
    Configuration config = Configuration.COMMAS;
    List<RelationshipDataLine> relationshipData = asList(relationship("1", "3", "KNOWS"));
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", nodeData(true, config, nodeIds, TRUE).getAbsolutePath(), "--relationships", relationshipData(true, config, relationshipData.iterator(), TRUE, true).getAbsolutePath());
    // THEN
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        Iterable<Node> allNodes = db.getAllNodes();
        int anonymousCount = 0;
        for (final String id : nodeIds) {
            if (id.isEmpty()) {
                anonymousCount++;
            } else {
                assertNotNull(Iterators.single(Iterators.filter(nodeFilter(id), allNodes.iterator())));
            }
        }
        assertEquals(anonymousCount, count(Iterators.filter(nodeFilter(""), allNodes.iterator())));
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Configuration(org.neo4j.unsafe.impl.batchimport.input.csv.Configuration) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 17 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method shouldIgnoreWhitespaceInAndAroundIntegerArrays.

@Test
public void shouldIgnoreWhitespaceInAndAroundIntegerArrays() throws Exception {
    // GIVEN
    // Faster to do all successful in one import than in N separate tests
    String[] values = new String[] { "   17", "21", "99   ", "  34  ", "-34", "        -12", "-92 " };
    File data = writeArrayCsv(new String[] { "s:short[]", "b:byte[]", "i:int[]", "l:long[]", "f:float[]", "d:double[]" }, values);
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--quote", "'", "--nodes", data.getAbsolutePath());
    // THEN
    // Expected value for integer types
    String iExpected = "[";
    for (String value : values) {
        iExpected += value.trim() + ", ";
    }
    iExpected = iExpected.substring(0, iExpected.length() - 2) + "]";
    // Expected value for floating point types
    String fExpected = "[";
    for (String value : values) {
        fExpected += Double.valueOf(value.trim()) + ", ";
    }
    fExpected = fExpected.substring(0, fExpected.length() - 2) + "]";
    int nodeCount = 0;
    try (Transaction tx = dbRule.beginTx()) {
        for (Node node : dbRule.getAllNodes()) {
            nodeCount++;
            assertEquals(6, node.getAllProperties().size());
            for (String key : node.getPropertyKeys()) {
                Object things = node.getProperty(key);
                String result = "";
                String expected = iExpected;
                switch(key) {
                    case "s":
                        result = Arrays.toString((short[]) things);
                        break;
                    case "b":
                        result = Arrays.toString((byte[]) things);
                        break;
                    case "i":
                        result = Arrays.toString((int[]) things);
                        break;
                    case "l":
                        result = Arrays.toString((long[]) things);
                        break;
                    case "f":
                        result = Arrays.toString((float[]) things);
                        expected = fExpected;
                        break;
                    case "d":
                        result = Arrays.toString((double[]) things);
                        expected = fExpected;
                        break;
                    default:
                        break;
                }
                assertEquals(expected, result);
            }
        }
        tx.success();
    }
    assertEquals(1, nodeCount);
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 18 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method allNodesById.

private Map<String, Node> allNodesById(GraphDatabaseService db) {
    try (Transaction tx = db.beginTx()) {
        Map<String, Node> nodes = new HashMap<>();
        for (Node node : db.getAllNodes()) {
            nodes.put(idOf(node), node);
        }
        tx.success();
        return nodes;
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) HashMap(java.util.HashMap) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 19 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method shouldBeEquivalentToUseRawAsciiOrCharacterAsQuoteConfiguration2.

@Test
public void shouldBeEquivalentToUseRawAsciiOrCharacterAsQuoteConfiguration2() throws Exception {
    // GIVEN
    // 126 ~ (tilde)
    char weirdDelimiter = 126;
    String weirdStringDelimiter = "~";
    String name1 = weirdDelimiter + "Weird" + weirdDelimiter;
    String name2 = "Start " + weirdDelimiter + "middle thing" + weirdDelimiter + " end!";
    File data = data(":ID,name", "1," + name1, "2," + name2);
    // WHEN given as string
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", data.getAbsolutePath(), "--quote", weirdStringDelimiter);
    // THEN
    assertEquals(weirdStringDelimiter, "" + weirdDelimiter);
    assertEquals(weirdStringDelimiter.charAt(0), weirdDelimiter);
    Set<String> names = asSet("Weird", name2);
    GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = db.beginTx()) {
        for (Node node : db.getAllNodes()) {
            String name = (String) node.getProperty("name");
            assertTrue("Didn't expect node with name '" + name + "'", names.remove(name));
        }
        assertTrue(names.isEmpty());
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 20 with Node

use of org.neo4j.graphdb.Node in project neo4j by neo4j.

the class ImportToolTest method shouldSkipEmptyFiles.

@Test
public void shouldSkipEmptyFiles() throws Exception {
    // GIVEN
    File data = data("");
    // WHEN
    importTool("--into", dbRule.getStoreDirAbsolutePath(), "--nodes", data.getAbsolutePath());
    // THEN
    GraphDatabaseService graphDatabaseService = dbRule.getGraphDatabaseAPI();
    try (Transaction tx = graphDatabaseService.beginTx()) {
        ResourceIterator<Node> allNodes = graphDatabaseService.getAllNodes().iterator();
        assertFalse("Expected database to be empty", allNodes.hasNext());
        tx.success();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) File(java.io.File) Test(org.junit.Test)

Aggregations

Node (org.neo4j.graphdb.Node)1271 Test (org.junit.Test)781 Transaction (org.neo4j.graphdb.Transaction)540 Relationship (org.neo4j.graphdb.Relationship)372 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)146 NotFoundException (org.neo4j.graphdb.NotFoundException)78 File (java.io.File)65 HashMap (java.util.HashMap)57 Label (org.neo4j.graphdb.Label)57 RelationshipType (org.neo4j.graphdb.RelationshipType)57 LinkedList (java.util.LinkedList)56 Path (org.neo4j.graphdb.Path)52 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)46 HashSet (java.util.HashSet)45 Map (java.util.Map)44 WeightedPath (org.neo4j.graphalgo.WeightedPath)37 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)35 ArrayList (java.util.ArrayList)30 List (java.util.List)27 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)26