Skip to content

Commit

Permalink
Merge pull request #1437 from danvwells/tests
Browse files Browse the repository at this point in the history
updating a few unit tests
  • Loading branch information
agrosner authored Dec 28, 2017
2 parents c6decde + 78debd6 commit db2caad
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.raizlabs.android.dbflow.processor.test

import com.raizlabs.android.dbflow.processor.definition.column.*
import com.raizlabs.android.dbflow.processor.definition.column.PrimaryReferenceAccessCombiner
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.NameAllocator
import com.squareup.javapoet.TypeName
import org.junit.Assert.assertEquals
import org.junit.Test
Expand All @@ -21,9 +21,11 @@ class ContentValuesCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"), -1)
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"), -1)
}

assertEquals("values.put(\"columnName\", model.name != null ? model.name : \"nonNull\");",
assertEquals("values.put(\"`columnName`\", model.name != null ? model.name : \"nonNull\");",
codeBuilder.build().toString().trim())
}

Expand All @@ -33,9 +35,11 @@ class ContentValuesCombinerTest {
TypeName.get(Boolean::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", index = -1)
combiner.apply {
codeBuilder.addCode("columnName", index = -1)
}

assertEquals("values.put(\"columnName\", model.name);",
assertEquals("values.put(\"`columnName`\", model.name);",
codeBuilder.build().toString().trim())
}

Expand All @@ -48,10 +52,12 @@ class ContentValuesCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"), -1)
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"), -1)
}

assertEquals("java.lang.String refname = com.fuzz.android.TestType_Helper.getName(model) != null ? global_converter.getDBValue(com.fuzz.android.TestType_Helper.getName(model)) : null;"
+ "\nvalues.put(\"columnName\", refname != null ? refname : \"nonNull\");",
+ "\nvalues.put(\"`columnName`\", refname != null ? refname : \"nonNull\");",
codeBuilder.build().toString().trim())
}

Expand All @@ -64,10 +70,12 @@ class ContentValuesCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"), -1)
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"), -1)
}

assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;"
+ "\nvalues.put(\"columnName\", refname != null ? refname : \"nonNull\");",
assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;"
+ "\nvalues.put(\"`columnName`\", refname != null ? refname : \"nonNull\");",
codeBuilder.build().toString().trim())
}

Expand All @@ -82,9 +90,11 @@ class SqliteStatementAccessCombinerTest {
Combiner(VisibleScopeColumnAccessor("name"), TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", CodeBlock.of("\$S", "nonNull"), 0)
combiner.apply {
codeBuilder.addCode("start", CodeBlock.of("\$S", "nonNull"), 0)
}

assertEquals("if (model.name != null) {" +
assertEquals("if (model.name != null) {" +
"\n statement.bindString(0 + start, model.name);" +
"\n} else {" +
"\n statement.bindString(0 + start, \"nonNull\");" +
Expand All @@ -100,9 +110,11 @@ class SqliteStatementAccessCombinerTest {
TypeName.get(Boolean::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", index = 0)
combiner.apply {
codeBuilder.addCode("start", index = 0)
}

assertEquals("statement.bindLong(0 + start, model.name ? 1 : 0);",
assertEquals("statement.bindLong(0 + start, model.name ? 1 : 0);",
codeBuilder.build().toString().trim())
}

Expand All @@ -115,10 +127,12 @@ class SqliteStatementAccessCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", CodeBlock.of("\$S", "nonNull"), 1)
combiner.apply {
codeBuilder.addCode("start", CodeBlock.of("\$S", "nonNull"), 1)
}

assertEquals("java.lang.String refname = com.fuzz.android.TestType_Helper.getName(model) != null ? global_converter.getDBValue(com.fuzz.android.TestType_Helper.getName(model)) : null;" +
"\nif (refname != null) {" +
assertEquals("java.lang.String refname = com.fuzz.android.TestType_Helper.getName(model) != null ? global_converter.getDBValue(com.fuzz.android.TestType_Helper.getName(model)) : null;" +
"\nif (refname != null) {" +
"\n statement.bindString(1 + start, refname);" +
"\n} else {" +
"\n statement.bindString(1 + start, \"nonNull\");" +
Expand All @@ -134,10 +148,11 @@ class SqliteStatementAccessCombinerTest {
TypeName.get(String::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "start", CodeBlock.of("\$S", "nonNull"), 1)

assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;" +
"\nif (refname != null) {" +
combiner.apply {
codeBuilder.addCode("start", CodeBlock.of("\$S", "nonNull"), 1)
}
assertEquals("java.lang.String refname = model.getName() != null ? global_converter.getDBValue(model.getName()) : null;" +
"\nif (refname != null) {" +
"\n statement.bindString(1 + start, refname);" +
"\n} else {" +
"\n statement.bindString(1 + start, \"nonNull\");" +
Expand All @@ -151,16 +166,13 @@ class LoadFromCursorAccessCombinerTest {
@Test
fun test_simpleCase() {
val combiner = LoadFromCursorAccessCombiner(
Combiner(VisibleScopeColumnAccessor("name"), TypeName.get(String::class.java)))
Combiner(VisibleScopeColumnAccessor("name"), TypeName.get(String::class.java)), false, NameAllocator())
val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"))
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"))
}

assertEquals("int index_columnName = cursor.getColumnIndex(\"columnName\");" +
"\nif (index_columnName != -1 && !cursor.isNull(index_columnName)) {" +
"\n model.name = cursor.getString(index_columnName);" +
"\n} else {" +
"\n model.name = \"nonNull\";" +
"\n}", codeBuilder.build().toString().trim())
assertEquals("model.name = cursor.getStringOrDefault(\"columnName\");", codeBuilder.build().toString().trim())
}

@Test
Expand All @@ -169,9 +181,11 @@ class LoadFromCursorAccessCombinerTest {
Combiner(VisibleScopeColumnAccessor("name"),
TypeName.get(Date::class.java),
wrapperLevelAccessor = TypeConverterScopeColumnAccessor("global_converter"),
wrapperFieldTypeName = TypeName.get(String::class.java)))
wrapperFieldTypeName = TypeName.get(String::class.java)),false, NameAllocator())
val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "columnName", CodeBlock.of("\$S", "nonNull"))
combiner.apply {
codeBuilder.addCode("columnName", CodeBlock.of("\$S", "nonNull"))
}

assertEquals("int index_columnName = cursor.getColumnIndex(\"columnName\");" +
"\nif (index_columnName != -1 && !cursor.isNull(index_columnName)) {" +
Expand All @@ -191,7 +205,9 @@ class PrimaryReferenceAccessCombiner {
Combiner(VisibleScopeColumnAccessor("id"), TypeName.get(Long::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "id")
combiner.apply {
codeBuilder.addCode("id")
}

assertEquals("clause.and(id.eq(model.id));", codeBuilder.build().toString().trim())
}
Expand All @@ -205,7 +221,9 @@ class PrimaryReferenceAccessCombiner {
TypeName.get(Date::class.java)))

val codeBuilder = CodeBlock.builder()
combiner.addCode(codeBuilder, "id")
combiner.apply {
codeBuilder.addCode("id")
}

assertEquals("java.util.Date refid = model.id != null ? global_converter.getDBValue(model.id) : null;\n" +
"clause.and(id.invertProperty().eq(refid));",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ class ForeignKeyAccessCombinerTest {
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(4))

assertEquals("if (model.name != null) {" +
"\n values.put(\"test\", model.name.test);" +
"\n values.put(\"test2\", model.name.getTest2());" +
"\n values.put(\"`test`\", model.name.test);" +
"\n values.put(\"`test2`\", model.name.getTest2());" +
"\n} else {" +
"\n values.putNull(\"test\");" +
"\n values.putNull(\"test2\");" +
"\n values.putNull(\"`test`\");" +
"\n values.putNull(\"`test2`\");" +
"\n}",
builder.build().toString().trim())
}
Expand All @@ -51,7 +51,7 @@ class ForeignKeyAccessCombinerTest {
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(4))

assertEquals("if (model.getName() != null) {" +
"\n statement.bindString(4 + start, model.getName().test);" +
"\n statement.bindStringOrNull(4 + start, model.getName().test);" +
"\n} else {" +
"\n statement.bindNull(4 + start);" +
"\n}",
Expand Down Expand Up @@ -93,11 +93,11 @@ class ForeignKeyAccessCombinerTest {
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(1))

assertEquals("if (model.modem != null) {" +
"\n values.put(\"number\", com.fuzz.AnotherHelper\$Helper.getNumber(model.modem));" +
"\n values.put(\"date\", global_converter.getDBValue(model.modem.date));" +
"\n values.put(\"`number`\", com.fuzz.AnotherHelper\$Helper.getNumber(model.modem));" +
"\n values.put(\"`date`\", global_converter.getDBValue(model.modem.date));" +
"\n} else {" +
"\n values.putNull(\"number\");" +
"\n values.putNull(\"date\");" +
"\n values.putNull(\"`number`\");" +
"\n values.putNull(\"`date`\");" +
"\n}",
builder.build().toString().trim())
}
Expand All @@ -116,12 +116,12 @@ class ForeignKeyAccessCombinerTest {
val builder = CodeBlock.builder()
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(0))

assertEquals("int index_testmodel_id = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id != -1 && !cursor.isNull(index_testmodel_id) && index_testmodel_type != -1 && !cursor.isNull(index_testmodel_type)) {" +
assertEquals("int index_testmodel_id_ParentModel_Table = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type_ParentModel_Table = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_id_ParentModel_Table) && index_testmodel_type_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_type_ParentModel_Table)) {" +
"\n model.testModel1 = com.raizlabs.android.dbflow.sql.language.SQLite.select().from(com.raizlabs.android.dbflow.test.container.ParentModel.class).where()" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.name.eq(cursor.getString(index_testmodel_id)))" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.type.eq(cursor.getString(index_testmodel_type)))" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.name.eq(cursor.getString(index_testmodel_id_ParentModel_Table)))" +
"\n .and(com.raizlabs.android.dbflow.test.container.ParentModel_Table.type.eq(cursor.getString(index_testmodel_type_ParentModel_Table)))" +
"\n .querySingle();" +
"\n} else {" +
"\n model.testModel1 = null;" +
Expand All @@ -142,12 +142,12 @@ class ForeignKeyAccessCombinerTest {
val builder = CodeBlock.builder()
foreignKeyAccessCombiner.addCode(builder, AtomicInteger(0))

assertEquals("int index_testmodel_id = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id != -1 && !cursor.isNull(index_testmodel_id) && index_testmodel_type != -1 && !cursor.isNull(index_testmodel_type)) {" +
assertEquals("int index_testmodel_id_ParentModel_Table = cursor.getColumnIndex(\"testmodel_id\");" +
"\nint index_testmodel_type_ParentModel_Table = cursor.getColumnIndex(\"testmodel_type\");" +
"\nif (index_testmodel_id_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_id_ParentModel_Table) && index_testmodel_type_ParentModel_Table != -1 && !cursor.isNull(index_testmodel_type_ParentModel_Table)) {" +
"\n model.testModel1 = new com.raizlabs.android.dbflow.test.container.ParentModel();" +
"\n model.testModel1.name = cursor.getString(index_testmodel_id);" +
"\n model.testModel1.type = cursor.getString(index_testmodel_type);" +
"\n model.testModel1.name = cursor.getString(index_testmodel_id_ParentModel_Table);" +
"\n model.testModel1.type = cursor.getString(index_testmodel_type_ParentModel_Table);" +
"\n} else {" +
"\n model.testModel1 = null;" +
"\n}", builder.build().toString().trim())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ class BooleanTypeColumnAccessorTest() {
@Test
fun test_canSetBoolean() {
val access = BooleanColumnAccessor()
assertEquals("cursor.getInt(index) == 1 ? true : false",
access.set(CodeBlock.of("cursor.getInt(index)")).toString())
assertEquals("cursor.getBoolean(index)",
access.set(CodeBlock.of("cursor.getBoolean(index)")).toString())
}
}

Expand Down

0 comments on commit db2caad

Please sign in to comment.