Skip to content

Commit

Permalink
pattern_import_export: add test
Browse files Browse the repository at this point in the history
convert_value_to_domain,
pk with multiple values,
fk with multiple values
  • Loading branch information
hparfr committed Apr 14, 2023
1 parent 6fea527 commit 8f38e48
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
57 changes: 57 additions & 0 deletions pattern_import_export/tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,60 @@ def test_string_domain_is_ignored(self):
self._patch_search("res.country.state")
self.converter.db_id_for(model, field, "name", "Rio de Janeiro")
self.assertEqual(self.search_domain, [[("name", "=", "Rio de Janeiro")]])

def test_convert_value_to_domain(self):
field_name = None
value = {
"partner_id": {
"name": "abcdef",
"phone": "06707507",
"country_id": {
"code": "FR",
"name": "France",
},
"user_id": {
"name": "Someone",
"false": False,
"true": True,
"none": None,
},
},
"direct_value": "some string",
"another_partner_id": {"name": "abcdef", "phone": "0000000"},
}
expected = [
["partner_id.name", "=", "abcdef"],
["partner_id.phone", "=", "06707507"],
["partner_id.country_id.code", "=", "FR"],
["partner_id.country_id.name", "=", "France"],
["partner_id.user_id.name", "=", "Someone"],
["partner_id.user_id.false", "=", False],
["partner_id.user_id.true", "=", True],
["partner_id.user_id.none", "=", None],
["direct_value", "=", "some string"],
["another_partner_id.name", "=", "abcdef"],
["another_partner_id.phone", "=", "0000000"],
]

result = self.env["res.partner"]._convert_value_to_domain(field_name, value)
for expectation in expected:
self.assertIn(expectation, result)
self.assertEqual(len(expected), len(result))

# now test with a field_name
expected2 = [
["partner_id.name", "=", "abcdef"],
["partner_id.phone", "=", "06707507"],
["partner_id.country_id.code", "=", "FR"],
["partner_id.country_id.name", "=", "France"],
["partner_id.user_id.name", "=", "Someone"],
["partner_id.user_id.false", "=", False],
["partner_id.user_id.true", "=", True],
["partner_id.user_id.none", "=", None],
]
result2 = self.env["res.partner"]._convert_value_to_domain(
"partner_id", value["partner_id"]
)
for expectation in expected2:
self.assertIn(expectation, result2)
self.assertEqual(len(expected2), len(result2))
78 changes: 78 additions & 0 deletions pattern_import_export/tests/test_pattern_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TestPatternImport(PatternCommon, SavepointCase):
def setUpClass(cls):
super().setUpClass()
cls.pattern_config_m2m.export_format = "json"
cls.pattern_config_o2m.export_format = "json"
cls.pattern_config.export_format = "json"

def run_pattern_file(self, pattern_file):
Expand Down Expand Up @@ -229,6 +230,44 @@ def test_update_with_key(self):
self.assertFalse(records)
self.assertEquals(unique_name, self.user3.name)

def test_update_with_muli_cols_pkey(self):
"""ensure we identify the row to update
based on multiple columns with
different depths
"""
unique_name = str(uuid4())
data = [
{
"partner_id#key|email": self.user3.partner_id.email,
"partner_id#key|name": self.user3.partner_id.name,
"partner_id#key|country_id|code": self.user3.partner_id.country_id.code,
"name": unique_name,
}
]
pattern_file = self.create_pattern(self.pattern_config_m2m, "import", data)
records = self.run_pattern_file(pattern_file)
self.assertFalse(records)
self.assertEqual(unique_name, self.user3.name)

# same test with ambigus key
# the record should not be updated because the key
# returns more than 1 record
ambigus = self.user3.search(
[["country_id.code", "=", self.user3.partner_id.country_id.code]]
)
self.assertTrue(len(ambigus) > 1, "Verify conditions")
unique_name_2 = str(uuid4())
data = [
{
"partner_id#key|country_id|code": self.user3.partner_id.country_id.code,
"name": unique_name_2,
}
]
pattern_file = self.create_pattern(self.pattern_config_m2m, "import", data)
records = self.run_pattern_file(pattern_file)
self.assertFalse(records)
self.assertEqual(unique_name, self.user3.name, "Ensure value not updated")

def test_update_o2m_with_key(self):
unique_name = str(uuid4())
contact_1_name = str(uuid4())
Expand Down Expand Up @@ -273,6 +312,45 @@ def test_update_o2m_with_key_only_one_record(self):
self.run_pattern_file(pattern_file)
self.assertEquals(unique_name, self.partner_1.name)

def test_update_o2m_with_sub_keys(self):
unique_name = str(uuid4())

# state_ie_27,ie,"Antrim","AM"
# there is multiple state with code = AM
# in demo data, but only one with Currency = euro

# ensure we picked a reference to only one record
only_one_rec = self.env["res.country"].search(
[["state_ids.code", "=", "AM"], ["currency_id.symbol", "=", "€"]]
)
self.assertEqual(len(only_one_rec), 1, "Ensure data for test valid")
previous_country = self.partner_1.country_id
new_country = only_one_rec
data = [
{
"email#key": self.partner_1.email,
"phone#key": self.partner_1.phone,
"name": unique_name,
"country_id|currency_id|symbol": "€",
"country_id|state_ids|code": "AM",
"title|name": "Professor",
}
]
pattern_file = self.create_pattern(self.pattern_config_o2m, "import", data)
self.run_pattern_file(pattern_file)
self.assertNotEqual(previous_country.id, new_country.id)
self.assertEqual(
unique_name, self.partner_1.name, "direct field has been updated"
)
self.assertEqual(
new_country.id,
self.partner_1.country_id.id,
"relation field has been updated",
)
self.assertEqual(
"Professor", self.partner_1.title.name, "relation field has been updated"
)

@mute_logger("odoo.sql_db")
def test_wrong_import(self):
data = [{"login#key": self.user3.login, "name": ""}]
Expand Down

0 comments on commit 8f38e48

Please sign in to comment.