Skip to content
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

Remove the need for a custom __str__ method #7

Merged
merged 4 commits into from Oct 10, 2016
Merged

Remove the need for a custom __str__ method #7

merged 4 commits into from Oct 10, 2016

Conversation

@c-w
Copy link
Contributor

@c-w c-w commented Oct 10, 2016

Resolves #5

@coveralls
Copy link

@coveralls coveralls commented Oct 10, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling e716157 on c-w:master into df190a5 on alexk307:master.

Copy link
Owner

@alexk307 alexk307 left a comment

Great job! Just a few style comments, but it looks good :) Thank you for contributing.

@@ -0,0 +1,17 @@
class FakeRedisClient(object):
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

I'm not so sure that we need this when we can just mock the Redis Wrapper itself. See https://github.com/alexk307/redis_cache/blob/master/tests/test_redis_cache.py#L18 for example

Copy link
Contributor Author

@c-w c-w Oct 10, 2016

Done.

argument on an object that does not implement a __str__ method
"""
redis_cache = RedisCache(self.address, self.port)
redis_cache.redis_client = FakeRedisClient()
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

instead of monkey patching yourself, you can just patch using the mock library :)

Copy link
Contributor Author

@c-w c-w Oct 10, 2016

Done.


primitive_argument = 'cache hit test'

instance1 = TestClass()
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

Maybe we should consider putting this in a loop instead of just calling the class/method 3 times. This is sort of a data driven test, so I think it should possibly be treated as such

Copy link
Contributor Author

@c-w c-w Oct 10, 2016

Found a way to make this test simpler and rationalized it with the other test -- less code is better!

state2_instance1 = TestClass(state2)
state2_instance2 = TestClass(state2)

value1 = state1_instance1.some_method()
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

Could you comment a bit here as to what you're testing? It does not seem obvious on first glance

Copy link
Contributor Author

@c-w c-w Oct 10, 2016

Made this test a bit more explicit.


try:
instance_namespace = arg.__class__.__name__
instance_state = sorted((field, _argument_to_string(value))
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

So this generates a cache key based on the object's name and the instance variables that it contains? And it gets those instance variables recursively, so if the instance variables are objects with state themselves, they also get expressed?

Copy link
Contributor Author

@c-w c-w Oct 10, 2016

Yes, that's correct. I re-structured the code and added some more comments to make this clearer.

c-w added 2 commits Oct 10, 2016
- Rationalize the complex/simple argument tests to remove duplication.
- Add some more explanations of what's being tested.
- Add some more comments about what's going on and why we need the
  complexity/recursion.
- Minimize the scope of the try/catch block.
@coveralls
Copy link

@coveralls coveralls commented Oct 10, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling 6c6984f on c-w:master into df190a5 on alexk307:master.

Copy link
Owner

@alexk307 alexk307 left a comment

just a quick comment!

self.assertEqual(instance1_return1, argument)
self.assertEqual(instance1_return2, argument)
self.assertEqual(instance1_return3, argument)
self.assertEqual(instance.call_count[argument], 1)
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

That makes a lot more sense! Thank you

self.assertEqual(instance1_return3, argument)
self.assertEqual(instance.call_count[argument], 1)

# the cache should also be hit for calls to the same method on a new
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

beautiful

state2 = SimpleObject('some complex type state', 123)

def cache_get(cache_key):
if cache_key == '-5024234577788600450':
Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

is there anyway that we can calculate these hash values instead of hardcoding them?

Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

other than this LGTM!

Copy link
Owner

@alexk307 alexk307 Oct 10, 2016

also, FWIW I'm thinking of not doing these numerical hash values, but instead using the method names/signatures as the hash keys. This way I can implement some sort of cache invalidation/busting.

Copy link
Contributor Author

@c-w c-w Oct 10, 2016

The cache key formatting is now a little bit more complicated so this required another refactor: rationalize the cache key formatting logic into one place so that it can be re-used from the unit tests.

This rationalizes all the logic required to create a cache key into one
place and therewith lets us remove the hard-coded or copy/pasted key
formatting logic in the unit tests.
@coveralls
Copy link

@coveralls coveralls commented Oct 10, 2016

Coverage Status

Coverage remained the same at 100.0% when pulling 01c64db on c-w:master into df190a5 on alexk307:master.

@ghost
Copy link

@ghost ghost commented Oct 10, 2016

Lets give it a go!

@ghost
Copy link

@ghost ghost commented Oct 10, 2016

Oops apparently my mobile thinks I'm on this account. I'll merge when I get home

@alexk307
Copy link
Owner

@alexk307 alexk307 commented Oct 10, 2016

@c-w merged, thanks again :)

@alexk307 alexk307 merged commit 4b47661 into alexk307:master Oct 10, 2016
2 checks passed
@alexk307
Copy link
Owner

@alexk307 alexk307 commented Oct 10, 2016

@c-w if you have any projects that need contributing, let me know, I'd be glad to lend a hand as well. Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants