-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
src/test/java/org/sqlite/driver/ResultSetIterableTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package org.sqlite.driver; | ||
|
||
import junit.framework.TestCase; | ||
import org.junit.Test; | ||
|
||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class ResultSetIterableTest { | ||
@Test | ||
public void close() throws SQLException { | ||
try (Connection c = DriverManager.getConnection(JDBC.MEMORY); | ||
PreparedStatement s = c.prepareStatement("SELECT ?"); | ||
ResultSetIterable<Long> rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { | ||
rsi.close(); | ||
} | ||
} | ||
@Test | ||
public void iterator() throws SQLException { | ||
try (Connection c = DriverManager.getConnection(JDBC.MEMORY); | ||
PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT 2"); | ||
ResultSetIterable<Long> rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { | ||
long sum = 0; | ||
for (Long l : rsi) { | ||
sum += l; | ||
} | ||
assertEquals(3L, sum); | ||
} | ||
} | ||
@Test | ||
public void stream() throws SQLException { | ||
try (Connection c = DriverManager.getConnection(JDBC.MEMORY); | ||
PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT 2"); | ||
ResultSetIterable<Long> rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { | ||
assertEquals(3L, rsi.stream().mapToLong(l -> l.longValue()).sum()); | ||
} | ||
} | ||
|
||
@Test | ||
public void empty() throws SQLException { | ||
try (Connection c = DriverManager.getConnection(JDBC.MEMORY); | ||
PreparedStatement s = c.prepareStatement("SELECT 1 LIMIT 0"); | ||
ResultSetIterable<Long> rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { | ||
assertEquals(0, rsi.stream().count()); | ||
|
||
{ | ||
Iterator<Long> iterator = rsi.iterator(); | ||
try { | ||
iterator.next(); | ||
fail("No such element"); | ||
} catch (NoSuchElementException e) { | ||
} | ||
try { | ||
iterator.next(); | ||
fail("Illegal state"); | ||
} catch (IllegalStateException e) { | ||
} | ||
} | ||
{ | ||
Iterator<Long> iterator = rsi.iterator(); | ||
assertFalse(iterator.hasNext()); | ||
assertFalse(iterator.hasNext()); | ||
} | ||
} | ||
} | ||
|
||
@Test(expected = SQLException.class) | ||
public void map_error() throws SQLException { | ||
try (Connection c = DriverManager.getConnection(JDBC.MEMORY); | ||
PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT 'a'"); | ||
ResultSetIterable<Long> rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { | ||
for (Long l : rsi) { | ||
assertEquals(1L, l.longValue()); | ||
} | ||
} | ||
} | ||
|
||
@Test(expected = SQLException.class) | ||
public void runtime_error() throws SQLException { | ||
try (Connection c = DriverManager.getConnection(JDBC.MEMORY); | ||
PreparedStatement s = c.prepareStatement("SELECT 1 UNION ALL SELECT '{[]}' -> 1"); | ||
ResultSetIterable<Long> rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { | ||
for (Long l : rsi) { | ||
assertEquals(1L, l.longValue()); | ||
} | ||
} | ||
} | ||
|
||
@Test(expected = SQLException.class) | ||
public void invalid_query() throws SQLException { | ||
try (Connection c = DriverManager.getConnection(JDBC.MEMORY); | ||
PreparedStatement s = c.prepareStatement("SELECT ?"); | ||
ResultSetIterable<Long> rsi = new ResultSetIterable<>(Query.from(s), rs -> rs.getLong(1))) { | ||
rsi.iterator(); | ||
} | ||
} | ||
} |