From a6410a2731b09f5d32b90cdf5f1c38d1e384064e Mon Sep 17 00:00:00 2001 From: Keita Kagurazaka Date: Wed, 17 May 2017 21:19:54 +0900 Subject: [PATCH] change Selector#value() and Selector#get() to respect Selector#offset() --- .../com/github/gfx/android/orma/Selector.java | 2 +- .../github/gfx/android/orma/test/QueryTest.java | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/github/gfx/android/orma/Selector.java b/library/src/main/java/com/github/gfx/android/orma/Selector.java index 2acd3f93..8629a681 100644 --- a/library/src/main/java/com/github/gfx/android/orma/Selector.java +++ b/library/src/main/java/com/github/gfx/android/orma/Selector.java @@ -240,7 +240,7 @@ public Model value() throws NoValueException { @Nullable public Model getOrNull(@IntRange(from = 0) long position) { return conn.querySingle(getSchema(), getSchema().getDefaultResultColumns(), - getWhereClause(), getBindArgs(), groupBy, having, orderBy, position); + getWhereClause(), getBindArgs(), groupBy, having, orderBy, position + Math.max(offset, 0)); } @NonNull diff --git a/library/src/test/java/com/github/gfx/android/orma/test/QueryTest.java b/library/src/test/java/com/github/gfx/android/orma/test/QueryTest.java index 2581fdc2..4dc735b8 100644 --- a/library/src/test/java/com/github/gfx/android/orma/test/QueryTest.java +++ b/library/src/test/java/com/github/gfx/android/orma/test/QueryTest.java @@ -162,6 +162,14 @@ public void valueOrNull() throws Exception { assertThat(book, is(nullValue())); } + @Test + public void valueWithOffset() throws Exception { + Book book = db.selectFromBook().offset(1).value(); + + assertThat(book.title, is("friday")); + assertThat(book.content, is("apple")); + } + @Test(expected = NoValueException.class) public void valueIfNull() throws Exception { db.deleteFromBook().execute(); @@ -191,6 +199,14 @@ public void testGetOrNull() throws Exception { assertThat(db.selectFromBook().get(10), is(nullValue())); } + @Test + public void testGetWithOffset() throws Exception { + Book book = db.selectFromBook().offset(1).get(0); + + assertThat(book.title, is("friday")); + assertThat(book.content, is("apple")); + } + @Test public void pluck() throws Exception { assertThat(db.selectFromBook().orderByTitleAsc().pluck(Book_Schema.INSTANCE.title),