Skip to content

Commit

Permalink
Test ResultSetIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed May 19, 2024
1 parent 82f590e commit a64d777
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/sqlite/driver/ResultSetIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ public boolean hasNext() {
@Override
public T next() {
if (!hasNext()) {
state = State.FAILED;
throw new NoSuchElementException();
}
state = State.NOT_READY;
try {
return mapper.map(rs);
} catch (SQLException e) {
state = State.FAILED;
return sneakyThrow(e);
}
}
Expand Down
104 changes: 104 additions & 0 deletions src/test/java/org/sqlite/driver/ResultSetIterableTest.java
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();
}
}
}

0 comments on commit a64d777

Please sign in to comment.