@@ -9,44 +9,38 @@
# Create your views here.
# vi: tabstop=8 expandtab shiftwidth=4 softtabstop=4
-from django.core.management.base import BaseCommand, CommandError
-from Post.models import Build, BuildFailure
-from optparse import make_option
+from django.core.management.base import BaseCommand
+from Post.models import Build
import time
import sys
class Command(BaseCommand):
help = 'Culls the database to size in rows'
- args = '<size>'
- option_list = BaseCommand.option_list + (
- make_option('-i',
- '--info',
- dest='info',
- action="store_true",
- help='Show the current database size'),
- )
+
+ def add_arguments(self, parser):
+ parser.add_argument('size', nargs='?', type=int,
+ help='Number of builds to keep')
+ parser.add_argument('-i',
+ '--info',
+ action="store_true",
+ help='Show the current database size')
def handle(self, *args, **options):
count = Build.objects.count()
if options['info']:
- print "Current builds table size: %d" % Build.objects.count()
+ print("Current builds table size: %d" % Build.objects.count())
return
- if len(args) > 0 and args[0]:
- try:
- new_size = int(args[0])
- except ValueError:
- print "Not a valid size"
- return
-
+ if options['size'] is not None:
+ new_size = options['size']
num_to_delete = count - new_size
- print "\nReducing the database size to %d which will DELETE %d rows" % (new_size, num_to_delete)
+ print("\nReducing the database size to %d which will DELETE %d rows" % (new_size, num_to_delete))
i = 1
while i != 0:
i = i-1
- print 'Ctrl+c TO CANCEL. Executing in... %d \r' % i,
+ print('Ctrl+c TO CANCEL. Executing in... %d \r' % i, end='')
sys.stdout.flush()
time.sleep(1)
@@ -20,7 +20,7 @@ def main():
#print query
items = BuildFailure.objects.raw(query)
for item in items:
- print "Deleting: ", item.id
+ print("Deleting: ", item.id)
item.delete()
if __name__ == "__main__":
@@ -1,5 +1,7 @@
import urllib.request, urllib.parse, urllib.error
import json
+from unittest.mock import patch
+from django.core.management import call_command
import re
from django.test import Client, TestCase, override_settings
from Post.models import BuildFailure, Build
@@ -385,3 +387,22 @@ class LinkBackSubmissionTest(TestCase):
bf = self._submit("http://example.com/build/42")
self.assertEqual(bf.BUILD.LINK_BACK, "http://example.com/build/42")
+
+
+@patch("Post.management.commands.culldb.time.sleep")
+class CullDbTest(TestCase):
+ def setUp(self):
+ for _ in range(3):
+ Build.objects.create()
+
+ def test_culls_to_size(self, _sleep):
+ call_command("culldb", "1")
+ self.assertEqual(Build.objects.count(), 1)
+
+ def test_zero_culls_everything(self, _sleep):
+ call_command("culldb", "0")
+ self.assertEqual(Build.objects.count(), 0)
+
+ def test_info_deletes_nothing(self, _sleep):
+ call_command("culldb", "--info")
+ self.assertEqual(Build.objects.count(), 3)
Both used Python 2 print and failed with SyntaxError. culldb also used option_list and optparse, removed in Django 1.10; declare the same arguments with add_arguments(). Add culldb tests. Found by ruff. AI-Generated: Claude Opus 5.5 Signed-off-by: Tim Orling <tim.orling@konsulko.com> --- Post/management/commands/culldb.py | 36 +++++++++++++----------------- Post/purge.py | 2 +- Post/test.py | 21 +++++++++++++++++ 3 files changed, 37 insertions(+), 22 deletions(-)