Skip to content

Commit

Permalink
Transaction::checkEmptyCollections() added
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
zozlak committed Aug 6, 2024
1 parent c0e3fad commit b2b616a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions config-sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ schema:
sharedObject: https://vocabs.acdh.oeaw.ac.at/schema#AgentOrPlaceOrPublication
container: https://vocabs.acdh.oeaw.ac.at/schema#Container
topCollection: https://vocabs.acdh.oeaw.ac.at/schema#TopCollection
collection: https://vocabs.acdh.oeaw.ac.at/schema#collection
namespaces:
ontology: https://vocabs.acdh.oeaw.ac.at/schema#
ontology:
Expand Down
37 changes: 37 additions & 0 deletions src/acdhOeaw/arche/doorkeeper/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,43 @@ public function checkAutoCreatedResources(): void {
}
}

/**
*
* @throws DoorkeeperException
*/
#[CheckAttribute]
public function checkEmptyCollections(): void {
$query = "
SELECT string_agg(DISTINCT ids, ', ')
FROM
resources r
JOIN metadata c USING (id)
JOIN identifiers USING (id)
WHERE
r.state = ?
AND r.transaction_id = ?
AND c.property = ?
AND c.value IN (?, ?)
AND NOT EXISTS (SELECT 1 FROM relations ch WHERE ch.property = ? AND ch.target_id = r.id)
";
$param = [
ArcheTransaction::STATE_ACTIVE,
$this->txId,
RDF::RDF_TYPE,
$this->schema->classes->topCollection, $this->schema->classes->collection,
$this->schema->parent
];
$t = microtime(true);
$query = $this->pdo->prepare($query);
$query->execute($param);
$invalidRes = $query->fetchColumn();
$t = microtime(true) - $t;
$this->log?->debug("\t\checkEmptyCollections performed in $t s");
if (!empty($invalidRes)) {
throw new DoorkeeperException("Transaction created empty collections: $invalidRes");
}
}

/**
*
* @return void
Expand Down
27 changes: 25 additions & 2 deletions tests/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function testCollectionAggregates(): void {

public function testTopCollectionAggregates(): void {
self::$config->schema->classes->collection = 'https://vocabs.acdh.oeaw.ac.at/schema#TopCollection';
self::$schema = new \acdhOeaw\arche\lib\Schema(self::$config->schema);
self::$schema = new \acdhOeaw\arche\lib\Schema(self::$config->schema);
$this->testCollectionAggregates();
}

Expand Down Expand Up @@ -316,7 +316,30 @@ public function testIsNewVersionOf(): void {
$resp = $e->getResponse();
$this->assertEquals(400, $resp->getStatusCode());
$this->assertStringContainsString("More than one $verProp pointing to some resources:", (string) $resp->getBody());
sleep(1); // to avoid removing resources between the transaction is fully rolled back
sleep(1); // to avoid removing resources before the transaction is fully rolled back
}
}

public function testEmptyCollection(): void {
$tcm = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#TopCollection');
$cm = self::createMetadata([], 'https://vocabs.acdh.oeaw.ac.at/schema#Collection');

self::$repo->begin();
$tcr = self::$repo->createResource($tcm);
$this->toDelete[] = $tcr;
$cr = self::$repo->createResource($cm);
$this->toDelete[] = $cr;
try {
self::$repo->commit();
$this->assertTrue(false);
} catch (ClientException $e) {
$msg = (string) $e->getResponse()->getBody();
$tcid = preg_replace('|^.*/|', '', $tcr->getUri());
$cid = preg_replace('|^.*/|', '', $cr->getUri());
$this->assertStringStartsWith("Transaction created empty collections: ", $msg);
$this->assertStringContainsString($tcid, $msg);
$this->assertStringContainsString($cid, $msg);
sleep(1); // to avoid removing resources before the transaction is fully rolled back
}
}
}

0 comments on commit b2b616a

Please sign in to comment.