SOLR-4514: better tests for CurrencyField using OpenExchangeRatesOrgProvider

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1451691 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2013-03-01 19:24:23 +00:00
parent 3a9683d7ac
commit 88931a58a0
7 changed files with 183 additions and 209 deletions

View File

@ -16,17 +16,20 @@
limitations under the License.
-->
<!-- Example exchange rates file. -->
<!-- Example exchange rates file,
NOTE: this data must be kept in sync with ./open-exchange-rates.json
-->
<currencyConfig version="1.0">
<rates>
<!-- Example -->
<rate from="USD" to="JPY" rate="81.29"/>
<!-- Fake rates for testing -->
<rate from="USD" to="JPY" rate="81.29"/>
<rate from="USD" to="EUR" rate="2.5"/>
<rate from="USD" to="GBP" rate="0.5"/>
<rate from="EUR" to="GBP" rate="0.5"/>
<rate from="MXN" to="USD" rate="0.5"/>
<!-- Asymmetric rate -->
<rate from="EUR" to="USD" rate="0.5"/>

View File

@ -1,167 +1,18 @@
{
"disclaimer": "This data is collected from various providers and provided free of charge for informational purposes only, with no guarantee whatsoever of accuracy, validity, availability or fitness for any purpose; use at your own risk. Other than that - have fun, and please share/watch/fork if you think data like this should be free!",
"license": "Data collected from various providers with public-facing APIs; copyright may apply; not for resale; no warranties given.",
"disclaimer": "This data is not real, it was synthetically created to match currency.xml. It is modeled after the data format available from openexchangerates.org. See https://openexchangerates.org/documentation for details",
"license": "http://www.apache.org/licenses/LICENSE-2.0",
"timestamp": 1332070464,
"IMPORTANT NOTE": "In order for tests to work, this data must be kept in sync with ./currency.xml",
"base": "USD",
"rates": {
"AED": 3.6732,
"AFN": 48.299999,
"ALL": 105.919998,
"AMD": 388.890015,
"ANG": 1.79,
"AOA": 94.769997,
"ARS": 4.35,
"AUD": 0.943931,
"AWG": 1.7899,
"AZN": 0.7863,
"BAM": 1.48775,
"BBD": 2,
"BDT": 82,
"BGN": 1.4962,
"BHD": 0.37703,
"BIF": 1304.170044,
"BMD": 1,
"BND": 1.2575,
"BOB": 6.91,
"BRL": 1.8003,
"BSD": 1,
"BTN": 50.185001,
"BWP": 7.2307,
"BYR": 8150,
"BZD": 1.9135,
"CAD": 0.9921,
"CDF": 917.276917,
"CHF": 0.9164,
"CLF": 0.02146,
"CLP": 482.75,
"CNY": 6.3239,
"COP": 1760,
"CRC": 507.600006,
"CUP": 1,
"CVE": 84.190002,
"CZK": 18.606001,
"DJF": 179.490005,
"DKK": 5.64424,
"DOP": 39.025002,
"DZD": 74.544998,
"EGP": 6.0385,
"ETB": 17.720449,
"EUR": 0.758956,
"FJD": 1.7734,
"FKP": 0.6316,
"GBP": 0.631373,
"GEL": 1.6469,
"GHS": 1.7455,
"GIP": 0.63165,
"GMD": 31.5,
"GNF": 7100,
"GTQ": 7.6975,
"GYD": 203.699997,
"HKD": 7.76306,
"HNL": 19.055,
"HRK": 5.7333,
"HTG": 41,
"HUF": 219.850006,
"IDR": 9118,
"IEP": 0.5978,
"ILS": 3.7542,
"INR": 50.165001,
"IQD": 1165.5,
"IRR": 12308,
"ISK": 127.440002,
"JMD": 86.699997,
"JOD": 0.7095,
"JPY": 83.445,
"KES": 83.18,
"KGS": 46.699402,
"KHR": 4010.300049,
"KMF": 373.424255,
"KPW": 900,
"KRW": 1125.849976,
"KWD": 0.27925,
"KZT": 147.690002,
"LAK": 7993.799805,
"LBP": 1504,
"LKR": 125.224998,
"LRD": 73.459999,
"LSL": 7.5768,
"LTL": 2.6219,
"LVL": 0.5291,
"LYD": 1.2572,
"MAD": 8.4611,
"MDL": 11.89,
"MGA": 2155,
"MKD": 46.705002,
"MMK": 6.51,
"MNT": 1322.5,
"MOP": 7.9958,
"MRO": 293,
"MUR": 29.110001,
"MVR": 15.36,
"MWK": 165.206207,
"MXN": 12.6745,
"MYR": 3.0575,
"MZN": 27.200001,
"NAD": 7.58,
"NGN": 157.600006,
"NIO": 23.215,
"NOK": 5.73163,
"NPR": 80.620003,
"NZD": 1.212269,
"OMR": 0.38485,
"PAB": 1,
"PEN": 2.674,
"PGK": 2.0627,
"PHP": 43.02,
"PKR": 90.800003,
"PLN": 3.1285,
"PYG": 4245,
"QAR": 3.6415,
"RON": 3.3256,
"RSD": 84.100502,
"RUB": 29.2342,
"RWF": 606.717468,
"SAR": 3.7505,
"SBD": 7.075973,
"SCR": 14.0447,
"SDG": 2.6765,
"SEK": 6.74525,
"SGD": 1.258,
"SHP": 0.63165,
"SLL": 4364.5,
"SOS": 1629,
"SRD": 3.2875,
"STD": 18650,
"SVC": 8.7475,
"SYP": 57.450001,
"SZL": 7.5752,
"THB": 30.700001,
"TJS": 4.7588,
"TMT": 2.85,
"TND": 1.5178,
"TOP": 1.693601,
"TRY": 1.796,
"TTD": 6.40015,
"TWD": 29.532,
"TZS": 1595,
"UAH": 8.029,
"UGX": 2481.699951,
"USD": 1,
"UYU": 19.469999,
"UZS": 1835.75,
"VEF": 4.295,
"VND": 20820,
"VUV": 90.199997,
"WST": 2.247475,
"XAF": 497.898987,
"XCD": 2.7,
"XDR": 0.652794,
"XOF": 498.399994,
"XPF": 90.639999,
"YER": 216.005005,
"ZAR": 7.5688,
"ZMK": 5271.5,
"ZWD": 378.700012,
"ZWL": 322.355011
"JPY": 81.29,
"EUR": 2.5,
"GBP": 0.5,
"MXN": 2.0
}
}

View File

@ -398,7 +398,7 @@
<!-- Currency type -->
<fieldType name="currency" class="solr.CurrencyField" currencyConfig="currency.xml" multiValued="false" />
<fieldType name="mock_currency" class="solr.CurrencyField" providerClass="solr.MockExchangeRateProvider" foo="bar" multiValued="false" />
<fieldType name="openexchangeratesorg_currency"
<fieldType name="oer_currency"
class="solr.CurrencyField"
multiValued="false"
providerClass="solr.OpenExchangeRatesOrgProvider"
@ -481,6 +481,7 @@
<!-- Test currency -->
<field name="amount" type="currency" indexed="true" stored="true" multiValued="false"/>
<field name="mock_amount" type="mock_currency" indexed="true" stored="true"/>
<field name="oer_amount" type="oer_currency" indexed="true" stored="true"/>
<!-- test different combinations of indexed and stored -->
<field name="bind" type="boolean" indexed="true" stored="false"/>

View File

@ -22,25 +22,51 @@ import org.apache.solr.core.SolrCore;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.Assume;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.Currency;
/**
* Tests currency field type.
* @see #field
*/
public class CurrencyFieldTest extends SolrTestCaseJ4 {
@Ignore("Abstract base class with test methods")
public abstract class AbstractCurrencyFieldTest extends SolrTestCaseJ4 {
/**
* "Assumes" that the specified list of currency codes are
* supported in this JVM
*/
public static void assumeCurrencySupport(String... codes) {
try {
// each JDK might have a diff list of supported currencies,
// these are the ones needed for this test to work.
for (String code : codes) {
Currency obj = Currency.getInstance(code);
assertNotNull(code, obj);
}
} catch (IllegalArgumentException e) {
Assume.assumeNoException(e);
}
}
@BeforeClass
public static void beforeClass() throws Exception {
assumeCurrencySupport("USD", "EUR", "MXN", "GBP", "JPY", "NOK");
initCore("solrconfig.xml", "schema.xml");
}
/** The field name to use in all tests */
public abstract String field();
@Test
public void testCurrencySchema() throws Exception {
IndexSchema schema = h.getCore().getSchema();
SchemaField amount = schema.getField("amount");
SchemaField amount = schema.getField(field());
assertNotNull(amount);
assertTrue(amount.isPolyField());
@ -66,9 +92,9 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
public void testCurrencyFieldType() throws Exception {
SolrCore core = h.getCore();
IndexSchema schema = core.getSchema();
SchemaField amount = schema.getField("amount");
SchemaField amount = schema.getField(field());
assertNotNull(amount);
assertTrue("amount is not a poly field", amount.isPolyField());
assertTrue(field() + " is not a poly field", amount.isPolyField());
FieldType tmp = amount.getType();
assertTrue(tmp instanceof CurrencyField);
String currencyValue = "1.50,EUR";
@ -88,7 +114,7 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
// A few tests on the provider directly
ExchangeRateProvider p = ((CurrencyField) tmp).getProvider();
Set<String> availableCurrencies = p.listAvailableCurrencies();
assert(availableCurrencies.size() == 4);
assertEquals(5, availableCurrencies.size());
assert(p.reload() == true);
assert(p.getExchangeRate("USD", "EUR") == 2.5);
}
@ -113,14 +139,14 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
final int emptyDocs = atLeast(50); // times 2
final int negDocs = atLeast(5);
assertU(adoc("id", "0", "amount", "0,USD")); // 0
assertU(adoc("id", "0", field(), "0,USD")); // 0
// lots of docs w/o values
for (int i = 100; i <= 100 + emptyDocs; i++) {
assertU(adoc("id", "" + i));
}
// docs with values in ranges we'll query
for (int i = 1; i <= 10; i++) {
assertU(adoc("id", "" + i, "amount", i + ",USD"));
assertU(adoc("id", "" + i, field(), i + ",USD"));
}
// more docs w/o values
for (int i = 500; i <= 500 + emptyDocs; i++) {
@ -128,78 +154,78 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
}
// some negative values
for (int i = -100; i > -100 - negDocs; i--) {
assertU(adoc("id", "" + i, "amount", i + ",USD"));
assertU(adoc("id", "" + i, field(), i + ",USD"));
}
assertU(adoc("id", "40", "amount", "0,USD")); // 0
assertU(adoc("id", "40", field(), "0,USD")); // 0
assertU(commit());
assertQ(req("fl", "*,score", "q",
"amount:[2.00,USD TO 5.00,USD]"),
field()+":[2.00,USD TO 5.00,USD]"),
"//*[@numFound='4']");
assertQ(req("fl", "*,score", "q",
"amount:[0.50,USD TO 1.00,USD]"),
field()+":[0.50,USD TO 1.00,USD]"),
"//*[@numFound='1']");
assertQ(req("fl", "*,score", "q",
"amount:[24.00,USD TO 25.00,USD]"),
field()+":[24.00,USD TO 25.00,USD]"),
"//*[@numFound='0']");
// "GBP" currency code is 1/2 of a USD dollar, for testing.
assertQ(req("fl", "*,score", "q",
"amount:[0.50,GBP TO 1.00,GBP]"),
field()+":[0.50,GBP TO 1.00,GBP]"),
"//*[@numFound='2']");
// "EUR" currency code is 2.5X of a USD dollar, for testing.
assertQ(req("fl", "*,score", "q",
"amount:[24.00,EUR TO 25.00,EUR]"),
field()+":[24.00,EUR TO 25.00,EUR]"),
"//*[@numFound='1']");
// Slight asymmetric rate should work.
assertQ(req("fl", "*,score", "q",
"amount:[24.99,EUR TO 25.01,EUR]"),
field()+":[24.99,EUR TO 25.01,EUR]"),
"//*[@numFound='1']");
// Open ended ranges without currency
assertQ(req("fl", "*,score", "q",
"amount:[* TO *]"),
field()+":[* TO *]"),
"//*[@numFound='" + (2 + 10 + negDocs) + "']");
// Open ended ranges with currency
assertQ(req("fl", "*,score", "q",
"amount:[*,EUR TO *,EUR]"),
field()+":[*,EUR TO *,EUR]"),
"//*[@numFound='" + (2 + 10 + negDocs) + "']");
// Open ended start range without currency
assertQ(req("fl", "*,score", "q",
"amount:[* TO 5,USD]"),
field()+":[* TO 5,USD]"),
"//*[@numFound='" + (2 + 5 + negDocs) + "']");
// Open ended start range with currency (currency for the * won't matter)
assertQ(req("fl", "*,score", "q",
"amount:[*,USD TO 5,USD]"),
field()+":[*,USD TO 5,USD]"),
"//*[@numFound='" + (2 + 5 + negDocs) + "']");
// Open ended end range
assertQ(req("fl", "*,score", "q",
"amount:[3 TO *]"),
field()+":[3 TO *]"),
"//*[@numFound='8']");
}
@Test
public void testCurrencyPointQuery() throws Exception {
clearIndex();
assertU(adoc("id", "" + 1, "amount", "10.00,USD"));
assertU(adoc("id", "" + 2, "amount", "15.00,EUR"));
assertU(adoc("id", "" + 1, field(), "10.00,USD"));
assertU(adoc("id", "" + 2, field(), "15.00,MXN"));
assertU(commit());
assertQ(req("fl", "*,score", "q", "amount:10.00,USD"), "//int[@name='id']='1'");
assertQ(req("fl", "*,score", "q", "amount:9.99,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", "amount:10.01,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", "amount:15.00,EUR"), "//int[@name='id']='2'");
assertQ(req("fl", "*,score", "q", "amount:7.50,USD"), "//int[@name='id']='2'");
assertQ(req("fl", "*,score", "q", "amount:7.49,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", "amount:7.51,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", field()+":10.00,USD"), "//int[@name='id']='1'");
assertQ(req("fl", "*,score", "q", field()+":9.99,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", field()+":10.01,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", field()+":15.00,MXN"), "//int[@name='id']='2'");
assertQ(req("fl", "*,score", "q", field()+":7.50,USD"), "//int[@name='id']='2'");
assertQ(req("fl", "*,score", "q", field()+":7.49,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", field()+":7.51,USD"), "//*[@numFound='0']");
}
@Ignore
@ -210,7 +236,7 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
int initDocs = 200000;
for (int i = 1; i <= initDocs; i++) {
assertU(adoc("id", "" + i, "amount", (r.nextInt(10) + 1.00) + ",USD"));
assertU(adoc("id", "" + i, field(), (r.nextInt(10) + 1.00) + ",USD"));
if (i % 1000 == 0)
System.out.println(i);
}
@ -218,15 +244,15 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
assertU(commit());
for (int i = 0; i < 1000; i++) {
double lower = r.nextInt(10) + 1.00;
assertQ(req("fl", "*,score", "q", "amount:[" + lower + ",USD TO " + (lower + 10.00) + ",USD]"), "//*");
assertQ(req("fl", "*,score", "q", "amount:[" + lower + ",EUR TO " + (lower + 10.00) + ",EUR]"), "//*");
assertQ(req("fl", "*,score", "q", field()+":[" + lower + ",USD TO " + (lower + 10.00) + ",USD]"), "//*");
assertQ(req("fl", "*,score", "q", field()+":[" + lower + ",EUR TO " + (lower + 10.00) + ",EUR]"), "//*");
}
for (int j = 0; j < 3; j++) {
long t1 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
double lower = r.nextInt(10) + 1.00;
assertQ(req("fl", "*,score", "q", "amount:[" + lower + ",USD TO " + (lower + (9.99 - (j * 0.01))) + ",USD]"), "//*");
assertQ(req("fl", "*,score", "q", field()+":[" + lower + ",USD TO " + (lower + (9.99 - (j * 0.01))) + ",USD]"), "//*");
}
System.out.println(System.currentTimeMillis() - t1);
@ -238,7 +264,7 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
long t1 = System.currentTimeMillis();
for (int i = 0; i < 1000; i++) {
double lower = r.nextInt(10) + 1.00;
assertQ(req("fl", "*,score", "q", "amount:[" + lower + ",EUR TO " + (lower + (9.99 - (j * 0.01))) + ",EUR]"), "//*");
assertQ(req("fl", "*,score", "q", field()+":[" + lower + ",EUR TO " + (lower + (9.99 - (j * 0.01))) + ",EUR]"), "//*");
}
System.out.println(System.currentTimeMillis() - t1);
@ -249,15 +275,15 @@ public class CurrencyFieldTest extends SolrTestCaseJ4 {
public void testCurrencySort() throws Exception {
clearIndex();
assertU(adoc("id", "" + 1, "amount", "10.00,USD"));
assertU(adoc("id", "" + 2, "amount", "15.00,EUR"));
assertU(adoc("id", "" + 3, "amount", "7.00,EUR"));
assertU(adoc("id", "" + 4, "amount", "6.00,GBP"));
assertU(adoc("id", "" + 5, "amount", "2.00,GBP"));
assertU(adoc("id", "" + 1, field(), "10.00,USD"));
assertU(adoc("id", "" + 2, field(), "15.00,EUR"));
assertU(adoc("id", "" + 3, field(), "7.00,EUR"));
assertU(adoc("id", "" + 4, field(), "6.00,GBP"));
assertU(adoc("id", "" + 5, field(), "2.00,GBP"));
assertU(commit());
assertQ(req("fl", "*,score", "q", "*:*", "sort", "amount desc", "limit", "1"), "//int[@name='id']='4'");
assertQ(req("fl", "*,score", "q", "*:*", "sort", "amount asc", "limit", "1"), "//int[@name='id']='3'");
assertQ(req("fl", "*,score", "q", "*:*", "sort", field()+" desc", "limit", "1"), "//int[@name='id']='4'");
assertQ(req("fl", "*,score", "q", "*:*", "sort", field()+" asc", "limit", "1"), "//int[@name='id']='3'");
}
@Test

View File

@ -0,0 +1,38 @@
package org.apache.solr.schema;
/*
* 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.
*/
import org.apache.lucene.index.StorableField;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.core.SolrCore;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.util.List;
import java.util.Random;
import java.util.Set;
/**
* Tests currency field type using OpenExchangeRatesOrgProvider.
*/
public class CurrencyFieldOpenExchangeTest extends AbstractCurrencyFieldTest {
public String field() {
return "oer_amount";
}
}

View File

@ -0,0 +1,52 @@
package org.apache.solr.schema;
/*
* 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.
*/
import org.apache.lucene.index.StorableField;
import org.apache.solr.SolrTestCaseJ4;
import org.apache.solr.core.SolrCore;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import java.util.List;
import java.util.Random;
import java.util.Set;
/**
* Tests currency field type using FileExchangeRateProvider
*/
public class CurrencyFieldXmlFileTest extends AbstractCurrencyFieldTest {
public String field() {
return "amount";
}
@Test
public void testAsymetricPointQuery() throws Exception {
clearIndex();
assertU(adoc("id", "" + 1, field(), "10.00,USD"));
assertU(adoc("id", "" + 2, field(), "15.00,EUR"));
assertU(commit());
assertQ(req("fl", "*,score", "q", field()+":15.00,EUR"), "//int[@name='id']='2'");
assertQ(req("fl", "*,score", "q", field()+":7.50,USD"), "//int[@name='id']='2'");
assertQ(req("fl", "*,score", "q", field()+":7.49,USD"), "//*[@numFound='0']");
assertQ(req("fl", "*,score", "q", field()+":7.51,USD"), "//*[@numFound='0']");
}
}

View File

@ -38,6 +38,9 @@ public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
@Override
@Before
public void setUp() throws Exception {
AbstractCurrencyFieldTest.assumeCurrencySupport
("USD", "EUR", "MXN", "GBP", "JPY");
super.setUp();
mockParams = new HashMap<String,String>();;
mockParams.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION, "open-exchange-rates.json");
@ -63,14 +66,14 @@ public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
public void testList() {
oerp.init(mockParams);
oerp.inform(loader);
assertEquals(159, oerp.listAvailableCurrencies().size());
assertEquals(5, oerp.listAvailableCurrencies().size());
}
@Test
public void testGetExchangeRate() {
oerp.init(mockParams);
oerp.inform(loader);
assertTrue(5.73163 == oerp.getExchangeRate("USD", "NOK"));
assertEquals(81.29D, oerp.getExchangeRate("USD", "JPY"), 0.0D);
}
@Test