Skip to content

Commit

Permalink
Porting tests
Browse files Browse the repository at this point in the history
  • Loading branch information
synhershko committed May 6, 2012
1 parent f4ef219 commit 13343c5
Show file tree
Hide file tree
Showing 5 changed files with 342 additions and 19 deletions.
7 changes: 7 additions & 0 deletions test/contrib/Spatial/Contrib.Spatial.Test.csproj
Expand Up @@ -84,7 +84,10 @@
<Link>LuceneTestCase.cs</Link>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SpatialMatchConcern.cs" />
<Compile Include="SpatialTestCase.cs" />
<Compile Include="SpatialTestQuery.cs" />
<Compile Include="StrategyTestCase.cs" />
<Compile Include="TestTestFramework.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -110,6 +113,10 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\Projects\spatial4n\Spatial4n.Core\Spatial4n.Core.csproj">
<Project>{777904AC-46EF-466C-9E13-2A52A6735987}</Project>
<Name>Spatial4n.Core</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\src\contrib\Spatial\Contrib.Spatial.csproj">
<Project>{35C347F4-24B2-4BE5-8117-A0E3001551CE}</Project>
<Name>Contrib.Spatial</Name>
Expand Down
35 changes: 35 additions & 0 deletions test/contrib/Spatial/SpatialMatchConcern.cs
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Lucene.Net.Contrib.Spatial.Test
{
public class SpatialMatchConcern
{
public readonly bool orderIsImportant;
public readonly bool resultsAreSuperset; // if the strategy can not give exact answers, but used to limit results

private SpatialMatchConcern(bool order, bool superset)
{
this.orderIsImportant = order;
this.resultsAreSuperset = superset;
}

public static SpatialMatchConcern EXACT = new SpatialMatchConcern(true, false);
public static SpatialMatchConcern FILTER = new SpatialMatchConcern(false, false);
public static SpatialMatchConcern SUPERSET = new SpatialMatchConcern(false, true);
}
}
110 changes: 110 additions & 0 deletions test/contrib/Spatial/SpatialTestQuery.cs
@@ -0,0 +1,110 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Spatial4n.Core.Context;
using Spatial4n.Core.Io;
using Spatial4n.Core.Query;

namespace Lucene.Net.Contrib.Spatial.Test
{
/// <summary>
/// Helper class to execute queries
/// </summary>
public class SpatialTestQuery
{
public String testname;
public String line;
public int lineNumber = -1;
public SpatialArgs args;
public List<String> ids = new List<String>();

public class SpatialTestQueryLineReader : LineReader<SpatialTestQuery>
{
private readonly SpatialArgsParser parser;
private readonly SpatialContext ctx;

public SpatialTestQueryLineReader(Stream @in, SpatialArgsParser parser, SpatialContext ctx)
: base(@in)
{
this.parser = parser;
this.ctx = ctx;
}

public SpatialTestQueryLineReader(StreamReader r, SpatialArgsParser parser, SpatialContext ctx)
: base(r)
{
this.parser = parser;
this.ctx = ctx;
}

public override SpatialTestQuery ParseLine(string line)
{
var test = new SpatialTestQuery {line = line, lineNumber = GetLineNumber()};

try
{
// skip a comment
if (line.StartsWith("["))
{
int idx0 = line.IndexOf(']');
if (idx0 > 0)
{
line = line.Substring(idx0 + 1);
}
}

int idx = line.IndexOf('@');

var pos = 0;
var st = line.Substring(0, idx).Split();
while (pos< st.Length)
{
test.ids.Add(st[pos++].Trim());
}
test.args = parser.Parse(line.Substring(idx + 1).Trim(), ctx);
return test;
}
catch (Exception ex)
{
throw new Exception("invalid query line: " + test.line, ex);
}
}
}

/// <summary>
/// Get Test Queries
/// </summary>
/// <param name="parser"></param>
/// <param name="ctx"></param>
/// <param name="name"></param>
/// <param name="in"></param>
/// <returns></returns>
public static IEnumerator<SpatialTestQuery> getTestQueries(
SpatialArgsParser parser,
SpatialContext ctx,
String name,
Stream @in)
{
return new SpatialTestQueryLineReader(new StreamReader(@in, Encoding.UTF8), parser, ctx);

}
}
}
171 changes: 171 additions & 0 deletions test/contrib/Spatial/StrategyTestCase.cs
@@ -0,0 +1,171 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.IO;
using Lucene.Net.Documents;
using Lucene.Net.Spatial;
using NUnit.Framework;
using Spatial4n.Core.Context;
using Spatial4n.Core.Io.Samples;
using Spatial4n.Core.Query;
using Spatial4n.Core.Shapes;

namespace Lucene.Net.Contrib.Spatial.Test
{
public abstract class StrategyTestCase<T> : SpatialTestCase where T : SpatialTestCase, SpatialFieldInfo
{
public static readonly String DATA_STATES_POLY = "states-poly.txt";
public static readonly String DATA_STATES_BBOX = "states-bbox.txt";
public static readonly String DATA_COUNTRIES_POLY = "countries-poly.txt";
public static readonly String DATA_COUNTRIES_BBOX = "countries-bbox.txt";
public static readonly String DATA_WORLD_CITIES_POINTS = "world-cities-points.txt";

public static readonly String QTEST_States_IsWithin_BBox = "states-IsWithin-BBox.txt";
public static readonly String QTEST_States_Intersects_BBox = "states-Intersects-BBox.txt";

public static readonly String QTEST_Cities_IsWithin_BBox = "cities-IsWithin-BBox.txt";

//private Logger log = Logger.getLogger(getClass().getName());

protected readonly SpatialArgsParser argsParser = new SpatialArgsParser();

protected SpatialStrategy<T> strategy;
protected SpatialContext ctx;
protected T fieldInfo;
protected bool storeShape = true;

protected void executeQueries(SpatialMatchConcern concern, params String[] testQueryFile)
{
Console.WriteLine("testing queried for strategy " + strategy);
foreach (String path in testQueryFile)
{
IEnumerator<SpatialTestQuery> testQueryIterator = getTestQueries(path, ctx);
runTestQueries(testQueryIterator, concern);
}
}

protected void getAddAndVerifyIndexedDocuments(String testDataFile)
{
List<Document> testDocuments = getDocuments(testDataFile);
addDocumentsAndCommit(testDocuments);
verifyDocumentsIndexed(testDocuments.Count);
}

protected List<Document> getDocuments(String testDataFile)
{
IEnumerator<SampleData> sampleData = getSampleData(testDataFile);
var documents = new List<Document>();
while (sampleData.MoveNext())
{
SampleData data = sampleData.Current;
var document = new Document();
document.Add(new Field("id", data.id, Field.Store.YES, Field.Index.ANALYZED));
document.Add(new Field("name", data.name, Field.Store.YES, Field.Index.ANALYZED));
Shape shape = ctx.ReadShape(data.shape);
foreach (var f in strategy.CreateFields(fieldInfo, shape, true, storeShape))
{
if (f != null)
{ // null if incompatibleGeometry && ignore
document.Add(f);
}
}
documents.Add(document);
}
return documents;
}

[Test]
public void testWWW()
{
Console.WriteLine(Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, @"\test-files\data", "file"));
}

protected IEnumerator<SampleData> getSampleData(String testDataFile)
{
var stream = File.OpenRead(Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, @"\test-files\data", testDataFile));
return new SampleDataReader(stream);
}

protected IEnumerator<SpatialTestQuery> getTestQueries(String testQueryFile, SpatialContext ctx)
{
var @in = File.OpenRead(Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, @"\test-files\", testQueryFile));
return SpatialTestQuery.getTestQueries(argsParser, ctx, testQueryFile, @in);
}

public void runTestQueries(
IEnumerator<SpatialTestQuery> queries,
SpatialMatchConcern concern)
{
while (queries.MoveNext())
{
SpatialTestQuery q = queries.Current;

String msg = q.line; //"Query: " + q.args.toString(ctx);
SearchResults got = executeQuery(strategy.MakeQuery(q.args, fieldInfo), 100);
if (concern.orderIsImportant)
{
var ids = q.ids.GetEnumerator();
foreach (var r in got.results)
{
String id = r.document.Get("id");
ids.MoveNext();
Assert.AreEqual("out of order: " + msg, ids.Current, id);
}
if (ids.MoveNext())
{
Assert.Fail(msg + " :: expect more results then we got: " + ids.Current);
}
}
else
{
// We are looking at how the results overlap
if (concern.resultsAreSuperset)
{
var found = new HashSet<String>();
foreach (var r in got.results)
{
found.Add(r.document.Get("id"));
}
foreach (String s in q.ids)
{
if (!found.Contains(s))
{
Assert.Fail("Results are mising id: " + s + " :: " + found);
}
}
}
else
{
var found = new List<String>();
foreach (SearchResult r in got.results)
{
found.Add(r.document.Get("id"));
}

// sort both so that the order is not important
q.ids.Sort();
found.Sort();
Assert.AreEqual(msg, q.ids.ToString(), found.ToString());
}
}
}
}

}
}
38 changes: 19 additions & 19 deletions test/contrib/Spatial/TestTestFramework.cs
Expand Up @@ -27,26 +27,26 @@ namespace Lucene.Net.Contrib.Spatial.Test
/// </summary>
public class TestTestFramework : LuceneTestCase
{
public void testQueries()
{
String name = StrategyTestCase.QTEST_Cities_IsWithin_BBox;
// public void testQueries()
// {
// String name = StrategyTestCase.QTEST_Cities_IsWithin_BBox;

InputStream @in = getClass().getClassLoader().getResourceAsStream(name);
SpatialContext ctx = SimpleSpatialContext.GEO_KM;
Iterator<SpatialTestQuery> iter = SpatialTestQuery.getTestQueries(
new SpatialArgsParser(), ctx, name, in );
List<SpatialTestQuery> tests = new ArrayList<SpatialTestQuery>();
while( iter.hasNext() ) {
tests.add( iter.next() );
}
Assert.assertEquals( 3, tests.size() );
// InputStream @in = getClass().getClassLoader().getResourceAsStream(name);
// SpatialContext ctx = SimpleSpatialContext.GEO_KM;
// Iterator<SpatialTestQuery> iter = SpatialTestQuery.getTestQueries(
// new SpatialArgsParser(), ctx, name, in );
// List<SpatialTestQuery> tests = new ArrayList<SpatialTestQuery>();
// while( iter.hasNext() ) {
// tests.add( iter.next() );
// }
// Assert.assertEquals( 3, tests.size() );

SpatialTestQuery sf = tests.get(0);
// assert
Assert.assertEquals( 1, sf.ids.size() );
Assert.assertTrue( sf.ids.get(0).equals( "G5391959" ) );
Assert.assertTrue( sf.args.getShape() instanceof Rectangle);
Assert.assertEquals( SpatialOperation.IsWithin, sf.args.getOperation() );
}
// SpatialTestQuery sf = tests.get(0);
// // assert
// Assert.assertEquals( 1, sf.ids.size() );
// Assert.assertTrue( sf.ids.get(0).equals( "G5391959" ) );
// Assert.assertTrue( sf.args.getShape() instanceof Rectangle);
// Assert.assertEquals( SpatialOperation.IsWithin, sf.args.getOperation() );
//}
}
}

0 comments on commit 13343c5

Please sign in to comment.