Search in sources :

Example 21 with Student

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

the class UpdateUsingUpdateMethodTest method testUpdateAllWithStaticUpdate.

// public void testUpdateM2OAssociationsOnMSideWithInstanceUpdate() {
// initForAssociations();
// s1.setClassroom(c1);
// s2.setClassroom(c1);
// assertTrue(c1.save());
// assertTrue(c2.save());
// assertTrue(s1.save());
// assertTrue(s2.save());
// Student st = new Student();
// st.setClassroom(c2);
// int rowsAffected = st.update(s1.getId());
// assertEquals(1, rowsAffected);
// rowsAffected = st.update(s2.getId());
// assertEquals(1, rowsAffected);
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s1.getId()));
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s2.getId()));
// }
//
// public void
// testUpdateM2OAssociationsAndOtherFieldsOnMSideWithInstanceUpdate() {
// initForAssociations();
// s1.setClassroom(c1);
// s2.setClassroom(c1);
// assertTrue(c1.save());
// assertTrue(c2.save());
// assertTrue(s1.save());
// assertTrue(s2.save());
// Student st = new Student();
// st.setName("Jackson");
// st.setClassroom(c2);
// int rowsAffected = st.update(s1.getId());
// assertEquals(1, rowsAffected);
// rowsAffected = st.update(s2.getId());
// assertEquals(1, rowsAffected);
// assertEquals("Jackson", getStudent(s1.getId()).getName());
// assertEquals("Jackson", getStudent(s2.getId()).getName());
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s1.getId()));
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s2.getId()));
// }
//
// public void testUpdateM2OAssociationsOnOSideWithInstanceUpdate() {
// initForAssociations();
// c1.getStudentCollection().add(s1);
// c1.getStudentCollection().add(s2);
// assertTrue(c1.save());
// assertTrue(c2.save());
// assertTrue(s1.save());
// assertTrue(s2.save());
// Classroom c = new Classroom();
// c.getStudentCollection().add(s1);
// c.getStudentCollection().add(s2);
// int rowsAffected = c.update(c2.get_id());
// assertEquals(2, rowsAffected);
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s1.getId()));
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s2.getId()));
// }
//
// public void
// testUpdateM2OAssociationsAndOtherFieldsOnOSideWithInstanceUpdate() {
// initForAssociations();
// c1.getStudentCollection().add(s1);
// c1.getStudentCollection().add(s2);
// assertTrue(c1.save());
// assertTrue(c2.save());
// assertTrue(s1.save());
// assertTrue(s2.save());
// Classroom c = new Classroom();
// c.setName("Game room");
// c.getStudentCollection().add(s1);
// c.getStudentCollection().add(s2);
// int rowsAffected = c.update(c2.get_id());
// assertEquals(3, rowsAffected);
// assertEquals("Game room", getClassroom(c2.get_id()).getName());
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s1.getId()));
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s2.getId()));
// }
//
// public void
// testUpdateM2OAssociationsOnMSideWithNotExistsRecordWithInstanceUpdate() {
// initForAssociations();
// s1.setClassroom(c1);
// s2.setClassroom(c1);
// assertTrue(c1.save());
// assertTrue(s1.save());
// assertTrue(s2.save());
// Student s = new Student();
// s.setClassroom(c2);
// int rowsAffected = s.update(s1.getId());
// assertEquals(0, rowsAffected);
// s.update(s2.getId());
// assertEquals(0, rowsAffected);
// assertEquals(c1.get_id(), getForeignKeyValue("student", "classroom",
// s1.getId()));
// assertEquals(c1.get_id(), getForeignKeyValue("student", "classroom",
// s2.getId()));
// }
//
// public void
// testUpdateM2OAssociationsOnOSideWithNotExistsRecordWithInstanceUpdate() {
// initForAssociations();
// c1.getStudentCollection().add(s1);
// c1.getStudentCollection().add(s2);
// assertTrue(c1.save());
// assertTrue(c2.save());
// assertTrue(s1.save());
// Classroom c = new Classroom();
// c.getStudentCollection().add(s1);
// c.getStudentCollection().add(s2);
// c.update(c2.get_id());
// assertEquals(c2.get_id(), getForeignKeyValue("student", "classroom",
// s1.getId()));
// }
//
// public void testUpdateM2OAssociationsOnMSideWithNullWithInstanceUpdate()
// {
// initForAssociations();
// s1.setClassroom(c1);
// s2.setClassroom(c1);
// assertTrue(c1.save());
// assertTrue(s1.save());
// assertTrue(s2.save());
// }
public void testUpdateAllWithStaticUpdate() {
    Student s;
    int[] ids = new int[5];
    for (int i = 0; i < 5; i++) {
        s = new Student();
        s.setName("Dusting");
        s.setAge(i + 10);
        s.save();
        ids[i] = s.getId();
    }
    ContentValues values = new ContentValues();
    values.put("age", 24);
    int affectedRows = DataSupport.updateAll(Student.class, values, "name = ? and age = ?", "Dusting", "13");
    assertEquals(1, affectedRows);
    Student updatedStu = getStudent(ids[3]);
    assertEquals(24, updatedStu.getAge());
    values.clear();
    values.put("name", "Dustee");
    affectedRows = DataSupport.updateAll(Student.class, values, "name = ?", "Dusting");
    assertEquals(5, affectedRows);
    List<Student> students = getStudents(ids);
    for (Student updatedStudent : students) {
        assertEquals("Dustee", updatedStudent.getName());
    }
}
Also used : ContentValues(android.content.ContentValues) Student(com.litepaltest.model.Student)

Example 22 with Student

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

the class SaveAllTest method testSaveAllWithO2O.

public void testSaveAllWithO2O() {
    List<IdCard> idcardList = new ArrayList<IdCard>();
    List<Student> studentList = new ArrayList<Student>();
    for (int i = 0; i < 50; i++) {
        IdCard idcard = new IdCard();
        idcard.setNumber(String.valueOf(new Random().nextInt(2000000)));
        Student student = new Student();
        student.setName("Jim");
        student.setAge(new Random().nextInt(20));
        student.setIdcard(idcard);
        idcardList.add(idcard);
        studentList.add(student);
    }
    DataSupport.saveAll(idcardList);
    DataSupport.saveAll(studentList);
    for (Student student : studentList) {
        List<IdCard> result = DataSupport.where(studentTable + "_id=?", String.valueOf(student.getId())).find(IdCard.class);
        assertEquals(1, result.size());
    }
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) IdCard(com.litepaltest.model.IdCard) Student(com.litepaltest.model.Student)

Example 23 with Student

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

the class SaveAllTest method testSaveAllWithM2OOnOneSide.

public void testSaveAllWithM2OOnOneSide() {
    Classroom classroom = new Classroom();
    classroom.setName("Music room");
    for (int i = 0; i < 50; i++) {
        Student student = new Student();
        student.setName("Tom");
        student.setAge(new Random().nextInt(20));
        classroom.getStudentCollection().add(student);
    }
    DataSupport.saveAll(classroom.getStudentCollection());
    classroom.save();
    List<Student> list = DataSupport.where(classroomTable + "_id = ?", String.valueOf(classroom.get_id())).find(Student.class);
    assertEquals(50, list.size());
}
Also used : Random(java.util.Random) Classroom(com.litepaltest.model.Classroom) Student(com.litepaltest.model.Student)

Example 24 with Student

use of com.litepaltest.model.Student 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 25 with Student

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

the class SaveAllTest method testSaveAllWithM2OOnManySide.

public void testSaveAllWithM2OOnManySide() {
    Classroom classroom = new Classroom();
    classroom.setName("English room");
    List<Student> studentList = new ArrayList<Student>();
    for (int i = 0; i < 50; i++) {
        Student student = new Student();
        student.setName("Tom");
        student.setAge(new Random().nextInt(20));
        student.setClassroom(classroom);
        studentList.add(student);
    }
    DataSupport.saveAll(studentList);
    classroom.save();
    List<Student> list = DataSupport.where(classroomTable + "_id = ?", String.valueOf(classroom.get_id())).find(Student.class);
    assertEquals(50, list.size());
}
Also used : Random(java.util.Random) Classroom(com.litepaltest.model.Classroom) ArrayList(java.util.ArrayList) Student(com.litepaltest.model.Student)

Aggregations

Student (com.litepaltest.model.Student)34 Classroom (com.litepaltest.model.Classroom)11 Teacher (com.litepaltest.model.Teacher)11 IdCard (com.litepaltest.model.IdCard)8 ArrayList (java.util.ArrayList)5 Cursor (android.database.Cursor)4 Calendar (java.util.Calendar)4 Random (java.util.Random)4 Date (java.util.Date)2 ContentValues (android.content.ContentValues)1 SQLiteException (android.database.sqlite.SQLiteException)1 Cellphone (com.litepaltest.model.Cellphone)1 DataSupportException (org.litepal.exceptions.DataSupportException)1