lucene-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Yonik Seeley <>
Subject solr test development
Date Fri, 15 Oct 2010 13:48:44 GMT
Maintaining and fixing tests in solr has been a pain - I think mostly
because in the past we probably reviewed core code well, but perhaps
not the test code.  For test code, we should really strive for
conciseness and maintainability.  Most things should be tested through
higher level requests, and not by poking around in the guts (which
makes it very hard to change how something is implemented).

I've been rewriting some broken tests... for example replacing this:

  public void testBasics() throws Exception {
    SolrCore core = h.getCore();
    SearchComponent tvComp = core.getSearchComponent("tvComponent");
    assertTrue("tvComp is null and it shouldn't be", tvComp != null);
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add(CommonParams.Q, "id:0");
    params.add(CommonParams.QT, "tvrh");
    params.add(TermVectorParams.TF, "true");
    params.add(TermVectorComponent.COMPONENT_NAME, "true");
    SolrRequestHandler handler = core.getRequestHandler("tvrh");
    SolrQueryResponse rsp;
    rsp = new SolrQueryResponse();
    rsp.add("responseHeader", new SimpleOrderedMap());
    handler.handleRequest(new LocalSolrQueryRequest(core, params), rsp);
    NamedList values = rsp.getValues();
    NamedList termVectors = (NamedList)
    assertTrue("termVectors is null and it shouldn't be", termVectors != null);
    if (VERBOSE) System.out.println("TVs:" + termVectors);
    NamedList doc = (NamedList) termVectors.getVal(0);
    assertTrue("doc is null and it shouldn't be", doc != null);
    assertEquals(doc.size(), 5);
    NamedList field = (NamedList) doc.get("test_posofftv");
    assertTrue("field is null and it shouldn't be", field != null);
    assertTrue(field.size() + " does not equal: " + 2, field.size() == 2);
    NamedList titl = (NamedList) field.get("titl");
    assertTrue("titl is null and it shouldn't be", titl != null);
    assertTrue(titl.get("tf") + " does not equal: " + 2, ((Integer)
titl.get("tf")) == 2);
    //there should not be any positions or offsets
    NamedList positions = (NamedList) titl.get("positions");
    assertTrue("positions is not null and it should be", positions == null);
    NamedList offsets = (NamedList) titl.get("offsets");
    assertTrue("offsets is not null and it should be", offsets == null);
    String uniqueKeyFieldName = (String) termVectors.getVal(1);
    assertTrue("uniqueKeyFieldName is null and it shouldn't be",
uniqueKeyFieldName != null);
    assertTrue(uniqueKeyFieldName + " is not equal to " + "id",
uniqueKeyFieldName.equals("id") == true);

With this:

  public void testBasics() throws Exception {
    assertJQ(req("","map", "qt",tv, "q", "id:0",
TermVectorComponent.COMPONENT_NAME, "true", TermVectorParams.TF,
       ,"/termVectors=={'doc-0':{'uniqueKey':'0'," +
            " 'test_basictv':{'anoth':{'tf':1},'titl':{'tf':2}}," +
            " 'test_offtv':{'anoth':{'tf':1},'titl':{'tf':2}}," +
            " 'test_posofftv':{'anoth':{'tf':1},'titl':{'tf':2}}," +
            " 'test_postv':{'anoth':{'tf':1},'titl':{'tf':2}}}," +
            " 'uniqueKeyFieldName':'id'}"

I think it's clear which is more easily understandable and
maintainable, and doesn't leak resources.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message