提交 74684173 编写于 作者: G guolindev

Add test cases for transaction API.

上级 98fc4df9
......@@ -26,7 +26,6 @@ import static junit.framework.TestCase.assertTrue;
public class LitePalTestCase {
protected void assertM2M(String table1, String table2, long id1, long id2) {
assertTrue(isIntermediateDataCorrect(table1, table2, id1, id2));
}
......
package com.litepaltest.test.crud.transaction
import android.content.ContentValues
import android.support.test.filters.SmallTest
import com.litepaltest.model.*
import com.litepaltest.test.LitePalTestCase
import junit.framework.TestCase
import org.junit.Assert
import org.junit.Test
import org.litepal.LitePal
import org.litepal.extension.find
import org.litepal.extension.runInTransaction
import java.lang.NullPointerException
import java.util.*
@SmallTest
class TransactionKotlinTest : LitePalTestCase() {
@Test
fun testTransactionForSave() {
val book = Book()
LitePal.runInTransaction {
book.bookName = "First Line of Android"
book.pages = 700
Assert.assertTrue(book.save())
val bookFromDb = LitePal.find(Book::class.java, book.id)
Assert.assertEquals("First Line of Android", bookFromDb.bookName)
Assert.assertEquals(700L, bookFromDb.pages.toInt().toLong())
false
}
Assert.assertTrue(book.isSaved)
val bookFromDb = LitePal.find(Book::class.java, book.id)
Assert.assertNull(bookFromDb)
}
@Test
fun testTransactionForSaveAll() {
val serial = UUID.randomUUID().toString()
val weiboMessage = WeiboMessage()
LitePal.runInTransaction {
weiboMessage.follower = "nobody"
val saveResult = weiboMessage.save()
val cellphones: MutableList<Cellphone> = ArrayList()
for (i in 0..19) {
val cellphone = Cellphone()
cellphone.setBrand("Apple")
cellphone.serial = serial + i % 10 // serial is unique, so this should save failed
cellphone.messages.add(weiboMessage)
cellphones.add(cellphone)
}
val saveAllResult = LitePal.saveAll(cellphones)
saveResult && saveAllResult
}
Assert.assertTrue(weiboMessage.isSaved)
val messageFromDb = LitePal.find(WeiboMessage::class.java, weiboMessage.id.toLong())
Assert.assertNull(messageFromDb)
val list = LitePal.where("serial like ?", "$serial%").find(Cellphone::class.java)
TestCase.assertTrue(list.isEmpty())
}
@Test
fun testTransactionForUpdate() {
val teacher = Teacher()
teacher.teacherName = "Tony"
teacher.teachYears = 3
teacher.age = 23
teacher.isSex = false
Assert.assertTrue(teacher.save())
LitePal.runInTransaction {
val values = ContentValues()
values.put("TeachYears", 13)
val rows = LitePal.update(Teacher::class.java, values, teacher.id.toLong())
Assert.assertEquals(1, rows.toLong())
val teacherFromDb = LitePal.find(Teacher::class.java, teacher.id.toLong())
Assert.assertEquals(13, teacherFromDb.teachYears.toLong())
// not set transaction successful
false
}
val teacherFromDb = LitePal.find(Teacher::class.java, teacher.id.toLong())
Assert.assertEquals(3, teacherFromDb.teachYears.toLong())
}
@Test
fun testTransactionForDelete() {
val tony = Student()
tony.name = "Tony"
tony.age = 23
tony.save()
val studentId = tony.id
LitePal.runInTransaction {
val rowsAffected = tony.delete()
Assert.assertEquals(1, rowsAffected.toLong())
val studentFromDb = LitePal.find<Student>(studentId.toLong())
Assert.assertNull(studentFromDb)
// not set transaction successful
false
}
val studentFromDb = LitePal.find<Student>(studentId.toLong())
Assert.assertNotNull(studentFromDb)
Assert.assertEquals("Tony", studentFromDb!!.name)
Assert.assertEquals(23, studentFromDb.age.toLong())
}
@Test
fun testTransactionForCRUD() {
var lastId = -1
LitePal.runInTransaction {
val tony = Student()
tony.name = "Tony"
tony.age = 23
tony.save()
val studentId = tony.id
var studentFromDb = LitePal.find<Student>(studentId.toLong())
Assert.assertNotNull(studentFromDb)
Assert.assertEquals("Tony", studentFromDb!!.name)
Assert.assertEquals(23, studentFromDb.age.toLong())
val updateModel = Student()
updateModel.age = 25
var rowsAffected = updateModel.update(studentId.toLong())
Assert.assertEquals(1, rowsAffected.toLong())
studentFromDb = LitePal.find(Student::class.java, studentId.toLong())
Assert.assertEquals(25, studentFromDb.age.toLong())
rowsAffected = tony.delete()
Assert.assertEquals(1, rowsAffected.toLong())
studentFromDb = LitePal.find(Student::class.java, studentId.toLong())
Assert.assertNull(studentFromDb)
Assert.assertTrue(tony.save())
studentFromDb = LitePal.find(Student::class.java, tony.id.toLong())
Assert.assertNotNull(studentFromDb)
lastId = tony.id
// not set transaction successful
false
}
val studentFromDb = LitePal.find<Student>(lastId.toLong())
Assert.assertNull(studentFromDb)
}
@Test
fun testTransactionSuccessfulForCRUD() {
var lastId = -1
LitePal.runInTransaction {
val tony = Student()
tony.name = "Tony"
tony.age = 23
tony.save()
val studentId = tony.id
var studentFromDb = LitePal.find<Student>(studentId.toLong())
Assert.assertNotNull(studentFromDb)
Assert.assertEquals("Tony", studentFromDb!!.name)
Assert.assertEquals(23, studentFromDb.age.toLong())
val updateModel = Student()
updateModel.age = 25
var rowsAffected = updateModel.update(studentId.toLong())
Assert.assertEquals(1, rowsAffected.toLong())
studentFromDb = LitePal.find(Student::class.java, studentId.toLong())
Assert.assertEquals(25, studentFromDb.age.toLong())
rowsAffected = tony.delete()
Assert.assertEquals(1, rowsAffected.toLong())
studentFromDb = LitePal.find(Student::class.java, studentId.toLong())
Assert.assertNull(studentFromDb)
Assert.assertTrue(tony.save())
studentFromDb = LitePal.find(Student::class.java, tony.id.toLong())
Assert.assertNotNull(studentFromDb)
lastId = tony.id
// not set transaction successful
true
}
val studentFromDb = LitePal.find<Student>(lastId.toLong())
Assert.assertNotNull(studentFromDb)
Assert.assertEquals("Tony", studentFromDb!!.name)
Assert.assertEquals(23, studentFromDb.age.toLong())
}
@Test
fun testTransactionWithException() {
val book = Book()
LitePal.runInTransaction {
book.bookName = "First Line of Android"
book.pages = 700
Assert.assertTrue(book.save())
val bookFromDb = LitePal.find(Book::class.java, book.id)
Assert.assertEquals("First Line of Android", bookFromDb.bookName)
Assert.assertEquals(700L, bookFromDb.pages.toInt().toLong())
if (true) throw NullPointerException("just throw to fail the transaction")
true
}
Assert.assertTrue(book.isSaved)
val bookFromDb = LitePal.find(Book::class.java, book.id)
Assert.assertNull(bookFromDb)
}
}
\ No newline at end of file
package com.litepaltest.test.crud.transaction;
import android.content.ContentValues;
import android.support.test.filters.SmallTest;
import com.litepaltest.model.Book;
import com.litepaltest.model.Cellphone;
import com.litepaltest.model.Student;
import com.litepaltest.model.Teacher;
import com.litepaltest.model.WeiboMessage;
import com.litepaltest.test.LitePalTestCase;
import org.junit.Assert;
import org.junit.Test;
import org.litepal.LitePal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static junit.framework.TestCase.assertTrue;
@SmallTest
public class TransactionTest extends LitePalTestCase {
@Test
public void testTransactionForSave() {
LitePal.beginTransaction();
Book book = new Book();
try {
book.setBookName("First Line of Android");
book.setPages(700);
Assert.assertTrue(book.save());
Book bookFromDb = LitePal.find(Book.class, book.getId());
Assert.assertEquals("First Line of Android", bookFromDb.getBookName());
Assert.assertEquals(700L, bookFromDb.getPages().intValue());
if (true) {
throw new NullPointerException("Throw a exception to fail the transaction.");
}
LitePal.setTransactionSuccessful();
} catch (Exception e) {
// do nothing
} finally {
LitePal.endTransaction();
}
Assert.assertTrue(book.isSaved());
Book bookFromDb = LitePal.find(Book.class, book.getId());
Assert.assertNull(bookFromDb);
}
@Test
public void testTransactionForSaveAll() {
LitePal.beginTransaction();
String serial = UUID.randomUUID().toString();
WeiboMessage weiboMessage = new WeiboMessage();
try {
weiboMessage.setFollower("nobody");
boolean saveResult = weiboMessage.save();
List<Cellphone> cellphones = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Cellphone cellphone = new Cellphone();
cellphone.setBrand("Apple");
cellphone.setSerial(serial + (i % 10)); // serial is unique, so this should save failed
cellphone.getMessages().add(weiboMessage);
cellphones.add(cellphone);
}
boolean saveAllResult = LitePal.saveAll(cellphones);
if (saveResult && saveAllResult) {
LitePal.setTransactionSuccessful();
}
} finally {
LitePal.endTransaction();
}
Assert.assertTrue(weiboMessage.isSaved());
WeiboMessage messageFromDb = LitePal.find(WeiboMessage.class, weiboMessage.getId());
Assert.assertNull(messageFromDb);
List<Cellphone> list = LitePal.where("serial like ?", serial + "%").find(Cellphone.class);
assertTrue(list.isEmpty());
}
@Test
public void testTransactionForUpdate() {
Teacher teacher = new Teacher();
teacher.setTeacherName("Tony");
teacher.setTeachYears(3);
teacher.setAge(23);
teacher.setSex(false);
Assert.assertTrue(teacher.save());
LitePal.beginTransaction();
ContentValues values = new ContentValues();
values.put("TeachYears", 13);
int rows = LitePal.update(Teacher.class, values, teacher.getId());
Assert.assertEquals(1, rows);
Teacher teacherFromDb = LitePal.find(Teacher.class, teacher.getId());
Assert.assertEquals(13, teacherFromDb.getTeachYears());
// not set transaction successful
LitePal.endTransaction();
teacherFromDb = LitePal.find(Teacher.class, teacher.getId());
Assert.assertEquals(3, teacherFromDb.getTeachYears());
}
@Test
public void testTransactionForDelete() {
Student tony = new Student();
tony.setName("Tony");
tony.setAge(23);
tony.save();
int studentId = tony.getId();
LitePal.beginTransaction();
int rowsAffected = tony.delete();
Assert.assertEquals(1, rowsAffected);
Student studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertNull(studentFromDb);
// not set transaction successful
LitePal.endTransaction();
studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertNotNull(studentFromDb);
Assert.assertEquals("Tony", studentFromDb.getName());
Assert.assertEquals(23, studentFromDb.getAge());
}
@Test
public void testTransactionForCRUD() {
LitePal.beginTransaction();
Student tony = new Student();
tony.setName("Tony");
tony.setAge(23);
tony.save();
int studentId = tony.getId();
Student studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertNotNull(studentFromDb);
Assert.assertEquals("Tony", studentFromDb.getName());
Assert.assertEquals(23, studentFromDb.getAge());
Student updateModel = new Student();
updateModel.setAge(25);
int rowsAffected = updateModel.update(studentId);
Assert.assertEquals(1, rowsAffected);
studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertEquals(25, studentFromDb.getAge());
rowsAffected = tony.delete();
Assert.assertEquals(1, rowsAffected);
studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertNull(studentFromDb);
Assert.assertTrue(tony.save());
studentFromDb = LitePal.find(Student.class, tony.getId());
Assert.assertNotNull(studentFromDb);
// not set transaction successful
LitePal.endTransaction();
studentFromDb = LitePal.find(Student.class, tony.getId());
Assert.assertNull(studentFromDb);
}
@Test
public void testTransactionSuccessfulForCRUD() {
LitePal.beginTransaction();
Student tony = new Student();
tony.setName("Tony");
tony.setAge(23);
tony.save();
int studentId = tony.getId();
Student studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertNotNull(studentFromDb);
Assert.assertEquals("Tony", studentFromDb.getName());
Assert.assertEquals(23, studentFromDb.getAge());
Student updateModel = new Student();
updateModel.setAge(25);
int rowsAffected = updateModel.update(studentId);
Assert.assertEquals(1, rowsAffected);
studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertEquals(25, studentFromDb.getAge());
rowsAffected = tony.delete();
Assert.assertEquals(1, rowsAffected);
studentFromDb = LitePal.find(Student.class, studentId);
Assert.assertNull(studentFromDb);
Assert.assertTrue(tony.save());
studentFromDb = LitePal.find(Student.class, tony.getId());
Assert.assertNotNull(studentFromDb);
LitePal.setTransactionSuccessful();
LitePal.endTransaction();
studentFromDb = LitePal.find(Student.class, tony.getId());
Assert.assertNotNull(studentFromDb);
Assert.assertEquals("Tony", studentFromDb.getName());
Assert.assertEquals(23, studentFromDb.getAge());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册