def test_generate_results_outputs_a_list_with_non_relevant_facts_stripped_out(self):
        facts = [
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/ontology/birthDate" } ,
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "birth date" } ,
                "o": { "datatype": "http://www.w3.org/2001/XMLSchema#date" , "type": "typed-literal" , "value": "1971-06-15+02:00" }
            }
        ]
        configuration = {
            "Sample": [
                #irrelevant fact since its not found in the facts
                {
                    "label": "Death Date",
                    "from": "http://dbpedia.org/ontology/deathDate"
                }
            ]
        }
        
        parser = ConfigurableParser(facts)
        output = yield parser.generate_results(configuration)

        expectedOutput = {
            "id": "Sample",
            "facts": []
        }
        self.assertEqual(output['facts'], expectedOutput['facts'])
    def test_process_type_generates_results_using_configuration(self):
        facts = [
            {
                "p": {
                    "type": "uri",
                    "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
                },
                "predicate_label": {
                    "type": "literal",
                    "value": "type"
                },
                "o": {
                    "type": "uri",
                    "value": "http://dbpedia.org/ontology/MusicalArtist"
                },
                "object_label": {
                    "type": "literal",
                    "xml:lang": "en",
                    "value": "musical artist"
                }
            }
        ]

        parser = ConfigurableParser(facts)
        rdfType = 'http://dbpedia.org/ontology/MusicalArtist'
        
        raw_configuration = '''{ "Sample": [] }'''

        m = mock_open(read_data=raw_configuration)
        parser.generate_results = Mock(name='generate_results')

        with patch('builtins.open', m, create=True):
            parser.process_type(rdfType)

        parser.generate_results.assert_called_once_with(json.loads(raw_configuration))
    def test_generate_results_respects_limit_defined_in_configuration(self):
        facts = [
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/ontology/associatedBand" } ,
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "associated band" } ,
                "o": { "type": "uri" , "value": "http://dbpedia.org/resource/Dr._Dre" } ,
                "object_label": { "type": "literal" , "xml:lang": "en" , "value": "Dr. Dre" }
            },
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/property/associatedActs" },
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "associated acts" },
                "o": { "type": "uri" , "value": "http://dbpedia.org/resource/Snoop_Dogg" },
                "object_label": { "type": "literal" , "xml:lang": "en" , "value": "Snoop Dogg" }
            },
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/property/associatedActs" },
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "associated acts" },
                "o": { "type": "uri" , "value": "http://dbpedia.org/resource/Rakim" },
                "object_label": { "type": "literal" , "xml:lang": "en" , "value": "Rakim" }
            }
        ]
        configuration = {
            "Sample": [
                {
                    "label": "Related Artist",
                    "from": ["http://dbpedia.org/ontology/associatedBand", "http://dbpedia.org/property/associatedActs"],
                    "limit": 1
                }
            ]
        }
        
        parser = ConfigurableParser(facts)
        output = yield parser.generate_results(configuration)

        expectedOutput = {
            "id": "Sample",
            "facts": [
                {
                    'predicate': {
                        'value': ["http://dbpedia.org/ontology/associatedBand", "http://dbpedia.org/property/associatedActs"],
                        'label': 'Related Artist'
                    },
                    'objects': [
                        {
                            "type": "uri" ,
                            "value": "http://dbpedia.org/resource/Dr._Dre",
                            "label": "Dr. Dre"
                        }
                    ]
                }
            ]
        }
        self.assertEqual(output['facts'], expectedOutput['facts'])
    def test_generate_results_outputs_correct_facts_for_single_predicate_spec(self):
        facts = [
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/ontology/birthDate" } ,
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "birth date" } ,
                "o": { "datatype": "http://www.w3.org/2001/XMLSchema#date" , "type": "typed-literal" , "value": "1971-06-15+02:00" }
            }
        ]
        configuration = {
            "Sample": [
                # {
                #     "label": "Birth Place",
                #     "from": "http://dbpedia.org/ontology/birthPlace"
                # },
                {
                    "label": "Birth Date",
                    "from": "http://dbpedia.org/ontology/birthDate"
                }
            ]
        }
        
        parser = ConfigurableParser(facts)
        output = yield parser.generate_results(configuration)

        expectedOutput = {
            "id": "Sample",
            "facts": [
                {
                    'predicate': {
                        'value': 'http://dbpedia.org/ontology/birthDate',
                        'label': 'Birth Date'
                    },
                    'objects': [{
                        'type': 'typed-literal',
                        'datatype': 'http://www.w3.org/2001/XMLSchema#date',
                        'value': '1971-06-15+02:00'
                    }]
                }
            ]
        }
        self.assertEqual(output['facts'], expectedOutput['facts'])
    def test_generate_results_ranks_output_according_to_given_ranker(self):
        facts = [
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/property/associatedActs" },
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "associated acts" },
                "o": { "type": "uri" , "value": "http://dbpedia.org/resource/Rakim" },
                "object_label": { "type": "literal" , "xml:lang": "en" , "value": "Rakim" }
            },
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/ontology/associatedBand" } ,
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "associated band" } ,
                "o": { "type": "uri" , "value": "http://dbpedia.org/resource/Dr._Dre" },
                "object_label": { "type": "literal" , "xml:lang": "en" , "value": "Dr. Dre" }
            },
            {
                "p": { "type": "uri" , "value": "http://dbpedia.org/property/associatedActs" },
                "predicate_label": { "type": "literal" , "xml:lang": "en" , "value": "associated acts" },
                "o": { "type": "uri" , "value": "http://dbpedia.org/resource/Snoop_Dogg" },
                "object_label": { "type": "literal" , "xml:lang": "en" , "value": "Snoop Dogg" }
            }
        ]
        configuration = {
            "Sample": [
                {
                    "label": "Related Artist",
                    "from": ["http://dbpedia.org/ontology/associatedBand", "http://dbpedia.org/property/associatedActs"]
                }
            ]
        }
        
        expected_output = {
            'predicate': {
                'label': 'Related Artist',
                'value': ["http://dbpedia.org/ontology/associatedBand", "http://dbpedia.org/property/associatedActs"]
            },
            'objects': [
                {
                    'label': "Snoop Dogg",
                    'value': "http://dbpedia.org/resource/Snoop_Dogg",
                    'type': "uri"
                },
                {
                    'label': "Dr. Dre",
                    'value': "http://dbpedia.org/resource/Dr._Dre",
                    'type': "uri"
                },
                {
                    'label': "Rakim",
                    'value': "http://dbpedia.org/resource/Rakim",
                    'type': "uri"
                }
            ]
        }
        
        #set the output from the ranker
        ranker = Mock()
        future = Future()
        future.set_result(expected_output)
        ranker.rank = Mock(return_value=future)

        parser = ConfigurableParser(facts, ranker=ranker)
        output = yield parser.generate_results(configuration)
        #there should only be one item in the facts array...
        #it should equal the expected output
        self.assertEqual(output['facts'][0], expected_output)
 def test_generate_results_outputs_correct_id_based_on_the_configuration(self):
     facts = []
     parser = ConfigurableParser(facts)
     output = yield parser.generate_results({ "Sample": [] })
     self.assertEqual(output['id'], 'Sample')