def setUp(self): WorkDay.purge() self.work_day = WorkDay(date=arrow.now(), time_in="9:00", time_out="16:30", note="Just Another Day", type=WorkDay.TYPE_NORMAL)
def __gen_work_days(self, start, count): date = arrow.get(start).replace(tzinfo=WorkDay.TIMEZONE) for i in range(0, count): wd = WorkDay(date=date, time_in="09:00", time_out="16:30", note=F"Test WorkDay #{i}") wd.save() date = date.shift(days=+1)
def test_hours(self): self.assertEqual(self.work_day.hours, 7.5) wd = WorkDay(time_in='10:30', time_out='10:31') self.assertEqual(wd.hours, 0.02) wd = WorkDay(time_in='10:30', time_out='11:00') self.assertEqual(wd.hours, 0.5) wd = WorkDay(time_in='10:45', time_out='11:00') self.assertEqual(wd.hours, 0.25) wd = WorkDay(time_in='9:00', time_out='15:40') self.assertEqual(wd.hours, 6.67) wd = WorkDay(time_in='9:05', time_out='17:05') self.assertEqual(wd.hours, 8.0)
def test_range(self): date = arrow.get("2020-01-01").replace(tzinfo=WorkDay.TIMEZONE) for i in range(0, 180): wd = WorkDay(date=date, time_in="09:00", time_out="16:30", note=F"Test WorkDay #{i}") wd.save() date = date.shift(days=+1) self.assertEqual(WorkDay.count(), 180) # Two dates day_range = WorkDay.range('2020-01-01', '2020-01-31') self.assertEqual(len(day_range), 31) self.assertIsInstance(day_range[0], WorkDay) self.assertEqual( day_range[0].date.int_timestamp, arrow.get("2020-01-01").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) self.assertEqual( day_range[30].date.int_timestamp, arrow.get("2020-01-31").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) # Defined range by date and delta days POSITIVE day_range = WorkDay.range('2020-02-19', days=+7) self.assertEqual(len(day_range), 8) # 8 b/c inclusive self.assertIsInstance(day_range[0], WorkDay) self.assertEqual( day_range[0].date.int_timestamp, arrow.get("2020-02-19").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) self.assertEqual( day_range[7].date.int_timestamp, arrow.get("2020-02-26").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) # Defined range by date and delta days NEGATIVE day_range = WorkDay.range('2020-02-19', days=-7) self.assertEqual(len(day_range), 8) # 8 b/c inclusive self.assertIsInstance(day_range[0], WorkDay) self.assertEqual( day_range[0].date.int_timestamp, arrow.get("2020-02-12").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) self.assertEqual( day_range[7].date.int_timestamp, arrow.get("2020-02-19").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp)
def __loadWorkDays(self): # Beginning of year startDate = arrow.get(F"{self.year}-01-01") endDate = arrow.now() if self.year != endDate.year: endDate = arrow.get(F"{self.year}-12-31") days = WorkDay.find( op="and", sort_by="date", type=self.type, date=F"btw:{startDate.int_timestamp}:{endDate.int_timestamp}" ) return days
def prune_unused(tags): entries = LogEntry.fetch() notes = Note.fetch() secrets = Secret.fetch() todos = Todo.fetch() work_days = WorkDay.fetch() unused_tags = [] for tag in tags: used = False for items in (entries, notes, secrets, todos, work_days): used = used_by(tag, items) if used: break if not used: unused_tags.append(tag) for tag in unused_tags: print(F"Pruning {tag.id:04d} - [{tag.name}].") tag.delete()
def range(): resp = None status = 200 try: query_string = request.args.copy() start = query_string.pop('start', None) end = query_string.pop('end', None) days = int(query_string.pop('days', 0)) work_days = [] if start and (end or days): work_days = WorkDay.range(start, end, days) else: raise TypeError( "Error: Requires 'start' and either 'end' or 'days'.") resp = {'work_days': work_days} except Exception as e: status = 500 resp = {"error": str(e)} return jsonify(resp), status
def test_used(self): numDays = 3 pto = self.__test_instance(type="Vacation") # Add a few in-range NORMAL work days for i in range(0, numDays): day = WorkDay(date=arrow.now().shift(days=i * -1), time_in="9:00", time_out="16:30", note=F"Normal #{i}", type=WorkDay.TYPE_NORMAL) day.save() # Add a few in-range Vacation work days for i in range(numDays, numDays + numDays): day = WorkDay(date=arrow.now().shift(days=i * (-1)), time_in="00:00", time_out="00:00", note=F"Vacation #{i}", type=WorkDay.TYPE_VACATION) day.save() # Add a few out-of-range Vacation work days for i in range(0, numDays): day = WorkDay(date=arrow.now().shift(days=7 + i), time_in="00:00", time_out="00:00", note=F"Future Vacation #{i}", type=WorkDay.TYPE_VACATION) day.save() self.assertEqual(pto.used, WorkDay.HOURS_PER_DAY * numDays)
def setUp(self): # Setup Flask Testing cartaro.flask_app.config['TESTING'] = True self.client = cartaro.flask_app.test_client() WorkDay.purge()
class WorkDayTest(unittest.TestCase): def setUp(self): WorkDay.purge() self.work_day = WorkDay(date=arrow.now(), time_in="9:00", time_out="16:30", note="Just Another Day", type=WorkDay.TYPE_NORMAL) def test_basics(self): self.assertIsInstance(self.work_day, Taggable) self.assertIsInstance(self.work_day.date, arrow.Arrow) def test_date(self): self.work_day.date = 1 self.assertIsInstance(self.work_day.date, arrow.Arrow) self.work_day.date = arrow.now() self.assertIsInstance(self.work_day.date, arrow.Arrow) with self.assertRaisesRegex(TypeError, 'must be of type INT, STR or Arrow'): self.work_day.date = [2021, 1, 18] def test_hours(self): self.assertEqual(self.work_day.hours, 7.5) wd = WorkDay(time_in='10:30', time_out='10:31') self.assertEqual(wd.hours, 0.02) wd = WorkDay(time_in='10:30', time_out='11:00') self.assertEqual(wd.hours, 0.5) wd = WorkDay(time_in='10:45', time_out='11:00') self.assertEqual(wd.hours, 0.25) wd = WorkDay(time_in='9:00', time_out='15:40') self.assertEqual(wd.hours, 6.67) wd = WorkDay(time_in='9:05', time_out='17:05') self.assertEqual(wd.hours, 8.0) def test_serialize(self): data = self.work_day.serialize() self.assertEqual(self.work_day.date.int_timestamp, data['date']) self.assertEqual(self.work_day.time_in, data['time_in']) self.assertEqual(self.work_day.time_out, data['time_out']) self.assertEqual(self.work_day.note, data['note']) self.assertEqual(self.work_day.type, data['type']) def test_range(self): date = arrow.get("2020-01-01").replace(tzinfo=WorkDay.TIMEZONE) for i in range(0, 180): wd = WorkDay(date=date, time_in="09:00", time_out="16:30", note=F"Test WorkDay #{i}") wd.save() date = date.shift(days=+1) self.assertEqual(WorkDay.count(), 180) # Two dates day_range = WorkDay.range('2020-01-01', '2020-01-31') self.assertEqual(len(day_range), 31) self.assertIsInstance(day_range[0], WorkDay) self.assertEqual( day_range[0].date.int_timestamp, arrow.get("2020-01-01").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) self.assertEqual( day_range[30].date.int_timestamp, arrow.get("2020-01-31").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) # Defined range by date and delta days POSITIVE day_range = WorkDay.range('2020-02-19', days=+7) self.assertEqual(len(day_range), 8) # 8 b/c inclusive self.assertIsInstance(day_range[0], WorkDay) self.assertEqual( day_range[0].date.int_timestamp, arrow.get("2020-02-19").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) self.assertEqual( day_range[7].date.int_timestamp, arrow.get("2020-02-26").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) # Defined range by date and delta days NEGATIVE day_range = WorkDay.range('2020-02-19', days=-7) self.assertEqual(len(day_range), 8) # 8 b/c inclusive self.assertIsInstance(day_range[0], WorkDay) self.assertEqual( day_range[0].date.int_timestamp, arrow.get("2020-02-12").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp) self.assertEqual( day_range[7].date.int_timestamp, arrow.get("2020-02-19").replace( tzinfo=WorkDay.TIMEZONE).int_timestamp)