# Path: pupa/scrape/popolo.py
# class Organization(BaseModel, SourceMixin, ContactDetailMixin, LinkMixin, IdentifierMixin,
#                    OtherNameMixin):
#     """
#     A single popolo-style Organization
#     """
# 
#     _type = 'organization'
#     _schema = org_schema
# 
#     def __init__(self, name, *, classification='', parent_id=None,
#                  founding_date='', dissolution_date='', image='',
#                  chamber=None):
#         """
#         Constructor for the Organization object.
#         """
#         super(Organization, self).__init__()
#         self.name = name
#         self.classification = classification
#         self.founding_date = founding_date
#         self.dissolution_date = dissolution_date
#         self.parent_id = pseudo_organization(parent_id, chamber)
#         self.image = image
# 
#     def __str__(self):
#         return self.name
# 
#     def validate(self):
#         schema = None
#         # these are implicitly declared & do not require sources
#         if self.classification in ('party', 'legislature', 'upper', 'lower', 'executive'):
#             schema = org_schema_no_sources
#         return super(Organization, self).validate(schema=schema)
# 
#     def add_post(self, label, role, **kwargs):
#         post = Post(label=label, role=role, organization_id=self._id, **kwargs)
#         self._related.append(post)
#         return post
# 
#     def add_member(self, name_or_person, role='member', **kwargs):
#         if isinstance(name_or_person, Person):
#             membership = Membership(person_id=name_or_person._id,
#                                     person_name=name_or_person.name,
#                                     organization_id=self._id,
#                                     role=role, **kwargs)
#         else:
#             membership = Membership(person_id=_make_pseudo_id(name=name_or_person),
#                                     person_name=name_or_person,
#                                     organization_id=self._id, role=role, **kwargs)
#         self._related.append(membership)
#         return membership
# 
# Path: pupa/exceptions.py
# class UnresolvedIdError(DataImportError):
#     """ Attempt was made to resolve an id that has no result. """
# 
# class SameOrgNameError(DataImportError):
#     """ Attempt was made to import two orgs with the same name. """
# 
#     def __init__(self, name):
#         super(SameOrgNameError, self).__init__('multiple orgs with same name "{}" in Jurisdiction '
#                                                .format(name))


import pytest
from opencivicdata.core.models import Organization, Jurisdiction, Division
from pupa.scrape import Organization as ScrapeOrganization
from pupa.importers import OrganizationImporter
from pupa.exceptions import UnresolvedIdError, SameOrgNameError


@pytest.mark.django_db
def test_deduplication_error_overlaps():
    create_jurisdictions()

    Organization.objects.create(name='World Wrestling Federation',
                                classification='international',
                                jurisdiction_id='jid1')
    wildlife = Organization.objects.create(name='World Wildlife Fund',
                                           classification='international',
                                           jurisdiction_id='jid1')
    wildlife.other_names.create(name='WWF')

    org = ScrapeOrganization('World Wrestling Federation', classification='international')
    org.add_name('WWF')
    od = org.as_dict()
    with pytest.raises(SameOrgNameError):
        OrganizationImporter('jid1').import_data([od])


@pytest.mark.django_db
def test_deduplication_overlap_name_distinct_juris():
    create_jurisdictions()

    org_jid_1 = Organization.objects.create(name='World Wrestling Federation',
                                            classification='international',
                                            jurisdiction_id='jid1')
    org_jid_1.other_names.create(name='WWF')


# Path: pupa/scrape/popolo.py
# class Organization(BaseModel, SourceMixin, ContactDetailMixin, LinkMixin, IdentifierMixin,
#                    OtherNameMixin):
#     """
#     A single popolo-style Organization
#     """
# 
#     _type = 'organization'
#     _schema = org_schema
# 
#     def __init__(self, name, *, classification='', parent_id=None,
#                  founding_date='', dissolution_date='', image='',
#                  chamber=None):
#         """
#         Constructor for the Organization object.
#         """
#         super(Organization, self).__init__()
#         self.name = name
#         self.classification = classification
#         self.founding_date = founding_date
#         self.dissolution_date = dissolution_date
#         self.parent_id = pseudo_organization(parent_id, chamber)
#         self.image = image
# 
#     def __str__(self):
#         return self.name
# 
#     def validate(self):
#         schema = None
#         # these are implicitly declared & do not require sources
#         if self.classification in ('party', 'legislature', 'upper', 'lower', 'executive'):
#             schema = org_schema_no_sources
#         return super(Organization, self).validate(schema=schema)
# 
#     def add_post(self, label, role, **kwargs):
#         post = Post(label=label, role=role, organization_id=self._id, **kwargs)
#         self._related.append(post)
#         return post
# 
#     def add_member(self, name_or_person, role='member', **kwargs):
#         if isinstance(name_or_person, Person):
#             membership = Membership(person_id=name_or_person._id,
#                                     person_name=name_or_person.name,
#                                     organization_id=self._id,
#                                     role=role, **kwargs)
#         else:
#             membership = Membership(person_id=_make_pseudo_id(name=name_or_person),
#                                     person_name=name_or_person,
#                                     organization_id=self._id, role=role, **kwargs)
#         self._related.append(membership)
#         return membership
# 
# Path: pupa/exceptions.py
# class UnresolvedIdError(DataImportError):
#     """ Attempt was made to resolve an id that has no result. """
# 
# class SameOrgNameError(DataImportError):
#     """ Attempt was made to import two orgs with the same name. """
# 
#     def __init__(self, name):
#         super(SameOrgNameError, self).__init__('multiple orgs with same name "{}" in Jurisdiction '
#                                                .format(name))


# Path: pupa/tests/importers/test_organization_importer.py
import pytest
from opencivicdata.core.models import Organization, Jurisdiction, Division
from pupa.scrape import Organization as ScrapeOrganization
from pupa.importers import OrganizationImporter
from pupa.exceptions import UnresolvedIdError, SameOrgNameError



@pytest.mark.django_db
def test_deduplication_error_overlaps():
    create_jurisdictions()

    Organization.objects.create(name='World Wrestling Federation',
                                classification='international',
                                jurisdiction_id='jid1')
    wildlife = Organization.objects.create(name='World Wildlife Fund',
                                           classification='international',
                                           jurisdiction_id='jid1')
    wildlife.other_names.create(name='WWF')

    org = ScrapeOrganization('World Wrestling Federation', classification='international')
    org.add_name('WWF')
    od = org.as_dict()
    with pytest.raises(SameOrgNameError):
        OrganizationImporter('jid1').import_data([od])


@pytest.mark.django_db
def test_deduplication_overlap_name_distinct_juris():
    create_jurisdictions()

    org_jid_1 = Organization.objects.create(name='World Wrestling Federation',
                                            classification='international',
                                            jurisdiction_id='jid1')
    org_jid_1.other_names.create(name='WWF')


    org = ScrapeOrganization(name="WWF", classification="international")