Skip to content

Commit 718ccdc

Browse files
committed
disallow write twice to 'estimate' and 'distribution' component (toml)
1 parent 36df9b6 commit 718ccdc

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

api/src/main/java/org/fairdatapipeline/api/Object_component_write.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ public CleanableFileChannel writeFileChannel() throws IOException {
6464
* @param estimateNumber the number to write.
6565
*/
6666
public void writeEstimate(Number estimateNumber) {
67+
if(this.been_used) {
68+
throw(new RuntimeException("obj component already written"));
69+
}
6770
var estimate =
6871
ImmutableEstimate.builder().internalValue(estimateNumber).rng(this.dp.coderun.rng).build();
6972

@@ -72,6 +75,7 @@ public void writeEstimate(Number estimateNumber) {
7275
} catch (IOException e) {
7376
throw (new RuntimeException("writeEstimate() -- IOException trying to write to file.", e));
7477
}
78+
this.been_used = true;
7579
}
7680

7781
/**
@@ -80,12 +84,16 @@ public void writeEstimate(Number estimateNumber) {
8084
* @param distribution the Distribution to write
8185
*/
8286
public void writeDistribution(Distribution distribution) {
87+
if(this.been_used) {
88+
throw(new RuntimeException("obj component already written"));
89+
}
8390
try (CleanableFileChannel fileChannel = this.getFileChannel()) {
8491
this.dp.coderun.parameterDataWriter.write(fileChannel, this.component_name, distribution);
8592
} catch (IOException e) {
8693
throw (new RuntimeException(
8794
"writeDistribution() -- IOException trying to write to file.", e));
8895
}
96+
this.been_used = true;
8997
}
9098

9199
/**

api/src/test/java/org/fairdatapipeline/api/CoderunIntegrationTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ void check_last_coderun(
325325
}
326326
}
327327

328+
@Test
329+
@Order(1)
330+
void testWriteEstimateFail() {
331+
String dataProduct = "human/population2";
332+
String component = "estimate-component";
333+
try (Coderun coderun = new Coderun(configPath, scriptPath, token)) {
334+
Data_product_write dp = coderun.get_dp_for_write(dataProduct, "toml");
335+
Object_component_write oc = dp.getComponent(component);
336+
oc.writeEstimate(estimate);
337+
assertThrows(RuntimeException.class, () -> oc.writeEstimate(estimate));
338+
}
339+
}
328340
@Test
329341
@Order(1)
330342
void testWriteEstimate() {
@@ -381,6 +393,18 @@ void testReadDistribution() {
381393
check_last_coderun(List.of(new Triplet<>(dataProduct, component, hash)), null);
382394
}
383395

396+
@Test
397+
@Order(5)
398+
void testWriteCategoricalDistributionFail() {
399+
String dataProduct = "human/cdistribution2";
400+
String component = "cdistribution-component";
401+
try (var coderun = new Coderun(configPath, scriptPath, token)) {
402+
Data_product_write dp = coderun.get_dp_for_write(dataProduct, "toml");
403+
Object_component_write oc = dp.getComponent(component);
404+
oc.writeDistribution(categoricalDistribution);
405+
assertThrows(RuntimeException.class, () -> oc.writeDistribution(distribution));
406+
}
407+
}
384408
@Test
385409
@Order(5)
386410
void testWriteCategoricalDistribution() {

0 commit comments

Comments
 (0)