def test_3point_data_understood_ok(): s = SolrSchema(io.BytesIO(new_field_types_schema)) user_data = (3.5, -2.5, 1.0) solr_data = "3.5,-2.5,1.0" field_inst = s.field_from_user_data("point3_field", user_data) assert field_inst.value == user_data assert field_inst.to_solr() == solr_data point_field = s.match_field("point3_field") assert point_field.from_solr(solr_data) == user_data
def test_binary_data_understood_ok(): s = SolrSchema(io.BytesIO(new_field_types_schema)) blob = "jkgh" coded_blob = base64.b64encode(blob.encode('utf8')) field_inst = s.field_from_user_data("binary_field", blob) print(field_inst.value == blob) assert field_inst.value == blob assert field_inst.to_solr().encode('utf8') == coded_blob binary_field = s.match_field("binary_field") assert binary_field.from_solr(coded_blob).decode('utf8') == blob
def check_broken_schemata(n, s): try: SolrSchema(io.BytesIO(s)) except SolrError: pass else: assert False
def test_uuid_data_understood_ok(): s = SolrSchema(io.BytesIO(new_field_types_schema)) user_data = "12980286-591b-40c6-aa08-b4393a6d13b3" field_inst = s.field_from_user_data('id', user_data) assert field_inst.value == uuid.UUID( "12980286-591b-40c6-aa08-b4393a6d13b3") user_data = uuid.UUID("12980286-591b-40c6-aa08-b4393a6d13b3") field_inst = s.field_from_user_data('id', user_data) assert field_inst.value == uuid.UUID( "12980286-591b-40c6-aa08-b4393a6d13b3") user_data = "NEW" field_inst = s.field_from_user_data('id', user_data) solr_data = "12980286-591b-40c6-aa08-b4393a6d13b3" uuid_field = s.match_field("id") assert uuid_field.from_solr(solr_data) == uuid.UUID( "12980286-591b-40c6-aa08-b4393a6d13b3")
# borrowed from sunburnt.test_search schema_string = \ """<schema name="timetric" version="1.1"> <types> <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/> </types> <fields> <field name="text" required="true" type="string" multiValued="true"/> </fields> <defaultSearchField>text</defaultSearchField> </schema>""" schema = SolrSchema(StringIO(schema_string)) class MockInterface(object): schema = schema interface = MockInterface class TestSolrSearch(TestCase): def test_wildcard_search(self): from adhocracy.lib.search.query import add_wildcard_query search = SolrSearch(interface) query = add_wildcard_query(search, 'text', 'one two') self.assertEqual(
def setUp(self): self.schema = io.BytesIO(good_schema) self.s = SolrSchema(self.schema)
class TestReadingSchema(object): @pytest.fixture(autouse=True) def setUp(self): self.schema = io.BytesIO(good_schema) self.s = SolrSchema(self.schema) def test_read_schema(self): """ Test that we can read in a schema correctly, that we get the right set of fields, the right default field, and the right unique key""" assert set(self.s.fields.keys()) \ == set(['boolean_field', 'int_field', 'text_field', 'location_field']) assert self.s.default_field_name == 'text_field' assert self.s.unique_key == 'int_field' def test_serialize_dict(self): """ Test that each of the fields will serialize the relevant datatype appropriately.""" for k, v, v2 in (('int_field', 1, '1'), ('text_field', 'text', 'text'), ('text_field', 'text', 'text'), ('boolean_field', True, 'true'), ('location_field', 'POINT (30 10)', 'POINT (30 10)')): assert self.s.field_from_user_data(k, v).to_solr() == v2 def test_missing_fields(self): assert set(self.s.missing_fields([])) \ == set(['int_field', 'text_field']) assert set(self.s.missing_fields(['boolean_field'])) \ == set(['int_field', 'text_field']) assert set(self.s.missing_fields(['int_field'])) == set(['text_field']) def test_serialize_value_list_fails_with_bad_field_name(self): try: self.s.field_from_user_data('text_field2', "a") except SolrError: pass else: assert False def test_serialize_value_list_fails_when_wrong_datatype(self): try: self.s.field_from_user_data('int_field', "a") except SolrError: pass else: assert False def test_unknown_field_type(self): """ Check operation of a field type that is unknown to Sunburnt. """ assert 'solr.SpatialRecursivePrefixTreeFieldType' \ not in SolrSchema.solr_data_types field = self.s.fields['location_field'] assert field #Boolean attributes are converted accordingly assert field.geo == True #All other attributes are strings assert field.units == 'degrees' assert field.distErrPct == '0.025' assert field.maxDistErr == '0.000009' #Test that the value is always consistent - both to and from Solr value = 'POLYGON ((30 10, 10 20, 20 40, 40 40, 30 10))' assert field.to_user_data(value) \ == field.from_user_data(value) \ == field.to_solr(value) \ == field.from_solr(value) #Queried values will be escaped accordingly assert field.to_query( value ) == 'POLYGON\\ \\(\\(30\\ 10,\\ 10\\ 20,\\ 20\\ 40,\\ 40\\ 40,\\ 30\\ 10\\)\\)'
def test_delete_queries(): s = SolrSchema(io.BytesIO(good_schema)) for queries, xml_string in delete_queries: check_delete_queries(s, queries, xml_string)
def test_delete_docs(): s = SolrSchema(io.BytesIO(good_schema)) for doc, xml_string in delete_docs: check_delete_docs(s, doc, xml_string)
def test_bad_updates(): s = SolrSchema(io.BytesIO(good_schema)) for obj in bad_updates: check_broken_updates(s, obj)
def test_update_serialization(): s = SolrSchema(io.BytesIO(good_schema)) for obj, xml_string in update_docs: check_update_serialization(s, obj, xml_string)