Search in sources :

Example 6 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class EntityGraphMapperTest method shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep.

@Test
public void shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep() {
    Student sheila = new Student();
    sheila.setName("Sheila Smythe");
    Student gary = new Student();
    gary.setName("Gary Jones");
    Student winston = new Student();
    winston.setName("Winston Charles");
    Course physics = new Course();
    physics.setName("GCSE Physics");
    physics.setStudents(Arrays.asList(gary, sheila));
    Course maths = new Course();
    maths.setName("A-Level Mathematics");
    maths.setStudents(Arrays.asList(sheila, winston));
    Teacher teacher = new Teacher();
    teacher.setName("Mrs Kapoor");
    teacher.setCourses(Arrays.asList(physics, maths));
    session.save(teacher);
    session.clear();
    assertThat(session.query("MATCH (t:Teacher {name:'Mrs Kapoor'}), " + "(p:Course {name:'GCSE Physics'}), (m:Course {name:'A-Level Mathematics'}), " + "(s:Student:DomainObject {name:'Sheila Smythe'}), " + "(g:Student:DomainObject {name:'Gary Jones'}), " + "(w:Student:DomainObject {name:'Winston Charles'}) " + "WHERE (t)-[:COURSES]->(p)-[:STUDENTS]->(s) and (t)-[:COURSES]->(m)-[:STUDENTS]->(s) and " + "(p)-[:STUDENTS]->(g) and (m)-[:STUDENTS]->(w) return t, p, m, s, g, w", emptyMap()).queryResults()).hasSize(1);
}
Also used : Teacher(org.neo4j.ogm.domain.education.Teacher) Student(org.neo4j.ogm.domain.education.Student) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 7 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class EducationTest method testTeachers.

@Test
public void testTeachers() throws Exception {
    Map<String, Teacher> teachers = loadTeachers();
    Teacher mrThomas = teachers.get("Mr Thomas");
    Teacher mrsRoberts = teachers.get("Mrs Roberts");
    Teacher missYoung = teachers.get("Miss Young");
    checkCourseNames(mrThomas, "Maths", "English", "Physics");
    checkCourseNames(mrsRoberts, "English", "History", "PE");
    checkCourseNames(missYoung, "History", "Geography", "Philosophy and Ethics");
}
Also used : Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Example 8 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class CompilerTest method shouldCorrectlyRemoveRelationshipWhenItemIsMovedToDifferentCollection.

@Test
public void shouldCorrectlyRemoveRelationshipWhenItemIsMovedToDifferentCollection() {
    Long teacherId = 0L;
    Long businessStudiesCourseId = 1L;
    Long designTechnologyCourseId = 2L;
    Long shivaniId = 3L;
    Course designTech = new Course("GCSE Design & Technology");
    designTech.setId(designTechnologyCourseId);
    Course businessStudies = new Course("GNVQ Business Studies");
    businessStudies.setId(businessStudiesCourseId);
    Teacher msThompson = new Teacher();
    msThompson.setId(teacherId);
    msThompson.setName("Ms Thompson");
    msThompson.setCourses(Arrays.asList(businessStudies, designTech));
    Student shivani = new Student("Shivani");
    shivani.setId(shivaniId);
    mappingContext.addNodeEntity(msThompson);
    mappingContext.addNodeEntity(businessStudies);
    mappingContext.addNodeEntity(designTech);
    mappingContext.addNodeEntity(shivani);
    mappingContext.addRelationship(new MappedRelationship(teacherId, "COURSES", businessStudiesCourseId, null, Teacher.class, Course.class));
    mappingContext.addRelationship(new MappedRelationship(teacherId, "COURSES", designTechnologyCourseId, null, Teacher.class, Course.class));
    mappingContext.addRelationship(new MappedRelationship(businessStudiesCourseId, "STUDENTS", shivaniId, null, Teacher.class, Student.class));
    // move shivani from one course to the other
    businessStudies.setStudents(Collections.emptyList());
    designTech.setStudents(Arrays.asList(shivani));
    // Save msThomson
    // we expect a new relationship to be created, and an old one deleted
    Compiler compiler = mapAndCompile(msThompson, -1);
    List<Statement> statements = compiler.createNodesStatements();
    assertThat(statements).isEmpty();
    statements = compiler.createRelationshipsStatements();
    assertThat(statements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`STUDENTS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : statements) {
        List rows = (List) statement.getParameters().get("rows");
        assertThat(rows).hasSize(1);
    }
    List<Statement> deleteRelsStatements = compiler.deleteRelationshipStatements();
    assertThat(deleteRelsStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MATCH (startNode)-[rel:`STUDENTS`]->(endNode) DELETE rel");
    assertThat(((List) deleteRelsStatements.get(0).getParameters().get("rows"))).hasSize(1);
// fixme: these other tests now need to be in their own test method, because
// a bug fix to the deletion code means that a second deletion won't (correctly) fire again
// expect a delete, but don't expect the new relationship to be created, because the fact of it
// is inaccessible from the businessStudies object
// expectOnSave(businessStudies,
// "MATCH ($1)-[_0:STUDENTS]->($3) WHERE id($1)=1 AND id($3)=3 DELETE _0");
// 
// // expect the new relationship, but don't expect the old one to be deleted, because the fact
// // of it is inaccessible from the designTech object
// expectOnSave(designTech,
// "MATCH ($2) WHERE id($2)=2 MATCH ($3) WHERE id($3)=3 MERGE ($2)-[_0:`STUDENTS`]->($3) RETURN id(_0) AS _0");
// 
// // we can't explore the object model from shivani at all, so no changes.
// expectOnSave(shivani, "");
}
Also used : Statement(org.neo4j.ogm.request.Statement) MappedRelationship(org.neo4j.ogm.context.MappedRelationship) Teacher(org.neo4j.ogm.domain.education.Teacher) ArrayList(java.util.ArrayList) List(java.util.List) Course(org.neo4j.ogm.domain.education.Course) Student(org.neo4j.ogm.domain.education.Student) Test(org.junit.Test)

Example 9 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class CompilerTest method shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep.

@Test
public void shouldCorrectlyPersistObjectGraphsSeveralLevelsDeep() {
    Student sheila = new Student();
    sheila.setName("Sheila Smythe");
    Student gary = new Student();
    gary.setName("Gary Jones");
    Student winston = new Student();
    winston.setName("Winston Charles");
    Course physics = new Course();
    physics.setName("GCSE Physics");
    physics.setStudents(Arrays.asList(gary, sheila));
    Course maths = new Course();
    maths.setName("A-Level Mathematics");
    maths.setStudents(Arrays.asList(sheila, winston));
    Teacher teacher = new Teacher();
    teacher.setName("Mrs Kapoor");
    teacher.setCourses(Arrays.asList(physics, maths));
    // Save teacher
    Compiler compiler = mapAndCompile(teacher, -1);
    List<Statement> createNodesStatements = compiler.createNodesStatements();
    assertThat(createNodesStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row CREATE (n:`Teacher`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`Course`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type", "UNWIND $rows as row CREATE (n:`DomainObject`:`Student`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
    for (Statement statement : createNodesStatements) {
        List rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("Teacher")) {
            assertThat(rows).hasSize(1);
        }
        if (statement.getStatement().contains("Student")) {
            assertThat(rows).hasSize(3);
        }
        if (statement.getStatement().contains("Course")) {
            assertThat(rows).hasSize(2);
        }
    }
    List<Statement> createRelsStatements = compiler.createRelationshipsStatements();
    assertThat(createRelsStatements).extracting(Statement::getStatement).containsOnly("UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`COURSES`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type", "UNWIND $rows as row MATCH (startNode) WHERE ID(startNode) = row.startNodeId WITH row,startNode MATCH (endNode) WHERE ID(endNode) = row.endNodeId MERGE (startNode)-[rel:`STUDENTS`]->(endNode) RETURN row.relRef as ref, ID(rel) as id, $type as type");
    for (Statement statement : createRelsStatements) {
        List rows = (List) statement.getParameters().get("rows");
        if (statement.getStatement().contains("STUDENTS")) {
            assertThat(rows).hasSize(4);
        }
        if (statement.getStatement().contains("COURSES")) {
            assertThat(rows).hasSize(2);
        }
    }
}
Also used : Statement(org.neo4j.ogm.request.Statement) Teacher(org.neo4j.ogm.domain.education.Teacher) ArrayList(java.util.ArrayList) List(java.util.List) Student(org.neo4j.ogm.domain.education.Student) Course(org.neo4j.ogm.domain.education.Course) Test(org.junit.Test)

Example 10 with Teacher

use of org.neo4j.ogm.domain.education.Teacher in project neo4j-ogm by neo4j.

the class IdentityMapTest method testChangedPropertyDetected.

@Test
public void testChangedPropertyDetected() {
    Teacher teacher = new Teacher("Miss White");
    // the id field must not be part of the memoised property list
    teacher.setId(115L);
    mappingContext.addNodeEntity(teacher);
    // the teacher's name property has changed.
    teacher.setName("Mrs Jones");
    assertThat(mappingContext.isDirty(teacher)).isTrue();
}
Also used : Teacher(org.neo4j.ogm.domain.education.Teacher) Test(org.junit.Test)

Aggregations

Teacher (org.neo4j.ogm.domain.education.Teacher)25 Test (org.junit.Test)24 School (org.neo4j.ogm.domain.education.School)12 Course (org.neo4j.ogm.domain.education.Course)9 List (java.util.List)6 Student (org.neo4j.ogm.domain.education.Student)6 MappedRelationship (org.neo4j.ogm.context.MappedRelationship)4 Result (org.neo4j.ogm.model.Result)4 Statement (org.neo4j.ogm.request.Statement)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Session (org.neo4j.ogm.session.Session)3 Map (java.util.Map)2 RowStatementFactory (org.neo4j.ogm.session.request.RowStatementFactory)2 HashSet (java.util.HashSet)1 TeachesAt (org.neo4j.ogm.domain.education.TeachesAt)1