<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PunNeng&#039;s playground</title>
	<atom:link href="http://edge.punneng.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://edge.punneng.net</link>
	<description>Hacking, playing and shooting</description>
	<lastBuildDate>Wed, 23 Jan 2013 19:11:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Hello Pyramid [Part 2] – Test First</title>
		<link>http://edge.punneng.net/2013/01/hello-pyramid-part-2-test-first/</link>
		<comments>http://edge.punneng.net/2013/01/hello-pyramid-part-2-test-first/#comments</comments>
		<pubDate>Wed, 23 Jan 2013 19:11:52 +0000</pubDate>
		<dc:creator>PunNeng</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[pyramid]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[unittest]]></category>

		<guid isPermaLink="false">http://edge.punneng.net/?p=65</guid>
		<description><![CDATA[Currently, there are some testcases from the previous post. They need to be implemented controllers. Model There is an important thing I need to introduce first, SQLAlchemy. Not only recording into the database, transacion is also involved. If we take a look at a code template which is generated from a tool of Pyramid, there [...]]]></description>
			<content:encoded><![CDATA[<p>Currently, there are some testcases from <a title="Hello Pyramid [Part 1] – Test First" href="http://weblog.punneng.net/2012/12/hello-pyramid-part-1-test-first/" target="_blank">the previous post</a>. They need to be implemented controllers.</p>
<h4>Model</h4>
<p>There is an important thing I need to introduce first, SQLAlchemy. Not only recording into the database, transacion is also involved. If we take a look at a code template which is generated from a tool of Pyramid, there is the transaction that is different in my source code.</p>
<p>I start at declaring some fields.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">Base = declarative_base<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Todo<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:
    __tablename__ = <span style="color: #483d8b;">'todos'</span>
    <span style="color: #008000;">id</span> = Column<span style="color: black;">&#40;</span>Integer, primary_key=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    task = Column<span style="color: black;">&#40;</span>String<span style="color: black;">&#40;</span><span style="color: #ff4500;">512</span><span style="color: black;">&#41;</span>, nullable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    created_at = Column<span style="color: black;">&#40;</span>DateTime, server_default=text<span style="color: black;">&#40;</span><span style="color: #483d8b;">'NOW()'</span><span style="color: black;">&#41;</span>, nullable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    done_at = Column<span style="color: black;">&#40;</span>DateTime<span style="color: black;">&#41;</span>
    priority = Column<span style="color: black;">&#40;</span>Integer, default=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># 1 =&amp;gt; the most priority, 10 =&amp;gt; not important now </span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, task, done_at=<span style="color: #008000;">None</span>, priority=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>Todo, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">task</span> = task
        <span style="color: #008000;">self</span>.<span style="color: black;">done_at</span> = done_at
        <span style="color: #008000;">self</span>.<span style="color: black;">priority</span> = priority</pre></div></div>

<p>There are the fields to identify some properties like id, what to do, when to be finished and priority.<br />
Another confusing is transaction</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">DBSession = scoped_session<span style="color: black;">&#40;</span>sessionmaker<span style="color: black;">&#40;</span>extension=ZopeTransactionExtension<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This code creates a database connection that passes ZopeTransactionExtension to scoped_session. ZopeTransactionExtension commits the transaction after finishing the processing the request. If there is any exception is raised then it will rollback.</p>
<p>The problem is if we follow an example on the site, it creates a transaction with transacton.manager. When it finishes work in transaction manager, it will commit so there is nothing to be rolled back. To add <a href="http://docs.sqlalchemy.org/en/latest/orm/session.html#sqlalchemy.orm.scoping.scoped_session.remove" target="_blank">DBSession.remove()</a> is for removing the data after testing finishes. The example in <a href="http://pyramid.readthedocs.org/en/latest/tutorials/wiki2/tests.html" target="_blank">this doc</a> doesn&#8217;t need to remove because it stores the data in sqlite which is a memory database. It removes automatically after testing.</p>
<p>Also, there is anothing to be added to make testing goes well. Let&#8217;s start at SQLAlchemy&#8217;s ORM method. Whatever it does through a SQLAlchemy&#8217;s session, it is like storing executed command to a stack and then execute all of them after returning in the function in the controller but what I expect is after saving, it should be retrived the result at that moment. Doesn&#8217;t need to wait until function returns.<br />
To do that, just flush</p>

<div class="wp_syntax"><div class="code"><pre class="pyrhon" style="font-family:monospace;">DBSession.flush()</pre></div></div>

<p>After flushing then todo&#8217;s instance is set some values such as there is a new todo that there is no id, it is a creating in the database. After flushing, id will be set as the id in row in the database. So if no flushing then no id to be set, we don&#8217;t know if it save passed. If fail, it will raise an exception that we don&#8217;t what happen exactly. It is just an error message from the database that is not friendly to the user. To catch the exception is out of the code&#8217;s scope also means we can&#8217;t catch the exception if no flushing.</p>
<p>Let&#8217;s back to the code. It&#8217;s not funny to flush every time after updating or deleting so add some helper methods and add some validation methods that SQLAlchemy doesn&#8217;t have.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> AppBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    _errors = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> save<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">is_valid</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                DBSession.<span style="color: black;">add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
                DBSession.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> IntegrityError:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">is_valid</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                DBSession.<span style="color: black;">merge</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
                DBSession.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> IntegrityError:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> delete<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            DBSession.<span style="color: black;">delete</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
            DBSession.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> IntegrityError: <span style="color: #808080; font-style: italic;"># still dont have an idea to test this failure</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span></pre></div></div>

<p>Below is the validation added by myself.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    @<span style="color: #008000;">property</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> errors<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._errors
&nbsp;
    @<span style="color: #008000;">property</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> is_valid<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">bool</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._errors<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> validate<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, validator, key, value<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>._errors:
            <span style="color: #008000;">self</span>._errors = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            validator.<span style="color: black;">to_python</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> Invalid <span style="color: #ff7700;font-weight:bold;">as</span> e:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>._errors.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>._errors<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
            <span style="color: #008000;">self</span>._errors<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>e<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>There is a hooked method named &#8216;validates&#8217; which is a decorator. It is invoked on value assinging.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    @validates<span style="color: black;">&#40;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> validate_task<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># validate</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> value</pre></div></div>

<p>Task is a declared feild in Todo class and this method is in Todo class too.<br />
The code below show how to extend this class.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">Base = declarative_base<span style="color: black;">&#40;</span>cls=AppBase<span style="color: black;">&#41;</span></pre></div></div>

<p>And here is the whole code in models.py</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: black;">&#40;</span>
    Column,
    Integer,
    Text,
    String,
    DateTime,
    Integer
    <span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">ext</span>.<span style="color: black;">declarative</span> <span style="color: #ff7700;font-weight:bold;">import</span> declarative_base
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">sql</span>.<span style="color: black;">expression</span> <span style="color: #ff7700;font-weight:bold;">import</span> text
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">exc</span> <span style="color: #ff7700;font-weight:bold;">import</span> IntegrityError
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">orm</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: black;">&#40;</span>
    scoped_session,
    sessionmaker,
    validates,
    <span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> zope.<span style="color: black;">sqlalchemy</span> <span style="color: #ff7700;font-weight:bold;">import</span> ZopeTransactionExtension
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> formencode <span style="color: #ff7700;font-weight:bold;">import</span> validators, Invalid
&nbsp;
DBSession = scoped_session<span style="color: black;">&#40;</span>sessionmaker<span style="color: black;">&#40;</span>extension=ZopeTransactionExtension<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
<span style="color: #808080; font-style: italic;">#DBSession = scoped_session(sessionmaker())</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AppBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    _errors = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> save<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">is_valid</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                DBSession.<span style="color: black;">add</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
                DBSession.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> IntegrityError:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">is_valid</span>:
            <span style="color: #ff7700;font-weight:bold;">try</span>:
                DBSession.<span style="color: black;">merge</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
                DBSession.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
            <span style="color: #ff7700;font-weight:bold;">except</span> IntegrityError:
                <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> delete<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            DBSession.<span style="color: black;">delete</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
            DBSession.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> IntegrityError: <span style="color: #808080; font-style: italic;"># still dont have an idea to test this failure</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">False</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">True</span>
&nbsp;
    @<span style="color: #008000;">property</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> errors<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._errors
&nbsp;
    @<span style="color: #008000;">property</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> is_valid<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">bool</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._errors<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> validate<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, validator, key, value<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>._errors:
            <span style="color: #008000;">self</span>._errors = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            validator.<span style="color: black;">to_python</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> Invalid <span style="color: #ff7700;font-weight:bold;">as</span> e:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>._errors.<span style="color: black;">get</span><span style="color: black;">&#40;</span>key<span style="color: black;">&#41;</span>:
                <span style="color: #008000;">self</span>._errors<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span> = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
            <span style="color: #008000;">self</span>._errors<span style="color: black;">&#91;</span>key<span style="color: black;">&#93;</span>.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>e<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
Base = declarative_base<span style="color: black;">&#40;</span>cls=AppBase<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Todo<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:
    __tablename__ = <span style="color: #483d8b;">'todos'</span>
    <span style="color: #008000;">id</span> = Column<span style="color: black;">&#40;</span>Integer, primary_key=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    task = Column<span style="color: black;">&#40;</span>String<span style="color: black;">&#40;</span><span style="color: #ff4500;">512</span><span style="color: black;">&#41;</span>, nullable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    created_at = Column<span style="color: black;">&#40;</span>DateTime, server_default=text<span style="color: black;">&#40;</span><span style="color: #483d8b;">'NOW()'</span><span style="color: black;">&#41;</span>, nullable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
    done_at = Column<span style="color: black;">&#40;</span>DateTime<span style="color: black;">&#41;</span>
    priority = Column<span style="color: black;">&#40;</span>Integer, default=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span> <span style="color: #808080; font-style: italic;"># 1 =&amp;gt; the most priority, 10 =&amp;gt; not important now </span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, task, done_at=<span style="color: #008000;">None</span>, priority=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">super</span><span style="color: black;">&#40;</span>Todo, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">task</span> = task
        <span style="color: #008000;">self</span>.<span style="color: black;">done_at</span> = done_at
        <span style="color: #008000;">self</span>.<span style="color: black;">priority</span> = priority
&nbsp;
    @validates<span style="color: black;">&#40;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> validate_task<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">validate</span><span style="color: black;">&#40;</span>validators.<span style="color: black;">String</span><span style="color: black;">&#40;</span>not_empty=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>, key, value<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> value
&nbsp;
    @validates<span style="color: black;">&#40;</span><span style="color: #483d8b;">'priority'</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> validate_priority<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, key, value<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">validate</span><span style="color: black;">&#40;</span>validators.<span style="color: black;">Int</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, key, value<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> value</pre></div></div>

<p>The validation helper I chosed is a href=&#8221;http://www.formencode.org/en/latest/&#8221; target=&#8221;_blank&#8221;>formencode</a>. As in the example, just needs validators.String(not_empty=True) means not allowed empty value and validators.Int() which is needed only Int.</p>
<h4>Controller</h4>
<p>Python frameworks often use word &#8216;views&#8217; instead controllers. I&#8217;m not used to it so I will call it &#8216;controller(s)&#8217; then. Let&#8217;s create the controller pacakge and put todos.py follow the test in the previous post.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> create</pre></div></div>

<p>There is a tricky part with the template engine. Because a bug on the builtin template engine, <a href="http://bugs.repoze.org/issue139" target="_blank">ZPT(Chameleon)</a>. The issue is using macro with doctype cause an error so I changed to mako</p>
<h5>Read</h5>
<p>Here is for listing.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> get_todo_set<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    todos = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>Todo.<span style="color: black;">done_at</span>==<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>.<span style="color: black;">order_by</span><span style="color: black;">&#40;</span>Todo.<span style="color: black;">priority</span>.<span style="color: black;">desc</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    done_todos = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: #008000;">filter</span><span style="color: black;">&#40;</span>Todo.<span style="color: black;">done_at</span><span style="color: #66cc66;">!</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>.<span style="color: black;">order_by</span><span style="color: black;">&#40;</span>Todo.<span style="color: black;">priority</span>.<span style="color: black;">desc</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> todos, done_todos
&nbsp;
@view_config<span style="color: black;">&#40;</span>route_name=<span style="color: #483d8b;">'todo_index'</span>, renderer=<span style="color: #483d8b;">'todos/index.mako'</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    todos, done_todos = get_todo_set<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">dict</span><span style="color: black;">&#40;</span>todos=todos, done_todos=done_todos<span style="color: black;">&#41;</span></pre></div></div>

<p>I add get_todo_set separately. It can be used anywhere. Then see the code in the testing. You will see</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'done_todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># test ordering</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">task</span>, <span style="color: #483d8b;">&quot;First task&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>.<span style="color: black;">task</span>, <span style="color: #483d8b;">&quot;Second task&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>There is a checking to a response that is retrived back from the controller.<br />
You can try seeing and compare the rest of the controlling mehtod with the testing.</p>
<p><a href="http://docs.pylonsproject.org/projects/pyramid/en/latest/api/view.html#pyramid.view.view_config">@view_config</a>, as a decorator set what to do. Paramerters in the code:</p>
<ul>
<li>route_name for linking URI to a function in the controller</li>
<li>renderer for assign a template file. It can be either xml or json and it will render the content-type as it is assigned.</li>
<li>request_method to assign the http request method to be received.</li>
<li>xhr to allow only XmlHttpRequest</li>
</ul>
<p>To test @view_config needs to be tested with functional test such as checking whether a status code is 200 or content-type is json.</p>
<h5>Create</h5>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@view_config<span style="color: black;">&#40;</span>route_name=<span style="color: #483d8b;">'todo_create'</span>, renderer=<span style="color: #483d8b;">'json'</span>, request_method=<span style="color: #483d8b;">'POST'</span>, xhr=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> create<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    todo = Todo<span style="color: black;">&#40;</span>
        task=request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'task'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>,
        priority=request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'priority'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
    <span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> todo.<span style="color: black;">save</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'errors'</span>: todo.<span style="color: black;">errors</span><span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>: todo.<span style="color: #008000;">id</span>, <span style="color: #483d8b;">'task'</span>: todo.<span style="color: black;">task</span>, <span style="color: #483d8b;">'priority'</span>:todo.<span style="color: black;">priority</span>, <span style="color: #483d8b;">'messages'</span>: <span style="color: #483d8b;">'%s has been created'</span> <span style="color: #66cc66;">%</span> todo.<span style="color: black;">task</span> <span style="color: black;">&#125;</span></pre></div></div>

<p>In the code, it needs only ajax request and needs to ensure that definitely create a todo. As explained above, id will be set after creating. I like cheking like this instead of leave the exception raised because I can check every case can happen.</p>
<h5>Update</h5>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@view_config<span style="color: black;">&#40;</span>route_name=<span style="color: #483d8b;">'todo_update'</span>, renderer=<span style="color: #483d8b;">'json'</span>, request_method=<span style="color: #483d8b;">'POST'</span>, xhr=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> update<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    todo_id = request.<span style="color: black;">matchdict</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'id'</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        todo = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">filter_by</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>=todo_id<span style="color: black;">&#41;</span>.<span style="color: black;">one</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> NoResultFound:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'errors'</span>: <span style="color: #483d8b;">&quot;No todo id: %s&quot;</span> <span style="color: #66cc66;">%</span> todo_id<span style="color: black;">&#125;</span>
&nbsp;
    todo.<span style="color: black;">task</span>=request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'task'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
    todo.<span style="color: black;">priority</span>=request.<span style="color: black;">POST</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'priority'</span>, <span style="color: #008000;">None</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> todo.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'errors'</span>: todo.<span style="color: black;">errors</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'task'</span>: todo.<span style="color: black;">task</span>, <span style="color: #483d8b;">'priority'</span>:todo.<span style="color: black;">priority</span>, <span style="color: #483d8b;">'messages'</span>: <span style="color: #483d8b;">'%s has been updated'</span> <span style="color: #66cc66;">%</span> todo.<span style="color: black;">task</span><span style="color: black;">&#125;</span></pre></div></div>

<p>The method to update is merge() by put an instance into DBSession, if id is already set then it will update. If not, it will create.</p>
<h5>Delete</h5>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">@view_config<span style="color: black;">&#40;</span>route_name=<span style="color: #483d8b;">'todo_delete'</span>, renderer=<span style="color: #483d8b;">'json'</span>, request_method=<span style="color: #483d8b;">'POST'</span>, xhr=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> delete<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>:
    todo_id = request.<span style="color: black;">matchdict</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'id'</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">try</span>:
        todo = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">filter_by</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>=todo_id<span style="color: black;">&#41;</span>.<span style="color: black;">one</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">except</span> NoResultFound:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'errors'</span>: <span style="color: #483d8b;">&quot;No todo id: %s&quot;</span> <span style="color: #66cc66;">%</span> todo_id<span style="color: black;">&#125;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> todo.<span style="color: black;">delete</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'errors'</span>: <span style="color: #483d8b;">&quot;%s can't be deleted&quot;</span> <span style="color: #66cc66;">%</span> todo.<span style="color: black;">task</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>: todo.<span style="color: #008000;">id</span>, <span style="color: #483d8b;">'messages'</span>: <span style="color: #483d8b;">'%s has been deleted'</span> <span style="color: #66cc66;">%</span> todo.<span style="color: black;">task</span><span style="color: black;">&#125;</span></pre></div></div>

<p>And the last one is deleting with a couple of error messages.</p>
<p>Actually, I wanted to work the UI out based on ajax but it took too long and I wanted to show the testing so I left it.</p>
<h5>Summary</h5>
<p>I&#8217;m happy with integration test. The tool like nosetest for doing the code coverage includes unittest is more convenient or unittest can test cover all of I want. It might be I just started so some code might not look ok especially SQLAlchemy part. </p>
<p>You can <a href="https://bitbucket.org/punneng/pyramid-testfirst/downloads" target="_blank">Download source code</a> or </p>
<pre>hg clone https://bitbucket.org/punneng/pyramid-testfirst</pre>
<p>I will find free time to try functional test.</p>
]]></content:encoded>
			<wfw:commentRss>http://edge.punneng.net/2013/01/hello-pyramid-part-2-test-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello Pyramid [Part 1] &#8211; Test First</title>
		<link>http://edge.punneng.net/2012/12/hello-pyramid-part-1-test-first/</link>
		<comments>http://edge.punneng.net/2012/12/hello-pyramid-part-1-test-first/#comments</comments>
		<pubDate>Fri, 21 Dec 2012 06:32:09 +0000</pubDate>
		<dc:creator>PunNeng</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[pyramid]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sqlalchemy]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://edge.punneng.net/?p=47</guid>
		<description><![CDATA[I&#8217;ve looked for a alternative web framework similar like Ruby on Rails and based on Python, not much configuration but able to be full configuration, possible to manage a routing and includes testing tools. I found Django, web2py and Pyramid. I&#8217;ve already known Django because I worked on it for 3 years. Also Pyramid looks [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve looked for a alternative web framework similar like Ruby on Rails and based on Python, not much configuration but able to be full configuration, possible to manage a routing and includes testing tools. I found Django, web2py and Pyramid. I&#8217;ve already known Django because I worked on it for 3 years. Also Pyramid looks more lightweight than web2py so I chosed Pyramid.</p>
<p>After spending 2-3 weeks with Pyramid, I realized that it&#8217;s not too complicated and there is good documents. But there is something a bit hard if you&#8217;ve worked on Rails is choosing an ORM. I spent a bit long to search for the orm and I chosed SQLAlchemy. Actually, I don&#8217;t know any thing about SQLAlchemy. Just heard that it&#8217;s quite light, not too heavy like ActiveRecord. After an experiment, it&#8217;s not too bad. Just need to used to how to read the document. Need to know where what you&#8217;re looking for. As far as I&#8217;m a developer, I&#8217;ve never found any document starts with writing test. I&#8217;s quite impressive to me. I actually want BDD like RSpect but I&#8217;m a newbie. Still finding now.</p>
<p>I&#8217;ll create a simple todolist and write the test covers CRUD. My tests should be failed totally. Once I implemented, they would be passed all. In order to to start, let&#8217;s follow the <a title="Pyramid Installation" href="http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/tutorials/wiki2/installation.html" target="_blank">installation</a> then you&#8217;ll see 2 testing tools which are Unittest and Test coverage. Unittest is a <a title="25.3. unittest — Unit testing framework" href="http://docs.python.org/2/library/unittest.html" target="_blank">Python builtin library</a>(PyUnit) and test coverage is a combination of <a title="Ned Batchelder: coverage.py" href="http://nedbatchelder.com/code/coverage/" target="_blank">Coverage</a> and <a title="Installation and quick start &amp;mdash; nose 1.2.1 documentation" href="https://nose.readthedocs.org/en/latest/" target="_blank">nose</a> to customize the testing files in the projects.</p>
<p>Before writing test cases, let&#8217;s see how the testing in Pyramid are&#8230; <a title="Unit, Integration, and Functional Testing" href="http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/testing.html" target="_blank">there are 3 kinds of tests</a>.</p>
<ul>
<li>Unit test is to test the smallest unit like functions or variables.</li>
<li>Integration test is a group of tests that tests the units as a component that the units work together.</li>
<li>Functional test is like the integration test which runs the application literally.</li>
</ul>
<h4>My test cases</h4>
<p>What I want to test..</p>
<ul>
<li>C &#8211; Create should post a request to a function and return back a dict that contains todo data.</li>
<li>R &#8211; Read(also Edit and list) should get the data that can be a dict of todo or a list of the dict of todos.</li>
<li>U &#8211; Update supposed to be put. I wanted to make it restfully but I&#8217;m a newbie. Let&#8217;s set it as the post request and return back the dict of updated todo.</li>
<li>D &#8211; Delete should post(delete in restful) the id and return back the deleted id</li>
</ul>
<p>What I concern about these test cases is what the test retrieves in the provided functions. It actually is the real http response to be retrieved like http status is 200 or content type is json but this kind of concern is the functional test. There is another server library to mock the http request and posts to the full stack of the application if you want to test.  </p>
<h4>Set up and Tear down</h4>
<p>A little more introduction about unittest in Pyramid. There are set up and tear down method for putting the code to be run when the test starts and run after the test ends. For example, testing.setUp() is the method to prepare the application environment for testing or the group of code to initial the data for testing in the set up method. Also after finish testing, remove that data in the tear down method.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> TestIntegrationTodolist<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> setUp<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">config</span> = testing.<span style="color: black;">setUp</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> create_engine
        <span style="color: #808080; font-style: italic;">#engine = create_engine('sqlite:///todolist.sqlite')</span>
        engine = create_engine<span style="color: black;">&#40;</span><span style="color: #483d8b;">'postgresql://dev:dev@localhost:5432/todolist_test'</span><span style="color: black;">&#41;</span>
&nbsp;
        DBSession.<span style="color: black;">configure</span><span style="color: black;">&#40;</span>bind=engine<span style="color: black;">&#41;</span>
        Base.<span style="color: black;">metadata</span>.<span style="color: black;">create_all</span><span style="color: black;">&#40;</span>engine<span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># the transaction commited after added so rolling back doesnt work on Postgres</span>
        <span style="color: #808080; font-style: italic;">#with transaction.manager:</span>
        instances = <span style="color: black;">&#40;</span>
            Todo<span style="color: black;">&#40;</span>task=<span style="color: #483d8b;">'Second task'</span>, priority=<span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>,
            Todo<span style="color: black;">&#40;</span>task=<span style="color: #483d8b;">'Thrid task'</span>, priority=<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>,
            Todo<span style="color: black;">&#40;</span>task=<span style="color: #483d8b;">'First task'</span>, priority=<span style="color: #ff4500;">10</span><span style="color: black;">&#41;</span>,
            Todo<span style="color: black;">&#40;</span>task=<span style="color: #483d8b;">&quot;Done task&quot;</span>, priority=<span style="color: #ff4500;">5</span>, done_at=<span style="color: #dc143c;">datetime</span>.<span style="color: black;">now</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: black;">&#41;</span>
        DBSession.<span style="color: black;">add_all</span><span style="color: black;">&#40;</span>instances<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> tearDown<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        DBSession.<span style="color: black;">remove</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        testing.<span style="color: black;">tearDown</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>it starts with conneting to PosgreSQL then create the connection between Pyramid and SQLAlchemy. After that is the group of code to initial the data for test. In the end, just remove the data.</p>
<h5>Read</h5>
<p>To Read in my todolist, there is 1 task which is listing all of the todo. What I excpect from the test is after request, it should return a list of dict of todos.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> test_index_pass<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; test_index_pass &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> index
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        response = index<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'done_todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># test ordering</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>.<span style="color: black;">task</span>, <span style="color: #483d8b;">&quot;First task&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'todos'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>.<span style="color: black;">task</span>, <span style="color: #483d8b;">&quot;Second task&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>There is an addiional done_todos which is a list of dict to contains the done todos. I intended to show them in another list in the same page. done_at is the key to specific which todos are done. </p>
<p>One facility method is testing.DummyRequest() to create a request as a parameter to send to the get function.</p>
<h5>Create</h5>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> test_create_pass<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; test_create_pass &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> create
        params = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'task'</span>:<span style="color: #483d8b;">'New task'</span>, <span style="color: #483d8b;">'priority'</span>:<span style="color: #ff4500;">1</span><span style="color: black;">&#125;</span>
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span>params=params, post=params<span style="color: black;">&#41;</span>
        response = create<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'id'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span>, params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'priority'</span><span style="color: black;">&#93;</span>, params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'priority'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_create_fail<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; test_create_fail &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> create
        params = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'task'</span>:<span style="color: #483d8b;">&quot;&quot;</span>, <span style="color: #483d8b;">'priority'</span>: <span style="color: #483d8b;">&quot;low&quot;</span><span style="color: black;">&#125;</span>
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span>params=params, post=params<span style="color: black;">&#41;</span>
        response = create<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'priority'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #483d8b;">'Please enter a value'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'priority'</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #483d8b;">'Please enter an integer value'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>incase of create, I provided 2 cases, pass and fail. If fail then show the notified message.</p>
<p>To create the post request with DummyRequest is just put the post value as a parameter. If want to know what are the parameters, here is the <a title="Source code for pyramid.testing" href="http://pyramid.readthedocs.org/en/latest/_modules/pyramid/testing.html#DummyRequest" target="_blank">doc</a>. You&#8217;ll see how to create a session too.</p>
<h5>Update</h5>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> test_update_pass<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; test_update_pass &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> update
        params = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'task'</span>:<span style="color: #483d8b;">'Updated task'</span>, <span style="color: #483d8b;">'priority'</span>:<span style="color: #ff4500;">1</span><span style="color: black;">&#125;</span>
        <span style="color: #808080; font-style: italic;"># provide the todo with id</span>
        todo = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">first</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span>params=params, matchdict=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>:todo.<span style="color: #008000;">id</span><span style="color: black;">&#125;</span>, post=params<span style="color: black;">&#41;</span>
        response = update<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        updated_todo = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">filter_by</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>=todo.<span style="color: #008000;">id</span><span style="color: black;">&#41;</span>.<span style="color: black;">one</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span>, params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>updated_todo.<span style="color: black;">task</span>, params<span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_update_fail<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; test_update_fail &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> update
        <span style="color: #808080; font-style: italic;"># test query not found</span>
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span>params=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>, matchdict=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>:<span style="color: #ff4500;">1</span><span style="color: black;">&#125;</span>, post=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
        response = update<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">&quot;No todo id: 1&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># test validation</span>
        params = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'task'</span>:<span style="color: #483d8b;">&quot;&quot;</span>, <span style="color: #483d8b;">'priority'</span>: <span style="color: #483d8b;">&quot;low&quot;</span><span style="color: black;">&#125;</span>
        todo = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">first</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span>params=params, matchdict=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>:todo.<span style="color: #008000;">id</span><span style="color: black;">&#125;</span>, post=params<span style="color: black;">&#41;</span>
        response = update<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertTrue</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'priority'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'task'</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: #483d8b;">'Please enter a number'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>It almost similar to create. One differece is a validation. I expected that priority should be a number but I put a string instead. It should display the message to let you know it&#8217;s not a number</p>
<h5>Delete</h5>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">    <span style="color: #ff7700;font-weight:bold;">def</span> test_delete_pass<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; test_delete_pass &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> delete
        todo = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">first</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span>params=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>,matchdict=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>:todo.<span style="color: #008000;">id</span><span style="color: black;">&#125;</span>, post=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
        response = delete<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        todo_count = DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">count</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertIsNot</span><span style="color: black;">&#40;</span>todo, DBSession.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Todo<span style="color: black;">&#41;</span>.<span style="color: black;">first</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'messages'</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">'%s has been deleted'</span> <span style="color: #66cc66;">%</span> todo.<span style="color: black;">task</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>todo_count, <span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> test_delete_fail<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot; test_delete_fail &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> todolist.<span style="color: black;">controllers</span>.<span style="color: black;">todos</span> <span style="color: #ff7700;font-weight:bold;">import</span> delete
        <span style="color: #808080; font-style: italic;"># test query not found</span>
        request = testing.<span style="color: black;">DummyRequest</span><span style="color: black;">&#40;</span>params=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>, matchdict=<span style="color: black;">&#123;</span><span style="color: #483d8b;">'id'</span>:<span style="color: #ff4500;">1</span><span style="color: black;">&#125;</span>, post=<span style="color: black;">&#123;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
        response = delete<span style="color: black;">&#40;</span>request<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>response<span style="color: black;">&#91;</span><span style="color: #483d8b;">'errors'</span><span style="color: black;">&#93;</span>, <span style="color: #483d8b;">&quot;No todo id: 1&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>These are for 2 cases too. If fails then shows a message says it has been deleted. Once deleted, the total amount should be decresed.</p>
<p>Then run the all codes above with nosetest, I got this&#8230;</p>
<div id="attachment_90" class="wp-caption alignnone" style="width: 310px"><a href="http://weblog.punneng.net/wp-content/uploads/2012/12/failed-nosetest.png"><img class="size-medium wp-image-90 " title="failed-nosetest" src="http://weblog.punneng.net/wp-content/uploads/2012/12/failed-nosetest-300x201.png" alt="Failed nosetest" width="300" height="201" /></a><p class="wp-caption-text">Failed nosetest</p></div>
<p>There is a few errors. It imported the packages but not found. So I needed to write these functions which are in the next post.<br />
Another thing in the picture above is a result of code coverage.</p>
]]></content:encoded>
			<wfw:commentRss>http://edge.punneng.net/2012/12/hello-pyramid-part-1-test-first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fix the handsfree/speaker &#8216;no sound&#8217; issue</title>
		<link>http://edge.punneng.net/2011/02/fix-the-handsfreespeaker-no-sound-issue/</link>
		<comments>http://edge.punneng.net/2011/02/fix-the-handsfreespeaker-no-sound-issue/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 06:08:24 +0000</pubDate>
		<dc:creator>PunNeng</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[iphone]]></category>

		<guid isPermaLink="false">http://edge.punneng.net/?p=42</guid>
		<description><![CDATA[iPhone sucks!! The problem is iPhone still thinks the handsfree is plugged in so it turns off the speaker. My iPhone has got this issue as well. I fixed by keep plugging and unplugging until it worked. That probably got the dust out from the jack. If that does not fix, let&#8217;s try.. You could [...]]]></description>
			<content:encoded><![CDATA[<p>iPhone sucks!!<br />
The problem is iPhone still thinks the handsfree is plugged in so it turns off the speaker.<br />
My iPhone has got this issue as well. I fixed by keep plugging and unplugging until it worked.<br />
That probably got the dust out from the jack.</p>
<p><iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/TM0GQ58pUEg" frameborder="0" allowfullscreen></iframe></p>
<p>If that does not fix, let&#8217;s try..</p>
<p><iframe title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/ayScKPuZPuM" frameborder="0" allowfullscreen></iframe></p>
<p>You could probably use alcohol to do this as well.<br />
Warning: do not﻿ drink Windex</p>
]]></content:encoded>
			<wfw:commentRss>http://edge.punneng.net/2011/02/fix-the-handsfreespeaker-no-sound-issue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple startup script for OSX</title>
		<link>http://edge.punneng.net/2011/01/simple-startup-script-for-osx/</link>
		<comments>http://edge.punneng.net/2011/01/simple-startup-script-for-osx/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 19:22:33 +0000</pubDate>
		<dc:creator>PunNeng</dc:creator>
				<category><![CDATA[Utilities]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[startup]]></category>

		<guid isPermaLink="false">http://edge.punneng.net/?p=21</guid>
		<description><![CDATA[Somebody just got a new macbook and he wanted to run something every time it is turned on. To create a startup script on OSX is not hard. Just a few steps. Firstly, make a folder in /Library/StartupItems. Then create a plist file and an executable file. That&#8217;s it. Let&#8217;s start with open Teminal and.. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.pokpitch.com/" target="_blank">Somebody</a> just got a new macbook and he wanted to run something every time it is turned on.</p>
<p>To create a startup script on OSX is not hard. Just a few steps.<br />
Firstly, make a folder in /Library/StartupItems. Then create a plist file and an executable file. That&#8217;s it.</p>
<p>Let&#8217;s start with open Teminal and..</p>
<pre class="terminal">$ cd /Library/StartupItems
$ sudo mkdir WhatEverNameYouWant
$ cd WhatEverNameYouWant</pre>
<p>and then create a file named StartupParameters.plist.<br />
You can use whatever text editor to create and add..</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">{
  Description     = &quot;WhatEverNameYouWant&quot;;
  Provides        = (&quot;ScriptNameYouWant&quot;);
  OrderPreference = &quot;None&quot;;
}</pre></div></div>

<p>Next, create ScriptNameYouWant and put the commands you want.<br />
Don&#8217;t forget to start the file with shebang #!/bin/sh to execute with Bourne shell.<br />
This can be other. It depends on what you will execute.</p>
<p>The file will look like..</p>

<div class="wp_syntax"><div class="code"><pre class="shell" style="font-family:monospace;">#!/bin/sh
The commands to execute
commands
and the last command</pre></div></div>

<p>Save and make it executable..</p>
<pre class="terminal">sudo chmod +x ScriptNameYouWant</pre>
<p>and restart your mac. See what you fuckup your Mac lol</p>
]]></content:encoded>
			<wfw:commentRss>http://edge.punneng.net/2011/01/simple-startup-script-for-osx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Quick start to Ruby with MySQL</title>
		<link>http://edge.punneng.net/2010/12/quick-start-to-ruby-with-mysql/</link>
		<comments>http://edge.punneng.net/2010/12/quick-start-to-ruby-with-mysql/#comments</comments>
		<pubDate>Tue, 14 Dec 2010 19:37:29 +0000</pubDate>
		<dc:creator>PunNeng</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[quickstarts]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://edge.punneng.net/?p=15</guid>
		<description><![CDATA[I&#8217;ve done Ruby on Rails with MySQL but never done done Ruby with MySQL. I just did this last week to check a small thing. I didn&#8217;t want to establish the whole Rails&#8217;s process. I takes a lot of memory. Let&#8217;s do a quick start. Install a driver first. You can get one that&#8217;s written [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve done Ruby on Rails with MySQL but never done done Ruby with MySQL. I just did this last week to check a small thing. I didn&#8217;t want to establish the whole Rails&#8217;s process. I takes a lot of memory.</p>
<p>Let&#8217;s do a quick start.</p>
<p>Install a driver first. You can get one that&#8217;s written <a title="MySQL driver for Ruby written by C" href="http://www.tmtm.org/en/mysql/ruby/" target="_blank">by C</a> or <a title="MySQL driver for Ruby written by Ruby" href="http://www.tmtm.org/en/ruby/mysql/" target="_blank">Ruby</a>. If you got gem then that&#8217;s easy.</p>
<pre class="terminal">$ sudo gem install mysql</pre>
<p>It&#8217;s written by C</p>
<p>It&#8217;s quite easy to start. Require &#8216;mysql&#8217; then create a connection to the database and then query. If install by gem, just require &#8216;rubygems&#8217; first.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mysql'</span>
&nbsp;
db = Mysql::new<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;host&quot;</span>, <span style="color:#996600;">&quot;user&quot;</span>, <span style="color:#996600;">&quot;passwd&quot;</span>, <span style="color:#996600;">&quot;db&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
res = db.<span style="color:#9900CC;">query</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;select * from mytable&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
res.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;id: #{row[0]}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>and this is what I got</p>
<pre class="terminal">id: 1
id: 2
id: 3
id: 4
...
...</pre>
<p>Looks simple. The index of an element in an array is sorted by the sequence of columns in the table.</p>
<p>Or you can get the column names instead of the indexes by each_hash</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">res.<span style="color:#9900CC;">each_hash</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>row<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;id: #{row['id']}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>More information at the pages of the driver.</p>
]]></content:encoded>
			<wfw:commentRss>http://edge.punneng.net/2010/12/quick-start-to-ruby-with-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What the heck is JSONP?</title>
		<link>http://edge.punneng.net/2010/12/what-the-heck-is-jsonp/</link>
		<comments>http://edge.punneng.net/2010/12/what-the-heck-is-jsonp/#comments</comments>
		<pubDate>Thu, 09 Dec 2010 19:53:34 +0000</pubDate>
		<dc:creator>PunNeng</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[jsonp]]></category>

		<guid isPermaLink="false">http://edge.punneng.net/?p=6</guid>
		<description><![CDATA[I&#8217;ve got it working but didn&#8217;t know how it works. To avoid the same origin policy, JSON is a good thing to help you. The thing on the same origin policy is the browser requests from JavaScript or Flash aren&#8217;t allowed because we need to protect from script injection attacks. If this happens, the resources [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve got it working but didn&#8217;t know how it works.</p>
<p>To avoid the <a title="Same origin policy" href="http://en.wikipedia.org/wiki/Same_origin_policy" target="_blank">same origin policy</a>, JSON is a good thing to help you. The thing on the same origin policy is the browser requests from JavaScript or Flash aren&#8217;t allowed because we need to protect from script injection attacks. If this happens, the resources like http cookies will be accessed then your authentication or password in the cookies can be stolen.</p>
<p>So we can&#8217;t fix this but we can avoid it. The basic idea is we can request any Javascript anywhere.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://somewhere/jsfile&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Once it loaded, the browser could execute this javascript.</p>
<p>But if it receives json then it does nothing. We can make a returned Javascritpt looks like this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> json_var <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'var1'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'val1'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It would be better if we can define the callback function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">myCallback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'var1'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'val1'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The server needs to provide this padding. The next thing is we have to declare this function(myCallback) to be executed once it loaded.</p>
<p>For instance, Graph on Facebook allows us to define the callback</p>
<pre><a href="https://graph.facebook.com/175459332480559/albums?callback=myCallback" target="_blank">https://graph.facebook.com/175459332480559/albums?callback=myCallback</a></pre>
<p>by putting the ?callback=yourCallback at the end of uri</p>
<p>I misunderstood when I started because I saw the doc from jQuery that can put the dataType as jsonp and I thought it was just another asynchronous request which can request across the domain name on the browser.</p>
<p>Basically, jQuery actually provides the callback function as well like</p>
<pre><a href="https://graph.facebook.com/19292868552?callback=jsonp1291493679672" target="_blank">https://graph.facebook.com/19292868552?callback=jsonp1291493679672</a></pre>
<p>jsonp1291493679672 is the callback function to be defined in window like window['jsonp1291493679672'] which defines data and returns this data to the callback function we put in the ajax request. What it does is, jQuery puts ?callback=? at the end of uri and replaces a question mark(?) with jsonp1291493679672. The numbers at the end comes from now() btw.</p>
]]></content:encoded>
			<wfw:commentRss>http://edge.punneng.net/2010/12/what-the-heck-is-jsonp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
