Skip to content

Implement the ability to diff HistoricalRecords #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 11, 2018

Conversation

kseever
Copy link
Contributor

@kseever kseever commented Jul 10, 2018

Description

This PR adds the ability to produce the difference between two HistoricalRecord instances.

Related Issue

#244

Motivation and Context

This change allows users to construct timelines of changes for a given record.

How Has This Been Tested?

Unit tests

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

(Credit goes to leportella for the initial commit)

Copy link

@rossmechanic rossmechanic left a comment

Choose a reason for hiding this comment

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

Small changes/feedback. Looks great tho!

History Diffing
-------------------

When you have two instances of the same HistoricalRecord (such as the HistoricalPoll example above),

Choose a reason for hiding this comment

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

Format HistoricalRecord and HistoricalPoll

-------------------

When you have two instances of the same HistoricalRecord (such as the HistoricalPoll example above),
you can perform diffs to see what changed. This will result in a ModelDelta containing properties including

Choose a reason for hiding this comment

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

Format ModelDelta

-------------------

When you have two instances of the same HistoricalRecord (such as the HistoricalPoll example above),
you can perform diffs to see what changed. This will result in a ModelDelta containing properties including

Choose a reason for hiding this comment

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

This will result in a ModelDelta with the following properties:

  1. A of changes between each of the two historical records
  2. A list with the names of all fields that differed from one record to the other
  3. The old and new records

p.save()

new_record, old_record = p.history.all()
delta = new_record.diff_against(old_record)

Choose a reason for hiding this comment

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

Might want to add some more code examples that show what that delta object contains – I wouldn't know what it has without looking through the code or inspecting the instance

class HistoricalChanges(object):
def diff_against(self, old_history):
if not isinstance(old_history, type(self)):
raise TypeError(("unsupported operand type(s) for diffing: "

Choose a reason for hiding this comment

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

Can remove the operand terminology here as we're no longer overriding -

type(self),
type(old_history)))

model_delta_class = getattr(self, 'model_delta_class', ModelDelta)

Choose a reason for hiding this comment

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

This is here so that we can potentially override the default ModelDelta with a different one? How would a user be able to set that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like setting it would be set as an attribute model_delta_class on the class this is being mixed into. I don't see a compelling reason to support that so I'm happy to remove.

@rossmechanic
Copy link

Can you also add this to the CHANGES.rst file under Unreleased?

@codecov-io
Copy link

codecov-io commented Jul 11, 2018

Codecov Report

Merging #416 into master will increase coverage by 0.1%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #416     +/-   ##
=========================================
+ Coverage   97.21%   97.32%   +0.1%     
=========================================
  Files          14       14             
  Lines         646      672     +26     
  Branches       89       93      +4     
=========================================
+ Hits          628      654     +26     
  Misses          9        9             
  Partials        9        9
Impacted Files Coverage Δ
simple_history/models.py 98.76% <100%> (+0.14%) ⬆️

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 174922a...cdb0e15. Read the comment docs.

@atodorov
Copy link
Contributor

@rossmechanic can you folks release a new package version on PyPI containing this change? I would really like to use it without forking or copying pieces of the code. Thanks.

@rossmechanic
Copy link

rossmechanic commented Jul 19, 2018

@atodorov sure. I wanted to get #396 in before, but I'll do a release later today. You could also pip install this git repo directly.

@rossmechanic
Copy link

@atodorov Just released version 2.3.0

Sign up for free to 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.

4 participants