Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Add unit test for record transformation. (#102, #170)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Mar 25, 2022
1 parent e2cf74c commit 2378455
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/RecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Arrays;
import java.util.concurrent.atomic.LongAdder;
import java.util.function.Consumer;

@ExtendWith(MetafixToDo.Extension.class)
public class RecordTest {

private static final String FIELD = "field";
Expand Down Expand Up @@ -314,4 +317,53 @@ public void shouldFindArrayIndexSubfieldAfterTransformingOtherSubfieldToNull() {
});
}

@Test
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/pull/170")
public void shouldPreserveOrderWhenTransformingArraySubfields() {
final Record record = new Record();
record.put(FIELD, Value.newArray(a -> {
for (int i = 0; i < 3; ++i) {
a.add(Value.newHash(h -> {
h.put(FIELD, VALUE);
h.put(OTHER_FIELD, OTHER_VALUE);
}));
}
}));

final String path = String.join(".", FIELD, "%s", OTHER_FIELD);

final LongAdder counter = new LongAdder();
record.transform(String.format(path, "*"), s -> {
counter.increment();
return s + counter;
});

Assertions.assertEquals(new Value(OTHER_VALUE + "1"), record.get(String.format(path, 1)));
Assertions.assertEquals(new Value(OTHER_VALUE + "2"), record.get(String.format(path, 2)));
Assertions.assertEquals(new Value(OTHER_VALUE + "3"), record.get(String.format(path, 3)));
}

@Test
@MetafixToDo("See https://github.com/metafacture/metafacture-fix/pull/170")
public void shouldPreserveOrderWhenTransformingOptionalArraySubfield() {
final Record record = new Record();
record.put(FIELD, Value.newArray(a -> {
a.add(Value.newHash(h -> {
h.put(FIELD, VALUE);
}));
a.add(Value.newHash(h -> {
h.put(FIELD, VALUE);
h.put(OTHER_FIELD, OTHER_VALUE);
}));
}));

final String path = String.join(".", FIELD, "%s", OTHER_FIELD);
final String value = "transformed value";

record.transform(String.format(path, "*"), s -> value);

Assertions.assertEquals(null, record.get(String.format(path, 1)));
Assertions.assertEquals(new Value(value), record.get(String.format(path, 2)));
}

}

0 comments on commit 2378455

Please sign in to comment.