From 2d770b6cb559c2120224ed25a3ae0ba88976aa7d Mon Sep 17 00:00:00 2001 From: Linz Jax Date: Thu, 31 Mar 2016 20:06:47 -0400 Subject: [PATCH] cleaned up test data --- cadasta/core/factories.py | 215 +++++++++--------- .../core/management/commands/loadfixtures.py | 55 ++++- 2 files changed, 155 insertions(+), 115 deletions(-) diff --git a/cadasta/core/factories.py b/cadasta/core/factories.py index 25f59f6ac..37325207e 100644 --- a/cadasta/core/factories.py +++ b/cadasta/core/factories.py @@ -4,15 +4,11 @@ from datetime import datetime, timezone from accounts.models import User -from organization.models import Organization -from tutelary.models import Policy, Role -from accounts.tests.factories import UserFactory -from organization.tests.factories import OrganizationFactory # ProjectsFactory - +from organization.models import Organization, Project +from tutelary.models import Policy, Role, PolicyInstance, RolePolicyAssign -class RoleFactory(factory.django.DjangoModelFactory): - class Meta: - model = Role +from accounts.tests.factories import UserFactory +from organization.tests.factories import OrganizationFactory, ProjectFactory class PolicyFactory(factory.django.DjangoModelFactory): @@ -30,27 +26,31 @@ def _adjust_kwargs(cls, **kwargs): return kwargs +class RoleFactory(factory.django.DjangoModelFactory): + class Meta: + model = Role + + class FixturesData(): def add_test_users(self): + users = [] + # the first two named users will have superuser access named_users = [ {'username': 'iross', 'email': 'iross@cadasta.org', 'first_name': 'Ian', 'last_name': 'Ross'}, {'username': 'oroick', 'email': 'oroick@cadasta.org', - 'first_name': 'Oliver', 'last_name': 'Roick'} - ] - named_users.append({ - 'username': 'testuserhb', - 'email': 'hbuser@example.com', + 'first_name': 'Oliver', 'last_name': 'Roick'}] + # add user's with names in languages that need to be tested. + languages = ['el_GR', 'ja_JP', 'hi_IN', 'hr_HR', 'lt_LT'] + lang_users = [{ 'first_name': 'עזרא', 'last_name': 'ברש' - }) - users = [] - languages = ['el_GR', 'ja_JP', 'hi_IN', 'hr_HR', 'lt_LT'] + }] for lang in languages: fake = Factory.create(lang) - named_users.append({ - 'username': 'testuser%s' % lang, - 'email': '%suser@example.com' % lang, + lang_users.append({ + 'username': 'testuser{}'.format(lang), + 'email': '{}user@example.com'.format(lang), 'first_name': fake.first_name(), 'last_name': fake.last_name() }) @@ -63,7 +63,7 @@ def add_test_users(self): last_login=datetime.now(tz=timezone.utc), is_active=True, )) - else: + elif n < len(named_users) + len(lang_users): users.append(UserFactory.create( password='password', email_verified=True, @@ -74,7 +74,6 @@ def add_test_users(self): self.stdout.write(self.style.SUCCESS('Successfully added test users.')) return users - # these are being applied but not listed in user objects. def add_test_users_and_roles(self): users = FixturesData.add_test_users(self) @@ -95,108 +94,102 @@ def add_test_users_and_roles(self): roles[org + '-' + abbrev] = RoleFactory.create( name=org + '-' + abbrev, policies=[pols['default'], pols[pol]], - variables={'organization': org} - ) + variables={'organization': org}) users[0].assign_policies(roles['superuser']) users[1].assign_policies(roles['superuser']) users[2].assign_policies(roles['cadasta-oa']) users[3].assign_policies(roles['habitat-for-humanity-oa']) - users[4].assign_policies(roles['habitat-for-humanity-oa'], roles['cadasta-oa']) + users[4].assign_policies(roles['habitat-for-humanity-oa'], + roles['cadasta-oa']) + + self.stdout.write(self.style.SUCCESS( + "\n{} and {} have superuser policies." + .format(users[0], users[1]))) + + self.stdout.write(self.style.SUCCESS( + "\n{} and {} have cadasta-oa policies." + .format(users[2], users[4]))) self.stdout.write(self.style.SUCCESS( - '\n%s and %s have superuser policies.\n%s and %s have cadasta-oa policies.\n%s and %s have habitat-for-humanity-oa policies.' % (users[0], users[1], users[2], users[4], users[3], users[4]))) + "\n{} and {} have habitat-for-humanity-oa policies." + .format(users[3], users[4]))) def add_test_organizations(self): orgs = [] - named_orgs = [{ - 'name': 'Test: Habitat for Humanity', - 'slug': 'habitat-for-humanity', - 'description': """Habitat for Humanity is a nonprofit, ecumenical Christian ministry - that builds with people in need regardless of race or religion. Since - 1976, Habitat has helped 6.8 million people find strength, stability - and independence through safe, decent and affordable shelter.""", - 'urls': ['http://www.habitat.org'], - 'contacts': [{'email': 'info@habitat.org'}], - 'add_users': None - }, { - 'name': 'Test: Cadasta', - 'slug': 'cadasta', - 'description': """Cadasta Foundation is dedicated to the support, continued - development and growth of the Cadasta Platform – an innovative, open - source suite of tools for the collection and management of ownership, - occupancy, and spatial data that meets the unique challenges of this - process in much of the world.""", - 'urls': ['http://www.cadasta.org'], - 'contacts': [{'email': 'info@cadasta.org'}], - 'add_users': User.objects.filter(username__startswith='testuser') - }] - for org in named_orgs: + users = User.objects.filter(username__startswith='testuser') + + for n in range(3): orgs.append(OrganizationFactory.create( - name=org['name'], - slug=org['slug'], - description=org['description'], - urls=org['urls'], - contacts=org['contacts'], - add_users=org['add_users'] + add_users=users )) - self.stdout.write(self.style.SUCCESS('\nSuccessfully added organizations "%s"' % Organization.objects.all())) - - # Uncomment once projects have been merged. - - # def add_test_projects(self): - # projs = [] - # orgs = Organization.objects.all() - # projs.append(ProjectFactory.create( - # name='Kibera', - # slug='kibera', - # description="""This is a test project. This is a test project. This is a test - # project. This is a test project. This is a test project. This is a - # test project. This is a test project. This is a test project. This - # is a test project.""", - # organization=orgs[0], - # country='KE' - # )) - # projs.append(ProjectFactory.create( - # name='H4H Test Project', - # slug='h4h-test-project', - # description="""This is a test project. This is a test project. This is a test - # project. This is a test project. This is a test project. This is a - # test project. This is a test project. This is a test project. This - # is a test project.""", - # organization=orgs[0], - # country='PH' - # )) - # projs.append(ProjectFactory.create( - # name='Cadasta Indonesia Test Project', - # slug='cadasta-indonesia-test-project', - # description="""This is another test project. This is another test project. This - # is another test project. This is another test project. This is - # another test project. This is a test project. This is another test - # project. This is another test project. This is another test - # project.""", - # organization=orgs[1], - # country='ID' - # )) - # projs.append(ProjectFactory.create( - # name='Cadasta Myanmar Test Project', - # slug='cadasta-myanmar-test-project', - # description="""This is another test project. This is another test project. This - # is another test project. This is another test project. This is - # another test project. This is a test project. This is another test - # project. This is another test project. This is another test - # project.""", - # organization=orgs[1], - # country='MM' - # )) + self.stdout.write(self.style.SUCCESS( + '\nSuccessfully added organizations {}' + .format(Organization.objects.all()))) + + def add_test_projects(self): + projs = [] + if Organization.objects.all().exists(): + orgs = Organization.objects.all() + else: + orgs = OrganizationFactory.create_batch(2) + projs.append(ProjectFactory.create( + name='Kibera', + project_slug='kibera', + description="""This is a test project. This is a test project. + This is a test project. This is a test project. This is a test + project. This is a test project. This is a test project. This + is a test project. This is a test project.""", + organization=orgs[0], + country='KE' + )) + projs.append(ProjectFactory.create( + name='H4H Test Project', + project_slug='h4h-test-project', + description="""This is a test project. This is a test project. + This is a test project. This is a test project. This is a test + project. This is a test project. This is a test project. This + is a test project. This is a test project.""", + organization=orgs[0], + country='PH' + )) + projs.append(ProjectFactory.create( + name='Cadasta Indonesia Test Project', + project_slug='cadasta-indonesia-test-project', + description="""This is another test project. This is another test + project. This is another test project. This is another test + project. This is another test project. This is a test project. + This is another test project. This is another test project. + This is another test project.""", + organization=orgs[1], + country='ID' + )) + projs.append(ProjectFactory.create( + name='Cadasta Myanmar Test Project', + project_slug='cadasta-myanmar-test-project', + description=""""This is another test project. This is another test + project. This is another test project. This is another test + project. This is another test project. This is a test project. + This is another test project. This is another test project. + This is another test project.""", + organization=orgs[1], + country='MM' + )) def delete_test_organizations(self): - orgs = Organization.objects.filter(name__startswith='Test:') + orgs = Organization.objects.filter(name__startswith='Organization #') for org in orgs: org.delete() - self.stdout.write(self.style.SUCCESS('Successfully deleted all test organizations. Remaining organizations: "%s"' % Organization.objects.all())) + PolicyInstance.objects.all().delete() + RolePolicyAssign.objects.all().delete() + Policy.objects.all().delete() + + self.stdout.write(self.style.SUCCESS( + """Successfully deleted all test organizations. + Remaining organizations: {}""" + .format(Organization.objects.all()))) def delete_test_users(self): users = User.objects.filter(username__startswith='testuser') @@ -208,4 +201,14 @@ def delete_test_users(self): if User.objects.filter(username=user).exists(): User.objects.get(username=user).delete() - self.stdout.write(self.style.SUCCESS('Successfully deleted test users. Remaining users: "%s"' % User.objects.all())) + self.stdout.write(self.style.SUCCESS( + 'Successfully deleted test users. Remaining users: {}' + .format(User.objects.all()))) + + def delete_test_projects(self): + projs = Project.objects.filter(name__contains='Test Project') + projs.delete() + + self.stdout.write(self.style.SUCCESS( + 'Successfully deleted test projects. Remaining users: {}' + .format(User.objects.all()))) diff --git a/cadasta/core/management/commands/loadfixtures.py b/cadasta/core/management/commands/loadfixtures.py index c8b09e7af..9d26bfb33 100644 --- a/cadasta/core/management/commands/loadfixtures.py +++ b/cadasta/core/management/commands/loadfixtures.py @@ -1,47 +1,77 @@ from django.core.management.base import BaseCommand -from accounts.models import User -from organization.models import Organization from core.factories import FixturesData class Command(BaseCommand): - help = 'Load in all test data for organizations, users, roles, and projects.' + help = """Loads in test data for + organizations, users, roles, + and projects. Using loadfixtures + without any additional arguments + will load in all test data.""" def add_arguments(self, parser): parser.add_argument('--users', action='store_true', dest='users', default=False, - help='Load in just the test users, and nothing else.' + help="""Load in just + the test users, and + nothing else.""" ) parser.add_argument('--organizations', action='store_true', dest='organizations', default=False, - help='Load in just the test organizations, and nothing else.' + help="""Load in just + the test + organizations, + and nothing else.""" + ) + + parser.add_argument('--projects', + action='store_true', + dest='projects', + default=False, + help="""Load in just + the test projects, + and nothing else.""" ) parser.add_argument('--delete-users', action='store_true', dest='delete-users', default=False, - help='Delete test users, but leave everything else.' + help="""Delete test + users, but leave + everything else.""" ) parser.add_argument('--delete-organizations', action='store_true', dest='delete-organizations', default=False, - help='Delete test organizations, but leave the users.' + help="""Delete test + organizations, but + leave the users.""" + ) + + parser.add_argument('--delete-projects', + action='store_true', + dest='delete-projects', + default=False, + help="""Delete test + projects, but leave + everything else.""" ) parser.add_argument('--delete', action='store_true', dest='delete', default=False, - help='Delete all of the test data.' + help="""Delete all of + the test data.""" ) def handle(self, *args, **options): @@ -52,18 +82,25 @@ def handle(self, *args, **options): elif options['organizations']: data.add_test_organizations(self) + elif options['projects']: + data.add_test_projects(self) + elif options['delete-organizations']: data.delete_test_organizations(self) elif options['delete-users']: data.delete_test_users(self) + elif options['delete-projects']: + data.delete_test_projects(self) + elif options['delete']: data.delete_test_users(self) data.delete_test_organizations(self) + data.delete_test_projects(self) else: data.add_test_users_and_roles(self) data.add_test_organizations(self) - # data.add_test_projects(self) + data.add_test_projects(self) self.stdout.write(self.style.SUCCESS("All test data loaded."))