Code review comment for lp:~jkakar/launchpadlib/testing-support

Revision history for this message
Jamu Kakar (jkakar) wrote :

You can get a readable diff with this command:

bzr diff -r ancestor:lp:launchpadlib `bzr st -S -r ancestor:lp:launchpadlib|cut -d ' ' -f 3|grep -v xml`|less

This branch introduces a new launchpadlib.testing.launchpad module
that contains a FakeLaunchpad. This is a fake version of
launchpadlib.launchpad.Launchpad for use in unit tests. The basic
idea is that you set sample data on the fake API in the setup phase
of your test, for example:

    def test_me(self):
        credentials = object()
        launchpad = FakeLaunchpad(credentials)
        launchpad.me = dict(name="foo", display_name="Foo")
        self.assertEqual("foo", launchpad.me.name)
        self.assertEqual("Foo", launchpad.me.display_name)

Objects that would normally be returned by the API are represented
as dicts, as can be seen above with the person set on
'launchpad.me'. The 'name' and 'display_name' attributes are
validated against the WADL definition to make sure that they are
allowed to exist and to make sure they contain valid values. Only
partial objects need to be supplied, to make it easy to write tests
without creating complete sample data sets. There are tests in the
branch that should help you make sense of the implementation.

Other notes:

- Method parameters are not validated at all.

- There's no support for deleting sample data set on the fake API
  instance.

- The implementation is a bit hackish, hard to understand and, in
  some places, relies on heuristics.

- I've tried as much as possible to mimic the behaviour of the real
  Launchpad instance, but there are no tests that ensure the fake
  and real versions of the API behave the same way. It'd be worth
  adding this kind of thing in the future.

- I haven't tried to use this to write tests for a real application
  yet, so there may some obvious things missing.

- There is no user documentation. It'd be good to add a doctest in
  the future that describes and demonstrates the behaviour of a
  FakeLaunchpad instance.

« Back to merge proposal