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_import_to_non_empty_registry(self):
"""Test load (and overwrite) process in a registry with some contents"""
# First, load some data
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.net')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
# Import new data, overwriting existing relationships
parser = self.get_parser(datadir('sortinghat_orgs_valid_alt.json'))
self.cmd.import_organizations(parser, True)
# Check the contents of the registry
orgs = api.registry(self.db)
self.assertEqual(len(orgs), 4)
# Bitergia
org = orgs[0]
def test_not_found_domain(self):
"""Check whether it raises an error when the domain is not available"""
# It should raise an error when the registry is empty
self.assertRaises(NotFoundError, api.domains, self.db, 'Example')
self.assertRaises(NotFoundError, api.domains, self.db, 'Example', True)
# Add some domains
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'es.example.com')
api.add_domain(self.db, 'Example', 'en.example.com')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
# It should fail when there are some domains available
self.assertRaises(NotFoundError, api.domains, self.db, 'libresoft.es')
self.assertRaises(NotFoundError, api.domains, self.db, 'us.example.com')
# Or even when looks for top domains
self.assertRaises(NotFoundError, api.domains, self.db, 'libresoft.es', True)
self.assertRaises(NotFoundError, api.domains, self.db, 'myexample.com', True)
self.assertRaises(NotFoundError, api.domains, self.db, '.myexample.com', True)
def test_get_domains(self):
"""Check if it returns the registry of domains"""
# Add some domains
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'u.example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'es.u.example.com')
api.add_domain(self.db, 'Example', 'en.u.example.com')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_domain(self.db, 'Bitergia', 'bitergia.org')
api.add_organization(self.db, 'LibreSoft')
doms = api.domains(self.db)
self.assertEqual(len(doms), 6)
dom0 = doms[0]
self.assertIsInstance(dom0, Domain)
self.assertEqual(dom0.domain, 'bitergia.com')
self.assertEqual(dom0.organization.name, 'Bitergia')
dom1 = doms[1]
def test_invalid_type_top_domain(self):
"""Check type values of top domain flag"""
self.assertRaisesRegexp(ValueError, TOP_DOMAIN_VALUE_ERROR,
api.add_domain, self.db, 'Example', 'example.com', 1)
self.assertRaisesRegexp(ValueError, TOP_DOMAIN_VALUE_ERROR,
api.add_domain, self.db, 'Example', 'example.com', 'False')
def test_delete_domains(self):
"""Check whether it deletes a set of domains"""
# First, add a set of organizations, including some domains
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com')
api.add_domain(self.db, 'Example', 'example.org')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_organization(self.db, 'LibreSoft')
# Delete some domains
api.delete_domain(self.db, 'Example', 'example.org')
api.delete_domain(self.db, 'Bitergia', 'bitergia.com')
with self.db.connect() as session:
doms1 = session.query(Domain).join(Organization).\
filter(Organization.name == 'Example').all()
self.assertEqual(len(doms1), 1)
self.assertEqual(doms1[0].domain, 'example.com')
doms2 = session.query(Domain).join(Organization).\
filter(Organization.name == 'Bitergia').all()
self.assertEqual(len(doms2), 0)
def test_domain(self):
"""Check if it returns the info about an existing domain"""
# Add some domains
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'u.example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'es.u.example.com')
api.add_domain(self.db, 'Example', 'en.u.example.com')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_domain(self.db, 'Bitergia', 'bitergia.org')
api.add_organization(self.db, 'LibreSoft')
# Find the given domain
doms = api.domains(self.db, 'example.com')
self.assertEqual(len(doms), 1)
dom0 = doms[0]
self.assertEqual(dom0.domain, 'example.com')
def test_top_domains(self):
"""Check top domains option"""
# Add some domains
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'u.example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'es.u.example.com')
api.add_domain(self.db, 'Example', 'en.u.example.com')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_domain(self.db, 'Bitergia', 'bitergia.org')
api.add_organization(self.db, 'LibreSoft')
# Look only for top domains
doms = api.domains(self.db, top=True)
self.assertEqual(len(doms), 2)
dom0 = doms[0]
self.assertIsInstance(dom0, Domain)
self.assertEqual(dom0.domain, 'example.com')
self.assertEqual(dom0.organization.name, 'Example')
def test_get_registry(self):
"""Check if it returns the registry of organizations"""
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com')
api.add_domain(self.db, 'Example', 'example.org')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_organization(self.db, 'LibreSoft')
orgs = api.registry(self.db)
self.assertEqual(len(orgs), 3)
org1 = orgs[0]
self.assertIsInstance(org1, Organization)
self.assertEqual(org1.name, 'Bitergia')
self.assertEqual(len(org1.domains), 1)
org2 = orgs[1]
self.assertIsInstance(org2, Organization)
self.assertEqual(org2.name, 'Example')
def test_get_domains(self):
"""Check if it returns the registry of domains"""
# Add some domains
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'u.example.com', is_top_domain=True)
api.add_domain(self.db, 'Example', 'es.u.example.com')
api.add_domain(self.db, 'Example', 'en.u.example.com')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_domain(self.db, 'Bitergia', 'bitergia.org')
api.add_organization(self.db, 'LibreSoft')
doms = api.domains(self.db)
self.assertEqual(len(doms), 6)
dom0 = doms[0]
self.assertIsInstance(dom0, Domain)
self.assertEqual(dom0.domain, 'bitergia.com')
self.assertEqual(dom0.organization.name, 'Bitergia')
def test_get_registry_term(self):
"""Check if it returns the info about orgs using a search term"""
api.add_organization(self.db, 'Example')
api.add_domain(self.db, 'Example', 'example.com')
api.add_domain(self.db, 'Example', 'example.org')
api.add_organization(self.db, 'Bitergia')
api.add_domain(self.db, 'Bitergia', 'bitergia.com')
api.add_organization(self.db, 'My Example')
api.add_domain(self.db, 'My Example', 'myexample.com')
# This query have to return two organizations
orgs = api.registry(self.db, 'Example')
self.assertEqual(len(orgs), 2)
# Example organization
org = orgs[0]
self.assertIsInstance(org, Organization)
self.assertEqual(org.name, 'Example')
self.assertEqual(len(org.domains), 2)
domains = org.domains