Skip to content

Commit

Permalink
Merge pull request #151 from pivotal-energy-solutions/add_latest_support
Browse files Browse the repository at this point in the history
Please support latest()
  • Loading branch information
macro1 committed Jan 1, 2015
2 parents 634040f + fe406c3 commit 6fd1b65
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
5 changes: 3 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Changes
=======

1.5.x (YYYY-MM-DD)
------------------
tip (unreleased)
----------------
- Do NOT delete the history elements when a user is deleted.
- Add support for ``latest``

1.5.3 (2014-11-18)
------------------
Expand Down
1 change: 1 addition & 0 deletions simple_history/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def get_meta_options(self, model):
"""
meta_fields = {
'ordering': ('-history_date', '-history_id'),
'get_latest_by': 'history_date',
}
if self.user_set_verbose_name:
name = self.user_set_verbose_name
Expand Down
37 changes: 37 additions & 0 deletions simple_history/tests/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,40 @@ def test_migrations_include_order(self):
found = True
self.assertEqual(type(field), models.IntegerField)
assert found, '_order not in fields ' + repr(model_state.fields)


class TestLatest(TestCase):
""""Test behavior of `latest()` without any field parameters"""

def setUp(self):
poll = Poll.objects.create(question="Does `latest()` work?", pub_date=yesterday)
poll.pub_date = today
poll.save()

def write_history(self, new_attributes):
poll_history = HistoricalPoll.objects.all()
for historical_poll, new_values in zip(poll_history, new_attributes):
for fieldname, value in new_values.items():
setattr(historical_poll, fieldname, value)
historical_poll.save()

def test_ordered(self):
self.write_history([
{'pk': 1, 'history_date': yesterday},
{'pk': 2, 'history_date': today},
])
assert HistoricalPoll.objects.latest().pk == 2

def test_jumbled(self):
self.write_history([
{'pk': 1, 'history_date': today},
{'pk': 2, 'history_date': yesterday},
])
assert HistoricalPoll.objects.latest().pk == 1

def test_sameinstant(self):
self.write_history([
{'pk': 1, 'history_date': yesterday},
{'pk': 2, 'history_date': yesterday},
])
assert HistoricalPoll.objects.latest().pk == 1

0 comments on commit 6fd1b65

Please sign in to comment.