Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhaoyufei133
litepal
提交
74684173
L
litepal
项目概览
zhaoyufei133
/
litepal
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
litepal
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
74684173
编写于
5月 25, 2020
作者:
G
guolindev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add test cases for transaction API.
上级
98fc4df9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
379 addition
and
1 deletion
+379
-1
sample/src/androidTest/java/com/litepaltest/test/LitePalTestCase.java
...ndroidTest/java/com/litepaltest/test/LitePalTestCase.java
+0
-1
sample/src/androidTest/java/com/litepaltest/test/crud/transaction/TransactionKotlinTest.kt
...itepaltest/test/crud/transaction/TransactionKotlinTest.kt
+193
-0
sample/src/androidTest/java/com/litepaltest/test/crud/transaction/TransactionTest.java
...om/litepaltest/test/crud/transaction/TransactionTest.java
+186
-0
未找到文件。
sample/src/androidTest/java/com/litepaltest/test/LitePalTestCase.java
浏览文件 @
74684173
...
...
@@ -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
));
}
...
...
sample/src/androidTest/java/com/litepaltest/test/crud/transaction/TransactionKotlinTest.kt
0 → 100644
浏览文件 @
74684173
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
sample/src/androidTest/java/com/litepaltest/test/crud/transaction/TransactionTest.java
0 → 100644
浏览文件 @
74684173
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录