Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
QuocDuong1306 committed Sep 17, 2024
1 parent b9e73dd commit 2b409bd
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
3 changes: 2 additions & 1 deletion edi_oca/models/edi_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ def edi_exec_snippet_do(self, record, **kwargs):
before_do_vals = self.edi_exec_snippet_before_do(record, **kwargs)
vals.update(before_do_vals)
if vals["todo"]:
self._evaluate_code_snippet(self.snippet_do, **vals)
return self._evaluate_code_snippet(self.snippet_do, **vals)
return True

@api.model
def edi_get_conf(self, trigger, model_name=None, partners=None, backend=None):
Expand Down
12 changes: 7 additions & 5 deletions edi_oca/tests/fake_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,18 @@ class FakeInputValidate(FakeComponentMixin):
def validate(self, value=None):
self._fake_it()
return



class FakeConfigurationListener(FakeComponentMixin):
_name = "fake.configuration.listener"
_inherit = "base.event.listener"
_apply_on = ["edi.exchange.consumer.test"]


# TODO: Add tests for partner_ids

def on_record_write(self, record, fields=None, **kwargs):
trigger = "on_record_write"
edi_configuration = self.env['edi.configuration']
edi_configuration = self.env["edi.configuration"]
if kwargs.get("vals", False):
for rec in record:
confs = edi_configuration.edi_get_conf(trigger, model_name=rec._name)
Expand All @@ -155,13 +155,15 @@ def on_record_write(self, record, fields=None, **kwargs):

def on_record_create(self, record, fields=None, **kwargs):
trigger = "on_record_create"
edi_configuration = self.env['edi.configuration']
edi_configuration = self.env["edi.configuration"]
backend_id = self.env.ref("edi_oca.demo_edi_backend")
val_list = kwargs.get("vals", False)
if val_list:
for rec, vals in zip(record, val_list):
kwargs["vals"] = {rec.id: vals}
confs = edi_configuration.edi_get_conf(trigger, model_name=rec._name, backend=backend_id)
confs = edi_configuration.edi_get_conf(
trigger, model_name=rec._name, backend=backend_id
)
print("Configuring: ", confs)
for conf in confs:
conf.edi_exec_snippet_do(rec, **kwargs)
Expand Down
78 changes: 64 additions & 14 deletions edi_oca/tests/test_edi_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
import unittest

from odoo_test_helper import FakeModelLoader
from odoo.addons.queue_job.tests.common import trap_jobs


from .common import EDIBackendCommonComponentRegistryTestCase
from .fake_components import (
FakeConfigurationListener,
FakeOutputChecker,
FakeOutputGenerator,
FakeOutputSender,
FakeConfigurationListener,
)


# This clashes w/ some setup (eg: run tests w/ pytest when edi_storage is installed)
# If you still want to run `edi` tests w/ pytest when this happens, set this env var.
@unittest.skipIf(os.getenv("SKIP_EDI_CONSUMER_CASE"), "Consumer test case disabled.")
class TestEDIConfigurations(EDIBackendCommonComponentRegistryTestCase):

@classmethod
def setUpClass(cls):
super().setUpClass()
Expand Down Expand Up @@ -85,30 +82,83 @@ def tearDownClass(cls):
cls.loader.restore_registry()
super().tearDownClass()

def test_edi_configuration(self):
def test_edi_send_via_edi_config(self):
# Create new consumer record

consumer_record = self.env["edi.exchange.consumer.test"].create(
{"name": "Test Consumer"}
)

# Check configuration on create
consumer_record.refresh()
exchange_record = consumer_record.exchange_record_ids
self.assertEqual(len(exchange_record), 1)
self.assertEqual(exchange_record.type_id, self.exchange_type_out)
self.assertEqual(exchange_record.edi_exchange_state, "output_sent")


# Check _edi_send_via_edi

# Check generate and send output record

# Write the existed consumer record
consumer_record.name = "Fix Consumer"
consumer_record.name = "Fixed Consumer"
# check Configuration on write
consumer_record.refresh()
exchange_record = consumer_record.exchange_record_ids - exchange_record
self.assertEqual(len(exchange_record), 1)
self.assertEqual(exchange_record.type_id, self.exchange_type_out)
self.assertEqual(exchange_record.edi_exchange_state, "output_sent")

def test_edi_code_snippet(self):
# Create new consumer record
consumer_record = self.env["edi.exchange.consumer.test"].create(
{"name": "Test Consumer"}
)
expected_value = {
"todo": True,
"snippet_do_vars": {
"a": 1,
"b": 2,
},
"event_only": True,
"tracked_fields": ["state"],
"edi_action": "new_action",
}
# Simulate the snippet_before_do
self.write_config.snippet_before_do = "result = " + str(expected_value)
# Execute with the raw data
vals = self.write_config.edi_exec_snippet_before_do(
consumer_record,
todo=False,
tracked_fields=[],
edi_action="generate",
)
# Check the new vals after execution
self.assertEqual(vals, expected_value)

# Check the snippet_do
expected_value = {
"change_state": True,
"snippet_do_vars": {
"a": 1,
"b": 2,
},
"record": consumer_record,
"tracked_fields": ["state"],
}
snippet_do = """\n
old_state = old_value.get("state", False)\n
new_state = vals.get("state", False)\n
result = {\n
"change_state": True if old_state and new_state and old_state != new_state else False,\n
"snippet_do_vars": snippet_do_vars,\n
"record": record,\n
"tracked_fields": tracked_fields,\n
}
"""
self.write_config.snippet_do = snippet_do
# Execute with the raw data
record_id = consumer_record.id
vals = self.write_config.edi_exec_snippet_do(
consumer_record,
todo=False,
tracked_fields=[],
edi_action="generate",
old_vals={record_id: dict(state="draft")},
vals={record_id: dict(state="confirmed")},
)
# Check the new vals after execution
self.assertEqual(vals, expected_value)

0 comments on commit 2b409bd

Please sign in to comment.