Skip to content

Commit

Permalink
Update SugarRecord to return sensible values on multiple operations
Browse files Browse the repository at this point in the history
Add tests to validate behavior of parameters
Closes #306

Thanks @Shyish for the pull request!
  • Loading branch information
Shyish authored and whoshuu committed Apr 11, 2015
1 parent ec8573e commit a45dd1a
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 53 deletions.
7 changes: 7 additions & 0 deletions example/src/main/java/com/example/models/NoSugarModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.models;


public class NoSugarModel {
public NoSugarModel() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,20 @@
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import static com.orm.SugarRecord.delete;
import static com.orm.SugarRecord.save;
import static org.junit.Assert.assertFalse;

@RunWith(RobolectricGradleTestRunner.class)
@Config(emulateSdk=18)
public class IncompleteAnnotatedModelTests {
@Test(expected=SQLiteException.class)
public void noIdFieldTest() {
public void saveNoIdFieldTest() {
save(new IncompleteAnnotatedModel());
}

@Test
public void deleteNoIdFieldTest() {
assertFalse(delete(new IncompleteAnnotatedModel()));
}
}
28 changes: 28 additions & 0 deletions example/src/test/java/com/example/sugartest/NoSugarModelTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.sugartest;

import com.example.models.NoSugarModel;
import com.orm.SugarRecord;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;


@RunWith(RobolectricGradleTestRunner.class)
@Config(emulateSdk=18)
public class NoSugarModelTests {
@Test
public void deleteTest() throws Exception {
NoSugarModel model = new NoSugarModel();
assertFalse(SugarRecord.delete(model));
}

@Test
public void saveInTransactionTest() throws Exception {
SugarRecord.saveInTx(new NoSugarModel(), new NoSugarModel());
assertEquals(-1L, SugarRecord.count(NoSugarModel.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
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;
Expand Down Expand Up @@ -82,33 +84,49 @@ public void deleteTest() throws Exception {
SimpleAnnotatedModel model = new SimpleAnnotatedModel();
save(model);
assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class));
SugarRecord.delete(model);
assertTrue(SugarRecord.delete(model));
assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class));
}

@Test
public void deleteUnsavedTest() throws Exception {
SimpleAnnotatedModel model = new SimpleAnnotatedModel();
assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class));
SugarRecord.delete(model);
assertFalse(SugarRecord.delete(model));
assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class));
}

@Test
public void deleteWrongTest() throws Exception {
SimpleAnnotatedModel model = new SimpleAnnotatedModel();
save(model);
assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class));
Field idField = model.getClass().getDeclaredField("id");
idField.setAccessible(true);
idField.set(model, Long.MAX_VALUE);
assertFalse(SugarRecord.delete(model));
assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class));
}

@Test
public void deleteAllTest() throws Exception {
for (int i = 1; i <= 100; i++) {
int elementNumber = 100;
for (int i = 1; i <= elementNumber; i++) {
save(new SimpleAnnotatedModel());
}
SugarRecord.deleteAll(SimpleAnnotatedModel.class);
assertEquals(elementNumber, SugarRecord.deleteAll(SimpleAnnotatedModel.class));
assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class));
}

@Test
public void deleteAllWhereTest() throws Exception {
for (int i = 1; i <= 100; i++) {
int elementNumber = 100;
for (int i = 1; i <= elementNumber; i++) {
save(new SimpleAnnotatedModel());
}
SugarRecord.deleteAll(SimpleAnnotatedModel.class, "id > ?", new String[]{"1"});
assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleAnnotatedModel.class,
"id > ?",
new String[]{"1"}));
assertEquals(1L, SugarRecord.count(SimpleAnnotatedModel.class));
}

Expand All @@ -119,22 +137,26 @@ public void deleteInTransactionFewTest() throws Exception {
SimpleAnnotatedModel third = new SimpleAnnotatedModel();
save(first);
save(second);
save(third);
assertEquals(3L, SugarRecord.count(SimpleAnnotatedModel.class));
SugarRecord.deleteInTx(first, second, third);
// Not saving last model
assertEquals(2L, SugarRecord.count(SimpleAnnotatedModel.class));
assertEquals(2, SugarRecord.deleteInTx(first, second, third));
assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class));
}

@Test
public void deleteInTransactionManyTest() throws Exception {
long elementNumber = 100;
List<SimpleAnnotatedModel> models = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
for (int i = 1; i <= elementNumber; i++) {
SimpleAnnotatedModel model = new SimpleAnnotatedModel();
models.add(model);
save(model);
// Not saving last model
if (i < elementNumber) {
save(model);
}
}
assertEquals(100L, SugarRecord.count(SimpleAnnotatedModel.class));
SugarRecord.deleteInTx(models);
assertEquals(elementNumber - 1, SugarRecord.count(SimpleAnnotatedModel.class));
assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models));
assertEquals(0L, SugarRecord.count(SimpleAnnotatedModel.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
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;
Expand Down Expand Up @@ -89,33 +91,49 @@ public void deleteTest() throws Exception {
SimpleExtendedModel model = new SimpleExtendedModel();
save(model);
assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class));
SugarRecord.delete(model);
assertTrue(SugarRecord.delete(model));
assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class));
}

@Test
public void deleteUnsavedTest() throws Exception {
SimpleExtendedModel model = new SimpleExtendedModel();
assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class));
SugarRecord.delete(model);
assertFalse(SugarRecord.delete(model));
assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class));
}

@Test
public void deleteWrongTest() throws Exception {
SimpleExtendedModel model = new SimpleExtendedModel();
save(model);
assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class));
Field idField = model.getClass().getSuperclass().getDeclaredField("id");
idField.setAccessible(true);
idField.set(model, Long.MAX_VALUE);
assertFalse(SugarRecord.delete(model));
assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class));
}

@Test
public void deleteAllTest() throws Exception {
for (int i = 1; i <= 100; i++) {
int elementNumber = 100;
for (int i = 1; i <= elementNumber; i++) {
save(new SimpleExtendedModel());
}
SugarRecord.deleteAll(SimpleExtendedModel.class);
assertEquals(elementNumber, SugarRecord.deleteAll(SimpleExtendedModel.class));
assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class));
}

@Test
public void deleteAllWhereTest() throws Exception {
for (int i = 1; i <= 100; i++) {
int elementNumber = 100;
for (int i = 1; i <= elementNumber; i++) {
save(new SimpleExtendedModel());
}
SugarRecord.deleteAll(SimpleExtendedModel.class, "id > ?", new String[]{"1"});
assertEquals(elementNumber - 1, SugarRecord.deleteAll(SimpleExtendedModel.class,
"id > ?",
new String[]{"1"}));
assertEquals(1L, SugarRecord.count(SimpleExtendedModel.class));
}

Expand All @@ -126,22 +144,26 @@ public void deleteInTransactionFewTest() throws Exception {
SimpleExtendedModel third = new SimpleExtendedModel();
save(first);
save(second);
save(third);
assertEquals(3L, SugarRecord.count(SimpleExtendedModel.class));
SugarRecord.deleteInTx(first, second, third);
// Not saving last model
assertEquals(2L, SugarRecord.count(SimpleExtendedModel.class));
assertEquals(2, SugarRecord.deleteInTx(first, second, third));
assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class));
}

@Test
public void deleteInTransactionManyTest() throws Exception {
long elementNumber = 100;
List<SimpleExtendedModel> models = new ArrayList<>();
for (int i = 1; i <= 100; i++) {
for (int i = 1; i <= elementNumber; i++) {
SimpleExtendedModel model = new SimpleExtendedModel();
models.add(model);
save(model);
// Not saving last model
if (i < elementNumber) {
save(model);
}
}
assertEquals(100L, SugarRecord.count(SimpleExtendedModel.class));
SugarRecord.deleteInTx(models);
assertEquals(elementNumber - 1, SugarRecord.count(SimpleExtendedModel.class));
assertEquals(elementNumber - 1, SugarRecord.deleteInTx(models));
assertEquals(0L, SugarRecord.count(SimpleExtendedModel.class));
}

Expand Down
Loading

0 comments on commit a45dd1a

Please sign in to comment.