Skip to content

Commit

Permalink
Updated LTRScoringAlgorithm tests to better reflect what they are doi…
Browse files Browse the repository at this point in the history
…ng (apache#64)
  • Loading branch information
Michael Nilsson authored Aug 29, 2016
1 parent 500b7f4 commit 56dbea6
Showing 1 changed file with 105 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.solr.ltr.ranking.Feature;
import org.apache.solr.ltr.ranking.RankSVMModel;
import org.apache.solr.ltr.rest.ManagedModelStore;
import org.apache.solr.ltr.util.FeatureException;
import org.apache.solr.ltr.util.ModelException;
import org.junit.BeforeClass;
import org.junit.Test;
Expand All @@ -48,7 +47,7 @@ public static void setup() throws Exception {
}

@Test
public void getInstanceTest() throws FeatureException, ModelException {
public void getInstanceTest() {
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5", 1d);
Expand All @@ -69,149 +68,127 @@ public void getInstanceTest() throws FeatureException, ModelException {
assertEquals(meta, m);
}

@Test(expected = ModelException.class)
public void getInvalidTypeTest() throws ModelException, FeatureException {
final List<Feature> features = getFeatures(new String[] {
"constant1", "constant5"});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("test2",
features, norms, "test", fstore.getFeatures(), null);
store.addMetadataModel(meta);
final LTRScoringAlgorithm m = store.getModel("test38290156821076");
}

@Test(expected = ModelException.class)
public void getInvalidNameTest() throws ModelException, FeatureException {
final List<Feature> features = getFeatures(new String[] {
"constant1", "constant5"});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("!!!??????????",
features, norms, "test", fstore.getFeatures(), null);
store.addMetadataModel(meta);
store.getModel("!!!??????????");
}

@Test(expected = ModelException.class)
public void existingNameTest() throws ModelException, FeatureException {
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final List<Feature> features = getFeatures(new String[] {
"constant1", "constant5"});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("test3",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);
final LTRScoringAlgorithm m = store.getModel("test3");
assertEquals(meta, m);
store.addMetadataModel(meta);
}

@Test(expected = ModelException.class)
public void duplicateFeatureTest() throws ModelException, FeatureException {
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final List<Feature> features = getFeatures(new String[] {
"constant1", "constant1"});
final List<Normalizer> norms =
@Test
public void nullFeatureWeightsTest() {
final ModelException expectedException =
new ModelException("Model test2 doesn't contain any weights");
try {
final List<Feature> features = getFeatures(new String[]
{"constant1", "constant5"});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("test4",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);

final LTRScoringAlgorithm meta = new RankSVMModel("test2",
features, norms, "test", fstore.getFeatures(), null);
fail("unexpectedly got here instead of catching "+expectedException);
} catch (ModelException actualException) {
assertEquals(expectedException.toString(), actualException.toString());
}
}

@Test(expected = ModelException.class)
public void missingFeatureTest() throws ModelException, FeatureException {
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5missing", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final List<Feature> features = getFeatures(new String[] {
"constant1", "constant1"});
final List<Normalizer> norms =
@Test
public void existingNameTest() {
final ModelException expectedException =
new ModelException("model 'test3' already exists. Please use a different name");
try {
final List<Feature> features = getFeatures(new String[]
{"constant1", "constant5"});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("test5",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);

final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final LTRScoringAlgorithm meta = new RankSVMModel("test3",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);
final LTRScoringAlgorithm m = store.getModel("test3");
assertEquals(meta, m);
store.addMetadataModel(meta);
fail("unexpectedly got here instead of catching "+expectedException);
} catch (ModelException actualException) {
assertEquals(expectedException.toString(), actualException.toString());
}
}

@Test(expected = ModelException.class)
public void notExistingClassTest() throws ModelException, FeatureException {
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5missing", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final List<Feature> features = getFeatures(new String[] {
"constant1", "constant5"});
final List<Normalizer> norms =
@Test
public void duplicateFeatureTest() {
final ModelException expectedException =
new ModelException("duplicated feature constant1 in model test4");
try {
final List<Feature> features = getFeatures(new String[]
{"constant1", "constant1"});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("test6",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final LTRScoringAlgorithm meta = new RankSVMModel("test4",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);
fail("unexpectedly got here instead of catching "+expectedException);
} catch (ModelException actualException) {
assertEquals(expectedException.toString(), actualException.toString());
}

}

@Test(expected = ModelException.class)
public void badModelClassTest() throws ModelException, FeatureException {
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5missing", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final List<Feature> features = getFeatures(new String[] {
"constant1", "constant5"});
final List<Normalizer> norms =
@Test
public void missingFeatureWeightTest() {
final ModelException expectedException =
new ModelException("no weight for feature constant5");
try {
final List<Feature> features = getFeatures(new String[]
{"constant1", "constant5"});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("test7",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);

final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5missing", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final LTRScoringAlgorithm meta = new RankSVMModel("test5",
features, norms, "test", fstore.getFeatures(),
params);
fail("unexpectedly got here instead of catching "+expectedException);
} catch (ModelException actualException) {
assertEquals(expectedException.toString(), actualException.toString());
}
}

@Test(expected = ModelException.class)
public void misingFeaturesTest() throws ModelException, FeatureException {
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5missing", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final List<Feature> features = getFeatures(new String[] {});
final List<Normalizer> norms =
@Test
public void emptyFeaturesTest() {
final ModelException expectedException =
new ModelException("no features declared for model test6");
try {
final List<Feature> features = getFeatures(new String[] {});
final List<Normalizer> norms =
new ArrayList<Normalizer>(
Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE));
final LTRScoringAlgorithm meta = new RankSVMModel("test8",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);
final Map<String,Object> weights = new HashMap<>();
weights.put("constant1", 1d);
weights.put("constant5missing", 1d);

Map<String,Object> params = new HashMap<String,Object>();
params.put("weights", weights);
final LTRScoringAlgorithm meta = new RankSVMModel("test6",
features, norms, "test", fstore.getFeatures(),
params);
store.addMetadataModel(meta);
fail("unexpectedly got here instead of catching "+expectedException);
} catch (ModelException actualException) {
assertEquals(expectedException.toString(), actualException.toString());
}
}
}
}

0 comments on commit 56dbea6

Please sign in to comment.