From 56dbea68e9943cb37781407d750f2eac560ce4c8 Mon Sep 17 00:00:00 2001 From: Michael Nilsson Date: Mon, 29 Aug 2016 13:51:45 -0400 Subject: [PATCH] Updated LTRScoringAlgorithm tests to better reflect what they are doing (#64) --- .../ltr/feature/TestLTRScoringAlgorithm.java | 233 ++++++++---------- 1 file changed, 105 insertions(+), 128 deletions(-) diff --git a/solr/contrib/ltr/src/test/org/apache/solr/ltr/feature/TestLTRScoringAlgorithm.java b/solr/contrib/ltr/src/test/org/apache/solr/ltr/feature/TestLTRScoringAlgorithm.java index 72ba5cd7859e..eb3828fac866 100644 --- a/solr/contrib/ltr/src/test/org/apache/solr/ltr/feature/TestLTRScoringAlgorithm.java +++ b/solr/contrib/ltr/src/test/org/apache/solr/ltr/feature/TestLTRScoringAlgorithm.java @@ -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; @@ -48,7 +47,7 @@ public static void setup() throws Exception { } @Test - public void getInstanceTest() throws FeatureException, ModelException { + public void getInstanceTest() { final Map weights = new HashMap<>(); weights.put("constant1", 1d); weights.put("constant5", 1d); @@ -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 features = getFeatures(new String[] { - "constant1", "constant5"}); - final List norms = - new ArrayList( - 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 features = getFeatures(new String[] { - "constant1", "constant5"}); - final List norms = - new ArrayList( - 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 weights = new HashMap<>(); - weights.put("constant1", 1d); - weights.put("constant5", 1d); - - Map params = new HashMap(); - params.put("weights", weights); - final List features = getFeatures(new String[] { - "constant1", "constant5"}); - final List norms = - new ArrayList( - 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 weights = new HashMap<>(); - weights.put("constant1", 1d); - weights.put("constant5", 1d); - - Map params = new HashMap(); - params.put("weights", weights); - final List features = getFeatures(new String[] { - "constant1", "constant1"}); - final List norms = + @Test + public void nullFeatureWeightsTest() { + final ModelException expectedException = + new ModelException("Model test2 doesn't contain any weights"); + try { + final List features = getFeatures(new String[] + {"constant1", "constant5"}); + final List norms = new ArrayList( 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 weights = new HashMap<>(); - weights.put("constant1", 1d); - weights.put("constant5missing", 1d); - - Map params = new HashMap(); - params.put("weights", weights); - final List features = getFeatures(new String[] { - "constant1", "constant1"}); - final List norms = + @Test + public void existingNameTest() { + final ModelException expectedException = + new ModelException("model 'test3' already exists. Please use a different name"); + try { + final List features = getFeatures(new String[] + {"constant1", "constant5"}); + final List norms = new ArrayList( Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE)); - final LTRScoringAlgorithm meta = new RankSVMModel("test5", - features, norms, "test", fstore.getFeatures(), - params); - store.addMetadataModel(meta); - + final Map weights = new HashMap<>(); + weights.put("constant1", 1d); + weights.put("constant5", 1d); + + Map params = new HashMap(); + 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 weights = new HashMap<>(); - weights.put("constant1", 1d); - weights.put("constant5missing", 1d); - - Map params = new HashMap(); - params.put("weights", weights); - final List features = getFeatures(new String[] { - "constant1", "constant5"}); - final List norms = + @Test + public void duplicateFeatureTest() { + final ModelException expectedException = + new ModelException("duplicated feature constant1 in model test4"); + try { + final List features = getFeatures(new String[] + {"constant1", "constant1"}); + final List norms = new ArrayList( Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE)); - final LTRScoringAlgorithm meta = new RankSVMModel("test6", - features, norms, "test", fstore.getFeatures(), - params); - store.addMetadataModel(meta); + final Map weights = new HashMap<>(); + weights.put("constant1", 1d); + weights.put("constant5", 1d); + + Map params = new HashMap(); + 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 weights = new HashMap<>(); - weights.put("constant1", 1d); - weights.put("constant5missing", 1d); - - Map params = new HashMap(); - params.put("weights", weights); - final List features = getFeatures(new String[] { - "constant1", "constant5"}); - final List norms = + @Test + public void missingFeatureWeightTest() { + final ModelException expectedException = + new ModelException("no weight for feature constant5"); + try { + final List features = getFeatures(new String[] + {"constant1", "constant5"}); + final List norms = new ArrayList( Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE)); - final LTRScoringAlgorithm meta = new RankSVMModel("test7", - features, norms, "test", fstore.getFeatures(), - params); - store.addMetadataModel(meta); - + final Map weights = new HashMap<>(); + weights.put("constant1", 1d); + weights.put("constant5missing", 1d); + + Map params = new HashMap(); + 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 weights = new HashMap<>(); - weights.put("constant1", 1d); - weights.put("constant5missing", 1d); - - Map params = new HashMap(); - params.put("weights", weights); - final List features = getFeatures(new String[] {}); - final List norms = + @Test + public void emptyFeaturesTest() { + final ModelException expectedException = + new ModelException("no features declared for model test6"); + try { + final List features = getFeatures(new String[] {}); + final List norms = new ArrayList( Collections.nCopies(features.size(),IdentityNormalizer.INSTANCE)); - final LTRScoringAlgorithm meta = new RankSVMModel("test8", - features, norms, "test", fstore.getFeatures(), - params); - store.addMetadataModel(meta); + final Map weights = new HashMap<>(); + weights.put("constant1", 1d); + weights.put("constant5missing", 1d); + + Map params = new HashMap(); + 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()); + } } -} +} \ No newline at end of file