<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anson Kelly on Medium]]></title>
        <description><![CDATA[Stories by Anson Kelly on Medium]]></description>
        <link>https://medium.com/@anson.kelly?source=rss-f7ea0178ae14------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*fWhlCz3gzhGZ8dZ7E7kCsA@2x.jpeg</url>
            <title>Stories by Anson Kelly on Medium</title>
            <link>https://medium.com/@anson.kelly?source=rss-f7ea0178ae14------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 00:51:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anson.kelly/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using realistic data locally]]></title>
            <link>https://medium.com/carwow-product-engineering/using-realistic-data-locally-93881c766a51?source=rss-f7ea0178ae14------2</link>
            <guid isPermaLink="false">https://medium.com/p/93881c766a51</guid>
            <category><![CDATA[postgres]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[backup]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Anson Kelly]]></dc:creator>
            <pubDate>Thu, 07 Sep 2017 12:31:46 GMT</pubDate>
            <atom:updated>2017-09-07T12:31:46.367Z</atom:updated>
            <content:encoded><![CDATA[<p>When developing features it is handy to be able to test with a “real” dataset and not just a bunch of randomly generated data.</p><p>This means it there are less surprises when the feature gets deployed — both in terms of scalability but also content.</p><p>An example that we ran into recently at carwow was adding a last page option to a paginated list. It doesn’t sound like a large task but, like most features, the devil is in the detail. If your local development environment only has a few hundred items then calculating how many pages of items there are is quick. In production, however, there could be millions of items which gives a different performance profile.</p><blockquote>Sidenote: Running the equivalent of “select count(*) from table_name” scales very badly but that’s another topic entirely</blockquote><p>Another example is the content — long names can overflow areas of the page and testing with a realistic dataset means less surprises like these.</p><h3>Lite Backups to the Rescue</h3><p>So that developers can develop with a dataset that is close to our production dataset we frequently restore backups of production data locally. Luckily for us, heroku makes it easy to generate regular backups.</p><p>There is a problem with doing this though, now that carwow has been going for over 4 years our production dataset has grown… our primary databases are well over 100G in size which is a bit much to be restoring locally so we use a compromise — we prune the dataset down to a more manageable size while still keeping the content consistent and the dataset large enough that most (if not all) performance related issues make themselves known.</p><p>We have found that pruning our data set to the last 30 days of user activity gives us a reasonable balance between keeping the lite backup size manageable while maintaining a good mix of actual data.</p><h3>Stop talking and show me the code</h3><p>To do this we have a basic heroku application that runs a Docker container. All we need is the heroku CLI and the postgres-client. Heres the Dockerfile:</p><pre>FROM debian:jessie<br><br>RUN apt-get update &amp;&amp; apt-get install -y --no-install-recommends git wget postgresql-client ca-certificates \<br>    &amp;&amp; wget http://cli-assets.heroku.com/branches/stable/heroku-linux-amd64.tar.gz \<br>    &amp;&amp; tar -xzf heroku-linux-amd64.tar.gz -C /usr/local/lib/ \<br>    &amp;&amp; ln -s /usr/local/lib/heroku/bin/heroku /usr/local/bin/heroku \<br>    &amp;&amp; rm heroku-linux-amd64.tar.gz \<br>    &amp;&amp; apt-get purge -y wget \<br>    &amp;&amp; apt-get autoremove -y \<br>    &amp;&amp; apt-get clean \<br>    &amp;&amp; rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*<br><br>RUN mkdir /app<br>WORKDIR /app<br><br>COPY generate_quotes_lite /app/<br>COPY generate_dealers_lite /app/</pre><p>We generate a new lite backup every night:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Sd1pdGy8-hXvQSiG6JqmQ.png" /><figcaption>Using the Heroku scheduler makes running daily tasks easy</figcaption></figure><p>In each generate_database_lite script we use the heroku API to fork each database, prune the dataset and then generate a backup of the smaller dataset. Here is an (abridged) version:</p><pre>#!/bin/bash<br>set -e</pre><pre># Copy the database (fork) <br>heroku addons:create heroku-postgresql:standard-2 --fork $APP::DATABASE_URL --as $DB --fast --app $APP --region eu<br><br>until heroku pg:wait --app=$APP<br>do<br>  echo &quot;  Waiting for DB fork...&quot;<br>done<br><br>DB_URI=`heroku pg:credentials:url $DB --app=$APP | grep &quot;postgres://&quot;`</pre><pre># Prune the data<br>psql $DB_URI -c &quot;DELETE FROM users WHERE created_at &lt; current_date - interval &#39;1 month&#39;&quot;<br><br>... other psql statements follow to prune data ...</pre><pre># Capture the backup<br>heroku pg:backups capture $DB --app=$APP</pre><pre># Delete the forked DB<br>heroku addons:destroy $DB --app=$APP --confirm $APP</pre><p>Those backups are then available for developers to download and use locally.</p><p>We use the heroku scheduler to generate a fresh set of lite backups each night.</p><p>This is nothing too complicated, but its a nice example of how a bit of automation can help make a developers life easier.</p><p>Like what you see? Want to be a part of the carwow tech community?</p><p>Head to our<a href="https://www.linkedin.com/jobs/search/?f_C=1722873&amp;locationId=OTHERS.worldwide"> jobs page</a> now.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93881c766a51" width="1" height="1" alt=""><hr><p><a href="https://medium.com/carwow-product-engineering/using-realistic-data-locally-93881c766a51">Using realistic data locally</a> was originally published in <a href="https://medium.com/carwow-product-engineering">Carwow Product, Design &amp; Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Test behaviour with Request specs rather than testing implementation with Controller specs]]></title>
            <link>https://medium.com/carwow-product-engineering/test-behaviour-with-request-specs-rather-than-testing-implementation-with-controller-specs-f4ebbe13f983?source=rss-f7ea0178ae14------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4ebbe13f983</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[carwow]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rspec]]></category>
            <dc:creator><![CDATA[Anson Kelly]]></dc:creator>
            <pubDate>Thu, 06 Jul 2017 16:12:18 GMT</pubDate>
            <atom:updated>2017-07-06T16:12:18.671Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wKJlnAGwkQOcrs4DLy82JA.png" /><figcaption>Test behaviour of the entire stack, not just part of it</figcaption></figure><p>At <a href="https://carwow.co.uk/">carwow</a> we have been upgrading our Rails apps from 4.2.x versions to the shiny new 5.0/5.1 versions.</p><p>One of the changes moving from Rails 4 to Rails 5 is the deprecation of controller tests in favour of higher level request specs.</p><p>Our primary Rails applications have been around for a few years now (the oldest app still in production started out as a Rails 3.2.1 application ~5 years ago) so there are 1000’s of controller tests that have been written over their lifetime.</p><p>When testing web applications I find that approaching the tests with the mindset of how the application will be used, rather than what the application is made up of produces a more robust and useful test suite.</p><p>As such this blog post doesn’t really introduce anything new that has not been said elsewhere. It’s purpose is to centralise the reasons to convert existing legacy controller specs into request specs that we can point new developers to. It also might help others.</p><p>Examples are using Rails 5.1 + RSpec 3.5</p><h3>Controller specs</h3><p>These have been around since the early days of Rails and have historically been the way to test that web requests “do the right thing(s)”.</p><p>Controller tests while looking like they test the request behaviour actually do not — they test the implementation of the controller object.</p><p>A (contrived) example:</p><pre>describe SessionsController <strong>do</strong><br>  describe &#39;GET #new&#39; <strong>do</strong><br>    it &quot;renders the login page&quot; <strong>do</strong><br>      get :new<br>      expect(response)<strong>.</strong>to render_template(&#39;sessions/new&#39;)<br>    <strong>end<br>    <br>   </strong>it &quot;assigns the session&quot; <strong>do</strong><br>     get :new<br>     expect(assigns(:session)).to be_a_new(Session)<br>   <strong>end</strong><br>  <strong>end</strong><br><br>  describe &quot;POST #create&quot; <strong>do</strong><br>    it &quot;redirects to the dashboard&quot; <strong>do<br>      </strong>post :create, session: FactoryGirl<strong>.</strong>attributes_for(:session)<br>      expect(response)<strong>.</strong>to redirect_to dashboard_url<br>    <strong>end</strong><br>  <strong>end</strong><br><br>  <em># other examples ...</em><br><strong>end</strong></pre><p>Here we have an examples of how controller tests are invasive and brittle.</p><p>Both assert_template and assigns assert the implementation, not the behaviour. Do our users care what the template is named, or what the name of the instance variable is? Nope.</p><p>In both cases what actually matters is the request processing behaviour and the response. Everything else is an implementation detail. We can rename the template or change the instance variable name and as long as the tests still pass then the behaviour remains the same and our users are happy.</p><p>Another thing to note is the action of each test eg get :new This is calling the new method on the controller. However that is not how our login behaviour works in the real world — a request first passes through the middleware stack and is then routed to the controller. So what we are doing here is skipping out a whole lot of behaviour that may change the request. This is another example of how controller specs test implementation rather than behaviour. Does it matter what the controller action is named? Nope not really (I’m purposely avoiding RESTful considerations here)</p><p>tldr: We should be testing behaviour not implementation.</p><h3>Request specs</h3><p>Are the way to test the behaviour of a request in a way that is much closer to the way the code will execute in a production environment.</p><p>Lets rewrite the above example as a request spec using <a href="https://github.com/teamcapybara/capybara">capybara</a> to mimic the behaviour of a user:</p><pre>describe &#39;Authentication&#39; do<br>  it &#39;allows users to log in&#39; do<br>    user = FactoryGirl.create(:user, password: &#39;password&#39;)</pre><pre>    get new_sessions_url</pre><pre>    fill_in &#39;email&#39;, with: user.email<br>    fill_in &#39;password&#39;, with: &#39;password&#39;<br>    click_button &#39;Login&#39;<br>    <br>    expect(current_url).to eq(dashboard_url)<br>    expect(page).to have_selector(&#39;.message&#39;, text: &#39;Welcome&#39;)<br>  end</pre><pre># other examples ...<br>end</pre><p>The first thing to notice is we now have 1 test rather than several. This is a more realistic example as we are now thinking in terms of behaviour, not implementation. Loading and submitting the login form is 1 action from a users perspective.</p><p>get sessions_url is using our routes to perform the action, not just calling a method directly. This is now explicitly testing that the route is set up correctly and that the middleware is not changing the request in unexpected ways.</p><p>We are testing the response to the new action by filling in the login form and submitting it in just the same way a user would. This is testing not only that the form appears to the user as expected, but that it also captures and submits to the expected location.</p><p>Then we assert that we end up on the dashboard (which is a protected route) with the correct message showing.</p><p><strong>Other benefits</strong></p><p>While having tests that mimic user interaction are a win there is another benefit — our implementation just got a lot easier to refactor.</p><p>With our previous controller spec changing the instance variables or template name would mean changing one or more tests, which then could mean altering the implementation to allow the tests to be changed and makes the whole process harder.</p><p>With the request spec version those changes can be made transparently- as long as the behaviour remains the same then we are free to refactor to our hearts content.</p><p><strong>Speed</strong></p><p>One of the arguments against using higher level (behavioural) tests is that they are slower. Lets see by running the above examples:</p><p>The controller specs:</p><pre>bundle exec rspec spec/controllers/sessions_controller_spec.rb<br>...</pre><pre>Finished in 0.17622 seconds (files took 1.74 seconds to load)<br>3 examples, 0 failures</pre><p>And the request specs:</p><pre>bundle exec rspec spec/requests/sessions_spec.rb<br>.</pre><pre>Finished in 0.22617 seconds (files took 1.72 seconds to load)<br>1 example, 0 failures</pre><p>As shown the request spec is slightly slower, but tests far more of our applications behaviour. Like most choices in the development world this is a tradeoff — test robustness against raw speed.</p><p>Obviously with these more than slightly contrived examples its not an absolute that request specs will be faster, but its safe to say they are not much slower.</p><p><strong>Wrapping it up</strong></p><p>Feature specs are the way to test your application’s behaviour closer to the way users will interact with it. The implementation details are just that — and should not have to be explicitly tested.</p><p>This helps us develop a test suite that describes our application’s behaviour, prevents regressions, and allows freedom to refactor implementation details without have brittle tests.</p><p>How many fragile and brittle controller tests still lurk in your Rails codebase? Is is time to upgrade them to request specs?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4ebbe13f983" width="1" height="1" alt=""><hr><p><a href="https://medium.com/carwow-product-engineering/test-behaviour-with-request-specs-rather-than-testing-implementation-with-controller-specs-f4ebbe13f983">Test behaviour with Request specs rather than testing implementation with Controller specs</a> was originally published in <a href="https://medium.com/carwow-product-engineering">Carwow Product, Design &amp; Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to make changes the right way]]></title>
            <link>https://medium.com/carwow-product-engineering/making-changes-the-right-way-b599668ed9bd?source=rss-f7ea0178ae14------2</link>
            <guid isPermaLink="false">https://medium.com/p/b599668ed9bd</guid>
            <category><![CDATA[product]]></category>
            <category><![CDATA[change-management]]></category>
            <category><![CDATA[devops]]></category>
            <category><![CDATA[startup-lessons]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Anson Kelly]]></dc:creator>
            <pubDate>Thu, 04 May 2017 10:01:01 GMT</pubDate>
            <atom:updated>2017-05-18T14:30:23.997Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a0mIHmPvBZ4EixZqPSopAw.jpeg" /></figure><h3>Change needs to be managed</h3><p>Software is a living entity and changes often as businesses expand and mature. A solution that worked well 6 months ago may well not cut the mustard today, especially in a startup environment where things change at a rapid rate.</p><p>Every so often we need to change out part of our infrastructure to meet new requirements from both our internal and external users. In the last year especially <a href="https://www.carwow.co.uk/">carwow</a> has grown at a fast rate and we need our tech stack to keep pace with that rapid growth.</p><p>Its hard to gain users, easy to lose them. All the marketing and sales effort that goes into attracting and retaining users can easily be for nought if we deliver a bad experience.<br>When we are upgrading components and making changes our users do not want to see a maintenance page, an error page or, even worse, lose data.</p><p>It is important to plan changes out, trial run them and then script them to be reproducible. There is not much worse than being in the middle of an upgrade when something unexpected occurs… sometimes stuff happens that you cannot control but that does not mean we should not attempt to control the risk as much as we can.</p><p>Here are some principles that we use at <a href="https://www.carwow.co.uk/">carwow</a> to manage making changes to our tech stack:</p><h3>Don’t do it unless you have to</h3><p>This might seem counter-intuitive to say in a blog post about change but it is important to consider. Any change to infrastructure is risky no matter how well you plan and prepare. If it is something that can be deferred until a time where the risk is lower, great — you have solved the problem and it’s time to move onto the next one.</p><h3>Limit the scope</h3><p>Don’t change everything at once. Modern tech stacks are composed of many different components and the more components that change at once the greater the risk of services going down, data being lost and users having a bad experience.</p><h3>Script all the things</h3><p>Every step / command / action must be written down. Don’t rely on your memory to help you out. Steps that are written down are much harder to skip or be forgotten about.</p><p>Go one step better and create a script that runs the changes — copying and pasting commands or clicking a UI is prone to human error, especially when there are more than a few steps involved. Automating the process removes the possibility of human error as the steps are always run in the same way, in the same order.</p><p>Writing it all down has another advantage as the process is kept for future use. You never know when you might need to repeat the process and having it all written down will save you time and hassle. No need to reinvent what you have already done.</p><p>We use bash scripts and rake tasks for smaller changes, with <a href="https://www.ansible.com/">Ansible</a> and/or <a href="https://www.terraform.io/">Terraform</a> being used to manage larger changes.</p><h3>Split the change into stages</h3><p>If your changes are applied in multiple step, break them up. Verify that each step has been successful before continuing on. You do not want to get to the end of the change process before realising that an earlier step had not actually done what it was meant to do.</p><p>This can take the form of performing counts, checking that a web request returns an expected response, or that a service has started (and has stayed running!)</p><p>Log every step, even if it is something simple. Log output from API calls and results from database queries. The more information you have, the better prepared you are to diagnose any problems that occur along the way.</p><h3>Give yourself a way out</h3><p>What happens when you are half way though a change process and realise it cannot be completed? This could be due to a step not working properly, the process taking too long for a maintenance window or something totally unexpected happening.</p><p>Being able to reverse the changes means you can get your environment back to where you started (read: undo the damage).</p><p>With some changes, this is not always easy to do. For instance, database migrations where large datasets are being manipulated. Here, it is important to think about what happens when something goes wrong and how it can be fixed.</p><h3>Practice makes perfect</h3><p>Your going to apply the change straight to your production environment. That’s a brave move… isn’t that kinda like playing Russian Roulette?<br>Test the change first. Have a duplicate environment (cloud services make this easy) set up like your production environment. Then test your change.</p><p>Did it work? No? Good thing that this was a test environment and no users were effected right?</p><p>Fix whatever went wrong and try again. Rinse and repeat until it all goes smoothly.</p><h3>Run in parallel / Phased changeover</h3><p>Flicking a switch from one service to another might work but it’s less risky to have a phased changeover (aka soft launch). If it is possible, run both old and new services side-by-side. Then you can switch between them to test that the new service / component works.</p><h3>Be retrospective</h3><p>After the change has happened (successfully we hope!) it is important to reflect on the process.<br>What worked? What did not work? How can we improve the process for next time?<br>There is no point making the same mistakes again. Make sure you share your experience with your team members and others.</p><p>Interested in making an Impact? <a href="https://www.carwow.co.uk/jobs">Join the carwow-team!</a><br>Feeling social? Connect with us on <a href="https://twitter.com/carwow_product">Twitter</a> and <a href="https://www.linkedin.com/company-beta/1722873/">LinkedIn</a> :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b599668ed9bd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/carwow-product-engineering/making-changes-the-right-way-b599668ed9bd">How to make changes the right way</a> was originally published in <a href="https://medium.com/carwow-product-engineering">Carwow Product, Design &amp; Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>