def test_smart_entity_lookup_cache_hit_manual_override_null_permid(
            self, mock_dynamo):
        manual_override = {"confidence": "1.0", "perm_id": None}
        mock_dynamo.return_value = {"manual_override": manual_override}

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {'statusCode': 404}

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
    def test_smart_entity_lookup_cache_hit_entity_match(self, mock_dynamo):
        entity_match = {"confidence": "0.9", "perm_id": 1234}
        mock_dynamo.return_value = {"entity_match": entity_match}

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {'statusCode': 200, 'body': entity_match}
        result_json = json.loads(result['body'])

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
        self.assertEqual(result_json['resolution_algorithm'], 'entity_match')
        self.assertEqual(expected_result['body'], result_json)
    def test_smart_entity_lookup_cache_miss(self, mock_dynamo):
        mock_dynamo.return_value = ''

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {
            'statusCode':
            404,
            'body':
            '{"status": "failed", '
            '"message": "Could not successfully smart-lookup entity for keyword [test]"}'
        }
        self.assertEqual(expected_result, result)
    def test_smart_entity_lookup_cache_hit_manual_override(self, mock_dynamo):
        manual_override = {"confidence": "1.0", "perm_id": 1234}
        mock_dynamo.return_value = {"manual_override": manual_override}

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {'statusCode': 200, 'body': manual_override}
        result_json = json.loads(result['body'])

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
        self.assertEqual(result_json['resolution_algorithm'],
                         'manual_override')
        self.assertEqual(expected_result['body']['resolution_algorithm'],
                         result_json['resolution_algorithm'])
    def test_smart_entity_lookup_cache_hit_precedence_check(self, mock_dynamo):
        open_calais = [{
            "confidence": "0.9",
            "is_company_score": "0.9",
            "perm_id": 1234
        }]
        manual_override = {"confidence": "0.9", "perm_id": 1234}
        entity_match = {"confidence": "0.9", "perm_id": 1234}
        mock_dynamo.return_value = {
            "open_calais": open_calais,
            "manual_override": manual_override,
            "entity_match": entity_match
        }

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {'statusCode': 200, 'body': manual_override}
        result_json = json.loads(result['body'])

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
        self.assertEqual(result_json['resolution_algorithm'],
                         'manual_override')
        self.assertEqual(expected_result['body'], result_json)

        mock_dynamo.return_value = {
            "open_calais": open_calais,
            "entity_match": entity_match
        }

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {'statusCode': 200, 'body': entity_match}
        result_json = json.loads(result['body'])

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
        self.assertEqual(result_json['resolution_algorithm'], 'entity_match')
        self.assertEqual(expected_result['body'], result_json)
    def test_smart_entity_lookup_cache_hit_manual_override_prevent_resolution(
            self, mock_dynamo):
        confidence = str(MIN_CONFIDENCE + 0.1)
        manual_override = {"prevent_resolution": True}
        entity_match = {"confidence": confidence, "perm_id": 1234}
        mock_dynamo.return_value = {
            "manual_override": manual_override,
            "entity_match": entity_match
        }

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {'statusCode': 404}

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
    def test_smart_entity_lookup_cache_hit_open_calais(self, mock_dynamo):
        open_calais = [{
            "confidence": "0.9",
            "is_company_score": "0.6",
            "perm_id": 1234
        }]
        mock_dynamo.return_value = {"open_calais": open_calais}

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {'statusCode': 200, 'body': open_calais[0]}
        result_json = json.loads(result['body'])

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
        self.assertEqual(result_json['resolution_algorithm'], 'open_calais')
        self.assertEqual(expected_result['body'], result_json)
    def test_smart_entity_lookup_cache_hit_open_calais_low_confidence(
            self, mock_dynamo):
        open_calais = [{"confidence": "0.6", "is_company_score": "0.6"}]
        mock_dynamo.return_value = {"open_calais": open_calais}

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {
            'statusCode': 404,
            'body': {
                'status':
                'failed',
                'message':
                'Could not successfully smart-lookup entity for keyword [test]'
            }
        }
        result_json = json.loads(result['body'])

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
        self.assertEqual(expected_result['body'], result_json)
    def test_smart_entity_lookup_cache_hit_manual_override_low_confidence(
            self, mock_dynamo):
        confidence = str(MIN_CONFIDENCE - 0.1)
        manual_override = {"confidence": confidence}
        mock_dynamo.return_value = {"manual_override": manual_override}

        result = entity_lookup_smart(self.good_event, None)

        expected_result = {
            'statusCode': 404,
            'body': {
                'status':
                'failed',
                'message':
                'Could not successfully smart-lookup entity for keyword [test]'
            }
        }
        result_json = json.loads(result['body'])

        self.assertEqual(expected_result['statusCode'], result['statusCode'])
        self.assertEqual(expected_result['body'], result_json)