Exemplo n.º 1
0
    def get(self, term):
        """
        Returns list of matching concepts or entities using lexical search
        """

        input_args = layperson_parser.parse_args()
        # params that don't directly translate to solr controller
        filtered_params = [
            'phenotype_group', 'phenotype_group_label', 'anatomical_system',
            'anatomical_system_label'
        ]
        args = {
            k: v
            for k, v in input_args.items() if k not in filtered_params
        }

        args['fq'] = {}
        if input_args['phenotype_group'] is not None:
            args['fq']['phenotype_closure'] = input_args['phenotype_group']
        if input_args['phenotype_group_label'] is not None:
            args['fq']['phenotype_closure_label'] = input_args[
                'phenotype_group_label']
        if input_args['anatomical_system'] is not None:
            args['fq']['anatomy_closure'] = input_args['anatomical_system']
        if input_args['anatomical_system_label'] is not None:
            args['fq']['anatomy_closure_label'] = input_args[
                'anatomical_system_label']

        q = GolrLayPersonSearch(term, user_agent=USER_AGENT, **args)
        results = q.autocomplete()
        return results
class TestGolrLayPersonSearch():
    @classmethod
    def setup_class(self):
        self.manager = GolrLayPersonSearch()

        # Mock the PySolr search function to
        # return our test docs
        input_fh = os.path.join(os.path.dirname(__file__),
                                'resources/solr/input/layperson-docs.json')
        input_docs = json.load(open(input_fh))
        self.pysolr_results = pysolr.Results(input_docs)
        self.manager.solr.search = MagicMock(return_value=self.pysolr_results)

    @classmethod
    def teardown_class(self):
        self.manager = None

    def test_lay_doc_conversion(self):
        """
        Given a sample solr output as a pysolr.Results object
        test that _process_layperson_results returns the
        expected object
        """
        expected_fh = os.path.join(os.path.dirname(__file__),
                                   'resources/solr/expected/layperson.json')
        processed_docs = json.load(open(expected_fh))
        output_docs = self.manager._process_layperson_results(
            self.pysolr_results)

        assert json.dumps(processed_docs,
                          sort_keys=True) == json.dumps(output_docs,
                                                        sort_keys=True)

    def test_autocomplete(self):
        """
        Given a mock PySolr.search method test that
        autocomplete() returns the expected object
        """
        expected_fh = os.path.join(os.path.dirname(__file__),
                                   'resources/solr/expected/layperson.json')
        processed_docs = json.load(open(expected_fh))
        output_docs = self.manager.autocomplete()

        assert json.dumps(processed_docs,
                          sort_keys=True) == json.dumps(output_docs,
                                                        sort_keys=True)