# 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_prevents_identical():
    create_jurisdictions()
    org1 = ScrapeOrganization('United Nations', classification='international')
    org2 = ScrapeOrganization('United Nations', classification='international',
                              founding_date='1945')
    OrganizationImporter('jid1').import_data([org1.as_dict()])
    assert Organization.objects.count() == 1

    OrganizationImporter('jid1').import_data([org2.as_dict()])
    assert Organization.objects.count() == 1


@pytest.mark.django_db
def test_pseudo_ids():
    create_jurisdictions()
    wild = Organization.objects.create(id='1', name='Wild', classification='party')
    senate = Organization.objects.create(id='2', name='Senate', classification='upper',
                                         jurisdiction_id='jid1')
    house = Organization.objects.create(id='3', name='House', classification='lower',
                                        jurisdiction_id='jid1')
    un = Organization.objects.create(id='4', name='United Nations', classification='international',
                                     jurisdiction_id='jid2')

    oi1 = OrganizationImporter('jid1')
    assert oi1.resolve_json_id('~{"classification":"upper"}') == senate.id
    assert oi1.resolve_json_id('~{"classification":"lower"}') == house.id
    assert oi1.resolve_json_id('~{"classification":"party", "name":"Wild"}') == wild.id


# 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_prevents_identical():
    create_jurisdictions()
    org1 = ScrapeOrganization('United Nations', classification='international')
    org2 = ScrapeOrganization('United Nations', classification='international',
                              founding_date='1945')
    OrganizationImporter('jid1').import_data([org1.as_dict()])
    assert Organization.objects.count() == 1

    OrganizationImporter('jid1').import_data([org2.as_dict()])
    assert Organization.objects.count() == 1


@pytest.mark.django_db
def test_pseudo_ids():
    create_jurisdictions()
    wild = Organization.objects.create(id='1', name='Wild', classification='party')
    senate = Organization.objects.create(id='2', name='Senate', classification='upper',
                                         jurisdiction_id='jid1')
    house = Organization.objects.create(id='3', name='House', classification='lower',
                                        jurisdiction_id='jid1')
    un = Organization.objects.create(id='4', name='United Nations', classification='international',
                                     jurisdiction_id='jid2')

    oi1 = OrganizationImporter('jid1')
    assert oi1.resolve_json_id('~{"classification":"upper"}') == senate.id
    assert oi1.resolve_json_id('~{"classification":"lower"}') == house.id
    assert oi1.resolve_json_id('~{"classification":"party", "name":"Wild"}') == wild.id


    with pytest.raises(UnresolvedIdError):