From 3fe82db8d5ed48ba2c9cfb86c54bca3bd0bd73b3 Mon Sep 17 00:00:00 2001 From: Matt Bertrand Date: Mon, 8 Jul 2024 09:50:23 -0400 Subject: [PATCH] Use ovewrite=True when calling pluggy function from upsert_offered_by --- learning_resources/utils.py | 4 ++-- learning_resources/utils_test.py | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/learning_resources/utils.py b/learning_resources/utils.py index 5a82bce6c3..e22d43230d 100644 --- a/learning_resources/utils.py +++ b/learning_resources/utils.py @@ -264,10 +264,10 @@ def upsert_offered_by_data(): for offeror in offered_by_json: offeror_fields = offeror["fields"] offered_by, _ = LearningResourceOfferor.objects.update_or_create( - name=offeror_fields["name"], + code=offeror_fields["code"], defaults=offeror_fields, ) - offeror_upserted_actions(offered_by) + offeror_upserted_actions(offered_by, overwrite=True) offerors.append(offeror_fields["name"]) invalid_offerors = LearningResourceOfferor.objects.exclude(name__in=offerors) for offeror in invalid_offerors: diff --git a/learning_resources/utils_test.py b/learning_resources/utils_test.py index f80ea5c15b..8e83946a5e 100644 --- a/learning_resources/utils_test.py +++ b/learning_resources/utils_test.py @@ -19,7 +19,11 @@ LearningResourceRunFactory, LearningResourceTopicFactory, ) -from learning_resources.models import LearningResourcePlatform, LearningResourceTopic +from learning_resources.models import ( + LearningResourceOfferor, + LearningResourcePlatform, + LearningResourceTopic, +) from learning_resources.utils import ( add_parent_topics_to_learning_resource, upsert_topic_data, @@ -305,3 +309,18 @@ def test_add_parent_topics_to_learning_resource(fixture_resource): fixture_resource.refresh_from_db() assert fixture_resource.topics.filter(pk=main_topic.id).exists() + + +def test_upsert_offered_by(mocker): + """Test that upsert_offered_by_data creates expected offerors and triggers pluggy""" + mock_upsert = mocker.patch("learning_resources.utils.offeror_upserted_actions") + with Path.open(Path(__file__).parent / "fixtures" / "offered_by.json") as inf: + offered_by_json = json.load(inf) + utils.upsert_offered_by_data() + assert LearningResourceOfferor.objects.count() == len(offered_by_json) + for offered_by_data in offered_by_json: + offeror = LearningResourceOfferor.objects.get( + code=offered_by_data["fields"]["code"] + ) + assert offeror.name == offered_by_data["fields"]["name"] + mock_upsert.assert_any_call(offeror, overwrite=True)