From 1896005d5c5570bab36d1fcf9b509e19b5eca590 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 19:09:37 -0200 Subject: [PATCH 01/23] Add interface PrimariKey --- library/src/main/java/com/orm/dsl/PrimaryKey.java | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 library/src/main/java/com/orm/dsl/PrimaryKey.java diff --git a/library/src/main/java/com/orm/dsl/PrimaryKey.java b/library/src/main/java/com/orm/dsl/PrimaryKey.java new file mode 100644 index 00000000..939eecc7 --- /dev/null +++ b/library/src/main/java/com/orm/dsl/PrimaryKey.java @@ -0,0 +1,6 @@ +package com.orm.dsl; + +/** + * Created by diogosq on 1/6/16. + */ +public interface PrimaryKey{} From 0bed2a1b2236a5195fa44bd7a1bfdafbe15e486c Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 19:09:53 -0200 Subject: [PATCH 02/23] Add interface PrimariKey --- library/src/main/java/com/orm/dsl/PrimaryKey.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/com/orm/dsl/PrimaryKey.java b/library/src/main/java/com/orm/dsl/PrimaryKey.java index 939eecc7..7ef2a3c1 100644 --- a/library/src/main/java/com/orm/dsl/PrimaryKey.java +++ b/library/src/main/java/com/orm/dsl/PrimaryKey.java @@ -1,6 +1,7 @@ package com.orm.dsl; -/** - * Created by diogosq on 1/6/16. - */ -public interface PrimaryKey{} +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention (RetentionPolicy.RUNTIME) +public @interface PrimaryKey{} \ No newline at end of file From 3e3e3eaa640f382fc07582eaab6748dc57545381 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 19:28:29 -0200 Subject: [PATCH 03/23] ADD findById using primaryKey notation --- .../src/main/java/com/orm/SugarRecord.java | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 4b9e04ef..1cffaf74 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -8,6 +8,9 @@ import android.text.TextUtils; import android.util.Log; +import com.orm.dsl.Ignore; +import com.orm.dsl.NotNull; +import com.orm.dsl.PrimaryKey; import com.orm.dsl.Table; import com.orm.dsl.Unique; import com.orm.util.NamingHelper; @@ -16,9 +19,11 @@ import com.orm.util.SugarCursor; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; @@ -131,8 +136,39 @@ public static List listAll(Class type, String orderBy) { return find(type, null, null, null, orderBy, null); } + /** + * Find field of parameter using @PrimaryKey notation + * @return One field of class + */ + private static Field findPrimaryKeyNotationField(@NotNull Class type){ + Field primaryField = null; + Field[] fields = type.getDeclaredFields(); + for(Field field : fields){ + + if(!field.isAnnotationPresent(Ignore.class) + && !Modifier.isStatic(field.getModifiers()) + && !Modifier.isTransient(field.getModifiers())){ + + if(field.isAnnotationPresent(PrimaryKey.class)){ + primaryField = field; + } + + } + } + return primaryField; + } + public static T findById(Class type, Long id) { - List list = find(type, "id=?", new String[]{String.valueOf(id)}, null, null, "1"); + List list; + + if (type.isAnnotationPresent(PrimaryKey.class)) { + Field primaryKeyField = findPrimaryKeyNotationField(type); + String pk = NamingHelper.toSQLName(primaryKeyField); + list = find(type, pk + "=?", new String[] {String.valueOf(id)}, null, null, "1"); + }else{ + list = find(type, "id=?", new String[]{String.valueOf(id)}, null, null, "1"); + } + if (list.isEmpty()) return null; return list.get(0); } From b0ccc215ab8567ecac78f941ec41e3a33ab330ae Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 19:31:35 -0200 Subject: [PATCH 04/23] ADD findById using where and primaryKey notation --- library/src/main/java/com/orm/SugarRecord.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 1cffaf74..b87acc15 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -178,7 +178,16 @@ public static T findById(Class type, Integer id) { } public static List findById(Class type, String[] ids) { - String whereClause = "id IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; + String whereClause; + + if (type.isAnnotationPresent(PrimaryKey.class)) { + Field primaryKeyField = findPrimaryKeyNotationField(type); + String pk = NamingHelper.toSQLName(primaryKeyField); + whereClause = pk+" IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; + }else{ + whereClause = "id IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; + } + return find(type, whereClause, ids); } From 53729a8193d2c19d48fadfdc9ff8462a72fb063d Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 19:42:43 -0200 Subject: [PATCH 05/23] ADD save using primaryKey notation --- .../src/main/java/com/orm/SugarRecord.java | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index b87acc15..63b13364 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -310,18 +310,37 @@ static long save(SQLiteDatabase db, Object object) { List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); Field idField = null; - for (Field column : columns) { - ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); - if (column.getName().equals("id")) { - idField = column; - } - } boolean isSugarEntity = isSugarEntity(object.getClass()); - if (isSugarEntity && entitiesMap.containsKey(object)) { + + if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ + for(Field column : columns){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + + if(!column.isAnnotationPresent(Ignore.class)){ + if(column.isAnnotationPresent(PrimaryKey.class)){ + idField = column; + } + } + } + + if (isSugarEntity && entitiesMap.containsKey(object)) { + values.put(NamingHelper.toSQLName(idField), entitiesMap.get(object)); + } + }else{ + for(Field column : columns){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + if(column.getName().equals("id")){ + idField = column; + } + } + + if (isSugarEntity && entitiesMap.containsKey(object)) { values.put("id", entitiesMap.get(object)); + } } + long id = db.insertWithOnConflict(NamingHelper.toSQLName(object.getClass()), null, values, SQLiteDatabase.CONFLICT_REPLACE); From 131b58f4a06cc27317c42be57a4266a13963dc3f Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 19:46:50 -0200 Subject: [PATCH 06/23] ADD update using primaryKey notation --- library/src/main/java/com/orm/SugarRecord.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 63b13364..c25542a1 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -389,8 +389,15 @@ static long update(SQLiteDatabase db, Object object) { e.printStackTrace(); } } else { - if (!column.getName().equals("id")) { - ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(!column.getName().equals(NamingHelper.toSQLName(idField))){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + } + }else{ + if(!column.getName().equals("id")){ + ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); + } } } } From 6a09b0413d7999255d7197c2d17f0c1ccfad5eb8 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 19:49:32 -0200 Subject: [PATCH 07/23] ADD inflate using primaryKey notation --- library/src/main/java/com/orm/SugarRecord.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index c25542a1..4ab50c92 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -421,10 +421,20 @@ public static boolean isSugarEntity(Class objectClass) { private static void inflate(Cursor cursor, Object object, Map entitiesMap) { List columns = ReflectionUtil.getTableFields(object.getClass()); - if (!entitiesMap.containsKey(object)) { - entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex(("ID")))); + + if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ + if (!entitiesMap.containsKey(object)) { + Field idField = findPrimaryKeyNotationField(object.getClass()); + entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex((NamingHelper.toSQLName(idField))))); + } + }else{ + if (!entitiesMap.containsKey(object)) { + entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex(("ID")))); + } } + + for (Field field : columns) { field.setAccessible(true); Class fieldType = field.getType(); From 3f695c41697b69876eed720020eb40c4ad127766 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 20:05:59 -0200 Subject: [PATCH 08/23] PROBLEM DELETE METHOD WITHOUT OBJECT CAN BE USE PRIMARYKEY NOTATION --- library/src/main/java/com/orm/SugarRecord.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 4ab50c92..7d73b982 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -451,6 +451,8 @@ private static void inflate(Cursor cursor, Object object, Map enti } } + //FIXME I don't know how make this method using PrimaryKey notation without the 'Object'. Suggestions? + @Deprecated public boolean delete() { Long id = getId(); Class type = getClass(); @@ -506,6 +508,7 @@ void inflate(Cursor cursor) { inflate(cursor, this, getSugarContext().getEntitiesMap()); } + public Long getId() { return id; } From 62537a7af28b1431c51602be462e92ba99f11cbd Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 20:16:25 -0200 Subject: [PATCH 09/23] ADD delete using object and primaryKey notation --- .../src/main/java/com/orm/SugarRecord.java | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 7d73b982..228420c4 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -469,7 +469,11 @@ public static boolean delete(Object object) { Class type = object.getClass(); if (type.isAnnotationPresent(Table.class)) { try { - Field field = type.getDeclaredField("id"); + + Field field = (object.getClass().isAnnotationPresent(PrimaryKey.class)) + ?findPrimaryKeyNotationField(type) + :type.getDeclaredField("id"); + field.setAccessible(true); Long id = (Long) field.get(object); if (id != null && id > 0L) { @@ -488,7 +492,27 @@ public static boolean delete(Object object) { return false; } } else if (SugarRecord.class.isAssignableFrom(type)) { - return ((SugarRecord) object).delete(); + if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ + Field idField = findPrimaryKeyNotationField(type); + idField.setAccessible(true); + Long id = null; + try{ + id = idField.getLong(object); + }catch(IllegalAccessException e){ + Log.i(SUGAR, "Cannot delete object using primaryKey notation " + e.getMessage(),e); + return false; + } + if (id != null && id > 0L) { + Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); + return getSugarDataBase().delete(NamingHelper.toSQLName(type), NamingHelper.toSQLName(idField)+"=?", + new String[]{id.toString()}) == 1; + } else { + Log.i(SUGAR, "Cannot delete object: " + type.getSimpleName() + " - object has not been saved"); + return false; + } + }else{ + return ((SugarRecord) object).delete(); + } } else { Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - not persisted"); return false; From 24e769b773ed0db02c7d9cbfd1cdaafee1b3c944 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Wed, 6 Jan 2016 20:17:58 -0200 Subject: [PATCH 10/23] ADD last FIXME TO THIS --- library/src/main/java/com/orm/SugarRecord.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 228420c4..e1dee11c 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -532,11 +532,12 @@ void inflate(Cursor cursor) { inflate(cursor, this, getSugarContext().getEntitiesMap()); } - + //FIXME I don't know how make this method using PrimaryKey notation without the 'Object'. Suggestions? public Long getId() { return id; } + //FIXME I don't know how make this method using PrimaryKey notation without the 'Object'. Suggestions? public void setId(Long id) { this.id = id; } From 61d15614e0bb496492b38ae42afed0500279fb88 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Thu, 7 Jan 2016 22:39:04 -0200 Subject: [PATCH 11/23] ADD SchemaGenerator to use primaryKey notation --- .../main/java/com/orm/SchemaGenerator.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index f5fd4f2a..5478c526 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -9,6 +9,7 @@ import com.orm.dsl.Column; import com.orm.dsl.MultiUnique; import com.orm.dsl.NotNull; +import com.orm.dsl.PrimaryKey; import com.orm.dsl.Unique; import com.orm.util.MigrationFileParser; import com.orm.util.NamingHelper; @@ -181,15 +182,32 @@ protected String createTableSQL(Class table) { List fields = ReflectionUtil.getTableFields(table); String tableName = NamingHelper.toSQLName(table); StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS "); - sb.append(tableName).append(" ( ID INTEGER PRIMARY KEY AUTOINCREMENT "); + + Field idField = null; + for (Field column : fields) { + if (column.isAnnotationPresent(PrimaryKey.class)) { + idField = column; + break; + } + } + + sb.append(tableName).append((idField != null)?" ( ID INTEGER PRIMARY KEY AUTOINCREMENT " + :" ( "+NamingHelper.toSQLName(idField)+" INTEGER PRIMARY KEY AUTOINCREMENT "); for (Field column : fields) { String columnName = NamingHelper.toSQLName(column); String columnType = QueryBuilder.getColumnType(column.getType()); if (columnType != null) { - if (columnName.equalsIgnoreCase("Id")) { - continue; + + if(idField != null){ + if(column.isAnnotationPresent(PrimaryKey.class)){ + continue; + } + }else{ + if (columnName.equalsIgnoreCase("Id")) { + continue; + } } if (column.isAnnotationPresent(Column.class)) { From e0a468cf8a2c60e6d7a49a8fb6ff4beebd35d0e1 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Thu, 7 Jan 2016 23:25:25 -0200 Subject: [PATCH 12/23] ADD Bugfix if condition to use primaryKey notation --- library/src/main/java/com/orm/SchemaGenerator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/src/main/java/com/orm/SchemaGenerator.java b/library/src/main/java/com/orm/SchemaGenerator.java index 5478c526..ad3b3511 100644 --- a/library/src/main/java/com/orm/SchemaGenerator.java +++ b/library/src/main/java/com/orm/SchemaGenerator.java @@ -191,7 +191,7 @@ protected String createTableSQL(Class table) { } } - sb.append(tableName).append((idField != null)?" ( ID INTEGER PRIMARY KEY AUTOINCREMENT " + sb.append(tableName).append((idField == null)?" ( ID INTEGER PRIMARY KEY AUTOINCREMENT " :" ( "+NamingHelper.toSQLName(idField)+" INTEGER PRIMARY KEY AUTOINCREMENT "); for (Field column : fields) { From 92fc2ca52bc4159e0ca0e8f50968e4ce0ffdf721 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Thu, 7 Jan 2016 23:39:03 -0200 Subject: [PATCH 13/23] ADD JUnit BigDecimal using pk notation and table notation without id field --- ...NotationBigDecimalFieldAnnotatedModel.java | 28 +++++++++++++++++++ .../sugartest/BigDecimalFieldTests.java | 26 +++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 example/src/main/java/com/example/models/PrimaryKeyNotationBigDecimalFieldAnnotatedModel.java diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationBigDecimalFieldAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationBigDecimalFieldAnnotatedModel.java new file mode 100644 index 00000000..23788a7a --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationBigDecimalFieldAnnotatedModel.java @@ -0,0 +1,28 @@ +package com.example.models; + +import com.orm.dsl.PrimaryKey; +import com.orm.dsl.Table; + +import java.math.BigDecimal; + +@Table +public class PrimaryKeyNotationBigDecimalFieldAnnotatedModel{ + + @PrimaryKey + private Long myId; + private BigDecimal decimal; + + public PrimaryKeyNotationBigDecimalFieldAnnotatedModel() {} + + public PrimaryKeyNotationBigDecimalFieldAnnotatedModel(BigDecimal decimal) { + this.decimal = decimal; + } + + public BigDecimal getBigDecimal() { + return decimal; + } + + public Long getMyId() { + return myId; + } +} diff --git a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java b/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java index 9e4b36bb..e1a06689 100644 --- a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java +++ b/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java @@ -3,6 +3,7 @@ import com.example.models.BigDecimalFieldAnnotatedModel; import com.example.models.BigDecimalFieldExtendedModel; +import com.example.models.PrimaryKeyNotationBigDecimalFieldAnnotatedModel; import com.orm.SugarRecord; import org.junit.Test; @@ -50,4 +51,29 @@ public void bigDecimalAnnotatedTest() { SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); assertEquals(decimal, model.getBigDecimal()); } + @Test + public void primryKeyNotationNullBigDecimalExtendedTest() { + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel()); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertNull(model.getBigDecimal()); + } + + @Test + public void primryKeyNotationBigDecimalExtendedTest() { + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } + + @Test + public void primryKeyNotationBigDecimalAnnotatedTest() { + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } } From e3cb720e20bcea517e9991895d8119e0771a3c04 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Thu, 7 Jan 2016 23:52:23 -0200 Subject: [PATCH 14/23] ADD JUnit BooleanField test using pk notation and table notation without id field --- ...rimaryKeyNotationBooleanFieldAnnotatedModel.java} | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) rename example/src/main/java/com/example/models/{BooleanFieldExtendedModel.java => PrimaryKeyNotationBooleanFieldAnnotatedModel.java} (50%) diff --git a/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java similarity index 50% rename from example/src/main/java/com/example/models/BooleanFieldExtendedModel.java rename to example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java index 972855cd..8b5cbe7c 100644 --- a/example/src/main/java/com/example/models/BooleanFieldExtendedModel.java +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java @@ -1,18 +1,20 @@ package com.example.models; -import com.orm.SugarRecord; +import com.orm.dsl.Table; -public class BooleanFieldExtendedModel extends SugarRecord { +@Table +public class PrimaryKeyNotationBooleanFieldAnnotatedModel{ private Boolean objectBoolean; private boolean rawBoolean; + private Long id; - public BooleanFieldExtendedModel() {} + public PrimaryKeyNotationBooleanFieldAnnotatedModel() {} - public BooleanFieldExtendedModel(Boolean objectBoolean) { + public PrimaryKeyNotationBooleanFieldAnnotatedModel(Boolean objectBoolean) { this.objectBoolean = objectBoolean; } - public BooleanFieldExtendedModel(boolean rawBoolean) { + public PrimaryKeyNotationBooleanFieldAnnotatedModel(boolean rawBoolean) { this.rawBoolean = rawBoolean; } From 89c43b0968489bbfb110b635d2cfd5296f780662 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Thu, 7 Jan 2016 23:52:38 -0200 Subject: [PATCH 15/23] ADD JUnit BooleanField test using pk notation and table notation without id field --- ...KeyNotationBooleanFieldAnnotatedModel.java | 11 +-- .../example/sugartest/BooleanFieldTests.java | 76 ++++++++++++++++--- 2 files changed, 73 insertions(+), 14 deletions(-) diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java index 8b5cbe7c..1e6dbae8 100644 --- a/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationBooleanFieldAnnotatedModel.java @@ -1,16 +1,17 @@ package com.example.models; +import com.orm.dsl.PrimaryKey; import com.orm.dsl.Table; @Table public class PrimaryKeyNotationBooleanFieldAnnotatedModel{ - private Boolean objectBoolean; - private boolean rawBoolean; - private Long id; + private Boolean objectBoolean; + private boolean rawBoolean; + @PrimaryKey private Long myId; - public PrimaryKeyNotationBooleanFieldAnnotatedModel() {} + public PrimaryKeyNotationBooleanFieldAnnotatedModel(){} - public PrimaryKeyNotationBooleanFieldAnnotatedModel(Boolean objectBoolean) { + public PrimaryKeyNotationBooleanFieldAnnotatedModel(Boolean objectBoolean){ this.objectBoolean = objectBoolean; } diff --git a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java b/example/src/test/java/com/example/sugartest/BooleanFieldTests.java index a117af6c..55cf997f 100644 --- a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java +++ b/example/src/test/java/com/example/sugartest/BooleanFieldTests.java @@ -2,7 +2,7 @@ import com.example.models.BooleanFieldAnnotatedModel; -import com.example.models.BooleanFieldExtendedModel; +import com.example.models.PrimaryKeyNotationBooleanFieldAnnotatedModel; import com.orm.SugarRecord; import org.junit.Test; @@ -18,15 +18,15 @@ public class BooleanFieldTests { @Test public void nullBooleanExtendedTest() { - save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + save(new BooleanFieldAnnotatedModel()); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertNull(model.getBoolean()); } @Test public void nullRawBooleanExtendedTest() { - save(new BooleanFieldExtendedModel()); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + save(new BooleanFieldAnnotatedModel()); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(false, model.getRawBoolean()); } @@ -47,15 +47,15 @@ public void nullRawBooleanAnnotatedTest() { @Test public void objectBooleanExtendedTest() { Boolean objectBoolean = new Boolean(true); - save(new BooleanFieldExtendedModel(objectBoolean)); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + save(new BooleanFieldAnnotatedModel(objectBoolean)); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(objectBoolean, model.getBoolean()); } @Test public void rawBooleanExtendedTest() { - save(new BooleanFieldExtendedModel(true)); - BooleanFieldExtendedModel model = SugarRecord.findById(BooleanFieldExtendedModel.class, 1); + save(new BooleanFieldAnnotatedModel(true)); + BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); } @@ -73,4 +73,62 @@ public void rawBooleanAnnotatedTest() { BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); } + + @Test + public void primaryKeyNullBooleanExtendedTest() { + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void primaryKeynullRawBooleanExtendedTest() { + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationNullBooleanAnnotatedTest() { + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void primaryKeyNotationNullRawBooleanAnnotatedTest() { + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationObjectBooleanExtendedTest() { + Boolean objectBoolean = new Boolean(true); + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(objectBoolean, model.getBoolean()); + } + + @Test + public void primaryKeyNotationRawBooleanExtendedTest() { + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationObjectBooleanAnnotatedTest() { + Boolean objectBoolean = new Boolean(true); + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(objectBoolean, model.getBoolean()); + } + + @Test + public void primaryKeyNotationRawBooleanAnnotatedTest() { + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } } From 2403f0978857337431b5e20c9b2c984f25157301 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 00:11:07 -0200 Subject: [PATCH 16/23] BUGFIX Primary key find logic --- .../src/main/java/com/orm/SugarRecord.java | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index e1dee11c..3ce0756b 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -160,9 +160,8 @@ private static Field findPrimaryKeyNotationField(@NotNull Class type){ public static T findById(Class type, Long id) { List list; - - if (type.isAnnotationPresent(PrimaryKey.class)) { - Field primaryKeyField = findPrimaryKeyNotationField(type); + Field primaryKeyField = findPrimaryKeyNotationField(type); + if (primaryKeyField != null) { String pk = NamingHelper.toSQLName(primaryKeyField); list = find(type, pk + "=?", new String[] {String.valueOf(id)}, null, null, "1"); }else{ @@ -179,9 +178,8 @@ public static T findById(Class type, Integer id) { public static List findById(Class type, String[] ids) { String whereClause; - - if (type.isAnnotationPresent(PrimaryKey.class)) { - Field primaryKeyField = findPrimaryKeyNotationField(type); + Field primaryKeyField = findPrimaryKeyNotationField(type); + if (primaryKeyField != null) { String pk = NamingHelper.toSQLName(primaryKeyField); whereClause = pk+" IN (" + QueryBuilder.generatePlaceholders(ids.length) + ")"; }else{ @@ -389,8 +387,8 @@ static long update(SQLiteDatabase db, Object object) { e.printStackTrace(); } } else { - if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ - Field idField = findPrimaryKeyNotationField(object.getClass()); + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ if(!column.getName().equals(NamingHelper.toSQLName(idField))){ ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); } @@ -422,9 +420,9 @@ public static boolean isSugarEntity(Class objectClass) { private static void inflate(Cursor cursor, Object object, Map entitiesMap) { List columns = ReflectionUtil.getTableFields(object.getClass()); - if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ if (!entitiesMap.containsKey(object)) { - Field idField = findPrimaryKeyNotationField(object.getClass()); entitiesMap.put(object, cursor.getLong(cursor.getColumnIndex((NamingHelper.toSQLName(idField))))); } }else{ @@ -470,9 +468,8 @@ public static boolean delete(Object object) { if (type.isAnnotationPresent(Table.class)) { try { - Field field = (object.getClass().isAnnotationPresent(PrimaryKey.class)) - ?findPrimaryKeyNotationField(type) - :type.getDeclaredField("id"); + Field idField = findPrimaryKeyNotationField(object.getClass()); + Field field = (idField != null)?idField:type.getDeclaredField("id"); field.setAccessible(true); Long id = (Long) field.get(object); @@ -492,8 +489,8 @@ public static boolean delete(Object object) { return false; } } else if (SugarRecord.class.isAssignableFrom(type)) { - if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ - Field idField = findPrimaryKeyNotationField(type); + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ idField.setAccessible(true); Long id = null; try{ From 39e4e60b95781301c71fe7c765d4d3abf19f4994 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 18:28:38 -0200 Subject: [PATCH 17/23] ADD SimpleAnnotatedModelTest using pk notation --- ...rimaryKeyNotationSimpleAnnotatedModel.java | 16 + .../PrimaryKeySimpleAnnotatedModelTests.java | 356 ++++++++++++++++++ 2 files changed, 372 insertions(+) create mode 100644 example/src/main/java/com/example/models/PrimaryKeyNotationSimpleAnnotatedModel.java create mode 100644 example/src/test/java/com/example/sugartest/PrimaryKeySimpleAnnotatedModelTests.java diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleAnnotatedModel.java new file mode 100644 index 00000000..e539238b --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleAnnotatedModel.java @@ -0,0 +1,16 @@ +package com.example.models; + +import com.orm.dsl.PrimaryKey; +import com.orm.dsl.Table; + +@Table +public class PrimaryKeyNotationSimpleAnnotatedModel{ + + @PrimaryKey private Long myId; + + public PrimaryKeyNotationSimpleAnnotatedModel(){} + + public Long getMyId(){ + return myId; + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeySimpleAnnotatedModelTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeySimpleAnnotatedModelTests.java new file mode 100644 index 00000000..bdcb1834 --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeySimpleAnnotatedModelTests.java @@ -0,0 +1,356 @@ +package com.example.sugartest; + +import com.example.models.PrimaryKeyNotationSimpleAnnotatedModel; +import com.orm.SugarRecord; +import com.orm.util.NamingHelper; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.NoSuchElementException; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeySimpleAnnotatedModelTests{ + @Test + public void emptyDatabaseTest() throws Exception{ + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void twoSaveTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void manySaveTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void defaultIdTest() throws Exception{ + assertEquals(1L, save(new PrimaryKeyNotationSimpleAnnotatedModel())); + } + + @Test + public void whereCountTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"1"})); + } + + @Test + public void whereNoCountTest() throws Exception{ + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"1"})); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"3"})); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", new String[] {"a"})); + } + + @Test + public void whereBrokenCountTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(-1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class, "di = ?", new String[] {"1"})); + } + + @Test + public void deleteTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + save(model); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertTrue(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteUnsavedTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertFalse(SugarRecord.delete(model)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteWrongTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + save(model); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + Field idField = model.getClass().getDeclaredField("myId"); + idField.setAccessible(true); + idField.set(model, Long.MAX_VALUE); + assertFalse(SugarRecord.delete(model)); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteAllTest() throws Exception{ + int elementNumber = 100; + for(int i = 1; i <= elementNumber; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + assertEquals(elementNumber, SugarRecord.deleteAll(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteAllWhereTest() throws Exception{ + int elementNumber = 100; + for(int i = 1; i <= elementNumber; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + assertEquals(elementNumber - 1, SugarRecord.deleteAll(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id > ?", new String[] + {"1"})); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteInTransactionFewTest() throws Exception{ + PrimaryKeyNotationSimpleAnnotatedModel first = new PrimaryKeyNotationSimpleAnnotatedModel(); + PrimaryKeyNotationSimpleAnnotatedModel second = new PrimaryKeyNotationSimpleAnnotatedModel(); + PrimaryKeyNotationSimpleAnnotatedModel third = new PrimaryKeyNotationSimpleAnnotatedModel(); + save(first); + save(second); + // Not saving last model + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertEquals(2, SugarRecord.deleteInTx(first, second, third)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void deleteInTransactionManyTest() throws Exception{ + long elementNumber = 100; + List models = new ArrayList<>(); + for(int i = 1; i <= elementNumber; i++){ + PrimaryKeyNotationSimpleAnnotatedModel model = new PrimaryKeyNotationSimpleAnnotatedModel(); + models.add(model); + // Not saving last model + if(i < elementNumber){ + save(model); + } + } + assertEquals(elementNumber - 1, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models)); + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void saveInTransactionTest() throws Exception{ + SugarRecord.saveInTx(new PrimaryKeyNotationSimpleAnnotatedModel(), new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationSimpleAnnotatedModel.class)); + } + + @Test + public void listAllTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = SugarRecord.listAll(PrimaryKeyNotationSimpleAnnotatedModel.class); + assertEquals(100, models.size()); + for(long i = 1; i <= 100; i++){ + assertEquals(new Long(i), models.get((int) i - 1).getMyId()); + } + } + + @Test + public void findTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + List models = SugarRecord.find(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", "2"); + assertEquals(1, models.size()); + assertEquals(new Long(2L), models.get(0).getMyId()); + } + + @Test + public void findWithQueryTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = + SugarRecord.findWithQuery(PrimaryKeyNotationSimpleAnnotatedModel.class, "Select * from " + + NamingHelper.toSQLName( + PrimaryKeyNotationSimpleAnnotatedModel.class) + + " where my_id >= ? ", "50"); + for(PrimaryKeyNotationSimpleAnnotatedModel model : models){ + assertEquals(new Long(75), model.getMyId(), 25L); + } + } + + @Test + public void findByIdTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(new Long(1L), SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, 1L).getMyId()); + } + + @Test + public void findByIdIntegerTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(new Long(1L), SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, 1).getMyId()); + } + + @Test + public void findByIdStringsNullTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertEquals(0, SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {""}).size()); + } + + @Test + public void findByIdStringsOneTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"1"}); + assertEquals(1, models.size()); + assertEquals(new Long(1L), models.get(0).getMyId()); + } + + @Test + public void findByIdStringsTwoTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"1", "3"}); + assertEquals(2, models.size()); + assertEquals(new Long(1L), models.get(0).getMyId()); + assertEquals(new Long(3L), models.get(1).getMyId()); + } + + @Test + public void findByIdStringsManyTest() throws Exception{ + for(int i = 1; i <= 10; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"1", "3", "6", "10"}); + assertEquals(4, models.size()); + assertEquals(new Long(1L), models.get(0).getMyId()); + assertEquals(new Long(3L), models.get(1).getMyId()); + assertEquals(new Long(6L), models.get(2).getMyId()); + assertEquals(new Long(10L), models.get(3).getMyId()); + } + + @Test + public void findByIdStringsOrderTest() throws Exception{ + for(int i = 1; i <= 10; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + List models = + SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, new String[] {"10", "6", "3", "1"}); + assertEquals(4, models.size()); + // The order of the query doesn't matter + assertEquals(new Long(1L), models.get(0).getMyId()); + assertEquals(new Long(3L), models.get(1).getMyId()); + assertEquals(new Long(6L), models.get(2).getMyId()); + assertEquals(new Long(10L), models.get(3).getMyId()); + } + + @Test + public void findByIdNullTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + assertNull(SugarRecord.findById(PrimaryKeyNotationSimpleAnnotatedModel.class, 2L)); + } + + @Test + public void findAllTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + Iterator cursor = SugarRecord.findAll(PrimaryKeyNotationSimpleAnnotatedModel.class); + for(int i = 1; i <= 100; i++){ + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(i), model.getMyId()); + } + } + + @Test + public void findAsIteratorTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + Iterator cursor = + SugarRecord.findAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id >= ?", "50"); + for(int i = 50; i <= 100; i++){ + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(i), model.getMyId()); + } + } + + @Test + public void findWithQueryAsIteratorTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + } + Iterator cursor = + SugarRecord.findWithQueryAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "Select * from " + + NamingHelper.toSQLName( + PrimaryKeyNotationSimpleAnnotatedModel.class) + + " where my_id >= ? ", "50"); + for(int i = 50; i <= 100; i++){ + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(i), model.getMyId()); + } + } + + @Test (expected = NoSuchElementException.class) + public void findAsIteratorOutOfBoundsTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + Iterator cursor = + SugarRecord.findAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", "1"); + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(1), model.getMyId()); + // This should throw a NoSuchElementException + cursor.next(); + } + + @Test (expected = UnsupportedOperationException.class) + public void disallowRemoveCursorTest() throws Exception{ + save(new PrimaryKeyNotationSimpleAnnotatedModel()); + Iterator cursor = + SugarRecord.findAsIterator(PrimaryKeyNotationSimpleAnnotatedModel.class, "my_id = ?", "1"); + assertTrue(cursor.hasNext()); + PrimaryKeyNotationSimpleAnnotatedModel model = cursor.next(); + assertNotNull(model); + assertEquals(new Long(1), model.getMyId()); + // This should throw a UnsupportedOperationException + cursor.remove(); + } + + @Test + public void vacuumTest() throws Exception{ + SugarRecord.executeQuery("Vacuum"); + } +} \ No newline at end of file From 8a5a9b1296ac187d34c6a89654ab5654dfa9baaf Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 18:37:01 -0200 Subject: [PATCH 18/23] BUGFIX Detected by PrimaryKeyNotationSimpleAnnotatedModelTest --- .../src/main/java/com/orm/SugarRecord.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/com/orm/SugarRecord.java b/library/src/main/java/com/orm/SugarRecord.java index 3ce0756b..20f7754b 100644 --- a/library/src/main/java/com/orm/SugarRecord.java +++ b/library/src/main/java/com/orm/SugarRecord.java @@ -23,12 +23,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; + import static com.orm.SugarContext.getSugarContext; public class SugarRecord { @@ -307,11 +307,10 @@ static long save(SQLiteDatabase db, Object object) { Map entitiesMap = getSugarContext().getEntitiesMap(); List columns = ReflectionUtil.getTableFields(object.getClass()); ContentValues values = new ContentValues(columns.size()); - Field idField = null; - boolean isSugarEntity = isSugarEntity(object.getClass()); - if(object.getClass().isAnnotationPresent(PrimaryKey.class)){ + Field idField = findPrimaryKeyNotationField(object.getClass()); + if(idField != null){ for(Field column : columns){ ReflectionUtil.addFieldValueToColumn(values, column, object, entitiesMap); @@ -474,7 +473,13 @@ public static boolean delete(Object object) { field.setAccessible(true); Long id = (Long) field.get(object); if (id != null && id > 0L) { - boolean deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[]{id.toString()}) == 1; + boolean deleted; + if(idField != null){ + deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), NamingHelper.toSQLName(idField) + "=?", + new String[] {id.toString()}) == 1; + }else{ + deleted = getSugarDataBase().delete(NamingHelper.toSQLName(type), "Id=?", new String[] {id.toString()}) == 1; + } Log.i(SUGAR, type.getSimpleName() + " deleted : " + id); return deleted; } else { @@ -482,10 +487,10 @@ public static boolean delete(Object object) { return false; } } catch (NoSuchFieldException e) { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no id"); + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - annotated object has no pk"); return false; } catch (IllegalAccessException e) { - Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access id"); + Log.i(SUGAR, "Cannot delete object: " + object.getClass().getSimpleName() + " - can't access pk"); return false; } } else if (SugarRecord.class.isAssignableFrom(type)) { From aea83ac7c944151e010c40b0c68d0fba8ddbe64b Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 18:43:45 -0200 Subject: [PATCH 19/23] Detached pk notation tests of old tests --- .../sugartest/BigDecimalFieldTests.java | 49 +++-------- .../example/sugartest/BooleanFieldTests.java | 81 +++---------------- ...rimaryKeyNotationBigDecimalFieldTests.java | 47 +++++++++++ .../PrimaryKeyNotationBooleanFieldTests.java | 77 ++++++++++++++++++ 4 files changed, 147 insertions(+), 107 deletions(-) create mode 100644 example/src/test/java/com/example/sugartest/PrimaryKeyNotationBigDecimalFieldTests.java create mode 100644 example/src/test/java/com/example/sugartest/PrimaryKeyNotationBooleanFieldTests.java diff --git a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java b/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java index e1a06689..b17095d3 100644 --- a/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java +++ b/example/src/test/java/com/example/sugartest/BigDecimalFieldTests.java @@ -3,7 +3,6 @@ import com.example.models.BigDecimalFieldAnnotatedModel; import com.example.models.BigDecimalFieldExtendedModel; -import com.example.models.PrimaryKeyNotationBigDecimalFieldAnnotatedModel; import com.orm.SugarRecord; import org.junit.Test; @@ -12,31 +11,30 @@ import java.math.BigDecimal; + import static com.orm.SugarRecord.save; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class BigDecimalFieldTests { +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class BigDecimalFieldTests{ @Test - public void nullBigDecimalExtendedTest() { + public void nullBigDecimalExtendedTest(){ save(new BigDecimalFieldExtendedModel()); - BigDecimalFieldExtendedModel model = - SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); + BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); assertNull(model.getBigDecimal()); } @Test - public void nullBigDecimalAnnotatedTest() { + public void nullBigDecimalAnnotatedTest(){ save(new BigDecimalFieldAnnotatedModel()); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); + BigDecimalFieldAnnotatedModel model = SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); assertNull(model.getBigDecimal()); } @Test - public void bigDecimalExtendedTest() { + public void bigDecimalExtendedTest(){ BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldExtendedModel(decimal)); BigDecimalFieldExtendedModel model = SugarRecord.findById(BigDecimalFieldExtendedModel.class, 1); @@ -44,36 +42,11 @@ public void bigDecimalExtendedTest() { } @Test - public void bigDecimalAnnotatedTest() { + public void bigDecimalAnnotatedTest(){ BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); save(new BigDecimalFieldAnnotatedModel(decimal)); - BigDecimalFieldAnnotatedModel model = - SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); - assertEquals(decimal, model.getBigDecimal()); - } - @Test - public void primryKeyNotationNullBigDecimalExtendedTest() { - save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel()); - PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = - SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); - assertNull(model.getBigDecimal()); - } - - @Test - public void primryKeyNotationBigDecimalExtendedTest() { - BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); - save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); - PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = - SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + BigDecimalFieldAnnotatedModel model = SugarRecord.findById(BigDecimalFieldAnnotatedModel.class, 1); assertEquals(decimal, model.getBigDecimal()); } - @Test - public void primryKeyNotationBigDecimalAnnotatedTest() { - BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); - save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); - PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = - SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); - assertEquals(decimal, model.getBigDecimal()); - } } diff --git a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java b/example/src/test/java/com/example/sugartest/BooleanFieldTests.java index 55cf997f..3625bed3 100644 --- a/example/src/test/java/com/example/sugartest/BooleanFieldTests.java +++ b/example/src/test/java/com/example/sugartest/BooleanFieldTests.java @@ -2,50 +2,50 @@ import com.example.models.BooleanFieldAnnotatedModel; -import com.example.models.PrimaryKeyNotationBooleanFieldAnnotatedModel; import com.orm.SugarRecord; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.annotation.Config; + import static com.orm.SugarRecord.save; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; -@RunWith(RobolectricGradleTestRunner.class) -@Config(sdk=18) -public class BooleanFieldTests { +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class BooleanFieldTests{ @Test - public void nullBooleanExtendedTest() { + public void nullBooleanExtendedTest(){ save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertNull(model.getBoolean()); } @Test - public void nullRawBooleanExtendedTest() { + public void nullRawBooleanExtendedTest(){ save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(false, model.getRawBoolean()); } @Test - public void nullBooleanAnnotatedTest() { + public void nullBooleanAnnotatedTest(){ save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertNull(model.getBoolean()); } @Test - public void nullRawBooleanAnnotatedTest() { + public void nullRawBooleanAnnotatedTest(){ save(new BooleanFieldAnnotatedModel()); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(false, model.getRawBoolean()); } @Test - public void objectBooleanExtendedTest() { + public void objectBooleanExtendedTest(){ Boolean objectBoolean = new Boolean(true); save(new BooleanFieldAnnotatedModel(objectBoolean)); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); @@ -53,14 +53,14 @@ public void objectBooleanExtendedTest() { } @Test - public void rawBooleanExtendedTest() { + public void rawBooleanExtendedTest(){ save(new BooleanFieldAnnotatedModel(true)); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); } @Test - public void objectBooleanAnnotatedTest() { + public void objectBooleanAnnotatedTest(){ Boolean objectBoolean = new Boolean(true); save(new BooleanFieldAnnotatedModel(objectBoolean)); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); @@ -68,67 +68,10 @@ public void objectBooleanAnnotatedTest() { } @Test - public void rawBooleanAnnotatedTest() { + public void rawBooleanAnnotatedTest(){ save(new BooleanFieldAnnotatedModel(true)); BooleanFieldAnnotatedModel model = SugarRecord.findById(BooleanFieldAnnotatedModel.class, 1); assertEquals(true, model.getRawBoolean()); } - @Test - public void primaryKeyNullBooleanExtendedTest() { - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertNull(model.getBoolean()); - } - - @Test - public void primaryKeynullRawBooleanExtendedTest() { - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertEquals(false, model.getRawBoolean()); - } - - @Test - public void primaryKeyNotationNullBooleanAnnotatedTest() { - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertNull(model.getBoolean()); - } - - @Test - public void primaryKeyNotationNullRawBooleanAnnotatedTest() { - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertEquals(false, model.getRawBoolean()); - } - - @Test - public void primaryKeyNotationObjectBooleanExtendedTest() { - Boolean objectBoolean = new Boolean(true); - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertEquals(objectBoolean, model.getBoolean()); - } - - @Test - public void primaryKeyNotationRawBooleanExtendedTest() { - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertEquals(true, model.getRawBoolean()); - } - - @Test - public void primaryKeyNotationObjectBooleanAnnotatedTest() { - Boolean objectBoolean = new Boolean(true); - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertEquals(objectBoolean, model.getBoolean()); - } - - @Test - public void primaryKeyNotationRawBooleanAnnotatedTest() { - save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); - PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); - assertEquals(true, model.getRawBoolean()); - } } diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBigDecimalFieldTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBigDecimalFieldTests.java new file mode 100644 index 00000000..620ab84c --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBigDecimalFieldTests.java @@ -0,0 +1,47 @@ +package com.example.sugartest; + + +import com.example.models.PrimaryKeyNotationBigDecimalFieldAnnotatedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.math.BigDecimal; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeyNotationBigDecimalFieldTests{ + + @Test + public void primryKeyNotationNullBigDecimalExtendedTest(){ + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel()); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertNull(model.getBigDecimal()); + } + + @Test + public void primryKeyNotationBigDecimalExtendedTest(){ + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } + + @Test + public void primryKeyNotationBigDecimalAnnotatedTest(){ + BigDecimal decimal = new BigDecimal(1234.5678901234567890123456789); + save(new PrimaryKeyNotationBigDecimalFieldAnnotatedModel(decimal)); + PrimaryKeyNotationBigDecimalFieldAnnotatedModel model = + SugarRecord.findById(PrimaryKeyNotationBigDecimalFieldAnnotatedModel.class, 1); + assertEquals(decimal, model.getBigDecimal()); + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBooleanFieldTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBooleanFieldTests.java new file mode 100644 index 00000000..0fc04fce --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationBooleanFieldTests.java @@ -0,0 +1,77 @@ +package com.example.sugartest; + + +import com.example.models.PrimaryKeyNotationBooleanFieldAnnotatedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeyNotationBooleanFieldTests{ + + @Test + public void primaryKeyNullBooleanExtendedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void primaryKeynullRawBooleanExtendedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationNullBooleanAnnotatedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertNull(model.getBoolean()); + } + + @Test + public void primaryKeyNotationNullRawBooleanAnnotatedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel()); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(false, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationObjectBooleanExtendedTest(){ + Boolean objectBoolean = new Boolean(true); + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(objectBoolean, model.getBoolean()); + } + + @Test + public void primaryKeyNotationRawBooleanExtendedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } + + @Test + public void primaryKeyNotationObjectBooleanAnnotatedTest(){ + Boolean objectBoolean = new Boolean(true); + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(objectBoolean)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(objectBoolean, model.getBoolean()); + } + + @Test + public void primaryKeyNotationRawBooleanAnnotatedTest(){ + save(new PrimaryKeyNotationBooleanFieldAnnotatedModel(true)); + PrimaryKeyNotationBooleanFieldAnnotatedModel model = SugarRecord.findById(PrimaryKeyNotationBooleanFieldAnnotatedModel.class, 1); + assertEquals(true, model.getRawBoolean()); + } +} From aebd1feaf83d33a951c31337e629e1d4e959ef56 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 18:48:54 -0200 Subject: [PATCH 20/23] ADD PrimaryKeyNotationRelationshipAnnotatedTests --- ...KeyNotationRelationshipAnnotatedModel.java | 24 ++++ ...KeyNotationRelationshipAnnotatedTests.java | 107 ++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipAnnotatedModel.java create mode 100644 example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipAnnotatedTests.java diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipAnnotatedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipAnnotatedModel.java new file mode 100644 index 00000000..7e538d51 --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipAnnotatedModel.java @@ -0,0 +1,24 @@ +package com.example.models; + +import com.orm.dsl.PrimaryKey; +import com.orm.dsl.Table; + +@Table +public class PrimaryKeyNotationRelationshipAnnotatedModel{ + private SimpleAnnotatedModel simple; + @PrimaryKey private Long myId; + + public PrimaryKeyNotationRelationshipAnnotatedModel(){} + + public PrimaryKeyNotationRelationshipAnnotatedModel(SimpleAnnotatedModel simple){ + this.simple = simple; + } + + public SimpleAnnotatedModel getSimple(){ + return simple; + } + + public Long getMyId(){ + return myId; + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipAnnotatedTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipAnnotatedTests.java new file mode 100644 index 00000000..0b3d432b --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipAnnotatedTests.java @@ -0,0 +1,107 @@ +package com.example.sugartest; + +import com.example.models.PrimaryKeyNotationRelationshipAnnotatedModel; +import com.example.models.SimpleAnnotatedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.util.List; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + + +@RunWith (RobolectricGradleTestRunner.class) +@Config (sdk = 18) +public class PrimaryKeyNotationRelationshipAnnotatedTests{ + @Test + public void emptyDatabaseTest() throws Exception{ + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + SimpleAnnotatedModel another_simple = new SimpleAnnotatedModel(); + save(another_simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(another_simple)); + assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipAnnotatedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception{ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + for(int i = 1; i <= 100; i++){ + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + List models = SugarRecord.listAll(PrimaryKeyNotationRelationshipAnnotatedModel.class); + assertEquals(100, models.size()); + for(PrimaryKeyNotationRelationshipAnnotatedModel model : models){ + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception{ + for(int i = 1; i <= 100; i++){ + SimpleAnnotatedModel simple = new SimpleAnnotatedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipAnnotatedModel(simple)); + } + List models = SugarRecord.listAll(PrimaryKeyNotationRelationshipAnnotatedModel.class); + assertEquals(100, models.size()); + for(PrimaryKeyNotationRelationshipAnnotatedModel model : models){ + assertEquals(model.getMyId(), model.getSimple().getId()); + } + } +} From fa319d7989cf5dea61f9780fa940a79542510090 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 18:58:10 -0200 Subject: [PATCH 21/23] ADD PrimaryKeyNotationRelationshipExtendedTests --- ...yKeyNotationRelationshipExtendedModel.java | 26 +++++ ...yKeyNotationRelationshipExtendedTests.java | 109 ++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipExtendedModel.java create mode 100644 example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipExtendedModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipExtendedModel.java new file mode 100644 index 00000000..461f37ac --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationRelationshipExtendedModel.java @@ -0,0 +1,26 @@ +package com.example.models; + + +import com.orm.SugarRecord; +import com.orm.dsl.PrimaryKey; + +public class PrimaryKeyNotationRelationshipExtendedModel extends SugarRecord{ + + @PrimaryKey private Long myId; + + private SimpleExtendedModel simple; + + public PrimaryKeyNotationRelationshipExtendedModel(){} + + public PrimaryKeyNotationRelationshipExtendedModel(SimpleExtendedModel simple){ + this.simple = simple; + } + + public SimpleExtendedModel getSimple(){ + return simple; + } + + public Long getMyId(){ + return myId; + } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java new file mode 100644 index 00000000..8660ab8b --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java @@ -0,0 +1,109 @@ +package com.example.sugartest; + +import com.example.models.PrimaryKeyNotationRelationshipExtendedModel; +import com.example.models.SimpleExtendedModel; +import com.orm.SugarRecord; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.annotation.Config; + +import java.util.List; + + +import static com.orm.SugarRecord.save; +import static org.junit.Assert.assertEquals; + + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18) +public class PrimaryKeyNotationRelationshipExtendedTests{ + @Test + public void emptyDatabaseTest() throws Exception { + assertEquals(0L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class)); + } + + @Test + public void oneSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(1L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void twoSameSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void twoDifferentSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + SimpleExtendedModel another_simple = new SimpleExtendedModel(); + save(another_simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + save(new PrimaryKeyNotationRelationshipExtendedModel(another_simple)); + assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(2L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void manySameSaveTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void manyDifferentSaveTest() throws Exception { + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class)); + assertEquals(100L, SugarRecord.count(PrimaryKeyNotationRelationshipExtendedModel.class)); + } + + @Test + public void listAllSameTest() throws Exception { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + for (int i = 1; i <= 100; i++) { + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + List models = + SugarRecord.listAll(PrimaryKeyNotationRelationshipExtendedModel.class); + assertEquals(100, models.size()); + for (PrimaryKeyNotationRelationshipExtendedModel model : models) { + assertEquals(simple.getId(), model.getSimple().getId()); + } + } + + @Test + public void listAllDifferentTest() throws Exception { + for (int i = 1; i <= 100; i++) { + SimpleExtendedModel simple = new SimpleExtendedModel(); + save(simple); + save(new PrimaryKeyNotationRelationshipExtendedModel(simple)); + } + List models = + SugarRecord.listAll(PrimaryKeyNotationRelationshipExtendedModel.class); + assertEquals(100, models.size()); + for (PrimaryKeyNotationRelationshipExtendedModel model : models) { + assertEquals(model.getId(), model.getSimple().getId()); + } + } +} From 0681b2bb03b09ff76475a8aec67860dd7110e192 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 18:58:19 -0200 Subject: [PATCH 22/23] ADD PrimaryKeyNotationRelationshipExtendedTests --- .../sugartest/PrimaryKeyNotationRelationshipExtendedTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java index 8660ab8b..fd649422 100644 --- a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationRelationshipExtendedTests.java @@ -103,7 +103,7 @@ public void listAllDifferentTest() throws Exception { SugarRecord.listAll(PrimaryKeyNotationRelationshipExtendedModel.class); assertEquals(100, models.size()); for (PrimaryKeyNotationRelationshipExtendedModel model : models) { - assertEquals(model.getId(), model.getSimple().getId()); + assertEquals(model.getMyId(), model.getSimple().getId()); } } } From 0670f9663ef103fdcb34277a71d3ff45dcb9bab9 Mon Sep 17 00:00:00 2001 From: Diogo Queiroz Date: Fri, 8 Jan 2016 19:19:35 -0200 Subject: [PATCH 23/23] ADD PrimaryKeyNotationRelationshipExtendedTests --- .../models/PrimaryKeyNotationSimpleModel.java | 40 +++++++++++ .../PrimaryKeyNotationCursorTests.java | 72 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 example/src/main/java/com/example/models/PrimaryKeyNotationSimpleModel.java create mode 100644 example/src/test/java/com/example/sugartest/PrimaryKeyNotationCursorTests.java diff --git a/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleModel.java b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleModel.java new file mode 100644 index 00000000..df2107c6 --- /dev/null +++ b/example/src/main/java/com/example/models/PrimaryKeyNotationSimpleModel.java @@ -0,0 +1,40 @@ +package com.example.models; + +import com.orm.SugarRecord; +import com.orm.dsl.PrimaryKey; + +public class PrimaryKeyNotationSimpleModel extends SugarRecord{ + @PrimaryKey + private Long myId; + private String str; + private int integer; + private boolean bool; + + public String getStr(){ + return str; + } + + public void setStr(String str){ + this.str = str; + } + + public int getInteger(){ + return integer; + } + + public void setInteger(int integer){ + this.integer = integer; + } + + public boolean isBool(){ + return bool; + } + + public void setBool(boolean bool){ + this.bool = bool; + } + + public Long getMyId(){ return myId; } + + public void setMyId(Long myId){ this.myId = myId; } +} diff --git a/example/src/test/java/com/example/sugartest/PrimaryKeyNotationCursorTests.java b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationCursorTests.java new file mode 100644 index 00000000..42d7e48e --- /dev/null +++ b/example/src/test/java/com/example/sugartest/PrimaryKeyNotationCursorTests.java @@ -0,0 +1,72 @@ +package com.example.sugartest; + +import android.annotation.TargetApi; +import android.content.Context; +import android.database.Cursor; +import android.os.Build; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CursorAdapter; +import android.widget.TextView; + +import com.example.models.PrimaryKeyNotationSimpleModel; +import com.orm.query.Select; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RuntimeEnvironment; +import org.robolectric.annotation.Config; + + +import static com.orm.SugarRecord.save; +import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertSame; + +@RunWith(RobolectricGradleTestRunner.class) +@Config(sdk=18) +public class PrimaryKeyNotationCursorTests{ + @Test + public void testColumnNames() { + save(new PrimaryKeyNotationSimpleModel()); + Cursor c = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + for (String col : new String[]{"STR", "INTEGER", "BOOL", "MY_ID"}) { + assertNotSame("Missing column for field: " + col, -1, c.getColumnIndex(col)); + } + } + @Test + public void testSugarCursor(){ + save(new PrimaryKeyNotationSimpleModel()); + Cursor cursor = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + assertNotSame("No _MY_ID", -1, cursor.getColumnIndex("MY_ID")); + assertNotSame("_MY_ID != MY_ID", cursor.getColumnIndex("_MY_ID"), cursor.getColumnIndex("MY_ID")); + } + + @Test + public void testNoColumn(){ + save(new PrimaryKeyNotationSimpleModel()); + Cursor cursor = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + assertSame(-1, cursor.getColumnIndex("nonexistent")); + } + + @TargetApi(Build.VERSION_CODES.HONEYCOMB) + @Test + public void testMakeAdapter() { + save(new PrimaryKeyNotationSimpleModel()); + Cursor c = Select.from(PrimaryKeyNotationSimpleModel.class).getCursor(); + CursorAdapter adapter = new CursorAdapter(RuntimeEnvironment.application, c, true) { + @Override + public View newView(Context context, Cursor cursor, ViewGroup parent) { + TextView tv = new TextView(context); + String s = cursor.getString(cursor.getColumnIndex("STR")); + tv.setText(s); + return null; + } + + @Override + public void bindView(View view, Context context, Cursor cursor) { + String s = cursor.getString(cursor.getColumnIndex("STR")); + ((TextView) view).setText(s); + } + }; + } +}