Skip to content
Snippets Groups Projects
TestRelationship.java 2.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • 汤伟's avatar
    汤伟 committed
    package com.ic.er;
    
    
    import com.ic.er.Exception.ERException;
    
    汤伟's avatar
    汤伟 committed
    import com.ic.er.entity.RelationshipDO;
    
    汤伟's avatar
    汤伟 committed
    import com.ic.er.common.Cardinality;
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    
    import java.util.List;
    
    public class TestRelationship {
    
        private View testView;
        private Entity teacher;
        private Entity student;
    
        @Before
    
    汤伟's avatar
    汤伟 committed
        public void init() throws Exception {
    
            ER.connectDB(true);
    
    汤伟's avatar
    汤伟 committed
            testView = ER.createView("testView", "wt22");
            teacher = testView.addEntity("teacher");
            student = testView.addEntity("student");
            Assert.assertNotNull(teacher);
            Assert.assertNotNull(student);
        }
    
        @Test
        public void createRelationshipTest() {
    
    汤伟's avatar
    汤伟 committed
            Relationship relationship = testView.createRelationship("teaches", teacher, student, Cardinality.ZeroToMany, Cardinality.ZeroToMany);
    
    汤伟's avatar
    汤伟 committed
            Assert.assertNotNull(relationship);
            Assert.assertEquals(relationship.getID(), Long.valueOf(1L));
        }
    
    
        @Test(expected = ERException.class)
    
    汤伟's avatar
    汤伟 committed
        public void deleteRelationshipTest() {
    
    汤伟's avatar
    汤伟 committed
            Relationship relationship = testView.createRelationship("teaches", teacher, student, Cardinality.ZeroToMany, Cardinality.ZeroToMany);
    
    汤伟's avatar
    汤伟 committed
            Assert.assertNotNull(relationship);
            relationship.deleteDB();
            Relationship relationship1 = Relationship.queryByID(1L);
        }
    
        @Test
        public void updateRelationshipTest() {
    
    汤伟's avatar
    汤伟 committed
            Relationship relationship = testView.createRelationship("teaches", teacher, student, Cardinality.ZeroToMany, Cardinality.ZeroToMany);
    
    汤伟's avatar
    汤伟 committed
            Assert.assertNotNull(relationship);
    
            String newName = "new name";
    
    汤伟's avatar
    汤伟 committed
            Cardinality newCardi = Cardinality.OneToMany;
    
    汤伟's avatar
    汤伟 committed
    
    
    汤伟's avatar
    汤伟 committed
            relationship.updateInfo(newName, null, null, newCardi, newCardi);
    
    汤伟's avatar
    汤伟 committed
            Relationship relationship1 = Relationship.queryByID(1L);
            Assert.assertNotNull(relationship1);
            Assert.assertEquals(relationship1.getName(), newName);
    
    汤伟's avatar
    汤伟 committed
            Assert.assertEquals(relationship1.getFirstCardinality(), newCardi);
    
    汤伟's avatar
    汤伟 committed
        }
    
        @Test
        public void queryRelationshipTest() {
    
    汤伟's avatar
    汤伟 committed
            Relationship relationship = testView.createRelationship("teaches", teacher, student, Cardinality.ZeroToMany, Cardinality.ZeroToMany);
            Relationship relationship2 = testView.createRelationship("teaches2", teacher, student, Cardinality.ZeroToMany, Cardinality.ZeroToMany);
    
    汤伟's avatar
    汤伟 committed
            Assert.assertNotNull(relationship);
            Assert.assertNotNull(relationship2);
    
            Relationship relationship1 = Relationship.queryByID(1L);
            Assert.assertNotNull(relationship1);
    
    汤伟's avatar
    汤伟 committed
            List<Relationship> results = Relationship.queryByRelationship(new RelationshipDO(relationship.getFirstEntity().getID(), relationship.getSecondEntity().getID()));
    
    汤伟's avatar
    汤伟 committed
            Assert.assertEquals(results.size(), 2);
        }
    }