Skip to content

Commit

Permalink
Add unit test and if in the usage.txt there is invalid data processed…
Browse files Browse the repository at this point in the history
… it and discarted (consider it old)
  • Loading branch information
jordimas committed Apr 26, 2024
1 parent efd7528 commit 4943008
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
97 changes: 97 additions & 0 deletions transcribe-batch/tests/testusage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2017 Jordi Mas i Hernandez <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

import unittest
import os
import datetime
from usage import Usage


class UsageTest(Usage):
datetime = None

def __init__(self):
self._set_filename("usage.txt")

def _get_time_now(self):
return self.datetime

def _set_time_now(self, value):
self.datetime = value


class TestUsage(unittest.TestCase):
def setUp(self):
filename = UsageTest().FILE
if os.path.exists(filename):
os.remove(filename)

def readLog(self):
with open(UsageTest().FILE, "r") as temp:
return temp.readlines()

def _test_log_one(self):
usage = UsageTest()
usage.rotate = False
usage._set_time_now(datetime.datetime(2016, 10, 5))
usage.log("conversion_error")
lines = self.readLog()

self.assertEqual(len(lines), 1)
self.assertEqual("2016-10-05 00:00:00 conversion_error\n", lines[0])

def test_log_rotate(self):
usage = UsageTest()
usage._set_time_now(datetime.datetime(2017, 10, 8, 13, 00, 00))
usage.log("conversion_error")

usage._set_time_now(datetime.datetime(2017, 10, 8, 15, 00, 00))
usage.log("conversion_error")

usage._set_time_now(
datetime.datetime(2017, 10, 8 + usage.DAYS_TO_KEEP, 14, 00, 00)
)
usage.log("conversion_error")

lines = self.readLog()
print(lines)

self.assertEqual(len(lines), 2)

def _test_is_old_line_true(self):
usage = UsageTest()
usage._set_time_now(datetime.datetime(2016, 11, 5))
is_old = usage._is_old_line("2016-10-05 00:00:00 conversion_error\n")
self.assertEqual(True, is_old)

def _test_is_old_line_false(self):
usage = UsageTest()
usage._set_time_now(datetime.datetime(2016, 11, 5))
is_old = usage._is_old_line("2016-11-05 00:00:00 conversion_error\n")
self.assertEqual(False, is_old)

def _test_is_old_line_exception(self):
usage = UsageTest()
usage._set_time_now(datetime.datetime(2016, 11, 5))
is_old = usage._is_old_line("INVALID DATE conversion_error\n")
self.assertEqual(True, is_old)


if __name__ == "__main__":
unittest.main()
16 changes: 12 additions & 4 deletions transcribe-service/usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def get_date_from_line(self, line):
return line.split("\t", 1)[0]

def log(self, action):
current_time = self._get_time_now().strftime('%Y-%m-%d %H:%M:%S')
try:
with lock:
with open(self.FILE, "a+") as file_out:
current_time = self._get_time_now().strftime('%Y-%m-%d %H:%M:%S')
file_out.write(f'{current_time}\t{action}\n')

if self.rotate and self._is_old_line(self._read_first_line()):
Expand Down Expand Up @@ -102,9 +102,17 @@ def _is_old_line(self, line):
if line is None:
return False

line = self.get_date_from_line(line)
line_datetime = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S')
return line_datetime < self._get_time_now() - datetime.timedelta(days = self.DAYS_TO_KEEP)
invalid = False

try:
line = self.get_date_from_line(line)
line_datetime = datetime.datetime.strptime(line, '%Y-%m-%d %H:%M:%S')

except Exception as exception:
logging.error("Usage._is_old_line. Error:" + str(exception))
invalid = True

return invalid or line_datetime < self._get_time_now() - datetime.timedelta(days = self.DAYS_TO_KEEP)

def _rotate_file(self):
NEW = "usage.new"
Expand Down

0 comments on commit 4943008

Please sign in to comment.