提交 44e25031 编写于 作者: G guolindev

Add kotlin extension method for saveAll.

Add SaveAllKotlinTest for kotlin style saveAll API.
Correct some comments.
上级 87d4880b
......@@ -28,14 +28,14 @@ import org.litepal.crud.async.FindExecutor
/**
* Finds multiple records by the cluster parameters. You can use the below
* way to finish a complicated query:
*
* LitePal.select("name").where("age > ?", "14").order("age").limit(1).offset(2).find<Person>()
*
* ```
* LitePal.select("name").where("age > ?", "14").order("age").limit(1).offset(2).find<Person>()
* ```
* You can also do the same job with SQLiteDatabase like this:
*
* getSQLiteDatabase().query(&quot;Person&quot;, &quot;name&quot;, &quot;age &gt; ?&quot;, new String[] { &quot;14&quot; }, null, null, &quot;age&quot;,
* &quot;2,1&quot;)
*
* ```
* getSQLiteDatabase().query("Person", "name", "age > ?", new String[] { "14" }, null, null, "age",
* "2,1")
* ```
* Obviously, the first way is much more semantic.<br>
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
......@@ -54,7 +54,7 @@ inline fun <reified T> FluentQuery.find(): List<T> = find(T::class.java)
inline fun <reified T> FluentQuery.findAsync() = findAsync(T::class.java)
/**
* It is mostly same as {@link FluentQuery#find(Class)} but an isEager
* It is mostly same as [FluentQuery.find(Class)] but an isEager
* parameter. If set true the associated models will be loaded as well.
*
* Note that isEager will only work for one deep level relation, considering the query efficiency.
......@@ -79,12 +79,12 @@ inline fun <reified T> FluentQuery.findAsync(isEager: Boolean) = findAsync(T::cl
/**
* Finds the first record by the cluster parameters. You can use the below
* way to finish a complicated query:
*
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(10).offset(2).findFirst&lt;Person&gt;()
*
* ```
* LitePal.select("name").where("age > ?", "14").order("age").limit(10).offset(2).findFirst<Person>()
* ```
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
* {@link FluentQuery#findFirst(Class, boolean)}.
* [FluentQuery.findFirst(Class, boolean)].
*
* @return An object with founded data from database, or null.
*/
......@@ -99,7 +99,7 @@ inline fun <reified T> FluentQuery.findFirst(): T? = findFirst(T::class.java)
inline fun <reified T> FluentQuery.findFirstAsync(): FindExecutor<T> = findFirstAsync(T::class.java)
/**
* It is mostly same as {@link FluentQuery#findFirst(Class)} but an isEager
* It is mostly same as [FluentQuery.findFirst(Class)] but an isEager
* parameter. If set true the associated models will be loaded as well.
*
* Note that isEager will only work for one deep level relation, considering the query efficiency.
......@@ -114,19 +114,19 @@ inline fun <reified T> FluentQuery.findFirst(isEager: Boolean): T? = findFirst(T
/**
* Finds the last record by the cluster parameters. You can use the below
* way to finish a complicated query:
*
* LitePal.select(&quot;name&quot;).where(&quot;age &gt; ?&quot;, &quot;14&quot;).order(&quot;age&quot;).limit(10).offset(2).findLast&lt;Person&gt;()
*
* ```
* LitePal.select("name").where("age > ?", "14").order("age").limit(10).offset(2).findLast<Person>()
* ```
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
* {@link FluentQuery#findLast(Class, boolean)}.
* [FluentQuery.findLast(Class, boolean)].
*
* @return An object with founded data from database, or null.
*/
inline fun <reified T> FluentQuery.findLast(): T? = findLast(T::class.java)
/**
* It is mostly same as {@link FluentQuery#findLast(Class)} but an isEager
* It is mostly same as [FluentQuery.findLast(Class)] but an isEager
* parameter. If set true the associated models will be loaded as well.
*
* Note that isEager will only work for one deep level relation, considering the query efficiency.
......@@ -140,28 +140,28 @@ inline fun <reified T> FluentQuery.findLast(isEager: Boolean): T? = findLast(T::
/**
* Count the records.
*
* LitePal.count&lt;Person&gt;()
*
* ```
* LitePal.count<Person>()
* ```
* This will count all rows in person table.
*
* You can also specify a where clause when counting.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count&lt;Person&gt;()
*
* ```
* LitePal.where("age > ?", "15").count<Person>()
* ```
* @return Count of the specified table.
*/
inline fun <reified T> FluentQuery.count() = count(T::class.java)
/**
* Calculates the average value on a given column.
*
* LitePal.average&lt;Person&gt;(&quot;age&quot;)
*
* ```
* LitePal.average<Person>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average&lt;Person&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").average<Person>("age")
* ```
* @param column
* The based on column to calculate.
* @return The average value on a given column.
......@@ -171,13 +171,13 @@ inline fun <reified T> FluentQuery.average(column: String) = average(T::class.ja
/**
* Calculates the maximum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.max&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.max<Person, Int>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").max<Person, Int>("age")
* ```
* @param columnName
* The based on column to calculate.
*
......@@ -188,13 +188,13 @@ inline fun <reified T, reified R> FluentQuery.max(columnName: String): R = max(T
/**
* Calculates the maximum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.max&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.max<Int>("person", "age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").max<Int>("person", "age")
* ```
* @param tableName
* Which table to query from.
* @param columnName
......@@ -206,13 +206,13 @@ inline fun <reified R> FluentQuery.max(tableName: String, columnName: String): R
/**
* Calculates the minimum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.min&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.min<Person, Int>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").min<Person, Int>("age")
* ```
* @param columnName
* The based on column to calculate.
* @return The minimum value on a given column.
......@@ -222,13 +222,13 @@ inline fun <reified T, reified R> FluentQuery.min(columnName: String): R = min(T
/**
* Calculates the minimum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.min&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.min<Int>("person", "age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").min<Int>("person", "age")
* ```
* @param tableName
* Which table to query from.
* @param columnName
......@@ -240,13 +240,13 @@ inline fun <reified R> FluentQuery.min(tableName: String, columnName: String): R
/**
* Calculates the sum of values on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.sum&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.sum<Person, Int>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").sum<Person, Int>("age")
* ```
* @param columnName
* The based on column to calculate.
* @return The sum value on a given column.
......@@ -256,13 +256,13 @@ inline fun <reified T, reified R> FluentQuery.sum(columnName: String): R = sum(T
/**
* Calculates the sum of values on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.sum&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.sum<Int>("person", "age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").sum<Int>("person", "age")
* ```
* @param tableName
* Which table to query from.
* @param columnName
......
......@@ -18,6 +18,7 @@ package org.litepal.extension
import android.content.ContentValues
import org.litepal.LitePal
import org.litepal.crud.LitePalSupport
/**
* Extension of LitePal class for Kotlin api.
......@@ -27,15 +28,15 @@ import org.litepal.LitePal
/**
* Count the records.
*
* LitePal.count&lt;Person&gt;()
*
* ```
* LitePal.count<Person>()
* ```
* This will count all rows in person table.
*
* You can also specify a where clause when counting.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).count&lt;Person&gt;()
*
* ```
* LitePal.where("age > ?", "15").count<Person>()
* ```
* @return Count of the specified table.
*/
inline fun <reified T> LitePal.count() = count(T::class.java)
......@@ -50,13 +51,13 @@ inline fun <reified T> LitePal.countAsync() = countAsync(T::class.java)
/**
* Calculates the average value on a given column.
*
* LitePal.average&lt;Person&gt;(&quot;age&quot;)
*
* ```
* LitePal.average<Person>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).average&lt;Person&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").average<Person>("age")
* ```
* @param column
* The based on column to calculate.
* @return The average value on a given column.
......@@ -76,13 +77,13 @@ inline fun <reified T> LitePal.averageAsync(column: String) = averageAsync(T::cl
/**
* Calculates the maximum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.max&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.max<Person, Int>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").max<Person, Int>("age")
* ```
* @param columnName
* The based on column to calculate.
*
......@@ -103,13 +104,13 @@ inline fun <reified T, reified R> LitePal.maxAsync(columnName: String) = maxAsyn
/**
* Calculates the maximum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.max&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.max<Int>("person", "age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).max&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").max<Int>("person", "age")
* ```
* @param tableName
* Which table to query from.
* @param columnName
......@@ -133,13 +134,13 @@ inline fun <reified R> LitePal.maxAsync(tableName: String, columnName: String) =
/**
* Calculates the minimum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.min&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.min<Person, Int>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").min<Person, Int>("age")
* ```
* @param columnName
* The based on column to calculate.
* @return The minimum value on a given column.
......@@ -159,13 +160,13 @@ inline fun <reified T, reified R> LitePal.minAsync(columnName: String) = minAsyn
/**
* Calculates the minimum value on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.min&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.min<Int>("person", "age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).min&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").min<Int>("person", "age")
* ```
* @param tableName
* Which table to query from.
* @param columnName
......@@ -189,13 +190,13 @@ inline fun <reified R> LitePal.minAsync(tableName: String, columnName: String) =
/**
* Calculates the sum of values on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.sum&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.sum<Person, Int>("age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum&lt;Person, Int&gt;(&quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").sum<Person, Int>("age")
* ```
* @param columnName
* The based on column to calculate.
* @return The sum value on a given column.
......@@ -215,13 +216,13 @@ inline fun <reified T, reified R> LitePal.sumAsync(columnName: String) = sumAsyn
/**
* Calculates the sum of values on a given column. The value is returned
* with the same data type of the column.
*
* LitePal.sum&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.sum<Int>("person", "age")
* ```
* You can also specify a where clause when calculating.
*
* LitePal.where(&quot;age &gt; ?&quot;, &quot;15&quot;).sum&lt;Int&gt;(&quot;person&quot;, &quot;age&quot;)
*
* ```
* LitePal.where("age > ?", "15").sum<Int>("person", "age")
* ```
* @param tableName
* Which table to query from.
* @param columnName
......@@ -244,9 +245,9 @@ inline fun <reified R> LitePal.sumAsync(tableName: String, columnName: String) =
/**
* Finds the record by a specific id.
*
* val person = LitePal.find&lt;Person&gt;(1)
*
* ```
* val person = LitePal.find<Person>(1)
* ```
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using[LitePal.find] with isEager parameter.
*
......@@ -295,9 +296,9 @@ inline fun <reified T> LitePal.findAsync(id: Long, isEager: Boolean) = find(T::c
/**
* Finds the first record of a single table.
*
* val person = LitePal.findFirst&lt;Person&gt;()
*
* ```
* val person = LitePal.findFirst<Person>()
* ```
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
* [LitePal.findFirst] with isEager parameter.
......@@ -339,9 +340,9 @@ inline fun <reified T> LitePal.findFirstAsync(isEager: Boolean) = findFirstAsync
/**
* Finds the last record of a single table.
*
* val p = LitePal.findLast&lt;Person&gt;()
*
* ```
* val p = LitePal.findLast<Person>()
* ```
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
* [LitePal.findLast] with isEager parameter.
......@@ -383,17 +384,15 @@ inline fun <reified T> LitePal.findLastAsync(isEager: Boolean) = findLastAsync(T
/**
* Finds multiple records by an id array.
*
* val people = LitePal.findAll&lt;Person&gt;(1, 2, 3)
*
* ```
* val people = LitePal.findAll<Person>(1, 2, 3)
* val bookIds = longArrayOf(10, 18)
*
* LitePal.findAll&lt;Book&gt;(*bookIds)
*
* LitePal.findAll<Book>(*bookIds)
* ```
* Of course you can find all records by passing nothing to the ids
* parameter.
*
* val allBooks = LitePal.findAll&lt;Book&gt;()
* val allBooks = LitePal.findAll<Book>()
*
* Note that the associated models won't be loaded by default considering
* the efficiency, but you can do that by using
......@@ -447,9 +446,9 @@ inline fun <reified T> LitePal.findAllAsync(isEager: Boolean, vararg ids: Long)
*
* The data in other tables which is referenced with the record will be
* removed too.
*
* LitePal.delete&lt;Person&gt;(1)
*
* ```
* LitePal.delete<Person>(1)
* ```
* This means that the record 1 in person table will be removed.
*
* @param id
......@@ -472,9 +471,9 @@ inline fun <reified T> LitePal.deleteAsync(id: Long) = deleteAsync(T::class.java
* Deletes all records with details given if they match a set of conditions
* supplied. This method constructs a single SQL DELETE statement and sends
* it to the database.
*
* LitePal.deleteAll&lt;Person&gt;(&quot;name = ? and age = ?&quot;, &quot;Tom&quot;, &quot;14&quot;)
*
* ```
* LitePal.deleteAll<Person>("name = ? and age = ?", "Tom", "14")
* ```
* This means that all the records which name is Tom and age is 14 will be
* removed.
*
......@@ -509,13 +508,11 @@ inline fun <reified T> LitePal.deleteAllAsync(vararg conditions: String?) = dele
/**
* Updates the corresponding record by id with ContentValues. Returns the
* number of affected rows.
*
* ```
* val cv = ContentValues()
*
* cv.put(&quot;name&quot;, &quot;Jim&quot;)
*
* LitePal.update&lt;Person&gt;(cv, 1)
*
* cv.put("name", "Jim")
* LitePal.update<Person>(cv, 1)
* ```
* This means that the name of record 1 will be updated into Jim.
*
* @param values
......@@ -544,13 +541,11 @@ inline fun <reified T> LitePal.updateAsync(values: ContentValues, id: Long) = up
* Updates all records with details given if they match a set of conditions
* supplied. This method constructs a single SQL UPDATE statement and sends
* it to the database.
*
* ```
* val cv = ContentValues()
*
* cv.put(&quot;name&quot;, &quot;Jim&quot;)
*
* LitePal.update&lt;Person&gt;(cv, &quot;name = ?&quot;, &quot;Tom&quot;)
*
* cv.put("name", "Jim")
* LitePal.update<Person>(cv, "name = ?", "Tom")
* ```
* This means that all the records which name is Tom will be updated into
* Jim.
*
......@@ -596,4 +591,29 @@ inline fun <reified T> LitePal.updateAllAsync(values: ContentValues, vararg cond
* @return Return true if the specified conditions data already exists in the table.
* False otherwise. Null conditions will result in false.
*/
inline fun <reified T> LitePal.isExist(vararg conditions: String?) = isExist(T::class.java, *conditions)
\ No newline at end of file
inline fun <reified T> LitePal.isExist(vararg conditions: String?) = isExist(T::class.java, *conditions)
/**
* Saves the collection into database.
* ```
* val people = listOf<Person>(...)
* people.saveAll();
* ```
* If the model in collection is a new record gets created in the database,
* otherwise the existing record gets updated.
*
* If saving process failed by any accident, the whole action will be
* cancelled and your database will be **rolled back**.
*
* This method acts the same result as the below way, but **much more
* efficient**.
* ```
* for (person in people) {
* person.save()
* }
* ```
*
* @param collection
* Holds all models to save.
*/
fun <T : LitePalSupport> Collection<T>.saveAll() = LitePal.saveAll(this)
\ No newline at end of file
package com.litepaltest.test.crud.save
import android.support.test.filters.SmallTest
import com.litepaltest.model.*
import junit.framework.TestCase
import junit.framework.TestCase.assertEquals
import org.junit.Before
import org.junit.Test
import org.litepal.LitePal.find
import org.litepal.LitePal.findBySQL
import org.litepal.LitePal.saveAll
import org.litepal.LitePal.where
import org.litepal.extension.saveAll
import org.litepal.util.DBUtility
import java.util.*
@SmallTest
class SaveAllKotlinTest {
var classroomTable: String? = null
var studentTable: String? = null
@Before
fun setUp() {
classroomTable = DBUtility.getTableNameByClassName(Classroom::class.java.name)
studentTable = DBUtility.getTableNameByClassName(Student::class.java.name)
}
@Test
fun testSaveAll() {
val cellList: MutableList<Cellphone> = ArrayList()
for (i in 0..49) {
val cellPhone = Cellphone()
cellPhone.setBrand("Samsung unique")
cellPhone.price = Math.random()
cellPhone.serial = UUID.randomUUID().toString()
cellList.add(cellPhone)
}
TestCase.assertTrue(cellList.saveAll())
for (cell in cellList) {
TestCase.assertTrue(cell.isSaved)
}
}
@Test
fun testSaveAllWithM2OOnOneSide() {
val classroom = Classroom()
classroom.name = "Music room"
for (i in 0..49) {
val student = Student()
student.name = "Tom"
student.age = Random().nextInt(20)
classroom.studentCollection.add(student)
}
TestCase.assertTrue(classroom.studentCollection.saveAll())
classroom.save()
val list = where(classroomTable + "_id = ?", classroom._id.toString()).find(Student::class.java)
assertEquals(50, list.size)
}
@Test
fun testSaveAllWithM2OOnManySide() {
val classroom = Classroom()
classroom.name = "English room"
val studentList: MutableList<Student> = ArrayList()
for (i in 0..49) {
val student = Student()
student.name = "Tom"
student.age = Random().nextInt(20)
student.classroom = classroom
studentList.add(student)
}
TestCase.assertTrue(studentList.saveAll())
classroom.save()
val list = where(classroomTable + "_id = ?", classroom._id.toString()).find(Student::class.java)
assertEquals(50, list.size)
}
@Test
fun testSaveAllWithO2O() {
val idcardList: MutableList<IdCard> = ArrayList()
val studentList: MutableList<Student> = ArrayList()
for (i in 0..49) {
val idcard = IdCard()
idcard.number = Random().nextInt(2000000).toString()
val student = Student()
student.name = "Jim"
student.age = Random().nextInt(20)
student.idcard = idcard
idcardList.add(idcard)
studentList.add(student)
}
TestCase.assertTrue(idcardList.saveAll())
TestCase.assertTrue(studentList.saveAll())
for (student in studentList) {
val result = where(studentTable + "_id=?", student.id.toString()).find(IdCard::class.java)
assertEquals(1, result.size)
}
}
@Test
fun testSaveAllWithM2M() {
val studentList: MutableList<Student> = ArrayList()
val teacherList: MutableList<Teacher> = ArrayList()
for (i in 0..49) {
val teacher = Teacher()
teacher.teacherName = "Lucy"
teacher.teachYears = Random().nextInt(10)
teacherList.add(teacher)
}
for (i in 0..49) {
val student = Student()
student.name = "Timmy"
student.age = Random().nextInt(20)
val index1 = Random().nextInt(50)
student.teachers.add(teacherList[index1])
var index2 = index1
while (index2 == index1) {
index2 = Random().nextInt(50)
}
student.teachers.add(teacherList[index2])
var index3 = index2
while (index3 == index2 || index3 == index1) {
index3 = Random().nextInt(50)
}
student.teachers.add(teacherList[index3])
studentList.add(student)
}
TestCase.assertTrue(studentList.saveAll())
TestCase.assertTrue(teacherList.saveAll())
val studentTable = DBUtility.getTableNameByClassName(Student::class.java.name)
val teacherTable = DBUtility.getTableNameByClassName(Teacher::class.java.name)
val tableName = DBUtility.getIntermediateTableName(studentTable, teacherTable)
for (student in studentList) {
val cursor = findBySQL(
"select * from " + tableName + " where " + studentTable + "_id=?", student.id.toString())
assertEquals(3, cursor.count)
cursor.close()
}
}
@Test
fun testSaveAllGenericData() {
val classroomList: MutableList<Classroom> = ArrayList()
for (i in 0..49) {
val classroom = Classroom()
classroom.name = "classroom $i"
for (j in 0..19) {
classroom.news.add("news $i")
}
for (k in 0..12) {
classroom.numbers.add(k)
}
classroomList.add(classroom)
}
TestCase.assertTrue(classroomList.saveAll())
assertEquals(50, classroomList.size)
for (classroom in classroomList) {
TestCase.assertTrue(classroom.isSaved)
val c = find(Classroom::class.java, classroom._id.toLong())
TestCase.assertTrue(c.name.startsWith("classroom"))
assertEquals(20, c.news.size)
assertEquals(13, c.numbers.size)
}
}
@Test
fun testSaveAllFailed() {
val cellphones: MutableList<Cellphone> = ArrayList()
val serial = UUID.randomUUID().toString()
for (i in 0..19) {
val cellphone = Cellphone()
cellphone.setBrand("Apple")
cellphone.serial = serial + i % 10 // serial is unique, so this should save failed
cellphones.add(cellphone)
}
TestCase.assertFalse(cellphones.saveAll())
val list = where("serial like ?", "$serial%").find(Cellphone::class.java)
TestCase.assertTrue(list.isEmpty())
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册