Search in sources :

Example 1 with SqlAdapter

use of com.codeslap.persistence.SqlAdapter in project persistence by casidiablo.

the class RawQueryTest method testAttachedTo.

@Test
public void testAttachedTo() {
    Cow cow = new Cow();
    cow.name = "Super Cow";
    Bug garrapata = new Bug();
    garrapata.itchFactor = new Random().nextFloat();
    Bug pulga = new Bug();
    pulga.itchFactor = new Random().nextFloat();
    SqlAdapter adapter = getAdapter();
    Object store = adapter.store(cow);
    assertNotNull(store);
    cow.id = 1;
    store = adapter.store(cow);
    assertNotNull(store);
    cow.name = "Ugly Cow";
    store = adapter.store(cow);
    assertNotNull(store);
    List<Cow> cows = adapter.findAll(Cow.class, "name = 'Ugly Cow'", null);
    assertEquals(1, cows.size());
    List<Cow> cowsEquals = adapter.findAll(cow);
    assertEquals(cows, cowsEquals);
    adapter.storeCollection(Arrays.asList(garrapata, pulga), cow, null);
    List<Bug> bugs = adapter.findAll(Bug.class);
    assertEquals(2, bugs.size());
    List<Bug> sameBugs = adapter.findAll(Bug.class, "cow_id = ?", new String[] { "1" });
    assertEquals(bugs, sameBugs);
    adapter.truncate(Cow.class, Bug.class);
    assertTrue(adapter.findAll(Bug.class).isEmpty());
    assertTrue(adapter.findAll(Cow.class).isEmpty());
    Object id = adapter.store(cow);
    assertNotNull(id);
    assertTrue(id instanceof Long);
    assertEquals(1L, id);
    adapter.store(garrapata, cow);
    adapter.store(pulga, cow);
    bugs = adapter.findAll(Bug.class);
    assertEquals(2, bugs.size());
    sameBugs = adapter.findAll(Bug.class, "cow_id = ?", new String[] { "1" });
    assertEquals(bugs, sameBugs);
    RawQuery rawQuery = Persistence.getRawQuery(new Activity());
    Cursor allAttached = rawQuery.findAll(new Bug(), cow);
    assertNotNull(allAttached);
    assertEquals(bugs.size(), allAttached.getCount());
    assertTrue(allAttached.moveToFirst());
    List<Bug> listFromCursor = new ArrayList<Bug>();
    do {
        long cowId = allAttached.getLong(allAttached.getColumnIndex("cow_id"));
        assertEquals(1, cowId);
        float itchFactor = allAttached.getFloat(allAttached.getColumnIndex("itch_factor"));
        Bug bug = new Bug();
        bug.itchFactor = itchFactor;
        listFromCursor.add(bug);
    } while (allAttached.moveToNext());
    Comparator<Bug> comparator = new Comparator<Bug>() {

        @Override
        public int compare(Bug foo, Bug bar) {
            return Float.floatToIntBits(foo.itchFactor) - Float.floatToIntBits(bar.itchFactor);
        }
    };
    Collections.sort(bugs, comparator);
    Collections.sort(listFromCursor, comparator);
    assertEquals(bugs, listFromCursor);
}
Also used : SqlAdapter(com.codeslap.persistence.SqlAdapter) Activity(android.app.Activity) Cursor(android.database.Cursor) RawQuery(com.codeslap.persistence.RawQuery) Test(org.junit.Test)

Aggregations

Activity (android.app.Activity)1 Cursor (android.database.Cursor)1 RawQuery (com.codeslap.persistence.RawQuery)1 SqlAdapter (com.codeslap.persistence.SqlAdapter)1 Test (org.junit.Test)1