Search in sources :

Example 16 with Classroom

use of com.litepaltest.model.Classroom in project LitePal by LitePalFramework.

the class SaveTest method testSaveAfterDelete.

public void testSaveAfterDelete() {
    Cellphone cell = new Cellphone();
    cell.setBrand("iPhone");
    cell.setPrice(4998.01);
    cell.setInStock('Y');
    cell.setSerial(UUID.randomUUID().toString());
    Assert.assertTrue(cell.save());
    Assert.assertTrue(isDataExists(getTableName(cell), cell.getId()));
    assertTrue(cell.delete() > 0);
    assertTrue(cell.save());
    Assert.assertTrue(isDataExists(getTableName(cell), cell.getId()));
    Student stu = new Student();
    stu.setName("Jimmy");
    IdCard idcard = new IdCard();
    idcard.setAddress("Washington");
    idcard.setNumber("123456");
    idcard.setStudent(stu);
    stu.setIdcard(idcard);
    stu.save();
    idcard.save();
    Assert.assertTrue(isDataExists(getTableName(stu), stu.getId()));
    Assert.assertTrue(isDataExists(getTableName(idcard), idcard.getId()));
    stu.delete();
    Assert.assertFalse(isDataExists(getTableName(stu), stu.getId()));
    Assert.assertFalse(isDataExists(getTableName(idcard), idcard.getId()));
    stu.save();
    idcard.save();
    Assert.assertTrue(isDataExists(getTableName(stu), stu.getId()));
    Assert.assertTrue(isDataExists(getTableName(idcard), idcard.getId()));
    Student danny = new Student();
    danny.setName("Danny");
    danny.setAge(14);
    Teacher cam = new Teacher();
    cam.setTeacherName("Cam");
    cam.setAge(33);
    cam.setSex(true);
    cam.setTeachYears(5);
    Teacher jack = new Teacher();
    jack.setTeacherName("Jack");
    jack.setAge(36);
    jack.setSex(false);
    jack.setTeachYears(11);
    danny.getTeachers().add(jack);
    danny.getTeachers().add(cam);
    cam.getStudents().add(danny);
    jack.getStudents().add(danny);
    danny.save();
    cam.save();
    jack.save();
    Assert.assertTrue(isDataExists(getTableName(danny), danny.getId()));
    Assert.assertTrue(isDataExists(getTableName(cam), cam.getId()));
    Assert.assertTrue(isDataExists(getTableName(jack), jack.getId()));
    danny.delete();
    Assert.assertFalse(isDataExists(getTableName(danny), danny.getId()));
    Assert.assertTrue(isDataExists(getTableName(cam), cam.getId()));
    Assert.assertTrue(isDataExists(getTableName(jack), jack.getId()));
    danny.save();
    Assert.assertTrue(isDataExists(getTableName(danny), danny.getId()));
    assertEquals(danny.getTeachers().size(), 2);
    Classroom c = new Classroom();
    c.setName("test classroom");
    Student s = new Student();
    s.setName("Tom");
    s.setClassroom(c);
    Student s2 = new Student();
    s2.setName("Tom");
    s2.setClassroom(c);
    assertTrue(c.save());
    assertTrue(s.save());
    assertTrue(s2.save());
    Assert.assertTrue(isDataExists(getTableName(c), c.get_id()));
    Assert.assertTrue(isDataExists(getTableName(s), s.getId()));
    Assert.assertTrue(isDataExists(getTableName(s), s2.getId()));
    c.delete();
    Assert.assertFalse(isDataExists(getTableName(c), c.get_id()));
    Assert.assertFalse(isDataExists(getTableName(s), s.getId()));
    Assert.assertFalse(isDataExists(getTableName(s), s2.getId()));
    c.save();
    s.save();
    s2.save();
    Assert.assertTrue(isDataExists(getTableName(c), c.get_id()));
    Assert.assertTrue(isDataExists(getTableName(s), s.getId()));
    Assert.assertTrue(isDataExists(getTableName(s), s2.getId()));
}
Also used : Teacher(com.litepaltest.model.Teacher) Classroom(com.litepaltest.model.Classroom) IdCard(com.litepaltest.model.IdCard) Cellphone(com.litepaltest.model.Cellphone) Student(com.litepaltest.model.Student)

Example 17 with Classroom

use of com.litepaltest.model.Classroom in project LitePal by LitePalFramework.

the class QueryEagerTest method testEagerFind.

public void testEagerFind() {
    Student s1 = DataSupport.find(Student.class, student1.getId(), true);
    Classroom c = s1.getClassroom();
    IdCard ic = s1.getIdcard();
    List<Teacher> tList = s1.getTeachers();
    assertNotNull(c);
    assertNotNull(ic);
    assertEquals(classroom.get_id(), c.get_id());
    assertEquals("Classroom 11", c.getName());
    assertEquals(idcard1.getId(), ic.getId());
    assertEquals("320311", ic.getNumber());
    assertEquals(student1.getTeachers().size(), tList.size());
    Calendar calendar = Calendar.getInstance();
    calendar.clear();
    calendar.set(1990, 9, 16, 0, 0, 0);
    assertEquals(calendar.getTime().getTime(), s1.getBirthday().getTime());
    for (Teacher t : tList) {
        if (t.getId() == teacher1.getId()) {
            assertEquals("Teacher 1", t.getTeacherName());
            assertEquals(teacher1.getTeachYears(), t.getTeachYears());
            assertTrue(t.isSex());
            continue;
        }
        if (t.getId() == teacher2.getId()) {
            assertEquals("Teacher 2", t.getTeacherName());
            assertFalse(t.isSex());
            continue;
        }
        fail();
    }
    s1 = DataSupport.find(Student.class, student1.getId());
    c = s1.getClassroom();
    assertNull(c);
    assertNull(s1.getIdcard());
    assertEquals(0, s1.getTeachers().size());
    c = DataSupport.find(Classroom.class, classroom.get_id(), true);
    assertEquals(2, c.getStudentCollection().size());
    assertEquals(1, c.getTeachers().size());
    for (Student s : c.getStudentCollection()) {
        if (s.getId() == student1.getId()) {
            assertEquals("Student 1", s.getName());
            continue;
        }
        if (s.getId() == student2.getId()) {
            assertEquals("Student 2", s.getName());
            calendar.clear();
            calendar.set(1989, 7, 7, 0, 0, 0);
            assertEquals(calendar.getTime().getTime(), s.getBirthday().getTime());
            continue;
        }
        fail();
    }
    Teacher t1 = DataSupport.find(Teacher.class, teacher2.getId(), true);
    List<Student> sList = t1.getStudents();
    assertEquals(teacher2.getStudents().size(), sList.size());
    for (Student s : sList) {
        if (s.getId() == student1.getId()) {
            assertEquals("Student 1", s.getName());
            calendar.clear();
            calendar.set(1990, 9, 16, 0, 0, 0);
            assertEquals(calendar.getTime().getTime(), s.getBirthday().getTime());
            continue;
        }
        if (s.getId() == student2.getId()) {
            assertEquals("Student 2", s.getName());
            continue;
        }
        fail();
    }
    Student s3 = DataSupport.find(Student.class, student3.getId());
    assertNull(s3.getBirthday());
}
Also used : Calendar(java.util.Calendar) Classroom(com.litepaltest.model.Classroom) Teacher(com.litepaltest.model.Teacher) IdCard(com.litepaltest.model.IdCard) Student(com.litepaltest.model.Student)

Example 18 with Classroom

use of com.litepaltest.model.Classroom in project LitePal by LitePalFramework.

the class Many2OneBiSaveTest method init.

public void init() {
    c1 = new Classroom();
    c1.setName("Computer room");
    s1 = new Student();
    s1.setName("Tom");
    s2 = new Student();
    s2.setName("Lily");
}
Also used : Classroom(com.litepaltest.model.Classroom) Student(com.litepaltest.model.Student)

Example 19 with Classroom

use of com.litepaltest.model.Classroom in project LitePal by LitePalFramework.

the class Many2OneUniSaveTest method init.

public void init() {
    c1 = new Classroom();
    c1.setName("Music room");
    t1 = new Teacher();
    t1.setTeacherName("John");
    t1.setAge(25);
    t2 = new Teacher();
    t2.setTeacherName("Sean");
    t2.setAge(35);
}
Also used : Classroom(com.litepaltest.model.Classroom) Teacher(com.litepaltest.model.Teacher)

Example 20 with Classroom

use of com.litepaltest.model.Classroom in project LitePal by LitePalFramework.

the class UpdateUsingUpdateMethodTest method testUpdateGenericData.

public void testUpdateGenericData() {
    Classroom c = new Classroom();
    c.setName("Math room");
    c.getNews().add("news");
    c.getNews().add("paper");
    c.update(classroom.get_id());
    Classroom result = DataSupport.find(Classroom.class, classroom.get_id());
    assertEquals("Math room", result.getName());
    StringBuilder builder = new StringBuilder();
    for (String s : result.getNews()) {
        builder.append(s);
    }
    assertEquals("newspaper", builder.toString());
    assertEquals(2, result.getNumbers().size());
    Classroom c2 = new Classroom();
    c2.setToDefault("numbers");
    c2.update(classroom.get_id());
    result = DataSupport.find(Classroom.class, classroom.get_id());
    assertEquals("Math room", result.getName());
    assertEquals(2, result.getNews().size());
    assertEquals(0, result.getNumbers().size());
}
Also used : Classroom(com.litepaltest.model.Classroom)

Aggregations

Classroom (com.litepaltest.model.Classroom)20 Student (com.litepaltest.model.Student)11 Teacher (com.litepaltest.model.Teacher)7 IdCard (com.litepaltest.model.IdCard)4 ArrayList (java.util.ArrayList)3 Calendar (java.util.Calendar)3 Cursor (android.database.Cursor)2 Random (java.util.Random)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 Cellphone (com.litepaltest.model.Cellphone)1 Computer (com.litepaltest.model.Computer)1 Headset (com.litepaltest.model.Headset)1 Product (com.litepaltest.model.Product)1 Date (java.util.Date)1 LitePalDB (org.litepal.LitePalDB)1