Skip to content

Commit

Permalink
Migrate Client Tests From Core (#149)
Browse files Browse the repository at this point in the history
## What is the goal of this PR?
We migrate one test and implement BDD steps required for typedb/typedb-behaviour#88 which migrated client-java tests from core to BDD as well.

## What are the changes implemented in this PR?
* Implement new `TransactionSteps` methods
* implement new `KeyspaceSteps` method
* Migrate a concurrecny test to `AnswerIT` -- should be in ACID tests in the future
  • Loading branch information
flyingsilverfin authored Aug 7, 2020
1 parent e8965f7 commit 1259d3e
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 24 deletions.
2 changes: 1 addition & 1 deletion dependencies/graknlabs/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def graknlabs_verification():
git_repository(
name = "graknlabs_verification",
remote = "https://github.com/graknlabs/verification",
commit = "e1cce9175641d864be0306c21fc6c495b87c24e4", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_verification
commit = "2ec55271b4c324018897754e7d9294e0d8ea911a", # sync-marker: do not remove this comment, this is used for sync-dependencies by @graknlabs_verification
)

def graknlabs_grabl_tracing():
Expand Down
1 change: 0 additions & 1 deletion test/assembly/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeoutException;
Expand Down
5 changes: 4 additions & 1 deletion test/behaviour/connection/keyspace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ grakn_test(
"@maven//:io_cucumber_cucumber_junit",
],
runtime_deps = [
":steps"
":steps",
"//test/behaviour/connection/session:steps",
"//test/behaviour/connection/transaction:steps",
"//test/behaviour/config:parameters",
],
data = [
"@graknlabs_verification//behaviour/connection:keyspace.feature",
Expand Down
15 changes: 14 additions & 1 deletion test/behaviour/connection/keyspace/KeyspaceSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class KeyspaceSteps {

Expand All @@ -46,7 +47,7 @@ public void connection_create_keyspace(String name) {
public void connection_create_keyspaces(List<String> names) {
// TODO: This step should be rewritten once we can create keypsaces without opening sessions
for (String name : names) {
client.session(name);
client.session(name).close();
}
}

Expand All @@ -71,6 +72,18 @@ public void connection_delete_keyspaces(List<String> names) {
}
}

@Then("connection delete keyspace(s); throws exception")
public void connection_delete_keyspaces_throws_exception(List<String> names) {
for (String keyspaceName : names) {
try {
client.keyspaces().delete(keyspaceName);
fail();
} catch (Exception e) {
// successfully failed
}
}
}

@When("connection delete keyspaces in parallel:")
public void connection_delete_keyspaces_in_parallel(List<String> names) {
assertTrue(THREAD_POOL_SIZE >= names.size());
Expand Down
2 changes: 2 additions & 0 deletions test/behaviour/connection/transaction/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ java_library(

# Internal Repository Dependencies
"@graknlabs_common//:common",
"@graknlabs_graql//java:graql",

# External Maven Dependencies
"@maven//:junit_junit",
"@maven//:org_hamcrest_hamcrest_library",
"@maven//:io_cucumber_cucumber_java",
]
)
Expand Down
42 changes: 41 additions & 1 deletion test/behaviour/connection/transaction/TransactionSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
package grakn.client.test.behaviour.connection.transaction;

import grakn.client.GraknClient;
import graql.lang.Graql;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.hamcrest.Matchers;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -40,7 +42,9 @@
import static grakn.common.util.Collections.list;
import static java.util.Objects.isNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class TransactionSteps {

Expand All @@ -65,6 +69,20 @@ public void for_each_session_open_transactions_of_type(List<GraknClient.Transact
}
}

@Then("for each session, open transaction(s) of type; throws exception")
public void for_each_session_open_transactions_of_type_throws_exception(List<GraknClient.Transaction.Type> types) {
for (GraknClient.Session session : sessions) {
for (GraknClient.Transaction.Type type : types) {
try {
GraknClient.Transaction transaction = session.transaction(type);
fail();
} catch (Exception e) {
// successfully threw
}
}
}
}

@Then("for each session, transaction(s) is/are null: {bool}")
public void for_each_session_transactions_are_null(boolean isNull) {
for_each_session_transactions_are(transaction -> assertEquals(isNull, isNull(transaction)));
Expand Down Expand Up @@ -166,7 +184,10 @@ private void for_each_session_transactions_in_parallel_are(Consumer<GraknClient.
for (CompletableFuture<GraknClient.Transaction> futureTransaction :
sessionsToTransactionsParallel.get(session)) {

assertions.add(futureTransaction.thenApply(transaction -> { assertion.accept(transaction); return null; }));
assertions.add(futureTransaction.thenApply(transaction -> {
assertion.accept(transaction);
return null;
}));
}
}
CompletableFuture.allOf(assertions.toArray(new CompletableFuture[0])).join();
Expand Down Expand Up @@ -222,4 +243,23 @@ private void for_each_session_in_parallel_transactions_in_parallel_are(Consumer<
}
CompletableFuture.allOf(assertions.toArray(new CompletableFuture[0])).join();
}


// ===================================//
// transaction behaviour with queries //
// ===================================//

@Then("for each transaction, define query; throws exception containing {string}")
public void for_each_transaction_execute_define_throws_exception(String expectedException, String defineQueryStatements) {
for (GraknClient.Session session : sessions) {
for (GraknClient.Transaction transaction : sessionsToTransactions.get(session)) {
try {
transaction.execute(Graql.parse(defineQueryStatements).asDefine()).get();
fail();
} catch (Exception e) {
assertThat(e.getMessage(), Matchers.containsString(expectedException));
}
}
}
}
}
9 changes: 7 additions & 2 deletions test/behaviour/graql/GraqlSteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
import grakn.client.answer.Numeric;
import grakn.client.concept.Concept;
import grakn.client.concept.Rule;
import grakn.client.concept.SchemaConcept;
import grakn.client.concept.thing.Attribute;
import grakn.client.concept.thing.Thing;
import grakn.client.test.behaviour.connection.ConnectionSteps;
import graql.lang.Graql;
import graql.lang.pattern.Conjunction;
Expand Down Expand Up @@ -91,6 +89,13 @@ public void transaction_is_initialised() {
assertTrue(tx.isOpen());
}

@Given("transaction is closed and opened without commit")
public void transaction_is_reopened_without_commit() {
tx.close();
assertFalse(tx.isOpen());
tx = session.transaction().write();
}

@Given("the integrity is validated")
public void integrity_is_validated(){

Expand Down
37 changes: 20 additions & 17 deletions test/integration/answer/AnswerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,22 @@
import grakn.common.test.server.GraknProperties;
import grakn.common.test.server.GraknSetup;
import graql.lang.Graql;
import graql.lang.query.GraqlGet;
import graql.lang.query.GraqlInsert;
import graql.lang.statement.Variable;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeoutException;

import static grakn.client.GraknClient.Transaction.BatchSize.ALL;
import static grakn.client.GraknClient.Transaction.Options.batchSize;
import static grakn.client.GraknClient.Transaction.Options.infer;
import static grakn.client.GraknClient.Transaction.Options.explain;
import static grakn.client.GraknClient.Transaction.Options.infer;
import static graql.lang.Graql.Token.ValueType.STRING;
import static graql.lang.Graql.define;
import static graql.lang.Graql.insert;
Expand Down Expand Up @@ -157,21 +158,6 @@ public void asynchronousWriteQueriesAreCompletedWhenTxCommit() {
}
}

@Test
public void writingInAReadTransactionThrows() {
try (GraknClient.Session session = client.session("test")) {
try (GraknClient.Transaction tx = session.transaction().read()) {
tx.execute(Graql.parse("define newentity sub entity;").asDefine());
tx.commit();
fail();
} catch (Exception ex) {
if (!ex.getMessage().contains("is read only")) {
fail();
}
}
}
}

@Test
public void whenQueryingWithInferenceOn_inferredResultsExist() {
try (GraknClient.Session session = client.session("infer_on")) {
Expand Down Expand Up @@ -319,6 +305,23 @@ public void whenQueryingWithBatchSizeCustom_runsCorrectly() {
}
}

@Test
public void concurrentReadQueriesReturnResultsConcurrently() {
try (GraknClient.Session session = client.session("concurrent_read")) {
try (GraknClient.Transaction tx = session.transaction().read()) {
GraqlGet query = Graql.match(var("x").sub("thing")).get();

Iterator<ConceptMap> iterator1 = tx.stream(query).get().iterator();
Iterator<ConceptMap> iterator2 = tx.stream(query).get().iterator();

while (iterator1.hasNext() || iterator2.hasNext()) {
assertEquals(iterator1.next(), iterator2.next());
assertEquals(iterator1.hasNext(), iterator2.hasNext());
}
}
}
}

private void setupLotsOfPeople(GraknClient.Session session, int numberOfPeople) {
try (GraknClient.Transaction tx = session.transaction().write()) {
tx.execute(Graql.parse("define person sub entity;").asDefine());
Expand Down

0 comments on commit 1259d3e

Please sign in to comment.