Skip to content

Commit

Permalink
Added a prototype for the model cache version calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
thedrow committed Mar 28, 2015
1 parent 67c51a3 commit e28fe05
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cache_version/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ModelCacheVersionCalculator(object):
def get_version(model):
pass
19 changes: 19 additions & 0 deletions cache_version/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import hashlib
from inspect_model import InspectModel

def get_model_cache_version(model):
inspected_model = InspectModel(model)

fields_and_types = {}

for field in inspected_model.fields:
fields_and_types[field] = getattr(model, field)

for field in inspected_model.relation_fields:
fields_and_types[field] = getattr(model, field)


for field in inspected_model.many_fields:
fields_and_types[field] = getattr(model, field)

return hashlib.sha1(str(fields_and_types)).hexdigest()
1 change: 1 addition & 0 deletions requirements/common.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Django>=1.4
django-inspect-model>=0.7.0
django-compat>=1.0.2
2 changes: 2 additions & 0 deletions requirements/testing.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pytest>=2.7.0
pytest-django>=2.8.0
pytest-describe>=0.10.0
pytest-mock>=0.4.1
27 changes: 27 additions & 0 deletions tests/unit/test_get_model_cache_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
import mock

from cache_version.models import get_model_cache_version

def describe_get_model_cache_version():
@pytest.fixture
def model():
return mock.sentinel.MODEL

@pytest.fixture
def mocked_inspect_model(mocker):
return mocker.patch('cache_version.models.InspectModel')

@pytest.fixture
def mocked_hashlib(mocker):
return mocker.patch('cache_version.models.hashlib')

def it_should_inspect_the_target_model(model, mocked_inspect_model):
get_model_cache_version(model)

mocked_inspect_model.assert_called_once_with(model)

def it_should_return_a_hash_of_all_field_names_and_field_types(model, mocked_inspect_model, mocked_hashlib):
get_model_cache_version(model)

mocked_hashlib.sha1(mock.ANY).hexdigest.assert_called_once_with()

0 comments on commit e28fe05

Please sign in to comment.