Search in sources :

Example 26 with Student

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

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

the class LitePalTestCase method getStudent.

protected Student getStudent(long id) {
    Student student = null;
    Cursor cursor = Connector.getDatabase().query(getTableName(Student.class), null, "id = ?", new String[] { String.valueOf(id) }, null, null, null);
    if (cursor.moveToFirst()) {
        student = new Student();
        String name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
        int age = cursor.getInt(cursor.getColumnIndexOrThrow("age"));
        student.setName(name);
        student.setAge(age);
    }
    cursor.close();
    return student;
}
Also used : Student(com.litepaltest.model.Student) Cursor(android.database.Cursor)

Example 28 with Student

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

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

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

the class One2OneBiSaveTest method init.

private void init() {
    s = new Student();
    s.setName("Jimmy");
    s.setAge(18);
    i = new IdCard();
    i.setNumber("9997777112");
    i.setAddress("Nanjing road");
}
Also used : IdCard(com.litepaltest.model.IdCard) 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