Skip to content

Commit

Permalink
refactor(test): move some test function around, use TempDir for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmhulbert committed Dec 16, 2024
1 parent 0626e2e commit 6ba36f1
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.janelia.saalfeldlab.util.n5

import io.github.oshai.kotlinlogging.KotlinLogging
import org.commonmark.internal.util.Parsing
import org.janelia.saalfeldlab.n5.N5Reader
import org.janelia.saalfeldlab.n5.universe.N5DatasetDiscoverer
import org.janelia.saalfeldlab.n5.universe.N5TreeNode
Expand Down Expand Up @@ -43,9 +42,7 @@ private val LOG = KotlinLogging.logger { }
@JvmOverloads
internal fun discoverAndParseRecursive(n5Reader: N5Reader, initialGroup: String = "/", callback: (N5TreeNode) -> Unit = {}): N5TreeNode {
val discoverer = getDiscoverer(n5Reader)
println("\tParsing ${n5Reader.uri}")
return discoverer.discoverAndParseRecursive(initialGroup) {
println("\t\tDiscovered dataset ${it.path} with metadata ${it.metadata}")
callback(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.janelia.saalfeldlab.n5.N5Writer
import org.janelia.saalfeldlab.n5.hdf5.N5HDF5Reader
import org.janelia.saalfeldlab.n5.universe.N5Factory
import org.janelia.saalfeldlab.n5.universe.StorageFormat
import org.janelia.saalfeldlab.paintera.serialization.GsonHelpers
import java.net.URI

class N5FactoryWithCache : N5Factory() {
Expand Down
31 changes: 23 additions & 8 deletions src/test/java/org/janelia/saalfeldlab/util/n5/N5HelpersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.imglib2.img.cell.CellGrid;
import org.janelia.saalfeldlab.labels.blocks.LabelBlockLookup;
import org.janelia.saalfeldlab.labels.blocks.LabelBlockLookupAdapter;
import org.janelia.saalfeldlab.n5.Compression;
import org.janelia.saalfeldlab.n5.CompressionAdapter;
import org.janelia.saalfeldlab.n5.DataType;
Expand All @@ -11,12 +13,16 @@
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.RawCompression;
import org.janelia.saalfeldlab.n5.universe.N5TreeNode;
import org.janelia.saalfeldlab.paintera.Paintera;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -49,6 +55,14 @@ private static void assertDatasetAttributesEquals(final DatasetAttributes expect
assertEquals(gsonWithCompression.toJson(expected.getCompression()), gsonWithCompression.toJson(actual.getCompression()));
}

@BeforeAll
public static void setupN5Factory() {

final var builder = new GsonBuilder();
builder.registerTypeHierarchyAdapter(LabelBlockLookup.class, LabelBlockLookupAdapter.getJsonAdapter());
Paintera.getN5Factory().gsonBuilder(builder);
}

@Test
public void testAsCellGrid() {

Expand All @@ -66,9 +80,10 @@ public void testVolumetricDataGroup() {
assertEquals(group + "/" + N5Helpers.PAINTERA_DATA_DATASET, N5Helpers.volumetricDataGroup(group, true));
}

@Test public void testIsMultiscale() throws IOException {
@Test
public void testIsMultiscale(@TempDir Path tmp) throws IOException {

final N5Writer writer = N5TestUtil.fileSystemWriterAtTmpDir(!LOG.isDebugEnabled());
final N5Writer writer = Paintera.getN5Factory().newWriter(tmp.toAbsolutePath().toString());
final String group = "group";
writer.createGroup(group);

Expand All @@ -91,9 +106,9 @@ public void testVolumetricDataGroup() {
}

@Test
public void testListAndSortScaleDatasets() throws IOException {
public void testListAndSortScaleDatasets(@TempDir Path tmp) throws IOException {

final N5Writer writer = N5TestUtil.fileSystemWriterAtTmpDir(!LOG.isDebugEnabled());
final N5Writer writer = Paintera.getN5Factory().newWriter(tmp.toAbsolutePath().toString());
final String group = "group";
writer.createGroup(group);
writer.setAttribute(group, N5Helpers.MULTI_SCALE_KEY, true);
Expand All @@ -109,9 +124,9 @@ public void testListAndSortScaleDatasets() throws IOException {
}

@Test
public void testDiscoverDatasets() throws IOException {
public void testDiscoverDatasets(@TempDir Path tmp) throws IOException {

final N5Writer writer = N5TestUtil.fileSystemWriterAtTmpDir(!LOG.isDebugEnabled());
final N5Writer writer = Paintera.getN5Factory().newWriter(tmp.toAbsolutePath().toString());
final String group = "group";
writer.createGroup(group);
writer.setAttribute(group, N5Helpers.MULTI_SCALE_KEY, true);
Expand All @@ -134,9 +149,9 @@ public void testDiscoverDatasets() throws IOException {
}

@Test
public void testGetDatasetAttributes() throws IOException {
public void testGetDatasetAttributes(@TempDir Path tmp) throws IOException {

final N5Writer writer = N5TestUtil.fileSystemWriterAtTmpDir(!LOG.isDebugEnabled());
final N5Writer writer = Paintera.getN5Factory().newWriter(tmp.toAbsolutePath().toString());
final DatasetAttributes attributes = new DatasetAttributes(new long[]{1}, new int[]{1}, DataType.UINT8, new GzipCompression());

// single scale
Expand Down
58 changes: 0 additions & 58 deletions src/test/java/org/janelia/saalfeldlab/util/n5/N5TestUtil.java

This file was deleted.

35 changes: 27 additions & 8 deletions src/test/java/org/janelia/saalfeldlab/util/n5/N5TypesTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.janelia.saalfeldlab.util.n5;

import com.google.gson.GsonBuilder;
import net.imglib2.type.numeric.integer.ByteType;
import net.imglib2.type.numeric.integer.IntType;
import net.imglib2.type.numeric.integer.LongType;
Expand All @@ -10,12 +11,19 @@
import net.imglib2.type.numeric.integer.UnsignedShortType;
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.type.numeric.real.FloatType;
import org.janelia.saalfeldlab.labels.blocks.LabelBlockLookup;
import org.janelia.saalfeldlab.labels.blocks.LabelBlockLookupAdapter;
import org.janelia.saalfeldlab.n5.DataType;
import org.janelia.saalfeldlab.n5.DatasetAttributes;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.RawCompression;
import org.janelia.saalfeldlab.paintera.Paintera;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -27,6 +35,14 @@

public class N5TypesTest {

@BeforeAll
public static void setupN5Factory() {

final var builder = new GsonBuilder();
builder.registerTypeHierarchyAdapter(LabelBlockLookup.class, LabelBlockLookupAdapter.getJsonAdapter());
Paintera.getN5Factory().gsonBuilder(builder);
}

@Test
public void testAndLogIsIntegerType() {

Expand All @@ -48,9 +64,9 @@ public void testAndLogIsIntegerType() {
}

@Test
public void testIsLabelMultisetType() throws IOException {
public void testIsLabelMultisetType(@TempDir Path tmp) throws IOException {

final N5Writer writer = N5TestUtil.fileSystemWriterAtTmpDir();
final N5Writer writer = Paintera.getN5Factory().newWriter(tmp.toAbsolutePath().toString());

final String noMultisetGroup = "no-multiset-group";
final String multisetGroup = "multiset-group";
Expand All @@ -67,10 +83,13 @@ public void testIsLabelMultisetType() throws IOException {
final String multisetGroupMultiscale = "multiset-group-multiscale";
writer.createGroup(noMultisetGroupMultiscale);
writer.createGroup(multisetGroupMultiscale);
writer.createDataset(noMultisetGroupMultiscale + "/s0", N5TestUtil.defaultAttributes());
writer.createDataset(noMultisetGroupMultiscale + "/s1", N5TestUtil.defaultAttributes());
writer.createDataset(multisetGroupMultiscale + "/s0", N5TestUtil.defaultAttributes());
writer.createDataset(multisetGroupMultiscale + "/s1", N5TestUtil.defaultAttributes());

final DatasetAttributes defaultAttributes = new DatasetAttributes(new long[]{1}, new int[]{1}, DataType.UINT8, new RawCompression());

writer.createDataset(noMultisetGroupMultiscale + "/s0", defaultAttributes);
writer.createDataset(noMultisetGroupMultiscale + "/s1", defaultAttributes);
writer.createDataset(multisetGroupMultiscale + "/s0", defaultAttributes);
writer.createDataset(multisetGroupMultiscale + "/s1", defaultAttributes);
writer.setAttribute(noMultisetGroupMultiscale + "/s0", N5Helpers.LABEL_MULTISETTYPE_KEY, false);
writer.setAttribute(multisetGroupMultiscale + "/s0", N5Helpers.LABEL_MULTISETTYPE_KEY, true);
assertFalse(N5Types.isLabelMultisetType(writer, noMultisetGroupMultiscale));
Expand Down Expand Up @@ -107,9 +126,9 @@ public void testMaxForType() {
}

@Test
public void testGetDataType() throws IOException {
public void testGetDataType(@TempDir Path tmp) throws IOException {

final N5Writer writer = N5TestUtil.fileSystemWriterAtTmpDir();
final N5Writer writer = Paintera.getN5Factory().newWriter(tmp.toAbsolutePath().toString());
for (final DataType t : DataType.values()) {
writer.createDataset(t.toString(), new long[]{1}, new int[]{1}, t, new RawCompression());
assertEquals(t, N5Types.getDataType(writer, t.toString()));
Expand Down
Loading

0 comments on commit 6ba36f1

Please sign in to comment.