From 13343c5c868c8cf980bbd5f853476492555f1e9f Mon Sep 17 00:00:00 2001 From: Itamar Syn-Hershko Date: Sun, 6 May 2012 22:18:49 +0300 Subject: [PATCH] Porting tests --- .../Spatial/Contrib.Spatial.Test.csproj | 7 + test/contrib/Spatial/SpatialMatchConcern.cs | 35 ++++ test/contrib/Spatial/SpatialTestQuery.cs | 110 +++++++++++ test/contrib/Spatial/StrategyTestCase.cs | 171 ++++++++++++++++++ test/contrib/Spatial/TestTestFramework.cs | 38 ++-- 5 files changed, 342 insertions(+), 19 deletions(-) create mode 100644 test/contrib/Spatial/SpatialMatchConcern.cs create mode 100644 test/contrib/Spatial/SpatialTestQuery.cs create mode 100644 test/contrib/Spatial/StrategyTestCase.cs diff --git a/test/contrib/Spatial/Contrib.Spatial.Test.csproj b/test/contrib/Spatial/Contrib.Spatial.Test.csproj index 628074f262..419d4b7573 100644 --- a/test/contrib/Spatial/Contrib.Spatial.Test.csproj +++ b/test/contrib/Spatial/Contrib.Spatial.Test.csproj @@ -84,7 +84,10 @@ LuceneTestCase.cs + + + @@ -110,6 +113,10 @@ + + {777904AC-46EF-466C-9E13-2A52A6735987} + Spatial4n.Core + {35C347F4-24B2-4BE5-8117-A0E3001551CE} Contrib.Spatial diff --git a/test/contrib/Spatial/SpatialMatchConcern.cs b/test/contrib/Spatial/SpatialMatchConcern.cs new file mode 100644 index 0000000000..af4c5713d5 --- /dev/null +++ b/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); + } +} diff --git a/test/contrib/Spatial/SpatialTestQuery.cs b/test/contrib/Spatial/SpatialTestQuery.cs new file mode 100644 index 0000000000..fdf7cac2a0 --- /dev/null +++ b/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 +{ + /// + /// Helper class to execute queries + /// + public class SpatialTestQuery + { + public String testname; + public String line; + public int lineNumber = -1; + public SpatialArgs args; + public List ids = new List(); + + public class SpatialTestQueryLineReader : LineReader + { + 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); + } + } + } + + /// + /// Get Test Queries + /// + /// + /// + /// + /// + /// + public static IEnumerator getTestQueries( + SpatialArgsParser parser, + SpatialContext ctx, + String name, + Stream @in) + { + return new SpatialTestQueryLineReader(new StreamReader(@in, Encoding.UTF8), parser, ctx); + + } + } +} diff --git a/test/contrib/Spatial/StrategyTestCase.cs b/test/contrib/Spatial/StrategyTestCase.cs new file mode 100644 index 0000000000..9829277532 --- /dev/null +++ b/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 : 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 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 testQueryIterator = getTestQueries(path, ctx); + runTestQueries(testQueryIterator, concern); + } + } + + protected void getAddAndVerifyIndexedDocuments(String testDataFile) + { + List testDocuments = getDocuments(testDataFile); + addDocumentsAndCommit(testDocuments); + verifyDocumentsIndexed(testDocuments.Count); + } + + protected List getDocuments(String testDataFile) + { + IEnumerator sampleData = getSampleData(testDataFile); + var documents = new List(); + 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 getSampleData(String testDataFile) + { + var stream = File.OpenRead(Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, @"\test-files\data", testDataFile)); + return new SampleDataReader(stream); + } + + protected IEnumerator 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 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(); + 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(); + 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()); + } + } + } + } + + } +} diff --git a/test/contrib/Spatial/TestTestFramework.cs b/test/contrib/Spatial/TestTestFramework.cs index 6dc7349456..e940f1cfcd 100644 --- a/test/contrib/Spatial/TestTestFramework.cs +++ b/test/contrib/Spatial/TestTestFramework.cs @@ -27,26 +27,26 @@ namespace Lucene.Net.Contrib.Spatial.Test /// 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 iter = SpatialTestQuery.getTestQueries( - new SpatialArgsParser(), ctx, name, in ); - List tests = new ArrayList(); - while( iter.hasNext() ) { - tests.add( iter.next() ); - } - Assert.assertEquals( 3, tests.size() ); + // InputStream @in = getClass().getClassLoader().getResourceAsStream(name); + // SpatialContext ctx = SimpleSpatialContext.GEO_KM; + // Iterator iter = SpatialTestQuery.getTestQueries( + // new SpatialArgsParser(), ctx, name, in ); + // List tests = new ArrayList(); + // 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() ); + //} } }