-
-
Notifications
You must be signed in to change notification settings - Fork 311
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
[14.0] edi: generate/send chain jobs + add identity exact match based on checksum #796
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb50b23
edi: fix backend jobs test old api
simahawk 2aaa9b0
edi: chain generate/send jobs
simahawk 9cb51cf
edi: improve send job retry
simahawk 378acd2
edi: add file checksum
simahawk fc0780a
edi: use job identity_key
simahawk e996f0f
edi: raise send job prio to max
simahawk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# Copyright 2020 ACSONE | ||
# Copyright 2021 Camptocamp | ||
# @author: Simone Orsi <[email protected]> | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
|
@@ -8,6 +9,8 @@ | |
from odoo import fields, tools | ||
from odoo.exceptions import UserError | ||
|
||
from odoo.addons.queue_job.tests.common import trap_jobs | ||
|
||
from .common import EDIBackendCommonComponentRegistryTestCase | ||
from .fake_components import FakeOutputChecker, FakeOutputGenerator, FakeOutputSender | ||
|
||
|
@@ -17,7 +20,6 @@ class EDIBackendTestOutputCase(EDIBackendCommonComponentRegistryTestCase): | |
def setUpClass(cls): | ||
super().setUpClass() | ||
cls._build_components( | ||
# TODO: test all components lookup | ||
cls, | ||
FakeOutputGenerator, | ||
FakeOutputSender, | ||
|
@@ -36,14 +38,14 @@ def setUp(self): | |
FakeOutputChecker.reset_faked() | ||
|
||
def test_generate_record_output(self): | ||
self.backend.with_context(fake_output="yeah!").exchange_generate(self.record) | ||
self.record.with_context(fake_output="yeah!").action_exchange_generate() | ||
self.assertEqual(self.record._get_file_content(), "yeah!") | ||
|
||
def test_generate_record_output_pdf(self): | ||
result = tools.file_open( | ||
pdf_content = tools.file_open( | ||
"result.pdf", subdir="addons/edi_oca/tests", mode="rb" | ||
).read() | ||
self.backend.with_context(fake_output=result).exchange_generate(self.record) | ||
self.record.with_context(fake_output=pdf_content).action_exchange_generate() | ||
|
||
def test_send_record(self): | ||
self.record.write({"edi_exchange_state": "output_pending"}) | ||
|
@@ -105,3 +107,53 @@ def test_send_not_generated_record(self): | |
err.exception.args[0], "Record ID=%d has no file to send!" % record.id | ||
) | ||
mocked.assert_not_called() | ||
|
||
|
||
class EDIBackendTestOutputJobsCase(EDIBackendCommonComponentRegistryTestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
cls._build_components( | ||
cls, | ||
FakeOutputGenerator, | ||
FakeOutputSender, | ||
FakeOutputChecker, | ||
) | ||
vals = { | ||
"model": cls.partner._name, | ||
"res_id": cls.partner.id, | ||
} | ||
cls.record = cls.backend.create_record("test_csv_output", vals) | ||
cls.record.type_id.exchange_file_auto_generate = True | ||
|
||
@classmethod | ||
def _setup_context(cls): | ||
# Re-enable jobs | ||
return dict(super()._setup_context(), test_queue_job_no_delay=False) | ||
|
||
def test_job(self): | ||
with trap_jobs() as trap: | ||
self.backend._check_output_exchange_sync(record_ids=self.record.ids) | ||
trap.assert_jobs_count(2) | ||
trap.assert_enqueued_job( | ||
self.record.action_exchange_generate, | ||
) | ||
trap.assert_enqueued_job( | ||
self.record.action_exchange_send, properties=dict(priority=0) | ||
) | ||
# No matter how many times we schedule jobs | ||
self.record.with_delay().action_exchange_generate() | ||
self.record.with_delay().action_exchange_generate() | ||
self.record.with_delay().action_exchange_generate() | ||
# identity key should prevent having new jobs for same record same file | ||
trap.assert_jobs_count(2) | ||
# but if we change the content | ||
self.record._set_file_content("something different") | ||
# 1st call will schedule another job | ||
self.record.with_delay().action_exchange_generate() | ||
# the 2nd one not | ||
self.record.with_delay().action_exchange_generate() | ||
trap.assert_jobs_count(3) | ||
self.record.with_delay().action_exchange_send() | ||
trap.assert_jobs_count(4) | ||
# TODO: test input in the same way |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,15 @@ | |
# @author: Simone Orsi <[email protected]> | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
import base64 | ||
|
||
import mock | ||
from freezegun import freeze_time | ||
|
||
from odoo import exceptions, fields | ||
from odoo.tools import mute_logger | ||
|
||
from odoo.addons.edi_oca.utils import get_checksum | ||
from odoo.addons.queue_job.delay import DelayableRecordset | ||
|
||
from .common import EDIBackendCommonTestCase | ||
|
@@ -216,3 +219,19 @@ def test_retry(self): | |
mocked.assert_not_called() | ||
self.assertEqual(record0.edi_exchange_state, "output_pending") | ||
self.assertFalse(record0.retryable) | ||
|
||
def test_checksum(self): | ||
filecontent = base64.b64encode(b"ABC") | ||
checksum1 = get_checksum(filecontent) | ||
vals = { | ||
"model": self.partner._name, | ||
"res_id": self.partner.id, | ||
"exchange_file": filecontent, | ||
} | ||
record0 = self.backend.create_record("test_csv_output", vals) | ||
self.assertEqual(record0.exchange_filechecksum, checksum1) | ||
filecontent = base64.b64encode(b"DEF") | ||
checksum2 = get_checksum(filecontent) | ||
record0.exchange_file = filecontent | ||
self.assertEqual(record0.exchange_filechecksum, checksum2) | ||
self.assertNotEqual(record0.exchange_filechecksum, checksum1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,25 @@ | |
# @author Simone Orsi <[email protected]> | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
|
||
import hashlib | ||
|
||
from odoo.addons.http_routing.models.ir_http import slugify | ||
from odoo.addons.queue_job.job import identity_exact_hasher | ||
|
||
|
||
def normalize_string(a_string, sep="_"): | ||
"""Normalize given string, replace dashes with given separator.""" | ||
return slugify(a_string).replace("-", sep) | ||
|
||
|
||
def get_checksum(filecontent): | ||
return hashlib.md5(filecontent).hexdigest() | ||
|
||
|
||
def exchange_record_job_identity_exact(job_): | ||
hasher = identity_exact_hasher(job_) | ||
# Include files checksum | ||
hasher.update( | ||
str(sorted(job_.recordset.mapped("exchange_filechecksum"))).encode("utf-8") | ||
) | ||
return hasher.hexdigest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do yo need to add the file checksum? The function already contains ID of the exchange record and function 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because is the only to way to know if the file is the same.
If by error you try to schedule another job you won't get duplicated jobs unless the content of the file is the same.
In most cases, if you rely only on the cron to process files you'll have the filter done by the check methods that will skip records not in the rate state or not w/ the right values to be generated/sent/received/processed.
However, if you force those checks and try to schedule a job for the a record whereas the file didn't change you shouldn't be able to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might come handy also to compare files w/o reading the whole file (eg: to check if you received the same file twice).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What will happen if the file is not generated?