Repository: Cloud-CV/EvalAI

Issue:
Modify database seeding method
## Deliverables

- [ ] Modify the seed command to accept an argument `-nc` which denotes the number of challenges that a developer wants to create for testing.
- [ ] Modify the seed script in such a way that it can be run multiple times (right now, if you run it once and then you try to run it again, then it throws an error). 


Discussion:
@Obtin: sure, go ahead. :) 
@Obtin Are you working on this?
@Obtin I see you are working in #1373. Please take one issue at a time. 😃 So that others can work too. 
Hi @Obtin is the seeding exception issue resolved? :smile: 
@Obtin Okay, cool. I can work on the seeding exception if you have other issues to work on. :smile: 
@isht3 if you are working on this, please complete it asap. 
@deshraj I'll finish this right now.

Resolving patch:
diff --git a/apps/base/management/commands/seed.py b/apps/base/management/commands/seed.py
--- a/apps/base/management/commands/seed.py
+++ b/apps/base/management/commands/seed.py
@@ -5,6 +5,10 @@ class Command(BaseCommand):
 
     help = "Seeds the database with random but sensible values."
 
+    def add_arguments(self, parser):
+        parser.add_argument('-nc', nargs='?', default=1, type=int, help='Number of challenges.')
+
     def handle(self, *args, **options):
+        self.nc = options['nc']
         self.stdout.write(self.style.SUCCESS('Starting the database seeder. Hang on...'))
-        call_command('runscript', 'seed', '--settings', 'settings.dev')
+        call_command('runscript', 'seed', '--script-args', self.nc)
diff --git a/scripts/seed.py b/scripts/seed.py
--- a/scripts/seed.py
+++ b/scripts/seed.py
@@ -31,7 +31,7 @@
 def check_database():
     if len(EmailAddress.objects.all()) > 0:
         print("Are you sure you want to wipe the existing development database and reseed it? (Y/N)")
-        if raw_input().lower() == "y":
+        if settings.TEST or raw_input().lower() == "y":
             destroy_database()
         else:
             return False
@@ -102,7 +102,7 @@ def create_challenge_host_team(user):
     return team
 
 
-def create_challenges(number_of_challenges=3, host_team=None):
+def create_challenges(number_of_challenges, host_team=None):
     """
     Creates past challenge, on-going challenge and upcoming challenge.
     """
@@ -114,13 +114,13 @@ def create_challenges(number_of_challenges=3, host_team=None):
                              host_team
                              )
         elif (i % 3 == 1):
-            create_challenge("{} Challenge" % (fake.first_name()),
+            create_challenge("{} Challenge".format(fake.first_name()),
                              timezone.now() - timedelta(days=500),
                              timezone.now() - timedelta(days=100),
                              host_team
                              )
         elif (i % 3 == 2):
-            create_challenge("{} Challenge" % (fake.first_name()),
+            create_challenge("{} Challenge".format(fake.first_name()),
                              timezone.now() + timedelta(days=100),
                              timezone.now() + timedelta(days=500),
                              host_team
@@ -242,7 +242,8 @@ def create_participant_team(user):
     return team
 
 
-def run():
+def run(*args):
+    NUMBER_OF_CHALLENGES = int(args[0])
     status = check_database()
     if status is False:
         print("Seeding aborted.")


Test patch:
diff --git a/settings/test.py b/settings/test.py
--- a/settings/test.py
+++ b/settings/test.py
@@ -21,3 +21,5 @@
         'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
     }
 }
+
+TEST = True
diff --git a/tests/unit/base/test_utils.py b/tests/unit/base/test_utils.py
--- a/tests/unit/base/test_utils.py
+++ b/tests/unit/base/test_utils.py
@@ -16,6 +16,8 @@
 from jobs.models import Submission
 from participants.models import Participant, ParticipantTeam
 
+from scripts import seed
+
 
 class BaseAPITestClass(APITestCase):
 
@@ -107,3 +109,12 @@ def test_random_file_name_with_id(self):
         filepath = obj.__call__(self.submission, self.test_file_path)
         expected = "submission_files/submission_{}/{}".format(self.submission.pk, filepath.split('/')[2])
         self.assertEqual(filepath, expected)
+
+
+class TestSeeding(BaseAPITestClass):
+
+    def test_if_seeding_works(self):
+        seed.run(1)
+        self.assertEqual(Challenge.objects.all().count(), 1)
+        seed.run(2)
+        self.assertEqual(Challenge.objects.all().count(), 2)