-
Notifications
You must be signed in to change notification settings - Fork 7
Remove the need for a custom __str__ method #7
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
Conversation
alexk307
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job! Just a few style comments, but it looks good :) Thank you for contributing.
tests/fakes.py
Outdated
| @@ -0,0 +1,17 @@ | |||
| class FakeRedisClient(object): | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
tests/test_redis_cache.py
Outdated
| argument on an object that does not implement a __str__ method | ||
| """ | ||
| redis_cache = RedisCache(self.address, self.port) | ||
| redis_cache.redis_client = FakeRedisClient() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of monkey patching yourself, you can just patch using the mock library :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
tests/test_redis_cache.py
Outdated
|
|
||
| primitive_argument = 'cache hit test' | ||
|
|
||
| instance1 = TestClass() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found a way to make this test simpler and rationalized it with the other test -- less code is better!
tests/test_redis_cache.py
Outdated
| state2_instance1 = TestClass(state2) | ||
| state2_instance2 = TestClass(state2) | ||
|
|
||
| value1 = state1_instance1.some_method() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you comment a bit here as to what you're testing? It does not seem obvious on first glance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made this test a bit more explicit.
|
|
||
| try: | ||
| instance_namespace = arg.__class__.__name__ | ||
| instance_state = sorted((field, _argument_to_string(value)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's correct. I re-structured the code and added some more comments to make this clearer.
- 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.
alexk307
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beautiful
tests/test_redis_cache.py
Outdated
| state2 = SimpleObject('some complex type state', 123) | ||
|
|
||
| def cache_get(cache_key): | ||
| if cache_key == '-5024234577788600450': |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there anyway that we can calculate these hash values instead of hardcoding them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
other than this LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
|
Lets give it a go! |
|
Oops apparently my mobile thinks I'm on this account. I'll merge when I get home |
|
@c-w merged, thanks again :) |
|
@c-w if you have any projects that need contributing, let me know, I'd be glad to lend a hand as well. Cheers! |
Resolves #5