Skip to content

Commit

Permalink
Fixed two test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
ppanopticon authored and Ralph Gasser committed Dec 8, 2021
1 parent af27a0c commit 6aa115f
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package org.vitrivr.cottontail.server.grpc
import io.grpc.ManagedChannel
import io.grpc.netty.NettyChannelBuilder
import org.apache.commons.lang3.RandomStringUtils
import org.junit.jupiter.api.*
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.vitrivr.cottontail.TestConstants
import org.vitrivr.cottontail.TestConstants.TEST_ENTITY_TUPLE_COUNT
import org.vitrivr.cottontail.client.SimpleClient
import org.vitrivr.cottontail.client.language.dml.Update
import org.vitrivr.cottontail.client.language.dql.Query
import org.vitrivr.cottontail.client.language.extensions.Literal
import org.vitrivr.cottontail.client.stub.SimpleClient
import org.vitrivr.cottontail.embedded
import java.util.concurrent.TimeUnit
import kotlin.time.ExperimentalTime
Expand All @@ -33,19 +35,19 @@ class DMLServiceTest {
this.channel = builder.build()
this.client = SimpleClient(this.channel)
assert(client.ping())
dropTestSchema(client)
createTestSchema(client)
createTestVectorEntity(client)
createTestEntity(client)
populateTestEntity(client)
populateVectorEntity(client)
GrpcTestUtils.dropTestSchema(client)
GrpcTestUtils.createTestSchema(client)
GrpcTestUtils.createTestVectorEntity(client)
GrpcTestUtils.createTestEntity(client)
GrpcTestUtils.populateTestEntity(client)
GrpcTestUtils.populateVectorEntity(client)

assert(client.ping())
}

@AfterEach
fun cleanup() {
dropTestSchema(this.client)
GrpcTestUtils.dropTestSchema(this.client)

/* Shutdown ManagedChannel. */
this.channel.shutdown()
Expand All @@ -60,63 +62,70 @@ class DMLServiceTest {
val newValue = RandomStringUtils.randomAlphabetic(4)

/* Perform update and sanity checks. */
val update = Update().from(TestConstants.TEST_ENTITY_FQN_INPUT).values(Pair(TestConstants.STRING_COLUMN_NAME, newValue))
val update = Update().from(GrpcTestUtils.TEST_ENTITY_FQN).values(Pair(GrpcTestUtils.STRING_COLUMN_NAME, newValue))
val r1 = this.client.update(update)
Assertions.assertTrue(r1.hasNext())
val el1 = r1.next()
Assertions.assertEquals(TEST_ENTITY_TUPLE_COUNT, el1.asLong(0))
Assertions.assertEquals(GrpcTestUtils.TEST_ENTITY_TUPLE_COUNT, el1.asLong(0))

/* Query and check values. */
val select = Query().select("*").from(TestConstants.TEST_ENTITY_FQN_INPUT)
val select = Query().select("*").from(GrpcTestUtils.TEST_ENTITY_FQN)
val r2 = this.client.query(select)
for (el2 in r2) {
Assertions.assertEquals(newValue, el2.asString(TestConstants.STRING_COLUMN_NAME))
Assertions.assertEquals(newValue, el2.asString(GrpcTestUtils.STRING_COLUMN_NAME))
}
}

@Test
fun testUpdateAllColumnsWithCommitAndQuery() {
/* Query and update values. */
val txId = this.client.begin()
val s1 = Query().select("*").from(TestConstants.TEST_ENTITY_FQN_INPUT)
val r1 = this.client.query(s1, txId)
val s1 = Query().select("*").from(GrpcTestUtils.TEST_ENTITY_FQN).txId(txId)
val r1 = this.client.query(s1)
for (el1 in r1) {
val update = Update().from(TestConstants.TEST_ENTITY_FQN_INPUT).values(Pair(TestConstants.INT_COLUMN_NAME, -1)).where(Literal(TestConstants.STRING_COLUMN_NAME, "=", el1.asString(TestConstants.STRING_COLUMN_NAME)!!))
val r2 = this.client.update(update, txId)
val update = Update()
.from(GrpcTestUtils.TEST_ENTITY_FQN)
.values(Pair(GrpcTestUtils.INT_COLUMN_NAME, -1))
.where(Literal(GrpcTestUtils.STRING_COLUMN_NAME, "=", el1.asString(GrpcTestUtils.STRING_COLUMN_NAME)!!))
.txId(txId)
val r2 = this.client.update(update)
Assertions.assertTrue(r2.hasNext())
val el2 = r2.next()
Assertions.assertEquals(1, el2.asLong(0))
}
this.client.commit(txId)

/* Query and check values. */
val select = Query().select("*").from(TestConstants.TEST_ENTITY_FQN_INPUT)
val select = Query().select("*").from(GrpcTestUtils.TEST_ENTITY_FQN)
val r2 = this.client.query(select)
for (el2 in r2) {
Assertions.assertEquals(-1, el2.asInt(TestConstants.INT_COLUMN_NAME))
Assertions.assertEquals(-1, el2.asInt(GrpcTestUtils.INT_COLUMN_NAME))
}
}

@Test
fun testUpdateAllColumnsWithRollbackAndQuery() {
/* Query and update values. */
val txId = this.client.begin()
val s1 = Query().select("*").from(TestConstants.TEST_ENTITY_FQN_INPUT)
val r1 = this.client.query(s1, txId)
val s1 = Query().select("*").from(GrpcTestUtils.TEST_ENTITY_FQN).txId(txId)
val r1 = this.client.query(s1)
for (el1 in r1) {
val update = Update().from(TestConstants.TEST_ENTITY_FQN_INPUT).values(Pair(TestConstants.INT_COLUMN_NAME, -1)).where(Literal(TestConstants.STRING_COLUMN_NAME, "=", el1.asString(TestConstants.STRING_COLUMN_NAME)!!))
val r2 = this.client.update(update, txId)
val update = Update().from(GrpcTestUtils.TEST_ENTITY_FQN)
.values(Pair(GrpcTestUtils.INT_COLUMN_NAME, -1))
.where(Literal(GrpcTestUtils.STRING_COLUMN_NAME, "=", el1.asString(GrpcTestUtils.STRING_COLUMN_NAME)!!))
.txId(txId)
val r2 = this.client.update(update)
Assertions.assertTrue(r2.hasNext())
val el2 = r2.next()
Assertions.assertEquals(1, el2.asLong(0))
}
this.client.rollback(txId)

/* Query and check values. */
val select = Query().select("*").from(TestConstants.TEST_ENTITY_FQN_INPUT)
val select = Query().select("*").from(GrpcTestUtils.TEST_ENTITY_FQN)
val r2 = this.client.query(select)
for (el2 in r2) {
Assertions.assertNotEquals(-1, el2.asInt(TestConstants.INT_COLUMN_NAME))
Assertions.assertNotEquals(-1, el2.asInt(GrpcTestUtils.INT_COLUMN_NAME))
}
}
}
113 changes: 62 additions & 51 deletions src/test/kotlin/org/vitrivr/cottontail/server/grpc/DQLServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package org.vitrivr.cottontail.server.grpc
import io.grpc.ManagedChannel
import io.grpc.netty.NettyChannelBuilder
import org.junit.jupiter.api.*
import org.junit.jupiter.api.Assertions.assertTrue
import org.vitrivr.cottontail.TestConstants
import org.vitrivr.cottontail.client.SimpleClient
import org.vitrivr.cottontail.client.language.basics.Direction
import org.vitrivr.cottontail.client.language.basics.Distances
import org.vitrivr.cottontail.client.language.dql.Query
import org.vitrivr.cottontail.client.language.extensions.And
import org.vitrivr.cottontail.client.language.extensions.Literal
import org.vitrivr.cottontail.embedded
import org.vitrivr.cottontail.server.grpc.GrpcTestUtils.STRING_COLUMN_NAME
import org.vitrivr.cottontail.server.grpc.GrpcTestUtils.TEST_VECTOR_ENTITY_FQN_INPUT
import org.vitrivr.cottontail.server.grpc.GrpcTestUtils.TWOD_COLUMN_NAME
import java.util.concurrent.TimeUnit
import kotlin.random.Random
import kotlin.time.ExperimentalTime

@ExperimentalTime
Expand All @@ -21,14 +25,15 @@ class DQLServiceTest {
private lateinit var channel: ManagedChannel
private lateinit var embedded: CottontailGrpcServer


@BeforeAll
fun startCottontail() {
this.embedded = embedded(TestConstants.testConfig())
this.channel = NettyChannelBuilder.forAddress("localhost", 1865).usePlaintext().build()
val builder = NettyChannelBuilder.forAddress("localhost", 1865)
builder.usePlaintext()
this.channel = builder.build()
this.client = SimpleClient(this.channel)
assert(client.ping())

/* Prepare test database. */
GrpcTestUtils.dropTestSchema(client)
GrpcTestUtils.createTestSchema(client)
GrpcTestUtils.createTestVectorEntity(client)
Expand All @@ -39,12 +44,8 @@ class DQLServiceTest {

@AfterAll
fun cleanup() {
/* Drop test schema. */
GrpcTestUtils.dropTestSchema(client)

/* Close SimpleClient. */
this.client.close()

/* Shutdown ManagedChannel. */
this.channel.shutdown()
this.channel.awaitTermination(5000, TimeUnit.MILLISECONDS)
Expand All @@ -55,12 +56,17 @@ class DQLServiceTest {

@BeforeEach
fun setup() {
assert(this.client.ping())
assert(client.ping())
}

@AfterEach
fun tearDown() {
assert(client.ping())
}

@Test
fun pingTest() {
assert(this.client.ping()) { "ping unsuccessful" }
assert(client.ping()) { "ping unsuccessful" }
}

@Test
Expand All @@ -72,64 +78,69 @@ class DQLServiceTest {

@Test
fun queryColumn() {
val query = Query().from(GrpcTestUtils.TEST_ENTITY_FQN).select(GrpcTestUtils.STRING_COLUMN_NAME)
val query = Query().from(GrpcTestUtils.TEST_ENTITY_FQN).select(STRING_COLUMN_NAME)
val result = client.query(query)
assert(result.numberOfColumns == 1)
val el = result.next()
assert(!el.asString(GrpcTestUtils.STRING_COLUMN_NAME).equals(""))
assert(!el.asString(STRING_COLUMN_NAME).equals(""))
}

@Test
fun queryBetween() {
val random = Random.Default
val lb = random.nextInt(98)
val ub = random.nextInt(lb+1, 100)
val query = Query().from(GrpcTestUtils.TEST_ENTITY_FQN)
.select(GrpcTestUtils.STRING_COLUMN_NAME)
.select(GrpcTestUtils.INT_COLUMN_NAME)
.where(Literal(GrpcTestUtils.INT_COLUMN_NAME, "BETWEEN", lb, ub))
fun queryColumnWithVector() {
val query = Query().from(TEST_VECTOR_ENTITY_FQN_INPUT).select(STRING_COLUMN_NAME)
val result = client.query(query)
for (el in result) {
assert(el.asInt(GrpcTestUtils.INT_COLUMN_NAME)!! in lb..ub)
}
assert(result.numberOfColumns == 1)
val el = result.next()
assert(!el.asString(STRING_COLUMN_NAME).equals(""))
}

@Test
fun queryBetweenWithAnd() {
val random = Random.Default
val lb = random.nextInt(50)
val query = Query().from(GrpcTestUtils.TEST_ENTITY_FQN)
.select(GrpcTestUtils.STRING_COLUMN_NAME)
.select(GrpcTestUtils.INT_COLUMN_NAME)
.where(
And(
Literal(GrpcTestUtils.INT_COLUMN_NAME, "BETWEEN", lb, lb+1),
Literal(GrpcTestUtils.INT_COLUMN_NAME, "BETWEEN", lb+1, lb+2)
)
)
fun haversineDistance() {
val query = Query()
.select("*")
.from(TEST_VECTOR_ENTITY_FQN_INPUT)
.distance(TWOD_COLUMN_NAME, arrayOf(5f, 10f), Distances.HAVERSINE, "distance")
.order("distance", Direction.ASC)
.limit(500)
val result = client.query(query)
for (el in result) {
assert(el.asInt(GrpcTestUtils.INT_COLUMN_NAME)!! == lb + 1)
}
val el = result.next()
val distance = el.asDouble("distance")
assert(distance != null)
}

@Test
fun queryColumnWithVector() {
val query = Query().from(GrpcTestUtils.TEST_VECTOR_ENTITY_FQN_INPUT).select(GrpcTestUtils.STRING_COLUMN_NAME)
fun queryNNSWithLikeStart() {
val query = Query()
.select("*")
.from(TEST_VECTOR_ENTITY_FQN_INPUT)
.distance(TWOD_COLUMN_NAME, arrayOf(5f, 10f), Distances.L2, "distance")
.where(Literal(STRING_COLUMN_NAME, "LIKE", "a%"))
.order("distance", Direction.ASC)
.limit(500)

val result = client.query(query)
assert(result.numberOfColumns == 1)
val el = result.next()
assert(!el.asString(GrpcTestUtils.STRING_COLUMN_NAME).equals(""))
for (r in result) {
val distance = r.asDouble("distance")
val string = r.asString(STRING_COLUMN_NAME)!!
assert(distance != null)
assertTrue(string.first() == 'a')
}
}

@Test
fun haversineDistance() {
arrayOf(5f, 10f)
Query().from(GrpcTestUtils.TEST_VECTOR_ENTITY_FQN_INPUT)
val query = nns
fun queryNNSWithLikeEnd() {
val query = Query().from(TEST_VECTOR_ENTITY_FQN_INPUT)
.select("*")
.distance(TWOD_COLUMN_NAME, arrayOf(5f, 10f), Distances.L2, "distance")
.where(Literal(STRING_COLUMN_NAME, "LIKE", "%z"))
.order("distance", Direction.ASC)
.limit(500)
val result = client.query(query)
val el = result.next()
val distance = el.asDouble("distance")
assert(distance != null)
for (r in result) {
val distance = r.asDouble("distance")
val string = r.asString(STRING_COLUMN_NAME)!!
assert(distance != null)
assertTrue(string.last() == 'z')
}
}
}

0 comments on commit 6aa115f

Please sign in to comment.