<commit_before>from lxml import objectify, etree
import pytest

from controlled_vocabularies.vocabulary_handler import VocabularyHandler
from . import factories

pytestmark = pytest.mark.django_db

# Namespaces
NS = {
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
    'dc': 'http://purl.org/dc/elements/1.1/',
    'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
}

PURL = 'http://purl.org/NET/UNTL/vocabularies/formats/'


def test_xml_response():
    vocab = factories.VocabularyFactory()
    vocab_handler = VocabularyHandler().xml_response(vocab)
    assert vocab_handler.vocab == vocab
    assert isinstance(vocab_handler, VocabularyHandler)


@pytest.fixture
def vocab_file_xml():
    prop = factories.PropertyFactory(property_name='definition')

    vocab_handler = VocabularyHandler().xml_response(prop.term_key.vocab_list)
    return prop, etree.fromstring(vocab_handler.vocab_file)


def test_create_xml_RDF_element(vocab_file_xml):
    _, root = vocab_file_xml

    assert root.tag == '{{{}}}RDF'.format(NS['rdf'])


def test_create_xml_Description_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Description', namespaces=NS)[0]
    attrib = element.get('{{{}}}about'.format(NS['rdf']))
    assert attrib == PURL


def test_create_xml_title_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:title', namespaces=NS)[0]
    assert element.text == prop.term_key.vocab_list.label


def test_create_xml_publisher_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:publisher', namespaces=NS)[0]
    assert element.text == 'University of North Texas Libraries'


def test_create_xml_Description_subelement_description(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:description', namespaces=NS)[0]
    assert element.text == prop.term_key.vocab_list.definition


def test_create_xml_language_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:language', namespaces=NS)[0]
    assert element.text == 'English'


def test_create_xml_date_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:date', namespaces=NS)[0]
    assert element.text == prop.term_key.vocab_list.created.strftime('%Y')


def test_create_xml_Property_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Property', namespaces=NS)[0]
    attrib = element.get('{{{}}}about'.format(NS['rdf']))
    assert attrib == '{}#{}'.format(PURL, prop.term_key.name)


def test_create_xml_label_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Property/rdfs:label', namespaces=NS)[0]
    assert element.text == prop.term_key.label


def test_create_xml_Property_subelement_description(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Property/dc:description', namespaces=NS)[0]
    assert element.text == prop.label


def test_create_xml_isDefinedBy_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Property/rdfs:isDefinedBy', namespaces=NS)[0]
    attrib = element.get('{{{}}}resource'.format(NS['rdf']))
    assert attrib == PURL


def test_py_response():
    vocab = factories.VocabularyFactory()
    vocab_handler = VocabularyHandler().py_response(vocab)<commit_msg>Changed constants so each namespace is its own constant and namespace dict is made up of them. Eases referencing individual namespaces.<commit_after>from lxml import objectify, etree
import pytest

from controlled_vocabularies.vocabulary_handler import VocabularyHandler
from . import factories

pytestmark = pytest.mark.django_db

# Namespaces
RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
DC = 'http://purl.org/dc/elements/1.1/'
RDFS = 'http://www.w3.org/2000/01/rdf-schema#'

NS = {
    'rdf': RDF,
    'dc': DC,
    'rdfs': RDFS
}

PURL = 'http://purl.org/NET/UNTL/vocabularies/formats/'


def test_xml_response():
    vocab = factories.VocabularyFactory()
    vocab_handler = VocabularyHandler().xml_response(vocab)
    assert vocab_handler.vocab == vocab
    assert isinstance(vocab_handler, VocabularyHandler)


@pytest.fixture
def vocab_file_xml():
    prop = factories.PropertyFactory(property_name='definition')

    vocab_handler = VocabularyHandler().xml_response(prop.term_key.vocab_list)
    return prop, etree.fromstring(vocab_handler.vocab_file)


def test_create_xml_RDF_element(vocab_file_xml):
    _, root = vocab_file_xml

    assert root.tag == '{{{}}}RDF'.format(RDF)


def test_create_xml_Description_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Description', namespaces=NS)[0]
    attrib = element.get('{{{}}}about'.format(RDF))
    assert attrib == PURL


def test_create_xml_title_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:title', namespaces=NS)[0]
    assert element.text == prop.term_key.vocab_list.label


def test_create_xml_publisher_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:publisher', namespaces=NS)[0]
    assert element.text == 'University of North Texas Libraries'


def test_create_xml_Description_subelement_description(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:description', namespaces=NS)[0]
    assert element.text == prop.term_key.vocab_list.definition


def test_create_xml_language_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:language', namespaces=NS)[0]
    assert element.text == 'English'


def test_create_xml_date_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Description/dc:date', namespaces=NS)[0]
    assert element.text == prop.term_key.vocab_list.created.strftime('%Y')


def test_create_xml_Property_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Property', namespaces=NS)[0]
    attrib = element.get('{{{}}}about'.format(RDF))
    assert attrib == '{}#{}'.format(PURL, prop.term_key.name)


def test_create_xml_label_element(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Property/rdfs:label', namespaces=NS)[0]
    assert element.text == prop.term_key.label


def test_create_xml_Property_subelement_description(vocab_file_xml):
    prop, root = vocab_file_xml

    element = root.xpath('rdf:Property/dc:description', namespaces=NS)[0]
    assert element.text == prop.label


def test_create_xml_isDefinedBy_element(vocab_file_xml):
    _, root = vocab_file_xml

    element = root.xpath('rdf:Property/rdfs:isDefinedBy', namespaces=NS)[0]
    attrib = element.get('{{{}}}resource'.format(RDF))
    assert attrib == PURL


def test_py_response():
    vocab = factories.VocabularyFactory()
    vocab_handler = VocabularyHandler().py_response(vocab)
{'fraction_non_alphanumeric': 0.1254957507082153, 'fraction_numerical': 0.00793201133144476, 'mean_word_length': 5.102852203975799, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 76}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '647543', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2506, 'n_words': 381}