Commits

Caleb Smith committed c2e3938

Additions to tests for check_entries command

  • Participants
  • Parent commits 844873f

Comments (0)

Files changed (2)

File timepiece/management/commands/check_entries.py

         """
         main()
         """
-        self.verbosity = kwargs.get('verbosity', 1)
+        verbosity = kwargs.get('verbosity', 1)
         start = self.find_start(**kwargs)
         people = self.find_people(*args)
         self.show_init(start, *args, **kwargs)
         all_entries = self.find_entries(people, start, *args, **kwargs)
         all_overlaps = self.check_all(all_entries, *args, **kwargs)
-        if self.verbosity >= 1:
+        if verbosity >= 1:
             print 'Total overlapping entries: %d' % all_overlaps
 
     def check_all(self, all_entries, *args, **kwargs):
         """
         With a list of entries, check each entry against every other
         """
+        verbosity = kwargs.get('verbosity', 1)
         user_total_overlaps = 0
         user = ''
         for index_a, entry_a in enumerate(entries):
             #Show the name the first time through
             if index_a == 0:
-                if args and self.verbosity >= 1 or self.verbosity >= 2:
+                if args and verbosity >= 1 or verbosity >= 2:
                     self.show_name(entry_a.user)
                     user = entry_a.user
             if entry_a.is_overlapping():
 #                    user_total_overlaps += 1            
 #                    self.show_overlap(entry_a, entry_b)
                     
-        if user_total_overlaps and user and self.verbosity >= 1:
+        if user_total_overlaps and user and verbosity >= 1:
             overlap_data = {
                 'first': user.first_name,
                 'last': user.last_name,
         month = kwargs.get('month', False)
         year = kwargs.get('year', False)
         days = kwargs.get('days', 0)
-        #If no flags are True, set to 2 months ago
-        start = datetime.datetime.now() - datetime.timedelta(weeks=8)
+        #If no flags are True, set to the beginning of last billing window
+        #to assure we catch all recent violates
+        start = datetime.datetime.now() - relativedelta(months=1, day=1)
         #Set the start date based on arguments provided through options
         if week:
             start = utils.get_week_start()
             start = datetime.datetime.now() - relativedelta(day=1, month=1)
         if days:
             start = datetime.datetime.now() - \
-            datetime.timedelta(days=self.options.days)
+            datetime.timedelta(days=days)
         start = start - relativedelta(
             hour=0, minute=0, second=0, microsecond=0)
         return start
     #output methods
     def show_init(self, start, *args, **kwargs):
         forever = kwargs.get('all', False)
+        verbosity = kwargs.get('verbosity', 1)
         if forever:
-            if self.verbosity >= 1:
+            if verbosity >= 1:
                 print 'Checking overlaps from the beginning ' + \
                     'of time'
         else:
-            if self.verbosity >= 1:
+            if verbosity >= 1:
                 print 'Checking overlap starting on: ' + \
                     start.strftime('%m/%d/%Y')
 

File timepiece/tests/management.py

 import datetime
 import re
 from StringIO import StringIO
+from dateutil.relativedelta import relativedelta
 
 from django.core.urlresolvers import reverse
 from django.contrib.auth.models import User, Permission
 
 from timepiece import models as timepiece
 from timepiece import forms as timepiece_forms
+from timepiece import utils
 from timepiece.management.commands import check_entries
 
 
         self.create_entry(data)
 
     #tests
+    def testFindStart(self):
+        #Establish some datetimes
+        now = datetime.datetime.now()
+        today = now - relativedelta(
+            hour=0, minute=0, second=0, microsecond=0)
+        last_billing = today - relativedelta(months=1, day=1)
+        yesterday = today - relativedelta(days=1)
+        ten_days_ago = today - relativedelta(days=10)
+        thisweek = utils.get_week_start(today)
+        thismonth = today - relativedelta(day=1)
+        thisyear = today - relativedelta(month=1, day=1)
+        #Use command flags to obtain datetimes
+        start_default = check_entries.Command().find_start()
+        start_yesterday = check_entries.Command().find_start(days=1)
+        start_ten_days_ago = check_entries.Command().find_start(days=10)
+        start_of_week = check_entries.Command().find_start(week=True)
+        start_of_month = check_entries.Command().find_start(month=True)
+        start_of_year = check_entries.Command().find_start(year=True)
+        #assure the returned datetimes are correct
+        self.assertEqual(start_default, last_billing)
+        self.assertEqual(start_yesterday, yesterday)
+        self.assertEqual(start_ten_days_ago, ten_days_ago)
+        self.assertEqual(start_of_week, thisweek)
+        self.assertEqual(start_of_month, thismonth)
+        self.assertEqual(start_of_year, thisyear)
+
     def testFindPeople(self):
         #Find one person by icontains first or last name, return all if no args
         people1 = check_entries.Command().find_people('firsT1')
         self.assertEqual(all_1, person1)
         self.assertEqual(all_2, person2)
         self.assertEqual(all_3, self.superuser)
-        
+
+    def testFindEntries(self):
+        start = check_entries.Command().find_start()
+        all_people = check_entries.Command().find_people()
+        entries = check_entries.Command().find_entries(all_people, start)
+        self.make_entry(valid=False)
+        all_overlaps = 0
+        while True:
+            try:
+                person_entries = entries.next()
+            except StopIteration:
+                print all_overlaps
+                return
+            else:
+                user_total_overlaps = check_entries.Command().check_entry(
+                    person_entries, verbosity=2)
+                all_overlaps += user_total_overlaps
+
     def testCheckOverlap(self):
         #define start and end times relative to a valid entry
         a_start_before = self.good_start - datetime.timedelta(minutes=5)
         for index_a, entry_a in enumerate(entries):
             for index_b in range(index_a, len(entries)):
                 entry_b = entries[index_b]
-                if entry_a.check_overlap(entry_b):
-                    print 'Conflict with %s and %s' % (entry_a, entry_b)
+                if entry_a.check_overlap(entry_b):                    
                     user_total_overlaps += 1
+#                    print 'Conflict with %s and %s' % (entry_a, entry_b)
+
 #                    check_entries.Command().show_overlap(entry_a, entry_b)
 #                    self.show_overlap(entry_a, entry_b)
 
-
-
-#Use:
-#check_entries.Command().method()
-        
         
 #    def testAllEntries(self):
 #        self.default_data.update({