Skip to content

Commit

Permalink
Merge pull request #959 from hinashi/fixed/import_warning_log
Browse files Browse the repository at this point in the history
Fixed exception error in entry import
  • Loading branch information
userlocalhost authored Sep 25, 2023
2 parents dec4b76 + 0537f1e commit 153ffcb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions entry/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,9 @@ def _object(
if isinstance(val, str):
if len(refs) >= 2:
Logger.warn(
"ambiguous object given: entry name(%s), entity ids(%s)",
(val, refs),
"ambiguous object given: entry name(%s), entity names(%s)",
val,
[x.name for x in refs],
)
ref_entry = Entry.objects.filter(name=val, schema__in=refs).first()
return ref_entry.id if ref_entry else "0"
Expand Down
19 changes: 19 additions & 0 deletions entry/tests/test_api_v2.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import datetime
import errno
import json
import logging
from datetime import date
from unittest import mock
from unittest.mock import Mock, patch

import yaml
from rest_framework.exceptions import ValidationError

from airone.lib.log import Logger
from airone.lib.test import AironeViewTest
from airone.lib.types import (
AttrTypeArrNamedObj,
Expand Down Expand Up @@ -2564,6 +2566,10 @@ def test_import_multi_entity(self):
@patch("entry.tasks.notify_create_entry.delay", Mock(side_effect=tasks.notify_create_entry))
@patch("entry.tasks.import_entries_v2.delay", Mock(side_effect=tasks.import_entries_v2))
def test_import_entry_has_referrals_with_entities(self):
ref_entity2: Entity = self.create_entity(self.user, "ref_entity2")
attr: EntityAttr = self.entity.attrs.get(name="ref")
attr.referral.add(ref_entity2)

fp = self.open_fixture_file("import_data_v2_with_entities.yaml")
resp = self.client.post("/entry/api/v2/import/", fp.read(), "application/yaml")
fp.close()
Expand Down Expand Up @@ -2608,6 +2614,19 @@ def test_import_entry_has_referrals_with_entities(self):
job_notify = Job.objects.get(target=entry, operation=JobOperation.NOTIFY_CREATE_ENTRY.value)
self.assertEqual(job_notify.status, Job.STATUS["DONE"])

with self.assertLogs(logger=Logger, level=logging.WARNING) as warning_log:
fp = self.open_fixture_file("import_data_v2.yaml")
resp = self.client.post("/entry/api/v2/import/", fp.read(), "application/yaml")
fp.close()
print(warning_log.output)
self.assertEqual(
warning_log.output,
[
"WARNING:airone:ambiguous object given: entry name(r-0), "
"entity names(['ref_entity', 'ref_entity2'])"
],
)

def test_import_invalid_data(self):
# nothing data
resp = self.client.post("/entry/api/v2/import/", None, "application/yaml")
Expand Down

0 comments on commit 153ffcb

Please sign in to comment.