Skip to content

Commit 60e907d

Browse files
authored
Changed test to use version returned by query in conditional put / delete. (#31)
1 parent b3725f0 commit 60e907d

File tree

1 file changed

+57
-9
lines changed

1 file changed

+57
-9
lines changed

driver/src/test/java/oracle/nosql/driver/BasicTest.java

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -797,15 +797,12 @@ public void testPutGetDelete() {
797797
(serialVersion > 2), /* modtime should be recent */
798798
recordKB);
799799

800-
/* save this for comparison below */
801-
Version version = getRes.getVersion();
802-
803800
/*
804-
* get the row version of the same row using a query and assert
805-
* that the versions are the same. A test could be created to use this
806-
* in a condition put/delete operation but that functionality is already
807-
* tested. This just ensures that the versions acquired from get()
808-
* and from row_version are identical
801+
* get the row version of the same row using a query and check that
802+
* the version can be used in a conditional put/delete.
803+
* Note we can't rely on comparing the byte arrays for each, because
804+
* the arrays may be slightly different based on versions of client
805+
* and server in use.
809806
*/
810807
try (QueryRequest queryReq = new QueryRequest()) {
811808
final String versionQuery = "select row_version($t) as version " +
@@ -815,9 +812,60 @@ public void testPutGetDelete() {
815812
MapValue result = queryRet.getResults().get(0);
816813
Version qVersion = Version.createVersion(
817814
result.get("version").asBinary().getValue());
818-
assertArrayEquals(version.getBytes(), qVersion.getBytes());
815+
816+
/*
817+
* Put an existing row with matching version, it should succeed.
818+
*/
819+
putReq = new PutRequest()
820+
.setOption(Option.IfVersion)
821+
.setMatchVersion(qVersion)
822+
.setValue(value)
823+
.setDurability(Durability.COMMIT_SYNC)
824+
.setTableName(tableName);
825+
putRes = handle.put(putReq);
826+
checkPutResult(putReq, putRes,
827+
true /* shouldSucceed */,
828+
true /* rowPresent */,
829+
null /* expPrevValue */,
830+
null /* expPrevVersion */,
831+
false, /* modtime should be zero */
832+
recordKB);
833+
newVersion = putRes.getVersion();
834+
}
835+
836+
/*
837+
* Get the version from a query again, and his time do a
838+
* conditional delete
839+
*/
840+
try (QueryRequest queryReq = new QueryRequest()) {
841+
final String versionQuery = "select row_version($t) as version " +
842+
"from " + tableName + " $t where id = 10";
843+
queryReq.setStatement(versionQuery);
844+
QueryResult queryRet = handle.query(queryReq);
845+
MapValue result = queryRet.getResults().get(0);
846+
Version qVersion = Version.createVersion(
847+
result.get("version").asBinary().getValue());
848+
849+
key = new MapValue().put("id", 10);
850+
DeleteRequest delReq = new DeleteRequest()
851+
.setMatchVersion(qVersion)
852+
.setKey(key)
853+
.setTableName(tableName);
854+
DeleteResult delRes = handle.delete(delReq);
855+
checkDeleteResult(delReq, delRes,
856+
true /* shouldSucceed */,
857+
true /* rowPresent */,
858+
null /* expPrevValue */,
859+
null /* expPrevVersion */,
860+
false, /* modtime should be zero */
861+
recordKB);
819862
}
820863

864+
/* Put the row back to store */
865+
putReq = new PutRequest().setValue(value).setTableName(tableName);
866+
putRes = handle.put(putReq);
867+
newVersion = putRes.getVersion();
868+
821869
/* Get a row with ABSOLUTE consistency */
822870
getReq.setConsistency(Consistency.ABSOLUTE);
823871
getRes = handle.get(getReq);

0 commit comments

Comments
 (0)