|
| 1 | +package dev.zarr.zarrjava; |
| 2 | + |
| 3 | +import dev.zarr.zarrjava.core.Attributes; |
| 4 | +import dev.zarr.zarrjava.store.FilesystemStore; |
| 5 | +import dev.zarr.zarrjava.store.StoreHandle; |
| 6 | +import dev.zarr.zarrjava.v2.Array; |
| 7 | +import dev.zarr.zarrjava.v2.ArrayMetadata; |
| 8 | +import dev.zarr.zarrjava.v2.DataType; |
| 9 | +import org.junit.jupiter.api.Assertions; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +public class ZarrAttributesTest extends ZarrTest { |
| 17 | + |
| 18 | + ArrayList<Object> listAttribute = new ArrayList<Object>() { |
| 19 | + { |
| 20 | + add(1); |
| 21 | + add(2.0d); |
| 22 | + add("string"); |
| 23 | + } |
| 24 | + }; |
| 25 | + int[] intArrayAttribute = new int[]{1, 2, 3}; |
| 26 | + long[] longArrayAttribute = new long[]{1, 2, 3}; |
| 27 | + double[] doubleArrayAttribute = new double[]{1.0, 2.0, 3.0}; |
| 28 | + float[] floatArrayAttribute = new float[]{1.0f, 2.0f, 3.0f}; |
| 29 | + Attributes testAttributes = new Attributes() {{ |
| 30 | + put("string", "stringvalue"); |
| 31 | + put("int", 42); |
| 32 | + put("float", 0.5f); |
| 33 | + put("double", 3.14); |
| 34 | + put("boolean", true); |
| 35 | + put("list", listAttribute); |
| 36 | + put("int_array", intArrayAttribute); |
| 37 | + put("long_array", longArrayAttribute); |
| 38 | + put("double_array", doubleArrayAttribute); |
| 39 | + put("float_array", floatArrayAttribute); |
| 40 | + put("nested", new Attributes() {{ |
| 41 | + put("element", "value"); |
| 42 | + }}); |
| 43 | + put("array_of_attributes", new Attributes[]{ |
| 44 | + new Attributes() {{ |
| 45 | + put("a", 1); |
| 46 | + }}, |
| 47 | + new Attributes() {{ |
| 48 | + put("b", 2); |
| 49 | + }} |
| 50 | + }); |
| 51 | + }}; |
| 52 | + |
| 53 | + static void assertListEquals(List<Object> a, List<Object> b) { |
| 54 | + Assertions.assertEquals(a.size(), b.size()); |
| 55 | + for (int i = 0; i < a.size(); i++) { |
| 56 | + Object aval = a.get(i); |
| 57 | + Object bval = b.get(i); |
| 58 | + if (aval instanceof List && bval instanceof List) { |
| 59 | + assertListEquals((List<Object>) aval, (List<Object>) bval); |
| 60 | + } else { |
| 61 | + Assertions.assertEquals(aval, bval); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void assertContainsTestAttributes(Attributes attributes) throws ZarrException { |
| 67 | + Assertions.assertEquals("stringvalue", attributes.getString("string")); |
| 68 | + Assertions.assertEquals(42, attributes.getInt("int")); |
| 69 | + Assertions.assertEquals(0.5, attributes.getFloat("float")); |
| 70 | + Assertions.assertEquals(3.14, attributes.getDouble("double")); |
| 71 | + Assertions.assertTrue(attributes.getBoolean("boolean")); |
| 72 | + assertListEquals(listAttribute, attributes.getList("list")); |
| 73 | + Assertions.assertArrayEquals(intArrayAttribute, attributes.getIntArray("int_array")); |
| 74 | + Assertions.assertArrayEquals(longArrayAttribute, attributes.getLongArray("long_array")); |
| 75 | + Assertions.assertArrayEquals(doubleArrayAttribute, attributes.getDoubleArray("double_array")); |
| 76 | + Assertions.assertArrayEquals(floatArrayAttribute, attributes.getFloatArray("float_array")); |
| 77 | + Assertions.assertEquals("value", attributes.getAttributes("nested").getString("element")); |
| 78 | + Assertions.assertArrayEquals( |
| 79 | + new Attributes[]{ |
| 80 | + new Attributes() {{ put("a", 1); }}, |
| 81 | + new Attributes() {{ put("b", 2); }} |
| 82 | + }, |
| 83 | + attributes.getArray("array_of_attributes", Attributes.class) |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testAttributesV2() throws IOException, ZarrException { |
| 89 | + StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("testAttributesV2"); |
| 90 | + |
| 91 | + ArrayMetadata arrayMetadata = dev.zarr.zarrjava.v2.Array.metadataBuilder() |
| 92 | + .withShape(10, 10) |
| 93 | + .withDataType(DataType.UINT8) |
| 94 | + .withChunks(5, 5) |
| 95 | + .putAttribute("specific", "attribute") |
| 96 | + .withAttributes(testAttributes) |
| 97 | + .withAttributes(new Attributes() {{ |
| 98 | + put("another", "attribute"); |
| 99 | + }}) |
| 100 | + .build(); |
| 101 | + |
| 102 | + dev.zarr.zarrjava.v2.Array array = dev.zarr.zarrjava.v2.Array.create(storeHandle, arrayMetadata); |
| 103 | + assertContainsTestAttributes(array.metadata().attributes()); |
| 104 | + Assertions.assertEquals("attribute", array.metadata().attributes().getString("specific")); |
| 105 | + Assertions.assertEquals("attribute", array.metadata().attributes().getString("another")); |
| 106 | + |
| 107 | + dev.zarr.zarrjava.v2.Array arrayOpened = Array.open(storeHandle); |
| 108 | + assertContainsTestAttributes(array.metadata().attributes()); |
| 109 | + Assertions.assertEquals("attribute", arrayOpened.metadata().attributes().getString("another")); |
| 110 | + Assertions.assertEquals("attribute", arrayOpened.metadata().attributes().getString("specific")); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testAttributesV3() throws IOException, ZarrException { |
| 115 | + StoreHandle storeHandle = new FilesystemStore(TESTOUTPUT).resolve("testAttributesV3"); |
| 116 | + |
| 117 | + dev.zarr.zarrjava.v3.ArrayMetadata arrayMetadata = dev.zarr.zarrjava.v3.Array.metadataBuilder() |
| 118 | + .withShape(10, 10) |
| 119 | + .withDataType(dev.zarr.zarrjava.v3.DataType.UINT8) |
| 120 | + .withChunkShape(5, 5) |
| 121 | + .putAttribute("specific", "attribute") |
| 122 | + .withAttributes(testAttributes) |
| 123 | + .withAttributes(new Attributes() {{ |
| 124 | + put("another", "attribute"); |
| 125 | + }}) |
| 126 | + |
| 127 | + .build(); |
| 128 | + |
| 129 | + dev.zarr.zarrjava.v3.Array array = dev.zarr.zarrjava.v3.Array.create(storeHandle, arrayMetadata); |
| 130 | + assertContainsTestAttributes(array.metadata().attributes()); |
| 131 | + Assertions.assertEquals("attribute", array.metadata().attributes().getString("specific")); |
| 132 | + Assertions.assertEquals("attribute", array.metadata().attributes().getString("another")); |
| 133 | + |
| 134 | + dev.zarr.zarrjava.v3.Array arrayOpened = dev.zarr.zarrjava.v3.Array.open(storeHandle); |
| 135 | + assertContainsTestAttributes(arrayOpened.metadata().attributes()); |
| 136 | + Assertions.assertEquals("attribute", arrayOpened.metadata().attributes().getString("specific")); |
| 137 | + Assertions.assertEquals("attribute", arrayOpened.metadata().attributes().getString("another")); |
| 138 | + } |
| 139 | +} |
0 commit comments