Skip to content

Commit

Permalink
feat(test): replace direct file access attempts with KVA calls, so te…
Browse files Browse the repository at this point in the history
…st class can be reused with different backends
  • Loading branch information
cmhulbert committed Feb 22, 2024
1 parent 33120d4 commit a876379
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 59 deletions.
112 changes: 54 additions & 58 deletions src/test/java/org/janelia/saalfeldlab/n5/zarr/N5ZarrTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,23 @@
*/
package org.janelia.saalfeldlab.n5.zarr;

import static org.junit.Assert.assertArrayEquals;
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.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.LongType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.integer.UnsignedIntType;
import net.imglib2.type.numeric.integer.UnsignedLongType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.view.Views;
import org.janelia.saalfeldlab.n5.AbstractN5Test;
import org.janelia.saalfeldlab.n5.Bzip2Compression;
import org.janelia.saalfeldlab.n5.Compression;
Expand All @@ -59,12 +53,14 @@
import org.janelia.saalfeldlab.n5.DatasetAttributes;
import org.janelia.saalfeldlab.n5.FileSystemKeyValueAccess;
import org.janelia.saalfeldlab.n5.GzipCompression;
import org.janelia.saalfeldlab.n5.KeyValueAccess;
import org.janelia.saalfeldlab.n5.LockedChannel;
import org.janelia.saalfeldlab.n5.N5Exception;
import org.janelia.saalfeldlab.n5.N5Exception.N5ClassCastException;
import org.janelia.saalfeldlab.n5.N5Reader;
import org.janelia.saalfeldlab.n5.N5Reader.Version;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.RawCompression;
import org.janelia.saalfeldlab.n5.N5Exception.N5ClassCastException;
import org.janelia.saalfeldlab.n5.StringDataBlock;
import org.janelia.saalfeldlab.n5.blosc.BloscCompression;
import org.janelia.saalfeldlab.n5.imglib2.N5Utils;
Expand All @@ -76,24 +72,26 @@
import org.junit.Ignore;
import org.junit.Test;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URISyntaxException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import net.imglib2.RandomAccess;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.integer.LongType;
import net.imglib2.type.numeric.integer.UnsignedByteType;
import net.imglib2.type.numeric.integer.UnsignedIntType;
import net.imglib2.type.numeric.integer.UnsignedLongType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
import net.imglib2.view.Views;
import static org.junit.Assert.assertArrayEquals;
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.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

/**
* @author Stephan Saalfeld <[email protected]>
Expand All @@ -102,6 +100,10 @@ public class N5ZarrTest extends AbstractN5Test {

static private final String testZarrDatasetName = "/test/data";

public static KeyValueAccess createKeyValueAccess() {
return new FileSystemKeyValueAccess(FileSystems.getDefault());
}

@Override
protected String tempN5Location() {

Expand All @@ -113,10 +115,10 @@ protected String tempN5Location() {
}

@Override
protected N5Writer createN5Writer() throws IOException {
protected N5Writer createN5Writer() {

final String testDirPath = tempN5Location();
return new ZarrKeyValueWriter(new FileSystemKeyValueAccess(FileSystems.getDefault()), testDirPath, new GsonBuilder(), true, true, ".",false) {
return new ZarrKeyValueWriter(createKeyValueAccess(), testDirPath, new GsonBuilder(), true, true, ".",false) {

@Override public void close() {

Expand All @@ -141,7 +143,7 @@ protected N5Writer createN5Writer(final String location, final String dimensionS
protected N5Writer createN5Writer(final String location, final String dimensionSeparator, final boolean cacheAttributes) throws IOException {


return new ZarrKeyValueWriter(new FileSystemKeyValueAccess(FileSystems.getDefault()), location, new GsonBuilder(), true, true, dimensionSeparator, cacheAttributes);
return new ZarrKeyValueWriter(createKeyValueAccess(), location, new GsonBuilder(), true, true, dimensionSeparator, cacheAttributes);
}

protected N5Writer createN5Writer(
Expand All @@ -151,13 +153,13 @@ protected N5Writer createN5Writer(
final boolean mapN5DatasetAttributes) throws IOException {


return new ZarrKeyValueWriter(new FileSystemKeyValueAccess(FileSystems.getDefault()), location, gsonBuilder, mapN5DatasetAttributes, true, dimensionSeparator, false);
return new ZarrKeyValueWriter(createKeyValueAccess(), location, gsonBuilder, mapN5DatasetAttributes, true, dimensionSeparator, false);
}

@Override
protected N5Reader createN5Reader(final String location, final GsonBuilder gson) throws IOException {

return new ZarrKeyValueReader(new FileSystemKeyValueAccess(FileSystems.getDefault()), location, gson, true, true, false);
return new ZarrKeyValueReader(createKeyValueAccess(), location, gson, true, true, false);
}

@Override
Expand Down Expand Up @@ -283,9 +285,7 @@ public void testVersion() throws NumberFormatException, IOException {
@Override
public void testReaderCreation() throws IOException, URISyntaxException {

final File tmpFile = Files.createTempDirectory("reader-create-test-").toFile();
tmpFile.delete();
final String canonicalPath = tmpFile.getCanonicalPath();
final String canonicalPath = tempN5Location();
try (N5Writer writer = createN5Writer(canonicalPath)) {

final N5Reader n5r = createN5Reader(canonicalPath);
Expand Down Expand Up @@ -397,6 +397,7 @@ public void testWriteReadSerializableBlock() {
}

@Test
@Override
public void testWriteReadStringBlock() {
DataType dataType = DataType.STRING;
int[] blockSize = new int[]{3, 2, 1};
Expand All @@ -414,12 +415,8 @@ public void testWriteReadStringBlock() {
DataBlock<?> loadedDataBlock = n5.readBlock("/test/group/dataset", attributes, 0L, 0L, 0L);
assertArrayEquals(stringBlock, (String[])loadedDataBlock.getData());
assertTrue(n5.remove("/test/group/dataset"));
} catch (IOException e) {
e.printStackTrace();
Assert.fail("Block cannot be written.");
}
}

}

private boolean runPythonTest(final String script, final String containerPath) throws InterruptedException {
Expand Down Expand Up @@ -699,17 +696,17 @@ public void testReadZarrNestedPython() throws IOException, InterruptedException
public void testRawCompressorNullInZarray() throws IOException, ParseException {

final ZarrKeyValueWriter n5 = (ZarrKeyValueWriter) createN5Writer();
final String testZarrDirPath = n5.getURI().getPath();
n5.createDataset(
testZarrDatasetName,
new long[]{1, 2, 3},
new int[]{1, 2, 3},
DataType.UINT16,
new RawCompression());
final String zarrayLocation = n5.keyValueAccess.compose(n5.uri, testZarrDatasetName, ".zarray");
final LockedChannel zarrayChannel = n5.keyValueAccess.lockForReading(zarrayLocation);
final JSONParser jsonParser = new JSONParser();
final File zArrayFile = Paths.get(testZarrDirPath, testZarrDatasetName, ".zarray").toFile();
try (FileReader freader = new FileReader(zArrayFile)) {
final JSONObject zarray = (JSONObject)jsonParser.parse(freader);
try (Reader reader = zarrayChannel.newReader()) {
final JSONObject zarray = (JSONObject)jsonParser.parse(reader);
final JSONObject compressor = (JSONObject)zarray.get("compressor");
assertNull(compressor);
} finally {
Expand Down Expand Up @@ -756,7 +753,6 @@ public void testAttributes() throws IOException {

}.getType()));

// test the case where the resulting file becomes shorter
n5.setAttribute(groupName, "key1", 1);
n5.setAttribute(groupName, "key2", 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ protected String tempN5Location() {
}

@Override
protected N5ZarrWriter createN5Writer() throws IOException {
protected N5ZarrWriter createN5Writer() {

final String testDirPath = tempN5Location();
return new N5ZarrWriter(testDirPath, new GsonBuilder(), ".", true, true) {
Expand Down

0 comments on commit a876379

Please sign in to comment.