Archive

Archive for September, 2015

TypeError: Need a valid target to patch. You supplied: ‘SomeClass’

September 21, 2015 Leave a comment

Problem:

I needed to mock out a method in a class that I was clearly importing. But when I tried to patch it gave me the following error where “SomeClass” is the name of the class I was trying to mock out.

TypeError: Need a valid target to patch. You supplied: ‘SomeClass’

Solution:
A very clear explanation of what I was doing wrong was on SO

http://stackoverflow.com/questions/16060724/patch-why-wont-the-relative-patch-target-name-work

Advertisements
Categories: python Tags:

Unittest using assertRaises

September 18, 2015 Leave a comment

Problem:
I needed to test that a function returns an exception.

Solution:
Sample code

# sample.py
def sum(a, b):
    total = None
    try:
        total = a + b
    except TypeError:
        raise TypeError("Integers only")

    return total

and

# test_assert.py
import unittest

from sample import sum


class TestSum(unittest.TestCase):
    def test_total(self):
        self.assertEquals(sum("3", 2), 4)

if __name__ == "__main__":
    unittest.main()

Running the test to check that an assert is raised

$ python test_assert.py
E
======================================================================
ERROR: test_total (__main__.TestSum)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test_assert.py", line 7, in test_total
    self.assertEquals(sum("3", 2), 4)
  File "/Users/rodnee/sample.py", line 6, in sum
    raise TypeError("Integers only")
TypeError: Integers only

----------------------------------------------------------------------
Ran 1 test in 0.000s

Changing the unit test to catch the raised exception

# test_assert.py
import unittest

from sample import sum


class TestSum(unittest.TestCase):
    def test_total(self):
        self.assertEquals(sum(2, 2), 4)
        # First argument is the Exception to be caught ie TypeError
        # Second argument is the method under test ie sum
        # Third to x arguments are the values to pass onto sum()
        self.assertRaises(TypeError, sum, "3", 2)

if __name__ == "__main__":
    unittest.main()

Running the updated unit test produces

$ python test_assert.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Source:
http://www.lengrand.fr/2011/12/pythonunittest-assertraises-raises-error/
https://pymotw.com/2/exceptions/
http://eu.wiley.com/WileyCDA/WileyTitle/productCd-1118901223.html

PostreSQL Magic

September 11, 2015 Leave a comment
Categories: Interesting, postgresql

Code Combat – Learn to code by playing a game

September 10, 2015 Leave a comment

codecombat_home

codecombat

This is an addictive and fun way to learn to program.

Categories: Interesting

NATS

September 9, 2015 Leave a comment

NATS is an open-source, high-performance, lightweight cloud native messaging system.

NATS was created by Derek Collison, Founder/CEO of Apcera who has spent 20+ years designing, building, and using publish-subscribe messaging systems. Unlike traditional enterprise messaging systems, NATS has an always-on dial tone that does whatever it takes to remain available. This forms a great base for building modern, reliable, and scalable cloud and distributed systems.

http://nats.io/

Categories: Interesting Tags:

Show current git branch on your bash prompt

September 8, 2015 Leave a comment

I started learning to use git and the examples I saw showed the branches on the command prompt which I think is very cool and helpful. And of course, I wanted that too.

In 3 simple steps.

1. Download
curl https://github.com/git/git/raw/master/contrib/completion/git-completion.bash -OL
2. Add the following to .bashrc

# Add Git configurations
source git-completion.bash
export PS1='\[\033[1;34m\]\!\[\033[0m\] \[\033[1;35m\]\u\[\033[0m\]:\[\033[1;35m\]\W\[\033[0m\] \[\033[1;92m\]$(__git_ps1 "(%s)")\[\033[0m\]\$ '
export GIT_PS1_SHOWDIRTYSTATE=1

3. Restart your terminal so that the changes take place or source your bashrc YMMV.

This is just the tip of the iceberg. You can get much much more at the resources listed below
Source:

http://blog.taylormcgann.com/2012/06/19/show-current-git-branch-your-bash-prompt/#comment-12515
http://thepugautomatic.com/2008/12/git-dirty-prompt/

Interesting – Using Selenium

September 8, 2015 Leave a comment