<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://parthrmodi.com/blog/atom.xml" rel="self" type="application/atom+xml" /><link href="https://parthrmodi.com/blog/" rel="alternate" type="text/html" /><updated>2024-08-19T15:06:26+00:00</updated><id>https://parthrmodi.com/blog/atom.xml</id><title type="html">Parth Modi</title><subtitle>Specializes in building various kind of applications with strong focus on backend technologies such as Ruby on Rails, Python.</subtitle><author><name>Parth Modi</name><email>parthmodi54@yahoo.com</email></author><entry><title type="html">Optimizing your queries with ActiveRecord</title><link href="https://parthrmodi.com/blog/2017/09/21/optimizing-your-queries-with-activerecord/" rel="alternate" type="text/html" title="Optimizing your queries with ActiveRecord" /><published>2017-09-21T00:00:00+00:00</published><updated>2017-09-21T00:00:00+00:00</updated><id>https://parthrmodi.com/blog/2017/09/21/optimizing-your-queries-with-activerecord</id><content type="html" xml:base="https://parthrmodi.com/blog/2017/09/21/optimizing-your-queries-with-activerecord/"><![CDATA[<p>Last week gave a talk about “Querying with Rails” in a <a href="https://www.meetup.com/AhmedabadRuby/events/242275060/">meetup</a> organized by the <a href="http://solutelabs.com">Solute Technolabs LLP</a>. Starting with basics of ActiveRecord, I focused on how Querying becomes difficult and complicated down the road with ever increasing features, and how to leverage ActiveRecord associations, joins, scopes and class methods.</p>

<p>Have a look at presentation:</p>

<iframe src="//www.slideshare.net/slideshow/embed_code/key/b9lqN0RNepTp4M" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen=""> </iframe>
<div style="margin-bottom:5px"> <strong> <a href="//www.slideshare.net/parthmodi359/querying-with-rails" title="Querying with Rails" target="_blank">Querying with Rails</a> </strong> from <strong><a href="https://www.slideshare.net/parthmodi359" target="_blank">Parth Modi</a></strong> </div>

<p>As always, feel free to share your feedback!</p>]]></content><author><name>Parth Modi</name><email>parthmodi54@yahoo.com</email></author><summary type="html"><![CDATA[Last week gave a talk about “Querying with Rails” in a meetup organized by the Solute Technolabs LLP. Starting with basics of ActiveRecord, I focused on how Querying becomes difficult and complicated down the road with ever increasing features, and how to leverage ActiveRecord associations, joins, scopes and class methods. Have a look at presentation: Querying with Rails from Parth Modi As always, feel free to share your feedback!]]></summary></entry><entry><title type="html">Modeling Has Many Relationships with DynamoDB</title><link href="https://parthrmodi.com/blog/aws/modeling-has-many-relationships-with-dynamodb" rel="alternate" type="text/html" title="Modeling Has Many Relationships with DynamoDB" /><published>2017-08-10T23:26:00+00:00</published><updated>2017-08-10T23:26:00+00:00</updated><id>https://parthrmodi.com/blog/aws/modeling-has-many-relationships-with-dynamodb</id><content type="html" xml:base="https://parthrmodi.com/blog/aws/modeling-has-many-relationships-with-dynamodb"><![CDATA[<p>DynamoDB is ideal when you need consistent high performance with large data effectively. Being NoSQL database, normalization isn’t much benefical and hence associations and joins are not formed exactly how they are in SQL databases. In this article I explained how to form <code class="language-plaintext highlighter-rouge">has_many</code> Relationships with DynamoDB.</p>

<p><a href="https://blog.codeship.com/modeling-has_many-relationships-with-dynamodb/">Modeling has_many Relationships with DynamoDB</a></p>

<blockquote>
  <p>Coming from a strong SQL background and being a fan of using ORM such as Active Record, adapting to the NoSQL mindset gave me an opportunity to rethink and explore the capabilities of NoSQL. But soon I realized that normalization is not always the right choice in the case of NoSQL databases. And more important, handling relations is a bit different.</p>
</blockquote>

<p>Hope you find this useful, share your feedback.</p>]]></content><author><name>Parth Modi</name><email>parthmodi54@yahoo.com</email></author><category term="aws" /><category term="dynamodb" /><category term="rails" /><category term="aws" /><category term="codeship" /><summary type="html"><![CDATA[DynamoDB is ideal when you need consistent high performance with large data effectively. Being NoSQL database, normalization isn’t much benefical and hence associations and joins are not formed exactly how they are in SQL databases. In this article I explained how to form has_many Relationships with DynamoDB. Modeling has_many Relationships with DynamoDB Coming from a strong SQL background and being a fan of using ORM such as Active Record, adapting to the NoSQL mindset gave me an opportunity to rethink and explore the capabilities of NoSQL. But soon I realized that normalization is not always the right choice in the case of NoSQL databases. And more important, handling relations is a bit different. Hope you find this useful, share your feedback.]]></summary></entry><entry><title type="html">Dry your RSpec Tests with Shared Examples</title><link href="https://parthrmodi.com/blog/dry-your-rspec-tests-with-shared-examples" rel="alternate" type="text/html" title="Dry your RSpec Tests with Shared Examples" /><published>2017-02-04T00:00:00+00:00</published><updated>2017-02-04T00:00:00+00:00</updated><id>https://parthrmodi.com/blog/dry-your-rspec-tests-with-shared-examples</id><content type="html" xml:base="https://parthrmodi.com/blog/dry-your-rspec-tests-with-shared-examples"><![CDATA[<p>When I refactored a project a few weeks ago, I spent most of my time writing specs. After writing several similar test cases for some APIs, I started to wonder whether I might be able to get rid of a lot of this duplication.</p>

<p>So I threw myself into reading up on the best practices for DRYing up tests (Don’t Repeat Yourself). And that’s how I came to know of <code class="language-plaintext highlighter-rouge">shared examples</code> and <code class="language-plaintext highlighter-rouge">shared contexts</code>.</p>

<p>In my case, I ended up using shared examples. And here’s what I’ve learned so far from applying these.</p>

<p>When you have multiple specs that describes similar behavior, it might be better to extract redundant examples in <code class="language-plaintext highlighter-rouge">shared examples</code> and use them in multiple specs.</p>

<!--more-->

<p>Suppose you have two models <em>User</em> and <em>Post</em>, and a user can have many posts. One should be able to view list of users and posts. Creating index action in users and posts controller will serve the purpose.</p>

<p>First write specs for index action of users controller which has responsibility of fetching users and rendering them with proper layout, and then write enough code to make tests pass.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># users_controller_spec.rb</span>
<span class="n">describe</span> <span class="s2">"GET #index"</span> <span class="k">do</span>
  <span class="n">before</span> <span class="k">do</span>
    <span class="mi">5</span><span class="p">.</span><span class="nf">times</span> <span class="k">do</span>
      <span class="no">FactoryGirl</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span>
    <span class="k">end</span>
    <span class="n">get</span> <span class="ss">:index</span>
  <span class="k">end</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">respond_with</span><span class="p">(</span><span class="ss">:ok</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">render_template</span><span class="p">(</span><span class="ss">:index</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">assigns</span><span class="p">(</span><span class="ss">:users</span><span class="p">)).</span><span class="nf">to</span> <span class="n">match</span><span class="p">(</span><span class="no">User</span><span class="p">.</span><span class="nf">all</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>

<span class="c1"># users_controller.rb</span>
<span class="k">class</span> <span class="nc">UsersController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
  <span class="o">...</span><span class="p">.</span>
  <span class="nf">def</span> <span class="n">index</span>
    <span class="vi">@users</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">all</span>
  <span class="k">end</span>
  <span class="o">...</span><span class="p">.</span>
<span class="nf">end</span></code></pre></figure>

<p>Typically index action of any controller fetches and aggregates data from few resources if required, and adds pagination, searching, sorting, filtering or scoping. Finally, all these data is presented to views via html, or JSON or XML in apis. To simplify example, index actions of controllers will just fetch data and show them via views.</p>

<p>Same goes for index action of posts controller:</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># posts_controller_spec.rb</span>
<span class="n">describe</span> <span class="s2">"GET #index"</span> <span class="k">do</span>
  <span class="n">before</span> <span class="k">do</span>
    <span class="mi">5</span><span class="p">.</span><span class="nf">times</span> <span class="k">do</span>
      <span class="no">FactoryGirl</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="ss">:post</span><span class="p">)</span>
    <span class="k">end</span>
    <span class="n">get</span> <span class="ss">:index</span>
  <span class="k">end</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">respond_with</span><span class="p">(</span><span class="ss">:ok</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">render_template</span><span class="p">(</span><span class="ss">:index</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">assigns</span><span class="p">(</span><span class="ss">:posts</span><span class="p">)).</span><span class="nf">to</span> <span class="n">match</span><span class="p">(</span><span class="no">Post</span><span class="p">.</span><span class="nf">all</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>

<span class="c1"># posts_controller.rb</span>
<span class="k">class</span> <span class="nc">PostsController</span> <span class="o">&lt;</span> <span class="no">ApplicationController</span>
  <span class="o">...</span><span class="p">.</span>
  <span class="nf">def</span> <span class="n">index</span>
    <span class="vi">@posts</span> <span class="o">=</span> <span class="no">Post</span><span class="p">.</span><span class="nf">all</span>
  <span class="k">end</span>
  <span class="o">...</span><span class="p">.</span>
<span class="nf">end</span></code></pre></figure>

<p>Rspec tests written for both users and posts controller are very similar. In both controllers,</p>

<ul>
  <li>Response code – should be ‘ok’</li>
  <li>Both index action should render proper partial or view - in our case <code class="language-plaintext highlighter-rouge">index</code></li>
  <li>The data to be rendered, such as posts or users</li>
</ul>

<p>Let’s DRY specs of index action by using shared examples.</p>

<h3 id="where-to-place">Where to place</h3>

<p>I like to place shared examples defined inside <em>specs/support/shared_examples</em> directory so that all shared example related files are loaded automatically.</p>

<p>To read about other commonly used conventions to put shared examples from here: <a href="https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-examples">shared examples documentation</a></p>

<h3 id="how-to-define">How to define</h3>
<p>Index action should respond with 200 success code (ok) and render index template.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># specs/support/shared_examples/index_examples.rb</span>
<span class="no">RSpec</span><span class="p">.</span><span class="nf">shared_examples</span> <span class="s2">"index examples"</span> <span class="k">do</span>
  <span class="n">it</span> <span class="p">{</span>	<span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">respond_with</span><span class="p">(</span><span class="ss">:ok</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">it</span> <span class="p">{</span>	<span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">render_template</span><span class="p">(</span><span class="ss">:index</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span></code></pre></figure>

<p>Apart from it blocks, before and after hooks, let blocks, context and describe blocks can also be defined inside shared examples. I personally prefer to keep shared examples simple and concise, and don’t add contexts and let blocks. Shared examples block also accepts parameters, which I will cover in subsequent sections.</p>

<h3 id="how-to-use">How to use</h3>

<p>Adding <code class="language-plaintext highlighter-rouge">include_examples "index examples"</code> to users and posts controller specs includes “index examples” to our tests.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># users_controller_spec.rb</span>
<span class="n">describe</span> <span class="s2">"GET #index"</span> <span class="k">do</span>
  <span class="n">before</span> <span class="k">do</span>
    <span class="mi">5</span><span class="p">.</span><span class="nf">times</span> <span class="k">do</span>
      <span class="no">FactoryGirl</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="ss">:user</span><span class="p">)</span>
    <span class="k">end</span>
    <span class="n">get</span> <span class="ss">:index</span>
  <span class="k">end</span>
  <span class="n">include_examples</span> <span class="s2">"index examples"</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">assigns</span><span class="p">(</span><span class="ss">:users</span><span class="p">)).</span><span class="nf">to</span> <span class="n">match</span><span class="p">(</span><span class="no">User</span><span class="p">.</span><span class="nf">all</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span>

<span class="c1"># similarly, in posts_controller_spec.rb</span>
<span class="n">describe</span> <span class="s2">"GET #index"</span> <span class="k">do</span>
  <span class="n">before</span> <span class="k">do</span>
    <span class="mi">5</span><span class="p">.</span><span class="nf">times</span> <span class="k">do</span>
      <span class="no">FactoryGirl</span><span class="p">.</span><span class="nf">create</span><span class="p">(</span><span class="ss">:post</span><span class="p">)</span>
    <span class="k">end</span>
    <span class="n">get</span> <span class="ss">:index</span>
  <span class="k">end</span>
  <span class="n">include_examples</span> <span class="s2">"index examples"</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">assigns</span><span class="p">(</span><span class="ss">:posts</span><span class="p">)).</span><span class="nf">to</span> <span class="n">match</span><span class="p">(</span><span class="no">Post</span><span class="p">.</span><span class="nf">all</span><span class="p">)</span> <span class="p">}</span>
<span class="k">end</span></code></pre></figure>

<p>You can also use <code class="language-plaintext highlighter-rouge">it_behaves_like</code> or <code class="language-plaintext highlighter-rouge">it_should_behaves_like</code> instead of include_examples in this case. <code class="language-plaintext highlighter-rouge">it_behaves_like</code> and <code class="language-plaintext highlighter-rouge">it_should_behaves_like</code> are actually aliases and works in same manner, so they can be used interchangeably. However, <code class="language-plaintext highlighter-rouge">include_examples</code> and <code class="language-plaintext highlighter-rouge">it_behaves_like</code> are different.</p>

<p>As stated in documentation,</p>

<ul>
  <li>include_examples` - includes the examples in the current context</li>
  <li><code class="language-plaintext highlighter-rouge">it_behaves_like</code> and <code class="language-plaintext highlighter-rouge">it_should_behave_like</code> - includes the examples in nested context</li>
</ul>

<h4 id="how-does-it-make-any-difference">How does it make any difference?</h4>
<p>RSpec documentation gives proper answer:</p>

<blockquote>
  <p>When you include parameterized examples in the current context multiple times, you may override previous method definitions and last declaration wins.</p>
</blockquote>

<p>So, when you face situation when parameterized examples contains methods that conflicts with another method in same context, you can replace <code class="language-plaintext highlighter-rouge">include_examples</code> with <code class="language-plaintext highlighter-rouge">it_behaves_like</code> method. This will create nested context and avoid this kind of situations.</p>

<h3 id="how-to-pass-parameters-to-shared-examples">How to pass parameters to shared examples</h3>

<p>Look at following line in users controller specs,
<code class="language-plaintext highlighter-rouge">it {  expect(assigns(:users)).to match(User.all) }</code> and  <code class="language-plaintext highlighter-rouge">it {  expect(assigns(:posts)).to match(Post.all) }</code>
from posts controller.</p>

<p>Now, controller specs can be refactored further by passing parameters to shared example as below:</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># specs/support/shared_examples/index_examples.rb</span>

<span class="c1"># here assigned_resource and resource class are parameters passed to index examples block</span>
<span class="no">RSpec</span><span class="p">.</span><span class="nf">shared_examples</span> <span class="s2">"index examples"</span> <span class="k">do</span> <span class="o">|</span><span class="n">assigned_resource</span><span class="p">,</span> <span class="n">resource_class</span><span class="o">|</span>
  <span class="n">it</span> <span class="p">{</span>	<span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">respond_with</span><span class="p">(</span><span class="ss">:ok</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">it</span> <span class="p">{</span>	<span class="n">expect</span><span class="p">(</span><span class="n">subject</span><span class="p">).</span><span class="nf">to</span> <span class="n">render_template</span><span class="p">(</span><span class="ss">:index</span><span class="p">)</span> <span class="p">}</span>
  <span class="n">it</span> <span class="p">{</span>  <span class="n">expect</span><span class="p">(</span><span class="n">assigns</span><span class="p">(</span><span class="n">assigned_resource</span><span class="p">)).</span><span class="nf">to</span> <span class="n">match</span><span class="p">(</span><span class="n">resource_class</span><span class="p">.</span><span class="nf">all</span><span class="p">)</span>   <span class="p">}</span>
<span class="k">end</span></code></pre></figure>

<p>Now, make following changes in users and posts controller specs.</p>

<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><span class="c1"># users_controller_spec.rb</span>
<span class="n">describe</span> <span class="s2">"GET #index"</span> <span class="k">do</span>
  <span class="n">before</span> <span class="k">do</span>
    <span class="o">...</span>
  <span class="k">end</span>
  <span class="n">include_examples</span> <span class="s2">"index examples"</span><span class="p">,</span> <span class="ss">:users</span><span class="p">,</span> <span class="no">User</span><span class="p">.</span><span class="nf">all</span>
<span class="k">end</span>

<span class="c1"># posts_controller_spec.rb</span>
<span class="n">describe</span> <span class="s2">"GET #index"</span> <span class="k">do</span>
  <span class="n">before</span> <span class="k">do</span>
    <span class="o">...</span>
  <span class="k">end</span>
  <span class="n">include_examples</span> <span class="s2">"index examples"</span><span class="p">,</span> <span class="ss">:posts</span><span class="p">,</span> <span class="no">Post</span><span class="p">.</span><span class="nf">all</span>
<span class="k">end</span></code></pre></figure>

<p>Now controller specs look clean, less redundant and more importantly, DRY. Furthermore, “index examples” can serve as basic structure for designing index action of other controllers.</p>

<h3 id="conclusion">Conclusion</h3>

<p>By moving common examples into separate file we can eliminate duplication and more importantly, we can improve consistency of our controller actions throughout the application. This is very useful in case of designing APIs, as we can use existing structure of RSpec tests to design tests and create APIs that adhere to common response structure. Mostly, I work with APIs and use shared examples to provide me common structure to design similar APIs.</p>

<p>The <a href="'https://medium.freecodecamp.com/how-to-dry-out-your-rspec-tests-using-shared-examples-d5cc5d33fd76'">article</a> was also published on <a href="'https://medium.freecodecamp.com/'">FreeCodeCamp publication</a>, for which I am thankful of <a href="https://medium.freecodecamp.org/@quincylarson">Quincy Larson</a>.</p>

<p>Feel free to share how you DRY up your specs and use shared examples.</p>]]></content><author><name>Parth Modi</name><email>parthmodi54@yahoo.com</email></author><category term="ruby" /><category term="rails" /><category term="rspec" /><category term="tdd" /><category term="web development" /><summary type="html"><![CDATA[When I refactored a project a few weeks ago, I spent most of my time writing specs. After writing several similar test cases for some APIs, I started to wonder whether I might be able to get rid of a lot of this duplication. So I threw myself into reading up on the best practices for DRYing up tests (Don’t Repeat Yourself). And that’s how I came to know of shared examples and shared contexts. In my case, I ended up using shared examples. And here’s what I’ve learned so far from applying these. When you have multiple specs that describes similar behavior, it might be better to extract redundant examples in shared examples and use them in multiple specs.]]></summary></entry></feed>