Skip to content

Commit

Permalink
Fix document field equals and hash code test
Browse files Browse the repository at this point in the history
For the document field equals and hash code tests, we try to mutate the
document field to intentionally produce a document field not equal to
our provided one. We do this by randomly choosing a document field that
has either
 - a randomly chosen field name and the same field value as the provided
   document field
 - a randomly chosen field value and the same field value as the
   provided document field

If we are unlucky, it can be that the document field chosen by this
method can be equal to the provided document field. In this case, our
test will fail because the mutation really should be not equal. In this
case, we should simply try the other mutation. Note that random document
field produced by the second method can be equal to the provided
document because it has the same field name and we can get unlucky with
our randomly chosen field values. It is not the case that the random
document field produced by the first method can be equal to the provided
document field; this is because the current implementation guarantees
that the field name length will be different guaranteeing that we have a
different field name. Nevertheless, we fix the issue here by checking
that our random choice gives us a non-equal document field, and assert
that if we got unlucky the other one will work for us.
  • Loading branch information
jasontedor committed Aug 15, 2017
1 parent 9bd5b83 commit d96cdfc
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ private static DocumentField mutateDocumentField(DocumentField documentField) {
List<Supplier<DocumentField>> mutations = new ArrayList<>();
mutations.add(() -> new DocumentField(randomUnicodeOfCodepointLength(15), documentField.getValues()));
mutations.add(() -> new DocumentField(documentField.getName(), randomDocumentField(XContentType.JSON).v1().getValues()));
return randomFrom(mutations).get();
final int index = randomFrom(0, 1);
final DocumentField randomCandidate = mutations.get(index).get();
if (!documentField.equals(randomCandidate)) {
return randomCandidate;
} else {
// we are unlucky and our random mutation is equal to our mutation, try the other candidate
final DocumentField otherCandidate = mutations.get(1 - index).get();
assert !documentField.equals(otherCandidate) : documentField;
return otherCandidate;
}
}

public static Tuple<DocumentField, DocumentField> randomDocumentField(XContentType xContentType) {
Expand Down

0 comments on commit d96cdfc

Please sign in to comment.