Skip to content

Commit

Permalink
SOLR-15440: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomglk committed Sep 7, 2023
1 parent a9577b2 commit ae6d160
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -526,34 +526,45 @@ public void testThatCorrectFieldValueFeatureIsUsedForDocValueTypes() throws Exce
final String field = fieldAndScorerClass[0];
final String fieldValue = fieldAndScorerClass[1];
final String fstore = "testThatCorrectFieldValueFeatureIsUsedForDocValueTypes" + field;
final String modelName = field + "-model";

assertU(adoc("id", "21", field, fieldValue));
assertU(commit());

loadFeature(
field, getObservingFieldValueFeatureClassName(), fstore, "{\"field\":\"" + field + "\"}");
loadModel(
field + "-model",
LinearModel.class.getName(),
new String[] {field},
fstore,
"{\"weights\":{\"" + field + "\":1.0}}");
loadFeatureAndModel(getObservingFieldValueFeatureClassName(), field, fstore, modelName);

final SolrQuery query = new SolrQuery("id:21");
query.add("rq", "{!ltr model=" + field + "-model reRankDocs=4}");
query.add("fl", "[fv]");
query(field, modelName);

ObservingFieldValueFeature.usedScorerClass = null; // to clear away any previous test's use
assertJQ("/query" + query.toQueryString(), "/response/numFound/==1");
assertJQ(
"/query" + query.toQueryString(),
"/response/docs/[0]/=={'[fv]':'"
+ FeatureLoggerTestUtils.toFeatureVector(field, "1.0")
+ "'}");
assertEquals(fieldAndScorerClass[2], ObservingFieldValueFeature.usedScorerClass);
}
}

void loadFeatureAndModel(String featureClassName, String field, String fstore, String modelName) throws Exception {
loadFeature(
field, featureClassName, fstore, "{\"field\":\"" + field + "\"}");

loadModel(
modelName,
LinearModel.class.getName(),
new String[]{field},
fstore,
"{\"weights\":{\"" + field + "\":1.0}}");
}

void query(String field, String modelName) throws Exception {
final SolrQuery query = new SolrQuery("id:21");
query.add("rq", "{!ltr model=" + modelName + " reRankDocs=4}");
query.add("fl", "[fv]");

ObservingFieldValueFeature.usedScorerClass = null; // to clear away any previous test's use
assertJQ("/query" + query.toQueryString(), "/response/numFound/==1");
assertJQ(
"/query" + query.toQueryString(),
"/response/docs/[0]/=={'[fv]':'"
+ FeatureLoggerTestUtils.toFeatureVector(field, "1.0")
+ "'}");
}

@Test
public void testParamsToMap() throws Exception {
final LinkedHashMap<String, Object> params = new LinkedHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,39 @@ protected String storedDvIsTrendy_FieldValueFeatureScorer_className() {
}

@Test
public void test_storedDvIsTrendy_FieldValueFeatureScorer_className_differs() throws Exception {
final String className = super.storedDvIsTrendy_FieldValueFeatureScorer_className();
final String legacyClassName = storedDvIsTrendy_FieldValueFeatureScorer_className();
public void test_LegacyFieldValueFeature_behavesDifferentlyThan_FieldValueFeature() throws Exception {
// the field storedDvIsTrendy has stored=true and docValues=true
final String field = "storedDvIsTrendy";
final String fieldValue = "1";
String fstore = "test_LegacyFieldValueFeature_behavesDifferentlyThan_FieldValueFeature" + field;

assertU(adoc("id", "21", field, fieldValue));
assertU(commit());
// demonstrate that & how the FieldValueFeature & LegacyFieldValueFeature implementations differ
assertNotEquals(className, legacyClassName);
assertEquals(className.replace("SortedDocValues", ""), legacyClassName);

// the LegacyFieldValueFeature does not use docValues
loadAndQuery(getObservingFieldValueFeatureClassName(), field, fstore);

String legacyScorerClass = ObservingFieldValueFeature.usedScorerClass;
assertEquals(
FieldValueFeature.FieldValueFeatureWeight.FieldValueFeatureScorer.class.getName(),
legacyScorerClass);

// the FieldValueFeature does use docValues
loadAndQuery(super.getObservingFieldValueFeatureClassName(), field, fstore);

String scorerClass = ObservingFieldValueFeature.usedScorerClass;
assertEquals(
FieldValueFeature.FieldValueFeatureWeight.SortedDocValuesFieldValueFeatureScorer.class.getName(),
scorerClass);
}

void loadAndQuery(String featureClassName, String field, String fstore) throws Exception {
final String modelName = field + "-model-" + featureClassName;
final String featureStoreName = fstore + featureClassName;

loadFeatureAndModel(featureClassName, field, featureStoreName, modelName);

query(field, modelName);
}
}

0 comments on commit ae6d160

Please sign in to comment.