Skip to content

Misc fixes to serializers' handling of attempted updates to missing content - #2682

Merged
rtibbles merged 3 commits into
learningequality:developfrom
micahscopes:assessment-item-serializer-use-tuple-id
Dec 15, 2020
Merged

Misc fixes to serializers' handling of attempted updates to missing content#2682
rtibbles merged 3 commits into
learningequality:developfrom
micahscopes:assessment-item-serializer-use-tuple-id

Conversation

@micahscopes

@micahscopesmicahscopes commented Dec 15, 2020

Copy link
Copy Markdown
Contributor

Description

If the front-end tries to update an assessment item that's missing from the back-end, this PR makes sure that a useful error is returned, instead of a confusing one.

This us a followup on #2655.

Issue Addressed

This will make #2679 and #2603 less severe by adding proper error handling for attempts to update missing items.

Steps to Test

  • run the included test

Implementation Notes

At a high level, how did you implement this?

  • Added a unit test for attempts to update missing assessment items
  • Stopped remove_id_values from mutating changes unexpectedly
  • Used id_value_lookup method for consistent keying of assessment items
  • Changed id_value_lookup to use a tuple instead of a string, for simpler conversion from front-end change["key"] array representation.
  • Fixed calculation of missing_keys to generate errors for missing items.

@micahscopes
micahscopesforce-pushed the assessment-item-serializer-use-tuple-id branch from 5382838 to d77d490CompareDecember 15, 2020 00:05
@micahscopes
micahscopes marked this pull request as draft December 15, 2020 00:07
@micahscopes
micahscopesforce-pushed the assessment-item-serializer-use-tuple-id branch from 3cd8470 to 802c3a6CompareDecember 15, 2020 01:05

# For the combined index, use any related objects' primary key
combined_index = (idx.pk if hasattr(idx, 'pk') else idx for idx in id_values)
return tuple(combined_index)

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to switch to using a tuple in order to make doing stuff like this simpler and less brittle.

@codecov

codecovBot commented Dec 15, 2020

Copy link
Copy Markdown

Codecov Report

Merging #2682 (802c3a6) into develop (541e175) will increase coverage by 0.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@ Coverage Diff @@## develop #2682 +/- ##
===========================================
+ Coverage 85.14% 85.16% +0.02% 
===========================================
Files 299 299 Lines 15079 15086 +7 ===========================================
+ Hits 12839 12848 +9 + Misses 2240 2238 -2 
Impacted FilesCoverage Δ
...tentcuration/tests/viewsets/test_assessmentitem.py88.37% <100.00%> (+0.17%)⬆️
...uration/contentcuration/viewsets/assessmentitem.py100.00% <100.00%> (ø)
contentcuration/contentcuration/viewsets/base.py89.40% <100.00%> (+0.53%)⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 541e175...802c3a6. Read the comment docs.


if len(all_validated_data_by_id) != len(updated_keys):
self.missing_keys = updated_keys.difference(
set(all_validated_data_by_id.keys())

@micahscopesmicahscopesDec 15, 2020

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was backwards, which meant that missing_keys was always ending up empty.

@micahscopes
micahscopes marked this pull request as ready for review December 15, 2020 01:20

@rtibblesrtibbles left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests pass, and new test passes. Code looks good.

id_values = (self.get_value(data, attr) for attr in id_attr)

# For the combined index, use any related objects' primary key
combined_index = (idx.pk if hasattr(idx, 'pk') else idx for idx in id_values)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hrm, I'm surprised this wasn't breaking previously - was it stringifying the content node before?

@micahscopesmicahscopesDec 15, 2020

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rtibbles:
Looks like the object being returned there, a rest_framework.relations.PKOnlyObject, returns its pk from its __str__ method:

ipdb> [(i, str(i)) for i in tuple(id_values)] [(<rest_framework.relations.PKOnlyObject object at 0x7f0a176501d0>, '4ce295a9caf94b28b18376e0452d2507'),
('5361ba5142ba4695b27256e5b74adfdd', '5361ba5142ba4695b27256e5b74adfdd')]

So I suppose this could be simplified a bit by just doing:

return tuple(str(self.get_value(data, attr)) for attr in id_attr)

The way it is now has the benefit of being explicit, though.

@rtibbles
rtibbles merged commit 8d857b5 into learningequality:developDec 15, 2020
@micahscopesmicahscopes linked an issue Dec 15, 2020 that may be closed by this pull request
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Asssessment item sync changes not exposing keys properly KeyError: 'contentnode_id' Assessment item sync changes get stuck with KeyError: 'id'

2 participants

@micahscopes@rtibbles