Search in sources :

Example 1 with Voter

use of org.neo4j.ogm.domain.election.Voter in project neo4j-ogm by neo4j.

the class RelationshipMappingTest method shouldNotDropUnmappedRelationshipModels.

// GH-727
@Test
public void shouldNotDropUnmappedRelationshipModels() {
    Session session = sessionFactory.openSession();
    Voter voter = new Voter("V");
    voter.candidateVotedFor = new Candidate("C");
    session.save(voter);
    session = sessionFactory.openSession();
    Iterable<Map<String, Object>> results = session.query("MATCH (v) - [r] - (c) WHERE id(v) = $id RETURN r", Collections.singletonMap("id", voter.getId())).queryResults();
    assertThat(results).hasSize(1);
    Map<String, Object> row = results.iterator().next();
    assertThat(row).containsKeys("r");
    assertThat(row.get("r")).isNotNull().isInstanceOf(RelationshipModel.class);
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) Voter(org.neo4j.ogm.domain.election.Voter) Map(java.util.Map) Session(org.neo4j.ogm.session.Session) Test(org.junit.Test)

Example 2 with Voter

use of org.neo4j.ogm.domain.election.Voter in project neo4j-ogm by neo4j.

the class RelationshipMappingTest method shouldAllowVoterToChangeHerMind.

@Test
public void shouldAllowVoterToChangeHerMind() {
    Iterable<Map<String, Object>> executionResult = session.query("CREATE " + "(a:Voter:Candidate {name:'A'}), " + "(b:Voter:Candidate {name:'B'}), " + "(v:Voter {name:'V'})-[:CANDIDATE_VOTED_FOR]->(b) " + "RETURN id(a) AS a_id, id(b) AS b_id, id(v) AS v_id", emptyMap()).queryResults();
    Map<String, ?> results = executionResult.iterator().next();
    Long aid = (Long) results.get("a_id");
    Long bid = (Long) results.get("b_id");
    Long vid = (Long) results.get("v_id");
    session.clear();
    Candidate a = session.load(Candidate.class, aid);
    Candidate b = session.load(Candidate.class, bid);
    Voter v = session.load(Voter.class, vid);
    v.candidateVotedFor = a;
    session.save(v);
    session.clear();
    assertThat(session.query("MATCH (a:Voter:Candidate {name:'A'}), " + "(b:Voter:Candidate {name:'B'}), " + "(v:Voter {name:'V'})-[:CANDIDATE_VOTED_FOR]->(a) return a, b, v", emptyMap()).queryResults()).hasSize(1);
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) Voter(org.neo4j.ogm.domain.election.Voter) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Voter

use of org.neo4j.ogm.domain.election.Voter in project neo4j-ogm by neo4j.

the class ElectionTest method shouldAllowVoterToChangeHerMind.

@Test
public void shouldAllowVoterToChangeHerMind() {
    Candidate a = new Candidate("A");
    Candidate b = new Candidate("B");
    Voter v = new Voter("V");
    v.candidateVotedFor = b;
    session.save(a);
    session.save(v);
    MappingContext context = ((Neo4jSession) session).context();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    session.clear();
    a = session.load(Candidate.class, a.getId());
    v = session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(b.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
    v.candidateVotedFor = a;
    session.save(v);
    session.clear();
    session.load(Candidate.class, b.getId());
    session.load(Voter.class, v.getId());
    assertThat(v.candidateVotedFor.getId()).isEqualTo(a.getId());
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", a.getId(), null, Voter.class, Candidate.class))).isTrue();
    assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isFalse();
    session.clear();
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) MappingContext(org.neo4j.ogm.context.MappingContext) Neo4jSession(org.neo4j.ogm.session.Neo4jSession) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Voter(org.neo4j.ogm.domain.election.Voter) Test(org.junit.Test)

Example 4 with Voter

use of org.neo4j.ogm.domain.election.Voter in project neo4j-ogm by neo4j.

the class ElectionTest method shouldAllowASelfReferenceToBeSavedFromTheReferredSide.

@Test
public void shouldAllowASelfReferenceToBeSavedFromTheReferredSide() {
    Candidate candidate = new Candidate("Hilary Clinton");
    candidate.candidateVotedFor = candidate;
    session.save(candidate.candidateVotedFor);
    session.clear();
    Long voterId = candidate.candidateVotedFor.getId();
    Voter voter = session.load(Voter.class, voterId);
    assertThat(voter.getId()).isNotNull();
    assertThat(voter.candidateVotedFor.getId()).isNotNull();
    assertThat(voter.candidateVotedFor.getId()).isEqualTo(voter.getId());
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) Voter(org.neo4j.ogm.domain.election.Voter) Test(org.junit.Test)

Example 5 with Voter

use of org.neo4j.ogm.domain.election.Voter in project neo4j-ogm by neo4j.

the class ElectionTest method shouldAllowACandidateToVoteForHerself.

@Test
public void shouldAllowACandidateToVoteForHerself() {
    Candidate candidate = new Candidate("Hilary Clinton");
    candidate.candidateVotedFor = candidate;
    session.save(candidate);
    assertThat(candidate.getId()).isNotNull();
    assertThat(candidate.candidateVotedFor.getId()).isNotNull();
    assertThat(candidate.candidateVotedFor.getId()).isEqualTo(candidate.getId());
    session.clear();
    Long voterId = candidate.getId();
    Voter voter = session.load(Voter.class, voterId);
    assertThat(voter.getId()).isNotNull();
    assertThat(voter.candidateVotedFor.getId()).isNotNull();
    assertThat(voter.candidateVotedFor.getId()).isEqualTo(voter.getId());
}
Also used : Candidate(org.neo4j.ogm.domain.election.Candidate) Voter(org.neo4j.ogm.domain.election.Voter) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 Candidate (org.neo4j.ogm.domain.election.Candidate)5 Voter (org.neo4j.ogm.domain.election.Voter)5 Map (java.util.Map)2 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)1 MappingContext (org.neo4j.ogm.context.MappingContext)1 Neo4jSession (org.neo4j.ogm.session.Neo4jSession)1 Session (org.neo4j.ogm.session.Session)1