Search in sources :

Example 16 with Teacher

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

the class UpdateUsingUpdateMethodTest method testUpdateAllWithDefaultValueWithInstanceUpdate.

public void testUpdateAllWithDefaultValueWithInstanceUpdate() {
    Teacher tea = null;
    int[] ids = new int[5];
    for (int i = 0; i < 5; i++) {
        tea = new Teacher();
        tea.setTeacherName("Rose Jackson");
        tea.setAge(50);
        tea.setTeachYears(15);
        tea.setSex(false);
        tea.save();
        ids[i] = tea.getId();
    }
    Teacher t = new Teacher();
    t.setTeacherName("");
    t.setTeachYears(0);
    t.setSex(true);
    t.setAge(22);
    int affectedTeacher = t.updateAll("teachername = 'Rose Jackson'");
    assertEquals(0, affectedTeacher);
    List<Teacher> teachers = getTeachers(ids);
    for (Teacher updatedTeacher : teachers) {
        assertEquals("Rose Jackson", updatedTeacher.getTeacherName());
        assertEquals(50, updatedTeacher.getAge());
        assertEquals(15, updatedTeacher.getTeachYears());
        assertEquals(false, updatedTeacher.isSex());
    }
}
Also used : Teacher(com.litepaltest.model.Teacher)

Example 17 with Teacher

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

the class SaveAllTest method testSaveAllWithM2M.

public void testSaveAllWithM2M() {
    List<Student> studentList = new ArrayList<Student>();
    List<Teacher> teacherList = new ArrayList<Teacher>();
    for (int i = 0; i < 50; i++) {
        Teacher teacher = new Teacher();
        teacher.setTeacherName("Lucy");
        teacher.setTeachYears(new Random().nextInt(10));
        teacherList.add(teacher);
    }
    for (int i = 0; i < 50; i++) {
        Student student = new Student();
        student.setName("Timmy");
        student.setAge(new Random().nextInt(20));
        int index1 = new Random().nextInt(50);
        student.getTeachers().add(teacherList.get(index1));
        int index2 = index1;
        while (index2 == index1) {
            index2 = new Random().nextInt(50);
        }
        student.getTeachers().add(teacherList.get(index2));
        int index3 = index2;
        while (index3 == index2 || index3 == index1) {
            index3 = new Random().nextInt(50);
        }
        student.getTeachers().add(teacherList.get(index3));
        studentList.add(student);
    }
    DataSupport.saveAll(studentList);
    DataSupport.saveAll(teacherList);
    String studentTable = DBUtility.getTableNameByClassName(Student.class.getName());
    String teacherTable = DBUtility.getTableNameByClassName(Teacher.class.getName());
    String tableName = DBUtility.getIntermediateTableName(studentTable, teacherTable);
    for (Student student : studentList) {
        Cursor cursor = DataSupport.findBySQL("select * from " + tableName + " where " + studentTable + "_id=?", String.valueOf(student.getId()));
        assertEquals(3, cursor.getCount());
        cursor.close();
    }
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) Teacher(com.litepaltest.model.Teacher) Student(com.litepaltest.model.Student) Cursor(android.database.Cursor)

Example 18 with Teacher

use of com.litepaltest.model.Teacher 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 19 with Teacher

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

the class LitePalTestCase method getTeachers.

protected List<Teacher> getTeachers(int[] ids) {
    List<Teacher> teachers = new ArrayList<Teacher>();
    Cursor cursor = Connector.getDatabase().query(getTableName(Teacher.class), null, getWhere(ids), null, null, null, null);
    if (cursor.moveToFirst()) {
        Teacher t = new Teacher();
        String teacherName = cursor.getString(cursor.getColumnIndexOrThrow("teachername"));
        int teachYears = cursor.getInt(cursor.getColumnIndexOrThrow("teachyears"));
        int age = cursor.getInt(cursor.getColumnIndexOrThrow("age"));
        int sex = cursor.getInt(cursor.getColumnIndexOrThrow("sex"));
        t.setTeacherName(teacherName);
        t.setTeachYears(teachYears);
        t.setAge(age);
        if (sex == 0) {
            t.setSex(false);
        } else if (sex == 1) {
            t.setSex(true);
        }
        teachers.add(t);
    }
    cursor.close();
    return teachers;
}
Also used : ArrayList(java.util.ArrayList) Teacher(com.litepaltest.model.Teacher) Cursor(android.database.Cursor)

Example 20 with Teacher

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

the class QueryEagerTest method testEagerFindLast.

public void testEagerFindLast() {
    resetData();
    Teacher t1 = DataSupport.findLast(Teacher.class);
    assertEquals(0, t1.getStudents().size());
    t1 = DataSupport.findLast(Teacher.class, true);
    assertTrue(0 < t1.getStudents().size());
}
Also used : Teacher(com.litepaltest.model.Teacher)

Aggregations

Teacher (com.litepaltest.model.Teacher)23 Student (com.litepaltest.model.Student)11 Classroom (com.litepaltest.model.Classroom)7 IdCard (com.litepaltest.model.IdCard)7 Cursor (android.database.Cursor)3 Calendar (java.util.Calendar)3 ArrayList (java.util.ArrayList)2 DataSupportException (org.litepal.exceptions.DataSupportException)2 Cellphone (com.litepaltest.model.Cellphone)1 Date (java.util.Date)1 Random (java.util.Random)1