Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_to_dict(self):
"""Test output of to_dict() method"""
uid = UniqueIdentity(uuid='John Smith')
self.session.add(uid)
c = Country(code='US', name='United States of America', alpha3='USA')
self.session.add(c)
prf = Profile(uuid='John Smith', name='Smith, J.',
email='jsmith@example.com', is_bot=True,
country_code='US')
self.session.add(prf)
self.session.commit()
# Tests
d = prf.to_dict()
self.assertIsInstance(d, dict)
self.assertEqual(d['uuid'], 'John Smith')
self.assertEqual(d['name'], 'Smith, J.')
self.assertEqual(d['email'], 'jsmith@example.com')
def test_unique_countries(self):
"""Check whether countries are unique"""
with self.assertRaisesRegex(IntegrityError, DUP_CHECK_ERROR):
c1 = Country(code='ES', name='Spain', alpha3='ESP')
self.session.add(c1)
c2 = Country(code='ES', name='España', alpha3='E')
self.session.add(c2)
self.session.commit()
def test_get_countries(self):
"""Check if it returns the list of countries"""
with self.db.connect() as session:
us = Country(code='US', name='United States of America', alpha3='USA')
es = Country(code='ES', name='Spain', alpha3='ESP')
gb = Country(code='GB', name='United Kingdom', alpha3='GBR')
session.add(es)
session.add(us)
session.add(gb)
cs = api.countries(self.db)
self.assertEqual(len(cs), 3)
c0 = cs[0]
self.assertIsInstance(c0, Country)
self.assertEqual(c0.code, 'ES')
self.assertEqual(c0.name, 'Spain')
self.assertEqual(c0.alpha3, 'ESP')
c1 = cs[1]
self.assertIsInstance(c1, Country)
def test_get_countries(self):
"""Check if it returns the list of countries"""
with self.db.connect() as session:
us = Country(code='US', name='United States of America', alpha3='USA')
es = Country(code='ES', name='Spain', alpha3='ESP')
gb = Country(code='GB', name='United Kingdom', alpha3='GBR')
session.add(es)
session.add(us)
session.add(gb)
cs = api.countries(self.db)
self.assertEqual(len(cs), 3)
c0 = cs[0]
self.assertIsInstance(c0, Country)
self.assertEqual(c0.code, 'ES')
self.assertEqual(c0.name, 'Spain')
self.assertEqual(c0.alpha3, 'ESP')
c1 = cs[1]
def test_merge_identities_and_swap_profile(self):
"""Test swap of profiles when a unique identity does not have one"""
# Add some countries, unique identities, identities and
# enrollments first
with self.db.connect() as session:
# Add a country
us = Country(code='US', name='United States of America', alpha3='USA')
session.add(us)
api.add_unique_identity(self.db, 'John Smith')
api.edit_profile(self.db, 'John Smith', name='John Smith', is_bot=True,
country_code='US')
api.add_unique_identity(self.db, 'Jane Rae')
# Merge John Smith and Jane Rae unique identities
# John Smith profile should be swapped to Jane Rae
api.merge_unique_identities(self.db, 'John Smith', 'Jane Rae')
with self.db.connect() as session:
uidentities = session.query(UniqueIdentity).all()
self.assertEqual(len(uidentities), 1)
def test_not_found(self):
"""Check whether it raises an error when the country is not available"""
# It should raise an error when the registry is empty
self.assertRaises(NotFoundError, api.countries, self.db, 'ES')
# It should do the same when there are some orgs available
with self.db.connect() as session:
us = Country(code='US', name='United States of America', alpha3='USA')
es = Country(code='ES', name='Spain', alpha3='ESP')
gb = Country(code='GB', name='United Kingdom', alpha3='GBR')
session.add(es)
session.add(us)
session.add(gb)
self.assertRaises(NotFoundError, api.countries, self.db, 'GR')
self.assertRaises(NotFoundError, api.countries, self.db, None, 'Greece')
self.assertRaises(NotFoundError, api.countries, self.db, 'GR', 'Greece')
def test_edit_new_profile(self):
"""Check if it creates an new profile"""
api.add_unique_identity(self.db, 'John Smith')
with self.db.connect() as session:
# Add a country
us = Country(code='US', name='United States of America', alpha3='USA')
session.add(us)
# There are not profiles for the given uuid yet
prf = session.query(Profile).\
filter(Profile.uuid == 'John Smith').first()
self.assertEqual(prf, None)
# Add the new profile
api.edit_profile(self.db, 'John Smith', name='Smith, J.', email='',
is_bot=True, country_code='US')
with self.db.connect() as session:
uid = session.query(UniqueIdentity).\
filter(UniqueIdentity.uuid == 'John Smith').first()
prf = uid.profile
def test_unique_identities(self):
"""Check if it returns the registry of unique identities"""
# Add a country
with self.db.connect() as session:
us = Country(code='US', name='United States of America', alpha3='USA')
session.add(us)
# Add some identities
jsmith_uuid = api.add_identity(self.db, 'scm', 'jsmith@example.com',
'John Smith', 'jsmith')
api.add_identity(self.db, 'scm', 'jsmith@bitergia.com', uuid=jsmith_uuid)
api.add_identity(self.db, 'mls', 'jsmith@bitergia.com', uuid=jsmith_uuid)
api.edit_profile(self.db, jsmith_uuid, email='jsmith@example.com',
is_bot=True, country_code='US')
jdoe_uuid = api.add_identity(self.db, 'scm', 'jdoe@example.com',
'John Doe', 'jdoe')
api.add_identity(self.db, 'scm', 'jdoe@libresoft.es', uuid=jdoe_uuid)
# Tests
def test_merge_identitites(self):
"""Test behavior merging unique identities"""
# Add some countries, unique identities, identities and
# enrollments first
with self.db.connect() as session:
# Add a country
us = Country(code='US', name='United States of America', alpha3='USA')
session.add(us)
api.add_unique_identity(self.db, 'John Smith')
api.add_identity(self.db, 'scm', 'jsmith@example.com',
uuid='John Smith')
api.add_identity(self.db, 'scm', 'jsmith@example.com', 'John Smith',
uuid='John Smith')
api.edit_profile(self.db, 'John Smith', name='John Smith', is_bot=True,
country_code='US')
api.add_unique_identity(self.db, 'John Doe')
api.add_identity(self.db, 'scm', 'jdoe@example.com',
uuid='John Doe')
api.edit_profile(self.db, 'John Doe', email='jdoe@example.com', is_bot=False)
api.add_unique_identity(self.db, 'Jane Rae')
def __read_countries_file(self):
"""Read countries from a CSV file"""
import csv
import pkg_resources
filename = pkg_resources.resource_filename('sortinghat', 'data/countries.csv')
with open(filename, 'r') as f:
reader = csv.DictReader(f, fieldnames=['name', 'code', 'alpha3'])
countries = [Country(**c) for c in reader]
return countries