Skip to content

Commit

Permalink
Add score for NAs #158
Browse files Browse the repository at this point in the history
  • Loading branch information
pkiraly committed Dec 11, 2023
1 parent 92fb2e6 commit eb16fcf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ private static List<RuleChecker> processRule(Schema schema, DataElement dataElem
for (RuleChecker ruleChecker : ruleCheckers) {
ruleChecker.setFailureScore(rule.getFailureScore());
ruleChecker.setSuccessScore(rule.getSuccessScore());
ruleChecker.setNaScore(rule.getNaScore());
String idValue = StringUtils.isNotBlank(rule.getId()) ? rule.getId() : String.valueOf(++id);
ruleChecker.setId(idValue);
if (rule.getHidden().equals(Boolean.TRUE))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class AndCheckerTest {
Expand All @@ -38,7 +39,13 @@ public void setUp() throws Exception {
.addField("title")
.addField("alt")
;
schema.getPathByLabel("name").setRule(Arrays.asList(new Rule().withAnd(Arrays.asList(new Rule().withMinCount(1), new Rule().withMaxCount(1)))));
schema.getPathByLabel("name")
.setRule(Arrays.asList(
new Rule().withAnd(Arrays.asList(
new Rule().withMinCount(1),
new Rule().withMaxCount(1)))
.withNaScore(-1)
));

cache = (CsvSelector) SelectorFactory.getInstance(schema.getFormat(), "a,b,a");
cache.setCsvReader(new CsvReader().setHeader(((CsvAwareSchema) schema).getHeader()));
Expand All @@ -54,12 +61,15 @@ public void update() {
List<RuleChecker> checkers = schema.getRuleCheckers();
AndChecker andChecker = (AndChecker) checkers.get(0);
assertEquals(2, andChecker.getCheckers().size());
assertEquals(-1, (int) andChecker.getNaScore());

assertEquals(MinCountChecker.class, andChecker.getCheckers().get(0).getClass());
MinCountChecker minCountChecker = (MinCountChecker) andChecker.getCheckers().get(0);
assertNull(minCountChecker.getNaScore());

assertEquals(MaxCountChecker.class, andChecker.getCheckers().get(1).getClass());
MaxCountChecker maxCountChecker = (MaxCountChecker) andChecker.getCheckers().get(1);
assertNull(maxCountChecker.getNaScore());

FieldCounter<RuleCheckerOutput> fieldCounter = new FieldCounter<>();
andChecker.update(cache, fieldCounter, RuleCheckingOutputType.BOTH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public void prefix() {
public void success() {
MaxCountChecker checker = (MaxCountChecker) new MaxCountChecker(schema.getPathByLabel("name"), 1)
.withSuccessScore(1)
.withNaScore(0)
.withFailureScore(-1);
.withNaScore(-1)
.withFailureScore(-2);

FieldCounter<RuleCheckerOutput> fieldCounter = new FieldCounter<>();
checker.update(cache, fieldCounter, RuleCheckingOutputType.BOTH);
Expand All @@ -42,24 +42,24 @@ public void success() {
public void failure() {
MaxCountChecker checker = (MaxCountChecker) new MaxCountChecker(schema.getPathByLabel("name"), 0)
.withSuccessScore(1)
.withNaScore(0)
.withFailureScore(-1);
.withNaScore(-1)
.withFailureScore(-2);

FieldCounter<RuleCheckerOutput> fieldCounter = new FieldCounter<>();
checker.update(cache, fieldCounter, RuleCheckingOutputType.BOTH);

assertEquals(2, fieldCounter.size());
assertEquals("name:maxCount", checker.getHeaderWithoutId());
Assert.assertEquals(RuleCheckingOutputStatus.FAILED, fieldCounter.get(checker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
Assert.assertEquals(-1, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getScore());
Assert.assertEquals(-2, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getScore());
}

@Test
public void na() {
MaxCountChecker checker = (MaxCountChecker) new MaxCountChecker(schema.getPathByLabel("name"), 1)
.withSuccessScore(1)
.withNaScore(0)
.withFailureScore(-1);
.withNaScore(-1)
.withFailureScore(-2);

cache = (CsvSelector) SelectorFactory.getInstance(schema.getFormat(), "");
cache.setCsvReader(new CsvReader().setHeader( ((CsvAwareSchema) schema).getHeader() ));
Expand All @@ -70,6 +70,6 @@ public void na() {
assertEquals(2, fieldCounter.size());
assertEquals("name:maxCount", checker.getHeaderWithoutId());
Assert.assertEquals(RuleCheckingOutputStatus.NA, fieldCounter.get(checker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
Assert.assertEquals(0, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getScore());
Assert.assertEquals(-1, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getScore());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package de.gwdg.metadataqa.api.rule.singlefieldchecker;

import de.gwdg.metadataqa.api.counter.FieldCounter;
import de.gwdg.metadataqa.api.model.selector.CsvSelector;
import de.gwdg.metadataqa.api.model.selector.SelectorFactory;
import de.gwdg.metadataqa.api.rule.CheckerTestBase;
import de.gwdg.metadataqa.api.rule.RuleCheckerOutput;
import de.gwdg.metadataqa.api.rule.RuleCheckingOutputStatus;
import de.gwdg.metadataqa.api.rule.RuleCheckingOutputType;
import de.gwdg.metadataqa.api.schema.CsvAwareSchema;
import de.gwdg.metadataqa.api.util.CsvReader;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -40,4 +44,24 @@ public void failure() {
assertEquals("name:maxLength", checker.getHeaderWithoutId());
assertEquals(RuleCheckingOutputStatus.FAILED, fieldCounter.get(checker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
}

@Test
public void na() {
MaxLengthChecker checker = (MaxLengthChecker) new MaxLengthChecker(schema.getPathByLabel("name"), 1)
.withSuccessScore(1)
.withNaScore(-1)
.withFailureScore(-2);

cache = (CsvSelector) SelectorFactory.getInstance(schema.getFormat(), "");
cache.setCsvReader(new CsvReader().setHeader( ((CsvAwareSchema) schema).getHeader() ));

FieldCounter<RuleCheckerOutput> fieldCounter = new FieldCounter<>();
checker.update(cache, fieldCounter, RuleCheckingOutputType.BOTH);
System.err.println(checker.getHeader());

assertEquals(2, fieldCounter.size());
assertEquals("name:maxLength", checker.getHeaderWithoutId());
Assert.assertEquals(RuleCheckingOutputStatus.NA, fieldCounter.get(checker.getHeader(RuleCheckingOutputType.STATUS)).getStatus());
Assert.assertEquals(-1, (int) fieldCounter.get(checker.getHeader(RuleCheckingOutputType.SCORE)).getScore());
}
}

0 comments on commit eb16fcf

Please sign in to comment.