Pylons+memcacheDB...削除とそのテスト

そして削除およびそのテストを記述。

コントローラphone.py

    def delete(self, id = None):
        if id is None:
            abort(404)

        ser = self.mc.get(id.encode('ascii'))

        if ser is None:
            abort(404)

        u = jsonpickle.Unpickler()
        phone = u.restore(ser)

        if not isinstance(phone, model.Phone):
            abort(404)

        self.mc.delete(phone.id)
        return render('/derived/phone/deleted.html')

テンプレート templates/derived/phone/deleted.html

<%inherit file="/base/index.html" />

<%def name="heading()">
    <h1 class="main">Record Deleted</h1>
</%def>

<p>This record has been deleted.</p>

テストtest_phone.py

    def test_delete(self):
        """Tests that valid data is deleted from the database, that the response
        redirects to the view() action that a flash message is set in the session"""
        response = self.app.get(
            url = url_for(controller = 'phone', action = 'delete', id = self._id),
            )

        mc = memcache.Client([config['app_conf']['memcachedb.servers']], debug = 0)
        ser = mc.get(self._id)

        assert ser is None
        assert '<p>This record has been deleted.</p>' in response

テスト実行

$ nosetests
..........
----------------------------------------------------------------------
Ran 10 tests in 1.554s

OK

Gitコミット先