<commit_before># -*- coding: utf-8 -*-
# Copyright (C) 2014 <NAME>
#
# This package is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License Version 2.1
# as published by the Free Software Foundation or any newer version.

# This package is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License Version 2.1 for more details.

# You should have received a copy of the GNU Lesser General Public
# License Version 2.1 along with this package; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA
import os
import shutil
import tempfile
import unittest
import RDF

import rdfmagic
from IPython.core.interactiveshell import InteractiveShell

testdata = '''
@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .

<#green-goblin>
    rel:enemyOf <#spiderman> ;
    a foaf:Person ;    # in the context of the Marvel universe
    foaf:name "Green Goblin" .

<#spiderman>
    rel:enemyOf <#green-goblin> ;
    a foaf:Person ;
    foaf:name "Spiderman", "Человек-паук"@ru .
'''

class TestRDFMagic(unittest.TestCase):
    def setUp(self):
        self.tempdir = tempfile.mkdtemp(prefix='test_rdfm_')
        self.tempfile = os.path.join(self.tempdir, 'spiderman.ttl')
        self.tempurl = 'file://' + self.tempfile
        with open(self.tempfile, 'w') as outstream:
            outstream.write(testdata)
            
        self.shell = InteractiveShell()
        self.magic = rdfmagic.SPARQLMagics(self.shell)
        for ns_cmd in [
            "libns http://jumpgate.caltech.edu/wiki/LibraryOntology#",
            "htslib http://jumpgate.caltech.edu/library/",
            "htsflow http://jumpgate.caltech.edu/flowcell/"]:
            self.magic.addns(ns_cmd)

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def test_sources_literal(self):
        results = self.magic.sparql(
            '-s file://' + self.tempurl,
            """select ?s ?p ?o
               where { ?s ?p ?o . }""")
        self.assertEqual(len(results), 7)
        self.assertEqual(set(results[0].keys()), {'s', 'p', 'o'})

        self.assertTrue(len(str(results)) > 0)
        self.assertTrue(len(str(results._repr_html_())) > 0)

    def test_sources_str(self):
        self.shell.user_ns['url'] = self.tempurl
        results = self.magic.sparql(
            '-s url',
            """select ?s ?p ?o
               where { ?s ?p ?o . }""")
        self.assertEqual(len(results), 7)
        self.assertEqual(set(results[0].keys()), {'s', 'p', 'o'})

        self.assertTrue(len(str(results)) > 0)<commit_msg>flake8 whitespace cleanup<commit_after># -*- coding: utf-8 -*-
# Copyright (C) 2014 <NAME>
#
# This package is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License Version 2.1
# as published by the Free Software Foundation or any newer version.

# This package is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
# License Version 2.1 for more details.

# You should have received a copy of the GNU Lesser General Public
# License Version 2.1 along with this package; if not, write to the
# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA

import os
import shutil
import tempfile
import unittest
import RDF

import rdfmagic
from IPython.core.interactiveshell import InteractiveShell

testdata = '''
@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .

<#green-goblin>
    rel:enemyOf <#spiderman> ;
    a foaf:Person ;    # in the context of the Marvel universe
    foaf:name "<NAME>" .

<#spiderman>
    rel:enemyOf <#green-goblin> ;
    a foaf:Person ;
    foaf:name "Spiderman", "Человек-паук"@ru .
'''


class TestRDFMagic(unittest.TestCase):
    def setUp(self):
        self.tempdir = tempfile.mkdtemp(prefix='test_rdfm_')
        self.tempfile = os.path.join(self.tempdir, 'spiderman.ttl')
        self.tempurl = 'file://' + self.tempfile
        with open(self.tempfile, 'w') as outstream:
            outstream.write(testdata)

        self.shell = InteractiveShell()
        self.magic = rdfmagic.SPARQLMagics(self.shell)
        for ns_cmd in [
                "libns http://jumpgate.caltech.edu/wiki/LibraryOntology#",
                "htslib http://jumpgate.caltech.edu/library/",
                "htsflow http://jumpgate.caltech.edu/flowcell/"]:
            self.magic.addns(ns_cmd)

    def tearDown(self):
        shutil.rmtree(self.tempdir)

    def test_sources_literal(self):
        results = self.magic.sparql(
            '-s file://' + self.tempurl,
            """select ?s ?p ?o
               where { ?s ?p ?o . }""")
        self.assertEqual(len(results), 7)
        self.assertEqual(set(results[0].keys()), {'s', 'p', 'o'})

        self.assertTrue(len(str(results)) > 0)
        self.assertTrue(len(str(results._repr_html_())) > 0)

    def test_sources_str(self):
        self.shell.user_ns['url'] = self.tempurl
        results = self.magic.sparql(
            '-s url',
            """select ?s ?p ?o
               where { ?s ?p ?o . }""")
        self.assertEqual(len(results), 7)
        self.assertEqual(set(results[0].keys()), {'s', 'p', 'o'})

        self.assertTrue(len(str(results)) > 0)
{'fraction_non_alphanumeric': 0.13677505866577272, 'fraction_numerical': 0.01592356687898089, 'mean_word_length': 2.800636942675159, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 2}, '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': '1185128', 'n_tokens_mistral': 2029, 'n_tokens_neox': 1925, 'n_words': 516}