# Path: rdfalchemy/samples/doap.py
# FOAF = Namespace("http://xmlns.com/foaf/0.1/")
# 
# Path: rdfalchemy/samples/foaf.py
# class Person(Agent):
#     rdf_type = FOAF.Person
#     first = rdfSingle(FOAF.firstName)
#     last = rdfSingle(FOAF.surname)
#     givenname = rdfSingle(FOAF.givenname)
#     surname = rdfSingle(FOAF.surname)
# 
# Path: rdfalchemy/orm.py
# def mapper(*classes):
#     """
#     Maps the classes given to allow descriptors with ranges to the
#     proper Class of that type
# 
#     The default, if no args are provided, is to map recursively all subclasses
#     of :class:`~rdfalchemy.rdfSubject.rdfSubject`
# 
#     Returns a dict of {rdf_type: mapped_class} for further processing
#     """
#     if not classes:
#         classes = allsub(rdfSubject)
#     class_dict = dict([(str(cl.rdf_type), cl) for cl in classes])
#     for cl in classes:  # for each class
#         for v in cl.__dict__.values():  # for each desciptor
#             # if its a descriptor with a range
#             if isinstance(v, rdfAbstract) and v.range_type:
#                 try:
#                     v._mappedClass = class_dict[str(v.range_type)]
#                 except KeyError:
#                     log.warn("No Class Found\nFailed to map %s range of %s" % (
#                         v, v.range_type))
#     return class_dict


import rdfalchemy
import logging
from rdfalchemy.samples.doap import FOAF
from rdfalchemy.samples.foaf import Person
from rdfalchemy.orm import mapper
from rdflib import ConjunctiveGraph
#log.setLevel(10)


Person.db = ConjunctiveGraph()
Person.knows = rdfalchemy.rdfMultiple(FOAF.knows, range_type=FOAF.Person)


def test_start():
    assert len(Person.db) == 0
    p = Person(last="Cooper", first="Philip")
    assert len(Person.db) == 3
    del p


def test_addBNodeKnowsL():
    Person.knows = rdfalchemy.rdfList(FOAF.knows, range_type=FOAF.Person)
    mapper()
    p1 = Person.get_by(first="Philip")
    p2 = Person(last="Cooper", first="Ben")
    p3 = Person(last="Cooper", first="Matt")
    assert len(Person.db) == 9
    p1.knows = [p2, p3]
    print len(Person.db)
    assert len(Person.db) == 14
    del p1.knows
    print len(Person.db)
    assert len(Person.db) == 3


def test_addBNodeKnowsM():

# Path: rdfalchemy/samples/doap.py
# FOAF = Namespace("http://xmlns.com/foaf/0.1/")
# 
# Path: rdfalchemy/samples/foaf.py
# class Person(Agent):
#     rdf_type = FOAF.Person
#     first = rdfSingle(FOAF.firstName)
#     last = rdfSingle(FOAF.surname)
#     givenname = rdfSingle(FOAF.givenname)
#     surname = rdfSingle(FOAF.surname)
# 
# Path: rdfalchemy/orm.py
# def mapper(*classes):
#     """
#     Maps the classes given to allow descriptors with ranges to the
#     proper Class of that type
# 
#     The default, if no args are provided, is to map recursively all subclasses
#     of :class:`~rdfalchemy.rdfSubject.rdfSubject`
# 
#     Returns a dict of {rdf_type: mapped_class} for further processing
#     """
#     if not classes:
#         classes = allsub(rdfSubject)
#     class_dict = dict([(str(cl.rdf_type), cl) for cl in classes])
#     for cl in classes:  # for each class
#         for v in cl.__dict__.values():  # for each desciptor
#             # if its a descriptor with a range
#             if isinstance(v, rdfAbstract) and v.range_type:
#                 try:
#                     v._mappedClass = class_dict[str(v.range_type)]
#                 except KeyError:
#                     log.warn("No Class Found\nFailed to map %s range of %s" % (
#                         v, v.range_type))
#     return class_dict


# Path: test/delete_test.py
import rdfalchemy
import logging
from rdfalchemy.samples.doap import FOAF
from rdfalchemy.samples.foaf import Person
from rdfalchemy.orm import mapper
from rdflib import ConjunctiveGraph

#log.setLevel(10)


Person.db = ConjunctiveGraph()
Person.knows = rdfalchemy.rdfMultiple(FOAF.knows, range_type=FOAF.Person)


def test_start():
    assert len(Person.db) == 0
    p = Person(last="Cooper", first="Philip")
    assert len(Person.db) == 3
    del p


def test_addBNodeKnowsL():
    Person.knows = rdfalchemy.rdfList(FOAF.knows, range_type=FOAF.Person)
    mapper()
    p1 = Person.get_by(first="Philip")
    p2 = Person(last="Cooper", first="Ben")
    p3 = Person(last="Cooper", first="Matt")
    assert len(Person.db) == 9
    p1.knows = [p2, p3]
    print len(Person.db)
    assert len(Person.db) == 14
    del p1.knows
    print len(Person.db)
    assert len(Person.db) == 3


def test_addBNodeKnowsM():

    Person.knows = rdfalchemy.rdfMultiple(FOAF.knows, range_type=FOAF.Person)