Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 Add long time job #23

Merged
merged 42 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2d1a1fa
:construction: Add long time job
AminovE99 Jan 13, 2020
4a47e49
:construction: Add TDD functions
madmath03 Jan 13, 2020
3deb8dd
:construction: Add test for bg job
AminovE99 Jan 16, 2020
41a488f
:construction: Add Todo for future
AminovE99 Jan 16, 2020
035feda
:construction: fix unused variable
AminovE99 Jan 16, 2020
8ef702d
:construction: Add another test
AminovE99 Jan 16, 2020
e3b0895
:construction: Fix test
AminovE99 Jan 21, 2020
50ce944
:white_check_mark: Fix tests
AminovE99 Jan 21, 2020
9f62767
:white_check_mark: Trying fix test
AminovE99 Jan 22, 2020
bc764fe
:white_check_mark: Trying fix test v2
AminovE99 Jan 22, 2020
34a9651
:white_check_mark: Trying fix test v3
AminovE99 Jan 22, 2020
08a8151
:white_check_mark: Remove time library
AminovE99 Jan 22, 2020
5c3a9c9
:white_check_mark: Blank commit
AminovE99 Jan 22, 2020
6f91d2f
Merge branch 'develop' into feature/job_to_bg_worker
madmath03 Jan 22, 2020
4726677
Merge branch 'feature/job_to_bg_worker' of github.com:Monogramm/ERPNe…
madmath03 Jan 22, 2020
5343d66
:lipstick: Update tests for validation
AminovE99 Jan 23, 2020
ef2cad1
:white_check_mark: Add new tests
AminovE99 Jan 23, 2020
4918b9b
:white_check_mark: Update tests
AminovE99 Jan 23, 2020
0f222e2
:white_check_mark: Last update test
AminovE99 Jan 23, 2020
eb72741
:lipstick: The last beautify of code
AminovE99 Jan 23, 2020
50742ed
:white_check_mark: Update tests
AminovE99 Jan 24, 2020
fcaa6df
:white_check_mark: Remove unnecesary library
AminovE99 Jan 24, 2020
e6128c6
:ok_hand: Fix unit tests process for bg jobs
madmath03 Jan 24, 2020
d8bbc0d
:white_check_mark: Update tests
AminovE99 Jan 24, 2020
94fd70b
:consturction: Add logs for debuging
AminovE99 Jan 27, 2020
bad4f0a
:whale: Add more time for tests
AminovE99 Jan 27, 2020
5e39492
:whale: Add bench doctor
AminovE99 Jan 27, 2020
5a49a0b
:wrench: Enable schedulers
AminovE99 Jan 27, 2020
a4163be
:wrench: Remove exec function from travis yml
AminovE99 Jan 27, 2020
75f5350
:wrench: Add bench doctor
AminovE99 Jan 28, 2020
b326b04
:wrench: Add bench doctor before enable scheduler
AminovE99 Jan 28, 2020
333c430
:construction: Add tests for OCR Read
AminovE99 Jan 28, 2020
a1af764
:construction: Mark every tests by print
AminovE99 Jan 28, 2020
9ad6064
:construction: Remove tests
AminovE99 Jan 28, 2020
3b12e65
:construction: Add getter instead of variable
AminovE99 Jan 28, 2020
a17948f
:construction: Logging tests and requeueing jobs
AminovE99 Jan 28, 2020
630b2e5
:construction: print values of worker
AminovE99 Jan 29, 2020
5278ed3
:white_check_mark: Remove while construction and add now parameter
AminovE99 Jan 29, 2020
db2bc2d
:lipstick: Remove old libraries
AminovE99 Jan 29, 2020
fd2df64
Merge branch 'develop' into feature/job_to_bg_worker
madmath03 Jan 29, 2020
ac7b9e4
Merge branch 'feature/job_to_bg_worker' of github.com:Monogramm/erpne…
madmath03 Jan 29, 2020
f75dd9b
:white_check_mark: Add test for Validation Error
AminovE99 Feb 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ script:
- docker-compose -f docker-compose.${DATABASE}.yml ps "erpnext_app" | grep "Up"
- docker-compose -f docker-compose.${DATABASE}.yml logs "erpnext_web"
- docker-compose -f docker-compose.${DATABASE}.yml ps "erpnext_web" | grep "Up"
- echo 'Wait until test finished (3 minutes)' && sleep 180
- echo 'Wait until test finished (5 minutes)' && sleep 300
- docker-compose -f docker-compose.${DATABASE}.yml logs "sut"
- docker-compose -f docker-compose.${DATABASE}.yml ps "sut" | grep "Exit 0"

Expand Down
4 changes: 4 additions & 0 deletions .travis/docker_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ FRAPPE_APP_UNIT_TEST_PROFILE="$(pwd)/sites/.${FRAPPE_APP_TO_TEST}_unit_tests.pro

#bench run-tests --help

bench doctor
bench enable-scheduler
bench doctor

echo "Executing Unit Tests of '${FRAPPE_APP_TO_TEST}' app..."
if [ "${TEST_VERSION}" = "10" ]; then
bench run-tests \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def delete_test_data():
#test_user.remove_roles("System Manager")
#test_user.delete()
frappe.db.sql("""delete from `tabUser` where email='test_user@example.com'""") # ValidationError without SQL

frappe.db.sql("""delete from `tabEmail Queue`""")

class TestOCRLanguage(unittest.TestCase):
def setUp(self):
Expand Down
39 changes: 28 additions & 11 deletions erpnext_ocr/erpnext_ocr/doctype/ocr_read/ocr_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

from spellchecker import SpellChecker


def get_words_from_text(message):
"""
This function return only list of words from text. Example: Cat in gloves,
Expand Down Expand Up @@ -50,14 +51,30 @@ def __init__(self, *args, **kwargs):
super(OCRRead, self).__init__(*args, **kwargs)

def read_image(self):
start_time = time.time()
text = read_document(self.file_to_read, self.language or 'eng',
self.spell_checker)
delta_time = time.time() - start_time
self.read_time = str(delta_time)
self.read_result = text
self.save()
return text
return read_ocr(self)

def read_image_bg(self, is_async=True, now=False):
return frappe.enqueue("erpnext_ocr.erpnext_ocr.doctype.ocr_read.ocr_read.read_ocr", queue="long",
timeout=1500, is_async=is_async, now=now, **{'obj': self})


@frappe.whitelist()
def read_ocr(obj):
"""Call Tesseract OCR to extract the text from a OCR Read object."""

if obj is None:
frappe.msgprint(frappe._("An expected error occurred."),
raise_exception=True)

start_time = time.time()
text = read_document(obj.file_to_read, obj.language or 'eng', obj.spell_checker)
delta_time = time.time() - start_time

obj.read_time = str(delta_time)
obj.read_result = text
obj.save()

return text


@frappe.whitelist()
Expand All @@ -71,9 +88,9 @@ def read_document(path, lang='eng', spellcheck=False, event="ocr_progress_bar"):
return None

if not lang_available(lang):
frappe.msgprint(
frappe._("The selected language is not available. Please contact your administrator."),
raise_exception=True)
frappe.msgprint(frappe._
("The selected language is not available. Please contact your administrator."),
raise_exception=True)

frappe.publish_realtime(event, {"progress": "0"}, user=frappe.session.user)

Expand Down
81 changes: 68 additions & 13 deletions erpnext_ocr/erpnext_ocr/doctype/ocr_read/test_ocr_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

from __future__ import unicode_literals


import frappe
import unittest
import os

from erpnext_ocr.erpnext_ocr.doctype.ocr_read.ocr_read import force_attach_file_doc
from erpnext_ocr.erpnext_ocr.doctype.ocr_read.ocr_read import force_attach_file_doc, read_ocr


# TODO Frappe default test records creation
#def _make_test_records(verbose):
# def _make_test_records(verbose):
# from frappe.test_runner import make_test_objects
#
# docs = [
Expand Down Expand Up @@ -78,7 +80,7 @@ def delete_ocr_reads():
doc.delete()

# Delete directly in DB to avoid validation errors
#frappe.db.sql("""delete from `tabOCR Read`""")
# frappe.db.sql("""delete from `tabOCR Read`""")

frappe.flags.test_ocr_reads_created = False

Expand All @@ -90,14 +92,66 @@ def setUp(self):
def tearDown(self):
delete_ocr_reads()

def test_ocr_read_image_bg(self):
frappe.set_user("Administrator")
doc = frappe.get_doc({
"doctype": "OCR Read",
"file_to_read": os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample1.jpg"),
"language": "eng"
})

self.assertEqual(None, doc.read_result)

doc.read_image_bg(is_async=False, now=True)

# Wait worker completion before moving on in the tests

# Check worker completion and get "new" document after update by bg job
new_doc = frappe.get_doc("OCR Read",
{"file_to_read": os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample1.jpg"),
"language": "eng"})

self.assertEqual(new_doc.read_result, doc.read_result)
self.assertIn("The quick brown fox", new_doc.read_result)
self.assertIn("jumped over the 5", new_doc.read_result)
self.assertIn("lazy dogs!", new_doc.read_result)
self.assertNotIn("And an elephant!", new_doc.read_result)

def test_ocr_read_image_bg_pdf(self):
frappe.set_user("Administrator")
doc = frappe.get_doc({
"doctype": "OCR Read",
"file_to_read": os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample2.pdf"),
"language": "eng"
})

self.assertEqual(None, doc.read_result)

doc.read_image_bg(is_async=False, now=True)

new_doc = frappe.get_doc("OCR Read", {
"file_to_read": os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample2.pdf"),
"language": "eng"})

self.assertEqual(new_doc.read_result, doc.read_result)
self.assertIn("Python Basics", new_doc.read_result)
self.assertNotIn("Java", new_doc.read_result)

def test_ocr_read_image(self):
frappe.set_user("Administrator")
doc = frappe.get_doc({
"doctype": "OCR Read",
"file_to_read": os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample1.jpg"),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample1.jpg"),
"language": "eng"
})

Expand All @@ -109,14 +163,13 @@ def test_ocr_read_image(self):
self.assertIn("lazy dogs!", recognized_text)
self.assertNotIn("And an elephant!", recognized_text)


def test_ocr_read_pdf(self):
frappe.set_user("Administrator")
doc = frappe.get_doc({
"doctype": "OCR Read",
"file_to_read": os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample2.pdf"),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample2.pdf"),
"language": "eng"
})

Expand All @@ -128,21 +181,20 @@ def test_ocr_read_pdf(self):
self.assertIn("Python Basics", recognized_text)
self.assertNotIn("Java", recognized_text)


def test_force_attach_file_doc(self):
doc = frappe.get_doc({
"doctype": "OCR Read",
"file_to_read": os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "Picture_010.png"),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "Picture_010.png"),
"language": "eng"
})

force_attach_file_doc('test.tif', doc.name)

forced_doc = frappe.get_doc({
"doctype": "OCR Read",
#"name": doc.name,
# "name": doc.name,
"file_to_read": "/private/files/test.tif",
"language": "eng"
})
Expand All @@ -154,7 +206,7 @@ def test_ocr_read_list(self):
# frappe.set_user("test1@example.com")
frappe.set_user("Administrator")
res = frappe.get_list("OCR Read", filters=[
["OCR Read", "file_to_read", "like", "%sample%"]], fields=["name", "file_to_read"])
["OCR Read", "file_to_read", "like", "%sample%"]], fields=["name", "file_to_read"])
self.assertEqual(len(res), 2)
files_to_read = [r.file_to_read for r in res]
self.assertTrue(os.path.join(os.path.dirname(__file__),
Expand All @@ -163,3 +215,6 @@ def test_ocr_read_list(self):
self.assertTrue(os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir, os.path.pardir,
"tests", "test_data", "sample2.pdf") in files_to_read)

def test_read_ocr(self):
self.assertRaises(frappe.ValidationError, read_ocr, obj=None)