# Path: pupa/scrape/popolo.py
# class Person(BaseModel, SourceMixin, ContactDetailMixin, LinkMixin, IdentifierMixin,
#              OtherNameMixin):
#     """
#     Details for a Person in Popolo format.
#     """
# 
#     _type = 'person'
#     _schema = person_schema
# 
#     def __init__(self, name, *, birth_date='', death_date='', biography='', summary='', image='',
#                  gender='', national_identity='',
#                  # specialty fields
#                  district=None, party=None, primary_org='', role='',
#                  start_date='', end_date='', primary_org_name=None):
#         super(Person, self).__init__()
#         self.name = name
#         self.birth_date = birth_date
#         self.death_date = death_date
#         self.biography = biography
#         self.summary = summary
#         self.image = image
#         self.gender = gender
#         self.national_identity = national_identity
#         if primary_org:
#             self.add_term(role, primary_org, district=district,
#                           start_date=start_date, end_date=end_date,
#                           org_name=primary_org_name)
#         if party:
#             self.add_party(party)
# 
#     def add_membership(self, name_or_org, role='member', **kwargs):
#         """
#             add a membership in an organization and return the membership
#             object in case there are more details to add
#         """
#         if isinstance(name_or_org, Organization):
#             membership = Membership(person_id=self._id,
#                                     person_name=self.name,
#                                     organization_id=name_or_org._id,
#                                     role=role, **kwargs)
#         else:
#             membership = Membership(person_id=self._id,
#                                     person_name=self.name,
#                                     organization_id=_make_pseudo_id(name=name_or_org),
#                                     role=role, **kwargs)
#         self._related.append(membership)
#         return membership
# 
#     def add_party(self, party, **kwargs):
#         membership = Membership(
#             person_id=self._id,
#             person_name=self.name,
#             organization_id=_make_pseudo_id(classification="party", name=party),
#             role='member', **kwargs)
#         self._related.append(membership)
# 
#     def add_term(self, role, org_classification, *, district=None,
#                  start_date='', end_date='', label='', org_name=None,
#                  appointment=False):
#         if org_name:
#             org_id = _make_pseudo_id(classification=org_classification,
#                                      name=org_name)
#         else:
#             org_id = _make_pseudo_id(classification=org_classification)
# 
#         if district:
#             if role:
#                 post_id = _make_pseudo_id(label=district,
#                                           role=role,
#                                           organization__classification=org_classification)
#             else:
#                 post_id = _make_pseudo_id(label=district,
#                                           organization__classification=org_classification)
#         elif appointment:
#             post_id = _make_pseudo_id(role=role,
#                                       organization__classification=org_classification)
#         else:
#             post_id = None
#         membership = Membership(person_id=self._id, person_name=self.name,
#                                 organization_id=org_id, post_id=post_id,
#                                 role=role, start_date=start_date, end_date=end_date, label=label)
#         self._related.append(membership)
#         return membership
# 
#     def __str__(self):
#         return self.name
# 
# Path: pupa/exceptions.py
# class UnresolvedIdError(DataImportError):
#     """ Attempt was made to resolve an id that has no result. """
# 
# class SameNameError(DataImportError):
#     """ Attempt was made to import two people with the same name. """
# 
#     def __init__(self, name):
#         super(SameNameError, self).__init__('multiple people with same name "{}" in Jurisdiction '
#                                             '- must provide birth_date to disambiguate'
#                                             .format(name))


import pytest
from pupa.scrape import Person as ScrapePerson
from pupa.importers import PersonImporter
from opencivicdata.core.models import Person, Organization, Membership, Division, Jurisdiction
from pupa.exceptions import UnresolvedIdError, SameNameError
    # ensure we're taking other_names into account for the name collision code
    Organization.objects.create(name='WWE', jurisdiction_id='jid')
    p1 = ScrapePerson('Dwayne Johnson', image='http://example.com/1')
    p2 = ScrapePerson('Rock', image='http://example.com/2')
    p2.add_name('Dwayne Johnson')

    # the people have the same name but are apparently different
    with pytest.raises(SameNameError):
        PersonImporter('jid').import_data([p1.as_dict(), p2.as_dict()])


@pytest.mark.django_db
def test_same_name_second_import():
    create_jurisdiction()
    # ensure two people with the same name don't import without birthdays
    o = Organization.objects.create(name='WWE', jurisdiction_id='jid')
    p1 = ScrapePerson('Dwayne Johnson', image='http://example.com/1')
    p2 = ScrapePerson('Dwayne Johnson', image='http://example.com/2')
    p1.birth_date = '1970'
    p2.birth_date = '1930'

    # when we give them birth dates all is well though
    PersonImporter('jid').import_data([p1.as_dict(), p2.as_dict()])

    # fake some memberships so future lookups work on these people
    for p in Person.objects.all():
        Membership.objects.create(person=p, organization=o)

    p3 = ScrapePerson('Dwayne Johnson', image='http://example.com/3')


# Path: pupa/scrape/popolo.py
# class Person(BaseModel, SourceMixin, ContactDetailMixin, LinkMixin, IdentifierMixin,
#              OtherNameMixin):
#     """
#     Details for a Person in Popolo format.
#     """
# 
#     _type = 'person'
#     _schema = person_schema
# 
#     def __init__(self, name, *, birth_date='', death_date='', biography='', summary='', image='',
#                  gender='', national_identity='',
#                  # specialty fields
#                  district=None, party=None, primary_org='', role='',
#                  start_date='', end_date='', primary_org_name=None):
#         super(Person, self).__init__()
#         self.name = name
#         self.birth_date = birth_date
#         self.death_date = death_date
#         self.biography = biography
#         self.summary = summary
#         self.image = image
#         self.gender = gender
#         self.national_identity = national_identity
#         if primary_org:
#             self.add_term(role, primary_org, district=district,
#                           start_date=start_date, end_date=end_date,
#                           org_name=primary_org_name)
#         if party:
#             self.add_party(party)
# 
#     def add_membership(self, name_or_org, role='member', **kwargs):
#         """
#             add a membership in an organization and return the membership
#             object in case there are more details to add
#         """
#         if isinstance(name_or_org, Organization):
#             membership = Membership(person_id=self._id,
#                                     person_name=self.name,
#                                     organization_id=name_or_org._id,
#                                     role=role, **kwargs)
#         else:
#             membership = Membership(person_id=self._id,
#                                     person_name=self.name,
#                                     organization_id=_make_pseudo_id(name=name_or_org),
#                                     role=role, **kwargs)
#         self._related.append(membership)
#         return membership
# 
#     def add_party(self, party, **kwargs):
#         membership = Membership(
#             person_id=self._id,
#             person_name=self.name,
#             organization_id=_make_pseudo_id(classification="party", name=party),
#             role='member', **kwargs)
#         self._related.append(membership)
# 
#     def add_term(self, role, org_classification, *, district=None,
#                  start_date='', end_date='', label='', org_name=None,
#                  appointment=False):
#         if org_name:
#             org_id = _make_pseudo_id(classification=org_classification,
#                                      name=org_name)
#         else:
#             org_id = _make_pseudo_id(classification=org_classification)
# 
#         if district:
#             if role:
#                 post_id = _make_pseudo_id(label=district,
#                                           role=role,
#                                           organization__classification=org_classification)
#             else:
#                 post_id = _make_pseudo_id(label=district,
#                                           organization__classification=org_classification)
#         elif appointment:
#             post_id = _make_pseudo_id(role=role,
#                                       organization__classification=org_classification)
#         else:
#             post_id = None
#         membership = Membership(person_id=self._id, person_name=self.name,
#                                 organization_id=org_id, post_id=post_id,
#                                 role=role, start_date=start_date, end_date=end_date, label=label)
#         self._related.append(membership)
#         return membership
# 
#     def __str__(self):
#         return self.name
# 
# Path: pupa/exceptions.py
# class UnresolvedIdError(DataImportError):
#     """ Attempt was made to resolve an id that has no result. """
# 
# class SameNameError(DataImportError):
#     """ Attempt was made to import two people with the same name. """
# 
#     def __init__(self, name):
#         super(SameNameError, self).__init__('multiple people with same name "{}" in Jurisdiction '
#                                             '- must provide birth_date to disambiguate'
#                                             .format(name))


# Path: pupa/tests/importers/test_people_importer.py
import pytest
from pupa.scrape import Person as ScrapePerson
from pupa.importers import PersonImporter
from opencivicdata.core.models import Person, Organization, Membership, Division, Jurisdiction
from pupa.exceptions import UnresolvedIdError, SameNameError

    # ensure we're taking other_names into account for the name collision code
    Organization.objects.create(name='WWE', jurisdiction_id='jid')
    p1 = ScrapePerson('Dwayne Johnson', image='http://example.com/1')
    p2 = ScrapePerson('Rock', image='http://example.com/2')
    p2.add_name('Dwayne Johnson')

    # the people have the same name but are apparently different
    with pytest.raises(SameNameError):
        PersonImporter('jid').import_data([p1.as_dict(), p2.as_dict()])


@pytest.mark.django_db
def test_same_name_second_import():
    create_jurisdiction()
    # ensure two people with the same name don't import without birthdays
    o = Organization.objects.create(name='WWE', jurisdiction_id='jid')
    p1 = ScrapePerson('Dwayne Johnson', image='http://example.com/1')
    p2 = ScrapePerson('Dwayne Johnson', image='http://example.com/2')
    p1.birth_date = '1970'
    p2.birth_date = '1930'

    # when we give them birth dates all is well though
    PersonImporter('jid').import_data([p1.as_dict(), p2.as_dict()])

    # fake some memberships so future lookups work on these people
    for p in Person.objects.all():
        Membership.objects.create(person=p, organization=o)

    p3 = ScrapePerson('Dwayne Johnson', image='http://example.com/3')


    with pytest.raises(SameNameError):