# Path: zenodo/modules/records/views.py
# @blueprint.app_template_filter('zenodo_related_links')
# def zenodo_related_links(record, communities):
#     """Get logos for related links."""
#     def apply_rule(item, rule):
#         r = copy.deepcopy(rule)
#         r['link'] = idutils.to_url(item['identifier'], item['scheme'], 'https')
#         return r
# 
#     def match_rules(item):
#         rs = []
#         for c in set(communities):
#             if c.id in current_app.config['ZENODO_RELATION_RULES']:
#                 rules = current_app.config['ZENODO_RELATION_RULES'][c.id]
#                 for r in rules:
#                     if item['relation'] == r['relation'] and \
#                        item['scheme'] == r['scheme'] and \
#                        item['identifier'].startswith(r['prefix']):
#                         rs.append(r)
#         return rs
# 
#     ret = []
#     for item in record.get('related_identifiers', []):
#         for r in match_rules(item):
#             ret.append(apply_rule(item, r))
# 
#     return ret


import json
import pytest
from datetime import datetime, timedelta
from flask import current_app, render_template, render_template_string, url_for
from helpers import login_user_via_session
from invenio_indexer.api import RecordIndexer
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
from invenio_records.api import Record
from invenio_search import current_search
from mock import Mock, patch
from zenodo.modules.records.views import zenodo_related_links

def test_relation_title(app):
    """Test relation title."""
    assert render_template_string(
        "{{ 'isCitedBy'|relation_title }}") == "Cited by"
    assert render_template_string(
        "{{ 'nonExistingRelation'|relation_title }}") == "nonExistingRelation"


def test_relation_logo(app):
    """Test relation logo."""
    no_relations = {}
    assert zenodo_related_links(no_relations, []) == []

    class MockCommunity(object):
        id = 'zenodo'

    github_relation = {
        'communities': [
            'zenodo',
        ],
        'related_identifiers': [
            {
                'scheme': 'url',
                'relation': 'isSupplementTo',
                'identifier': 'https://github.com/'
                              'TaghiAliyev/BBiCat/tree/v1.0.4-alpha',
            }
        ],
    }

# Path: zenodo/modules/records/views.py
# @blueprint.app_template_filter('zenodo_related_links')
# def zenodo_related_links(record, communities):
#     """Get logos for related links."""
#     def apply_rule(item, rule):
#         r = copy.deepcopy(rule)
#         r['link'] = idutils.to_url(item['identifier'], item['scheme'], 'https')
#         return r
# 
#     def match_rules(item):
#         rs = []
#         for c in set(communities):
#             if c.id in current_app.config['ZENODO_RELATION_RULES']:
#                 rules = current_app.config['ZENODO_RELATION_RULES'][c.id]
#                 for r in rules:
#                     if item['relation'] == r['relation'] and \
#                        item['scheme'] == r['scheme'] and \
#                        item['identifier'].startswith(r['prefix']):
#                         rs.append(r)
#         return rs
# 
#     ret = []
#     for item in record.get('related_identifiers', []):
#         for r in match_rules(item):
#             ret.append(apply_rule(item, r))
# 
#     return ret


# Path: tests/unit/records/test_views.py
import json
import pytest
from datetime import datetime, timedelta
from flask import current_app, render_template, render_template_string, url_for
from helpers import login_user_via_session
from invenio_indexer.api import RecordIndexer
from invenio_pidstore.models import PersistentIdentifier, PIDStatus
from invenio_records.api import Record
from invenio_search import current_search
from mock import Mock, patch
from zenodo.modules.records.views import zenodo_related_links


def test_relation_title(app):
    """Test relation title."""
    assert render_template_string(
        "{{ 'isCitedBy'|relation_title }}") == "Cited by"
    assert render_template_string(
        "{{ 'nonExistingRelation'|relation_title }}") == "nonExistingRelation"


def test_relation_logo(app):
    """Test relation logo."""
    no_relations = {}
    assert zenodo_related_links(no_relations, []) == []

    class MockCommunity(object):
        id = 'zenodo'

    github_relation = {
        'communities': [
            'zenodo',
        ],
        'related_identifiers': [
            {
                'scheme': 'url',
                'relation': 'isSupplementTo',
                'identifier': 'https://github.com/'
                              'TaghiAliyev/BBiCat/tree/v1.0.4-alpha',
            }
        ],
    }

    assert zenodo_related_links(github_relation, [MockCommunity]) == [