<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ranvir Singh</title>
    <description>The latest articles on DEV Community by Ranvir Singh (@singh1114).</description>
    <link>https://dev.to/singh1114</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F152306%2Fc34489cb-341e-49e2-a642-57079a015055.jpeg</url>
      <title>DEV Community: Ranvir Singh</title>
      <link>https://dev.to/singh1114</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/singh1114"/>
    <language>en</language>
    <item>
      <title>How does Django validate passwords?</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Mon, 21 Sep 2020 11:21:34 +0000</pubDate>
      <link>https://dev.to/singh1114/how-does-django-validate-passwords-339o</link>
      <guid>https://dev.to/singh1114/how-does-django-validate-passwords-339o</guid>
      <description>&lt;p&gt;This blog was first posted on &lt;a href="https://ranvir.xyz/blog/how-does-django-validate-passwords"&gt;my blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A few days ago, I was working on one of my old Django projects. It was running an old version of Django and I wanted to keep it updated with the latest changes of the framework.&lt;/p&gt;

&lt;p&gt;So to check the &lt;a href="https://ranvir.xyz/blog/django-admin-tips-and-tricks/"&gt;admin site&lt;/a&gt;, I tried to create the superuser after connection to the local database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;createsuperuser&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I passed a password similar to the username it failed saying, &lt;code&gt;The password is too similar to the username.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OBpUFo2q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/94D2x9G/Screenshot-2020-09-19-at-1-32-17-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OBpUFo2q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/94D2x9G/Screenshot-2020-09-19-at-1-32-17-AM.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This error triggered me to check the working behind this password validation feature.&lt;/p&gt;

&lt;p&gt;The first thing that I looked into was the &lt;code&gt;manage.py&lt;/code&gt; file itself which in turn was importing and executing a method called, &lt;code&gt;execute_from_command_line&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I traced it back and found a package &lt;code&gt;commands&lt;/code&gt; containing everything that I wanted to know. This directory had two files.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="mf"&gt;1.&lt;/span&gt; &lt;span class="n"&gt;createsuperuser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;
&lt;span class="mf"&gt;2.&lt;/span&gt; &lt;span class="n"&gt;changepassword&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The changepassword command
&lt;/h2&gt;

&lt;p&gt;Since I had never used/ heard about the &lt;code&gt;changepassword&lt;/code&gt; command, I thought of trying it first and to my great pleasure, it worked. You have to pass the username as the first argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;changepassword&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AOQAs5SB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/YRfMkYW/Screenshot-2020-09-19-at-3-10-09-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AOQAs5SB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/YRfMkYW/Screenshot-2020-09-19-at-3-10-09-AM.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sometimes you find gold when you read the code, right? 😍&lt;/p&gt;

&lt;p&gt;Now let's get back to the business and look into the &lt;code&gt;createsuperuser&lt;/code&gt; command class in more detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fetching the correct database
&lt;/h2&gt;

&lt;p&gt;If you have been using Django for some time, you would know that Django allows you to change a lot of things depending upon the settings you define.&lt;/p&gt;

&lt;p&gt;This also includes using some random model as your base User model. This is the first thing that the superuser creation &lt;code&gt;__init__&lt;/code&gt; constructor method checks for.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the superuser without interaction
&lt;/h2&gt;

&lt;p&gt;You can use a version of the command that allows you to create the superuser without any interaction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;python&lt;/span&gt; &lt;span class="n"&gt;manage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;py&lt;/span&gt; &lt;span class="n"&gt;createsuperuser&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="n"&gt;ranvir&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="n"&gt;abc&lt;/span&gt;&lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;abc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;no&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;input&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gE2JWFOX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/LJtHHq5/Screenshot-2020-09-19-at-3-17-49-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gE2JWFOX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/LJtHHq5/Screenshot-2020-09-19-at-3-17-49-AM.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although the user created using this process will have no password. We can create the password either using the &lt;code&gt;changepassword&lt;/code&gt; command or the admin panel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Required fields and interactive mode
&lt;/h2&gt;

&lt;p&gt;For the default &lt;code&gt;User&lt;/code&gt; model, &lt;code&gt;email&lt;/code&gt; is the only required field but you can change that by changing your &lt;code&gt;REQUIRED_FIELD&lt;/code&gt; setting as well.&lt;/p&gt;

&lt;p&gt;In the interactive mode( which is the default mode as well), the first thing that the prompt asks you to fill, is the username.&lt;/p&gt;

&lt;p&gt;Django tries to smartly suggest the current system username as the default username. (Just Wow)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--T61GFhYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/pr1Z1Qw/Screenshot-2020-09-19-at-2-14-46-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--T61GFhYD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/pr1Z1Qw/Screenshot-2020-09-19-at-2-14-46-AM.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It won't suggest the system username if it is already taken. (That's AI for me 😂)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TOM5OjoO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/r3YxbKL/Screenshot-2020-09-19-at-2-15-41-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TOM5OjoO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/r3YxbKL/Screenshot-2020-09-19-at-2-15-41-AM.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the username, you have to fill in the required field which is the email field for the default User model.&lt;/p&gt;

&lt;p&gt;Finally, you have to fill in the password field.&lt;/p&gt;

&lt;h2&gt;
  
  
  The validate password method
&lt;/h2&gt;

&lt;p&gt;Sorry for keeping you waiting this long before jumping onto the real reason behind the post.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;validatepassword&lt;/code&gt; is the function that is used to validate the password provided by the user.&lt;/p&gt;

&lt;p&gt;Again, we can configure all these validators as well, if these different password validation doesn't work for you, go forward and remove the classes from your settings file.&lt;/p&gt;

&lt;p&gt;These are the default validators.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;AUTH_PASSWORD_VALIDATORS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'NAME'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'django.contrib.auth.password_validation.UserAttributeSimilarityValidator'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'NAME'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'django.contrib.auth.password_validation.MinimumLengthValidator'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'NAME'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'django.contrib.auth.password_validation.CommonPasswordValidator'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s"&gt;'NAME'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'django.contrib.auth.password_validation.NumericPasswordValidator'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we use the default validators, then the password,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Should not be similar to &lt;code&gt;username&lt;/code&gt;, &lt;code&gt;first_name&lt;/code&gt;, &lt;code&gt;last_name&lt;/code&gt; and &lt;code&gt;email&lt;/code&gt;. It also checks for the similarity using &lt;a href="https://docs.python.org/2.4/lib/sequence-matcher.html"&gt;SequenceMatcher&lt;/a&gt;. It should be less than 0.7 similar which you can customize. (Told you, it's AI)&lt;/li&gt;
&lt;li&gt;Should be greater than 8 characters.&lt;/li&gt;
&lt;li&gt;Should not be in the list of common passwords. The list of common passwords is in the file, &lt;code&gt;common-passwords.txt.gz&lt;/code&gt;. It contains a list of around 20000 common passwords which you should not use.&lt;/li&gt;
&lt;li&gt;Should not contain all numeric characters.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I would suggest keeping the basic configuration intact for the password handling. You can use your own validations on top of it as well.&lt;/p&gt;

&lt;p&gt;So, that's it for this time. Till next time.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Achieving asynchronous behavior using asyncio in Python</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Tue, 01 Sep 2020 06:23:03 +0000</pubDate>
      <link>https://dev.to/singh1114/achieving-asynchronous-behavior-using-asyncio-in-python-2acc</link>
      <guid>https://dev.to/singh1114/achieving-asynchronous-behavior-using-asyncio-in-python-2acc</guid>
      <description>&lt;p&gt;This post was first published on &lt;a href="https://ranvir.xyz/blog/asynchronous-behaviour-using-asyncio-in-python/"&gt;my blog.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A lot has changed since I last published my post on handling long-running async tasks in Python using celery. Last time we used it to run a few async tasks to fetch data from &lt;a href="https://ranvir.xyz/blog/using-celery-to-run-long-running-task-asynchronously"&gt;some service which took around 1-5 minutes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Although I will still use celery for these types of long-running tasks, there is a subset of tasks that are better handled inside the execution thread itself. Today we will discuss a way of handling such operations.&lt;/p&gt;

&lt;p&gt;To the people who know me, I switched to a new role which allowed me to write Node.js over a year ago and I have been working on it since then. During my time working and building APIs using it, I have started falling for the way in which Node handles &lt;a href="https://ranvir.xyz/blog/basic-introduction-to-node.js-async-await"&gt;Async behavior and long-running tasks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The idea is simple, if the task is waiting for some IO, we can run some other task in the mean-time. Only the part that needs it, will wait for it.&lt;/p&gt;

&lt;p&gt;I wanted to use the same concept in Python. Fortunately, in Python version &lt;code&gt;3.4&lt;/code&gt;, they have introduced &lt;code&gt;asyncio&lt;/code&gt; for the same purpose. In this post, we are going to talk a little about it and try to understand its importance.&lt;/p&gt;

&lt;p&gt;By the end of the post, you will be able to understand the importance of async functions and will be able to start using them into your codebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisite
&lt;/h2&gt;

&lt;p&gt;I am using &lt;code&gt;Python 3.8&lt;/code&gt; for this tutorial and you might want to use the same to run and try out the examples. You can try them &lt;a href="https://www.python.org/shell/"&gt;online as well&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic async function in Python
&lt;/h2&gt;

&lt;p&gt;This is how we write a basic async function in Python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 1: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Response&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have to start by importing the &lt;code&gt;asyncio&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;Async functions are defined just like a normal function, you just have to add the keyword, &lt;code&gt;async&lt;/code&gt;. If you want to wait for the execution of some &lt;a href="https://docs.python.org/3/library/asyncio-task.html#coroutines"&gt;coroutine&lt;/a&gt;, you have to use the keyword, &lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Finally, you can run the function using &lt;code&gt;asyncio.run&lt;/code&gt; method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Anytime you use the await keyword inside the async function, the thread will not move forward until we get a response from the called function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This simple implementation will not help you understand the advantage of using async functions. In the next section, we will talk about the time taken for the execution and there you will really start to see the difference.&lt;/p&gt;

&lt;h2&gt;
  
  
  Time taken for the full execution
&lt;/h2&gt;

&lt;p&gt;To understand the benefits of using &lt;code&gt;async&lt;/code&gt; functions, we will compare the time taken to run the same code in &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;sync&lt;/code&gt; behavior.&lt;/p&gt;

&lt;p&gt;Let's first write the async code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;
&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 1: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 2: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 3: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 4: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;func3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;func4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Completed after: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have defined 4 different functions doing almost the same thing. Let's assume there is a long-running function called &lt;code&gt;async_sleep&lt;/code&gt; which is doing some IO operation. For example: Fetching data from the database.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;asyncio.gather()&lt;/code&gt; is used to run the tasks passed to it concurrently.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The response after running this code is as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;Completed&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;1.1852593421936035&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The function completes the execution somewhere between 1-2 seconds for every execution. (Ran it 5 times to check).&lt;/p&gt;

&lt;p&gt;Now let's try to run the &lt;code&gt;sync&lt;/code&gt; version of the same code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;
&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sync_sleep&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 1: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sync_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 2: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sync_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 3: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sync_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"started func 4: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sync_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;func3&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;func4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Completed after: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response that we got after executing this was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;Completed&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;4.168870687484741&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This saved us &lt;code&gt;~3&lt;/code&gt; seconds for executing 4 functions. Now if we wanted to run 10000 such functions, we would save ourselves &lt;code&gt;~2000&lt;/code&gt; seconds/ i.e. &lt;code&gt;35-40&lt;/code&gt; minutes.&lt;/p&gt;

&lt;p&gt;That's awesome, right.&lt;/p&gt;

&lt;p&gt;Even if we are just running 2-3 such functions in each iteration these small savings can be very important to provide better user experience to your customers.&lt;/p&gt;

&lt;p&gt;Now that we have understood the advantage of using &lt;code&gt;async&lt;/code&gt; functions, we must know a few more things about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Order of execution for async functions
&lt;/h2&gt;

&lt;p&gt;We should understand that while running the functions in async mode, we don't really know about the order in which functions are executed. Let's understand the concept with an example.&lt;/p&gt;

&lt;p&gt;The functions will return as soon as the execution is completed.&lt;/p&gt;

&lt;p&gt;We can mock the different execution time by sleeping for a random time between 0 and 1 using, &lt;code&gt;random.randint(0, 10) * 0.1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is the full code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;asyncio&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;time&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;random&lt;/span&gt;
&lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;random_sleep_time&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;randint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;random_sleep_time&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"completed func &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gather&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;"Completed after: &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The response to the following code is,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;completed&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="n"&gt;completed&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="n"&gt;completed&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="n"&gt;completed&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;span class="n"&gt;completed&lt;/span&gt; &lt;span class="n"&gt;func&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
&lt;span class="n"&gt;Completed&lt;/span&gt; &lt;span class="n"&gt;after&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.9764895439147949&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Practical Usage
&lt;/h2&gt;

&lt;p&gt;After the release of Python 3.8, which moved the &lt;code&gt;asyncio.run()&lt;/code&gt; method to stable API, you can start using it without any problem.&lt;/p&gt;

&lt;p&gt;The only issue with moving from a &lt;code&gt;synchronous&lt;/code&gt; approach to an async one, is to change the way of thinking about the problems. You have to change the way you think about every little detail. Once you reach that point of thinking in terms of async functions, believe me, you will love it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does it work under the hood?
&lt;/h2&gt;

&lt;p&gt;Let's check the type of the async function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;lt;class 'coroutine'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, the async function is of type &lt;code&gt;coroutine&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://www.geeksforgeeks.org/coroutine-in-python/"&gt;GeeksForGeeks article&lt;/a&gt; does a very good job of explaining coroutines.&lt;/p&gt;

&lt;p&gt;To sum up what they wrote in the post, &lt;strong&gt;subroutines&lt;/strong&gt; are the set of instructions that have one entry point and are executed serially.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;strong&gt;Coroutines&lt;/strong&gt; can suspend or give away the control to other Coroutines, allowing multiple tasks to run at the same time.&lt;/p&gt;

&lt;p&gt;Python uses this concept to allow async behavior.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling timeout in async functions
&lt;/h2&gt;

&lt;p&gt;It is necessary to have a timeout for waiting for the task to get completed. We are not supposed to wait forever for it to complete. &lt;code&gt;asyncio&lt;/code&gt; also provided the ability to add a timeout to the async function so that you can skip the execution before its completion.&lt;/p&gt;

&lt;p&gt;A practical application of this can be a case when you are calling a third party API in your application and the third party itself is down. In that case, you don't want to wait for a long time.&lt;/p&gt;

&lt;p&gt;You can use the &lt;code&gt;timeout&lt;/code&gt; method to solve your problem. See the code for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Execution completed'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;async_sleep&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;timeout&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;TimeoutError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Timeout error'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;asyncio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The async function will raise &lt;code&gt;TimeoutError&lt;/code&gt; if the coroutine doesn't return before the given &lt;code&gt;timeout&lt;/code&gt; is passed.&lt;/p&gt;

&lt;p&gt;Response after executing the above code is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;Timeout&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are other awesome methods in the &lt;code&gt;asyncio&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;You can check them at &lt;a href="https://docs.python.org/3/library/asyncio-task.html"&gt;python's official website&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;If you want to provide a better and faster experience to your users, you can start using the &lt;code&gt;asyncio&lt;/code&gt; module in your application. This will definitely help you find cases in which you can reduce the execution time of your API or whatever else that you are doing.&lt;/p&gt;

&lt;p&gt;Just forcing yourself to write in this way for a few months can yield big returns in the future. Would love to hear your thoughts, please reach out using the comments below.&lt;/p&gt;

</description>
      <category>python</category>
      <category>productivity</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Spring Boot Continuous Integration pipeline using CircleCI</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Sun, 31 May 2020 09:36:49 +0000</pubDate>
      <link>https://dev.to/singh1114/spring-boot-continuous-integration-pipeline-using-circleci-2544</link>
      <guid>https://dev.to/singh1114/spring-boot-continuous-integration-pipeline-using-circleci-2544</guid>
      <description>&lt;p&gt;This post was first published on my blog. Please read, &lt;a href="https://ranvir.xyz/blog/spring-boot-ci-pipeline-using-circleci/" rel="noopener noreferrer"&gt;Spring Boot Continuous Integration pipeline using CircleCI&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It is very important to automate the process of running the tests before deploying your code. &lt;a href="https://circleci.com/" rel="noopener noreferrer"&gt;CircleCI&lt;/a&gt; is a platform which can you to create &lt;a href="https://en.wikipedia.org/wiki/Continuous_integration" rel="noopener noreferrer"&gt;CI&lt;/a&gt; pipelines for your project.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FFgf1hTz%2FMain-Images-1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FFgf1hTz%2FMain-Images-1.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you haven't yet set your test environment, you can do so by following my last post on &lt;a href="https://dev.totesting-in-spring-boot-java-with-junit-for-beginners"&gt;Spring boot integration tests&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;CircleCI will try to run the project according to the configuration specified in a special &lt;code&gt;yml&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Login to CircleCI and turn on the test for the repository
&lt;/h2&gt;

&lt;p&gt;You can easily log in to Circle CI using your GitHub or Bitbucket account. After that you can turn on the CI pipeline by clicking on &lt;code&gt;Set up Project&lt;/code&gt; button for that repository.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F1MJd984%2FScreenshot-2020-04-24-at-12-52-04-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F1MJd984%2FScreenshot-2020-04-24-at-12-52-04-AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the next step it will ask you about the language of the project. You can choose any and according to your choice it will create a branch with &lt;code&gt;.circleci/config.yml&lt;/code&gt; file in the root directory.&lt;/p&gt;

&lt;p&gt;After a lot of tweaks to the config file that CircleCI created for me, I ended up with this.&lt;/p&gt;

&lt;h2&gt;
  
  
  .cirlceci file
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="m"&gt;2.1&lt;/span&gt;

&lt;span class="na"&gt;jobs&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;build&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;docker&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;circleci/openjdk:8-jdk&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;circleci/mysql:5.7&lt;/span&gt;
        &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;MYSQL_ROOT_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rootpw&lt;/span&gt;
          &lt;span class="na"&gt;MYSQL_DATABASE&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;test_db&lt;/span&gt;
          &lt;span class="na"&gt;MYSQL_USER&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;user&lt;/span&gt;
          &lt;span class="na"&gt;MYSQL_PASSWORD&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;passw0rd&lt;/span&gt;
    &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;checkout&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Waiting for MySQL to be ready&lt;/span&gt;
          &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
            &lt;span class="s"&gt;for i in `seq 1 10`;&lt;/span&gt;
            &lt;span class="s"&gt;do&lt;/span&gt;
              &lt;span class="s"&gt;nc -z 127.0.0.1 3306 &amp;amp;&amp;amp; echo Success &amp;amp;&amp;amp; exit 0&lt;/span&gt;
              &lt;span class="s"&gt;echo -n .&lt;/span&gt;
              &lt;span class="s"&gt;sleep 1&lt;/span&gt;
            &lt;span class="s"&gt;done&lt;/span&gt;
            &lt;span class="s"&gt;echo Failed waiting for MySQL &amp;amp;&amp;amp; exit 1 &lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Install MySQL CLI; Import dummy data; run an example query&lt;/span&gt;
          &lt;span class="na"&gt;command&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
            &lt;span class="s"&gt;sudo apt-get update&lt;/span&gt;
            &lt;span class="s"&gt;sudo apt-get install mysql*&lt;/span&gt;
            &lt;span class="s"&gt;mysql -h 127.0.0.1 -u root -prootpw --execute="use test_db;"&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="s"&gt;mvn test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are using two docker images, &lt;code&gt;Java 8&lt;/code&gt; and &lt;code&gt;MySQL&lt;/code&gt; and according to the way circle CI is set up both the images are built on the separate containers. So, we have to wait for MySQL to start before running the project.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;- checkout&lt;/code&gt;, we are getting the latest code from the given branch. Then we are waiting for the MySQL to be ready. Eventually, we are running a few SQL commands to connect to the database. Lastly, we are trying to run the tests.&lt;/p&gt;

&lt;p&gt;Just push this part of the code and it will run the tests for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Test changes that I had to do.
&lt;/h2&gt;

&lt;p&gt;The earlier written tests are good for local, and they work great when trying it on the local. But while running the tests on the CI, we have to test a little more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Strategy for testing
&lt;/h3&gt;

&lt;p&gt;In the single Test case I am going to test two APIs, one for creating the &lt;code&gt;A School&lt;/code&gt; and then checking if the entry was created or not.&lt;/p&gt;

&lt;h3&gt;
  
  
  Changes required in configuration
&lt;/h3&gt;

&lt;p&gt;Since on CI, we have to create the tables again and again we have to change the following in the &lt;code&gt;application-test.properties&lt;/code&gt; file. Also, since we have changed the database name, we can change that configuration as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.datasource.url=jdbc:mysql://localhost:3306/test_db
spring.jpa.hibernate.ddl-auto=create-drop
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Changes required in Test file.
&lt;/h3&gt;

&lt;p&gt;According to the strategy change, we will change the way we write our test as well. This is how our updated test file looks like.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;package&lt;/span&gt; &lt;span class="nn"&gt;io.singh1114.springboottut&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.json.JSONException&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.junit.Test&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.junit.runner.RunWith&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.skyscreamer.jsonassert.JSONAssert&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.boot.test.context.SpringBootTest&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.boot.test.web.client.TestRestTemplate&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.boot.web.server.LocalServerPort&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.http.*&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.test.context.ActiveProfiles&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.springframework.test.context.junit4.SpringJUnit4ClassRunner&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nd"&gt;@ActiveProfiles&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@RunWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SpringJUnit4ClassRunner&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="nd"&gt;@SpringBootTest&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;webEnvironment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SpringBootTest&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;WebEnvironment&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;RANDOM_PORT&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SchoolControllerTest&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@LocalServerPort&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="nc"&gt;TestRestTemplate&lt;/span&gt; &lt;span class="n"&gt;restTemplate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TestRestTemplate&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;testGetSchoolData&lt;/span&gt; &lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;JSONException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;HttpHeaders&lt;/span&gt; &lt;span class="n"&gt;postHeaders&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpHeaders&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;postHeaders&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setContentType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MediaType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;APPLICATION_JSON&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;requestJson&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"{\"name\":\"School 1\", \"principle\":\"Mr. Charles\", \"address\":\"California\"}"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;HttpEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;schoolPostEntity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="n"&gt;requestJson&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;postHeaders&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;restTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exchange&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;createURLWithPort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/school"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;POST&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schoolPostEntity&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;HttpHeaders&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpHeaders&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="nc"&gt;HttpEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;restTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exchange&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;createURLWithPort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/schools"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;GET&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;expected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"[{\"id\":1,\"name\":\"School 1\",\"principle\":\"Mr. Charles\",\"address\":\"California\"}]"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="nc"&gt;JSONAssert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBody&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;createURLWithPort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"http://localhost:"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly, we are sending a &lt;code&gt;POST&lt;/code&gt; request to the &lt;code&gt;/school&lt;/code&gt; API to add data to the school database and finally we are sending a &lt;code&gt;GET&lt;/code&gt; request to get the data for &lt;code&gt;/schools&lt;/code&gt; API.&lt;/p&gt;

&lt;p&gt;We can confirm that the things are working fine by running the tests in debug mode and stopping once the &lt;code&gt;POST&lt;/code&gt; request is made and checking in the database at the same time.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2Fzb9CB2p%2FScreenshot-2020-04-24-at-1-21-29-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2Fzb9CB2p%2FScreenshot-2020-04-24-at-1-21-29-AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the test is done, you can run the same SQL command to see that the table was deleted.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FLryGrDm%2FScreenshot-2020-04-24-at-1-23-39-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2FLryGrDm%2FScreenshot-2020-04-24-at-1-23-39-AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, you can push the code to see the tests running on CircleCI.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F5FLQ44M%2FScreenshot-2020-04-24-at-1-26-43-AM.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.ibb.co%2F5FLQ44M%2FScreenshot-2020-04-24-at-1-26-43-AM.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can learn more about the changes in the following Pull Requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/singh1114/java_tutorial/pull/1" rel="noopener noreferrer"&gt;Circle CI setup PR&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/singh1114/java_tutorial/pull/2" rel="noopener noreferrer"&gt;Test changes PR&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you liked the post. Please share your views in the comment section below. Also, please &lt;a href="https://ranvir.xyz/blog/subscribe" rel="noopener noreferrer"&gt;Subscribe&lt;/a&gt; if you want to read more such posts.&lt;/p&gt;

</description>
      <category>java</category>
      <category>springboot</category>
      <category>testing</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to KNN | K-nearest neighbor algorithm using Examples</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Thu, 02 Apr 2020 18:37:04 +0000</pubDate>
      <link>https://dev.to/singh1114/introduction-to-knn-k-nearest-neighbor-algorithm-using-examples-4l7c</link>
      <guid>https://dev.to/singh1114/introduction-to-knn-k-nearest-neighbor-algorithm-using-examples-4l7c</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4S7Urt69--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3rMdVO4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4S7Urt69--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3rMdVO4.png" alt="KNN algorithm python"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post was first posted on my blog. Please read &lt;a href="https://ranvir.xyz/blog/k-nearest-neighbor-algorithm-using-sklearn-distance-metric/"&gt;K-nearest neighbor algorithm with Sklearn&lt;/a&gt; and upvote on &lt;a href="https://www.reddit.com/r/coding/comments/fni7no/introduction_to_knn_knearest_neighbor_algorithm/"&gt;Reddit&lt;/a&gt; and &lt;a href="https://news.ycombinator.com/item?id=22718355"&gt;hackernews&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;KNN&lt;/code&gt; also known as K-nearest neighbor is a &lt;a href="https://ranvir.xyz/blog/how-to-evaluate-your-machine-learning-model-like-a-pro-metrics/#supervised-learning-and-classification-problems"&gt;supervised and pattern classification learning algorithm&lt;/a&gt; which helps us to find which class the new input(test value) belongs to when &lt;code&gt;k&lt;/code&gt; nearest neighbors are chosen and distance is calculated between them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It attempts to estimate the conditional distribution of &lt;code&gt;Y&lt;/code&gt; given &lt;code&gt;X&lt;/code&gt;, and classify a given observation(test value) to the class with highest estimated probability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It first identifies the &lt;code&gt;k&lt;/code&gt; points in the training data that are closest to the &lt;code&gt;test value&lt;/code&gt; and calculates the distance between all those categories. The test value will belong to the category whose distance is the least.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WwpfzOHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/XXWScgF.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WwpfzOHx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/XXWScgF.png" alt="KNN algorithm distance"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Probability of classification of test value in KNN
&lt;/h2&gt;

&lt;p&gt;It calculates the probability of test value to be in class &lt;code&gt;j&lt;/code&gt; using this function&lt;/p&gt;

&lt;p&gt;

&lt;/p&gt;
&lt;div class="katex-element"&gt;
  &lt;span class="katex-display"&gt;&lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;Pr(Y=j∣X=xo)=1K∑iϵNoI(yi=j)
P_r(Y=j|X=x_o) = \frac{1}{K}\sum_{i\epsilon N_o}I(y_i = j)
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;P&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;r&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathnormal"&gt;Y&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;span class="mord"&gt;∣&lt;/span&gt;&lt;span class="mord mathnormal"&gt;X&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;x&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;o&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mopen nulldelimiter"&gt;&lt;/span&gt;&lt;span class="mfrac"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;K&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="frac-line"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mclose nulldelimiter"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mop op-limits"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;span class="mord mathnormal mtight"&gt;ϵ&lt;/span&gt;&lt;span class="mord mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;N&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size3 size1 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;o&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span&gt;&lt;span class="mop op-symbol large-op"&gt;∑&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;I&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord"&gt;&lt;span class="mord mathnormal"&gt;y&lt;/span&gt;&lt;span class="msupsub"&gt;&lt;span class="vlist-t vlist-t2"&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;span class="pstrut"&gt;&lt;/span&gt;&lt;span class="sizing reset-size6 size3 mtight"&gt;&lt;span class="mord mathnormal mtight"&gt;i&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-s"&gt;​&lt;/span&gt;&lt;/span&gt;&lt;span class="vlist-r"&gt;&lt;span class="vlist"&gt;&lt;span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord mathnormal"&gt;j&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Ways to calculate the distance in KNN
&lt;/h2&gt;

&lt;p&gt;The distance can be calculated using different ways which include these methods,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Euclidean Method&lt;/li&gt;
&lt;li&gt;Manhattan Method&lt;/li&gt;
&lt;li&gt;Minkowski Method&lt;/li&gt;
&lt;li&gt;etc...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more information on distance metrics which can be used, please read &lt;a href="https://www.saedsayad.com/k_nearest_neighbors.htm"&gt;this post on KNN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You can use any method from the list by passing &lt;code&gt;metric&lt;/code&gt; parameter to the KNN object. Here is an answer on &lt;a href="https://stackoverflow.com/questions/21052509/sklearn-knn-usage-with-a-user-defined-metric"&gt;Stack Overflow which will help&lt;/a&gt;. You can even use some random distance metric.&lt;/p&gt;

&lt;p&gt;Also &lt;a href="https://stackoverflow.com/questions/34408027/how-to-allow-sklearn-k-nearest-neighbors-to-take-custom-distance-metric"&gt;read this answer as well&lt;/a&gt; if you want to use your own method for distance calculation.&lt;/p&gt;

&lt;h2&gt;
  
  
  The process of KNN with Example
&lt;/h2&gt;

&lt;p&gt;Let's consider that we have a dataset containing heights and weights of dogs and horses marked properly. We will create a plot using weight and height of all the entries.&lt;/p&gt;

&lt;p&gt;Now whenever a new entry comes in from the test dataset, we will choose a value of &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For the sake of this example, let's assume that we choose 4 as the value of &lt;code&gt;k&lt;/code&gt;. We will find the distance of the nearest four values and the one having the least distance will have more probability and is assumed as the winner.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sfHa8JIM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/heaLl5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sfHa8JIM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/heaLl5s.png" alt="KNN algorithm example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  KneighborsClassifier: KNN Python Example
&lt;/h2&gt;

&lt;p&gt;GitHub Repo: &lt;a href="https://github.com/singh1114/ml/blob/master/datascience/Machine%20learning/knn/knn.ipynb"&gt;KNN GitHub Repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Data source used: &lt;a href="https://github.com/singh1114/ml/blob/master/datascience/Machine%20learning/knn/KNN_Project_Data"&gt;GitHub of Data Source&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In K-nearest neighbor algorithm most of the time you don't really know about the meaning of the input parameters or the classification classes available.&lt;/p&gt;

&lt;p&gt;In the case of interviews, this is done to hide the real customer data from the potential employee.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# Import everything
&lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;pandas&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;numpy&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;matplotlib.pyplot&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;seaborn&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sns&lt;/span&gt;
&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="n"&gt;matplotlib&lt;/span&gt; &lt;span class="n"&gt;inline&lt;/span&gt;

&lt;span class="c1"&gt;# Create a DataFrame
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;read_csv&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'KNN_Project_Data'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Print the head of the data.
&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.imgur.com/2bDtkX4.png"&gt;KNN algorithm Head of dataframe&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The head of the data clearly says that we have a few variables and a target class that contain different classes for given parameters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why normalize/ standardize the variables for KNN
&lt;/h3&gt;

&lt;p&gt;As we can already see that the data in the data frame is not standardized, if we don't normalize the data the outcome will be fairly different and we won't be able to get the correct results.&lt;/p&gt;

&lt;p&gt;This happens because some feature has a good amount of deviation in them (values range from 1-1000). This will lead to a very bad plot producing a lot of defects in the model.&lt;/p&gt;

&lt;p&gt;For more info on normalization, check this answer on &lt;a href="https://stats.stackexchange.com/a/287439"&gt;stack exchange&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sklearn provides a very simple way to standardize your data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;sklearn.preprocessing&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;

&lt;span class="n"&gt;scaler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;StandardScaler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'TARGET CLASS'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;sc_transform&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scaler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'TARGET CLASS'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;axis&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;sc_df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sc_transform&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Now you can safely use sc_df as your input features.
&lt;/span&gt;&lt;span class="n"&gt;sc_df&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R0C5D4na--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/6ADY6NW.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R0C5D4na--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/6ADY6NW.png" alt="KNN algorithm normalized data frame"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Test/Train split using sklearn
&lt;/h3&gt;

&lt;p&gt;We can simply &lt;a href="https://ranvir.xyz/blog/how-to-evaluate-your-machine-learning-model-like-a-pro-metrics/#test-train-split-using-sklearn"&gt;split the data using sklearn&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;sklearn.model_selection&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;train_test_split&lt;/span&gt;

&lt;span class="n"&gt;X&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sc_transform&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;df&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'TARGET CLASS'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_test&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;train_test_split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;test_size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;0.3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using KNN and finding an optimal k value
&lt;/h3&gt;

&lt;p&gt;Choosing a good value of &lt;code&gt;k&lt;/code&gt; can be a daunting task. We are going to automate this task using Python. We were able to find a good value of &lt;code&gt;k&lt;/code&gt; which can minimize the error rate in the model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Initialize an array that stores the error rates.
&lt;/span&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;sklearn.neighbors&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;KNeighborsClassifier&lt;/span&gt;

&lt;span class="n"&gt;error_rates&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
    &lt;span class="n"&gt;knn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;KNeighborsClassifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n_neighbors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;knn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;preds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;knn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;error_rates&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;preds&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="n"&gt;error_rates&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'blue'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;linestyle&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'dashed'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;markerfacecolor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'red'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;markersize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Error Rate vs. K Value'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'K'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Error Rate'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nJODkr48--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/j4EsgY8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nJODkr48--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/j4EsgY8.png" alt="Finding optimal k value"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Seeing the graph, we can see that &lt;code&gt;k=30&lt;/code&gt; gives a very optimal value of error rate.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;
&lt;span class="n"&gt;knn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;KNeighborsClassifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n_neighbors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;knn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_train&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y_train&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;preds&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;knn&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;predict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;X_test&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Evaluating the KNN model
&lt;/h2&gt;

&lt;p&gt;Read the following post to learn more about &lt;a href="https://ranvir.xyz/blog/how-to-evaluate-your-machine-learning-model-like-a-pro-metrics/"&gt;evaluating a machine learning model&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;sklearn.metrics&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;confusion_matrix&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;classification_report&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;confusion_matrix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;preds&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;classification_report&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y_test&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;preds&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UjmgMUKy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Iu5PoLx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UjmgMUKy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Iu5PoLx.png" alt="confusion matrix and classification report"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Benefits of using KNN algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;KNN algorithm is widely used for different kinds of learnings because of its uncomplicated and easy to apply nature.&lt;/li&gt;
&lt;li&gt;There are only two metrics to provide in the algorithm. value of &lt;code&gt;k&lt;/code&gt; and &lt;code&gt;distance metric&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Work with any number of classes, not just binary classifiers.&lt;/li&gt;
&lt;li&gt;It is fairly easy to add new data to algorithm.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Disadvantages of KNN algorithm
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;The cost of predicting the &lt;code&gt;k&lt;/code&gt; nearest neighbors is very high.&lt;/li&gt;
&lt;li&gt;Doesn't work as expected when working with a big number of features/parameters.&lt;/li&gt;
&lt;li&gt;Hard to work with categorical features.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good read that &lt;a href="https://jakevdp.github.io/blog/2013/04/29/benchmarking-nearest-neighbor-searches-in-python/"&gt;benchmarks various options present in sklearn for Knn&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hope you liked the post. Feel free to share any issues or any questions that you have in the comments on the &lt;a href="https://ranvir.xyz/blog/k-nearest-neighbor-algorithm-using-sklearn-distance-metric/"&gt;original article&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>computerscience</category>
      <category>python</category>
    </item>
    <item>
      <title>Using Google Optimize to show different versions of the same website</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Thu, 26 Mar 2020 20:59:13 +0000</pubDate>
      <link>https://dev.to/singh1114/using-google-optimize-to-show-different-versions-of-the-same-website-1jo1</link>
      <guid>https://dev.to/singh1114/using-google-optimize-to-show-different-versions-of-the-same-website-1jo1</guid>
      <description>&lt;p&gt;This post was first written on my blog. Read &lt;a href="https://blog.ranvir.xyz/google-optimizer-show-different-version-and-increase-load-time/"&gt;why not to use Google Optimizer and How to use if you want to?&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;How would you like to show a modified version of the same webpage to diverse users differently?&lt;/p&gt;

&lt;p&gt;What we want to achieve in the post is to change the structure of the page dynamically without you making any change to your code( False promise).&lt;/p&gt;

&lt;h2&gt;
  
  
  Back Story (You can skip if you want)
&lt;/h2&gt;

&lt;p&gt;I recently launched another version of &lt;a href="https://blog.ranvir.xyz/react-blog-using-netlify-cms-and-gatsby/"&gt;my blog using Gatsby&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So the idea behind creating another version of the blog was to slowly move everything to the new version without really affecting the traffic over my &lt;a href="https://ranvir.xyz/blog"&gt;Jekyll blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And why I want to move is already there in the &lt;a href="https://blog.ranvir.xyz/react-blog-using-netlify-cms-and-gatsby/"&gt;my blog using Gatsby post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also, I wanted to check which version of the website users like the most.&lt;/p&gt;

&lt;p&gt;As the &lt;a href="https://ranvir.xyz/blog"&gt;main blog&lt;/a&gt; contains most of the posts and does get a fair amount of traffic, I wanted to move a few users from the old website to the new one and check if the performance was improved even a little.&lt;/p&gt;

&lt;p&gt;Before jumping into any conclusion, I wanted to validate my theory with numbers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance metric I was looking to improve
&lt;/h2&gt;

&lt;p&gt;The key metric that I was looking to improve was the &lt;code&gt;bounce rate&lt;/code&gt;. One of the pages for which I was getting maximum page views was having a high value of bounce rate( 88%).&lt;/p&gt;

&lt;p&gt;I settled on the following strategy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Let,
   x &lt;span class="o"&gt;=&lt;/span&gt; Bounce rate of the page on Jekyll blog
   y &lt;span class="o"&gt;=&lt;/span&gt; Bounce rate of the page on React blog

If, y &amp;lt; x - 0.25x:
   move this page to React.

Else:
   No need to move.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means if the bounce rate of the new website comes out to somewhere around 65%, I will move the page to the new location with applied redirects.&lt;/p&gt;

&lt;p&gt;But still, there was a problem, as I was not running my own &lt;code&gt;nginx&lt;/code&gt;, I was not able to apply any of the &lt;a href="https://en.wikipedia.org/wiki/Virtual_routing_and_forwarding"&gt;routes forwarding techniques&lt;/a&gt; available in &lt;code&gt;nginx&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;My only option was to choose another service, I searched online and came across &lt;a href="https://optimize.google.com/"&gt;Google optmize&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It was fairly easy to understand and you can get started by using your existing Google account.&lt;/p&gt;

&lt;p&gt;Once you are in, it will ask simple things to complete the setup.&lt;/p&gt;

&lt;p&gt;The first thing that you can do is to &lt;code&gt;Create an Experience&lt;/code&gt;. Google Optimize provides 4 options to choose from.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UAsj4_6P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/bPf7DyR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UAsj4_6P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/bPf7DyR.png" alt="Create Experience with Google optimize"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before heading in to choose which experience you want to add, you will have to add the &lt;a href="https://chrome.google.com/webstore/detail/google-optimize/bhdplaindhdkiflmbfbciehdccfhegci"&gt;Google Optimize chrome extension&lt;/a&gt; which will allow you to make changes to the website.&lt;/p&gt;

&lt;p&gt;The one that I wanted to use was the &lt;code&gt;Redirect/ Split URL&lt;/code&gt; test. You can choose the one that suits your situation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://support.google.com/optimize/answer/7012154"&gt;Read this post for more details&lt;/a&gt; to choose from all the options.&lt;/p&gt;

&lt;h2&gt;
  
  
  A-B Test
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---02F4shB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/pX1JDYq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---02F4shB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/pX1JDYq.png" alt="A/B Testing using Google Optimize"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ranvir.xyz/blog/read-these-6-tips-to-take-advantage-of-a-b-testing/"&gt;A/B testing&lt;/a&gt; is a type of testing where you create two or more versions of a single webpage and show modified versions to each user.&lt;/p&gt;

&lt;p&gt;Google Optimize allows you to split traffic.&lt;/p&gt;

&lt;p&gt;Splitting the traffic means that each user who lands on the targeted webpage will either one of the versions that you have specified according to the traffic split that you have defined.&lt;/p&gt;

&lt;p&gt;If you have selected 50-50 split, nearly half of the users will see one result and another half will see the other.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you try to access the targeted web page, you will always see the same version for some time, no matter how hard you try. It takes some time to start triggering the next version and once that is live, you will see that version for some time( Most probably the traffic is split on the basis of time).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Optimize makes it fairly easy to create two or more versions. It loads the targeted webpage inside the given optimize environment so that you can apply CSS and make required changes on the fly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mVYrfEho--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/XEojyWu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mVYrfEho--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/XEojyWu.png" alt="Direct code change panel using Google Optimize"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Optimize will then store all the versions and wait for the users to land on the targeted page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Redirect Test
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bh8sp2Kl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/SfmzDsp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bh8sp2Kl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/SfmzDsp.png" alt="Redirect testing using Google Optimize"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Redirect/Split test was the one that I wanted to choose. As I have already specified what this means, I will skip explaining it in more detail.&lt;/p&gt;

&lt;p&gt;You will know when you want to use it when you have two versions of the same webpage on separate URLs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multivariate tests
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZTu-A69B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/DEEaTzj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZTu-A69B--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/DEEaTzj.png" alt="Multivariate Testing using Google Optimize"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A multivariate test is a type of A/B testing where you makes 2 or more combination of changes deployed together.&lt;/p&gt;

&lt;p&gt;I haven't really understood the real use case of this test.&lt;/p&gt;

&lt;p&gt;If you have used it before, do let me know.&lt;/p&gt;

&lt;h2&gt;
  
  
  Personalize Test
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d8sMQJ5P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/X1pjqQt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d8sMQJ5P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/X1pjqQt.png" alt="Personalize test options using Google Optimize"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Personalization Test will show different versions of your webpage to different people depending on their personality.&lt;/p&gt;

&lt;p&gt;You can create separate versions as we do in A/B testing and target different versions with different demographics, devices and many other options.&lt;/p&gt;

&lt;h2&gt;
  
  
  Turning the test ON
&lt;/h2&gt;

&lt;p&gt;You can turn the test &lt;code&gt;on&lt;/code&gt; by adding the optimize key to your &lt;a href="https://ranvir.xyz/blog/google-analytics-for-search-engine-optimisation/"&gt;Google Analytics&lt;/a&gt; string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nx"&gt;gtag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;config&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UA-*****-*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;optimize_id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;G**-******&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you are live with your &lt;code&gt;optimize_id&lt;/code&gt; change. You will see the traffic being diverted and data available in the Optimize account.&lt;/p&gt;

&lt;p&gt;Although it takes some time to compute the real data and come up with proper statistics.&lt;/p&gt;

&lt;p&gt;You can keep running a redirect test for 3 months.&lt;/p&gt;

&lt;p&gt;And after you get the correct result back you can decide what you want to do with your web page.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some numbers that will stop you from using Google Optimize
&lt;/h2&gt;

&lt;p&gt;I am a big fan of checking Google Page Speed statistics after every addition of dependency in my code.&lt;/p&gt;

&lt;p&gt;And as &lt;code&gt;Google Optimize&lt;/code&gt; is an added dependency, you might want to check the changes in the behavior of your site.&lt;/p&gt;

&lt;p&gt;The changes that I saw were pretty drastic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gs"&gt;**Old website page speed Score without optimize tag**&lt;/span&gt;
&lt;span class="p"&gt;
-&lt;/span&gt; Mobile score: 71, web score: 97

&lt;span class="gs"&gt;**New website page speed scores**&lt;/span&gt;
&lt;span class="p"&gt;
-&lt;/span&gt; Mobile score: 86, web score: 99

&lt;span class="gs"&gt;**Old website Score with optimize tag**&lt;/span&gt;
&lt;span class="p"&gt;
-&lt;/span&gt; Mobile score: 55, web score: 95
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can already see that there is a whole lot of difference in the page speed before and after the addition of the Google Optimize tag.&lt;/p&gt;

&lt;p&gt;I will leave the decision making step to you.&lt;/p&gt;

&lt;p&gt;You can make your own judgment whether you want to use Google Optimize or not. For me most of the visitors are Desktop, so I can totally ignore the mobile visitor and keep it that way. Still, after checking those results, it took the optimize tag away and went back to running my dumb blog.&lt;/p&gt;

&lt;h2&gt;
  
  
  If not Google Optimizer, what?
&lt;/h2&gt;

&lt;p&gt;I still haven't discovered the solution yet. I will update this when I get a real answer. Also, let me know guys if you get to any.&lt;/p&gt;

&lt;p&gt;Anyway if you can afford to have hire a developer and you have the control over your server, you can implement such things with great ease on the server level.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/1bkpuvyW_48"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>writing</category>
      <category>performance</category>
      <category>marketing</category>
      <category>testing</category>
    </item>
    <item>
      <title>Create react based blog using Netlify CMS and Gatsby</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Sun, 22 Mar 2020 19:53:38 +0000</pubDate>
      <link>https://dev.to/singh1114/create-react-based-blog-using-netlify-cms-and-gatsby-2c21</link>
      <guid>https://dev.to/singh1114/create-react-based-blog-using-netlify-cms-and-gatsby-2c21</guid>
      <description>&lt;p&gt;This post was originally shared on my blog. Read &lt;a href="https://ranvir.xyz/blog/react-blog-using-netlify-cms-and-gatsby/"&gt;create a react blog using gatsby&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Some weeks ago, my colleague shared the Gatsby blog that he created for his personal blog.&lt;/p&gt;

&lt;p&gt;He has also written a good post on &lt;a href="https://www.arbazsiddiqui.me/javascript-proxies-real-world-use-cases/"&gt;JavaScript Proxies&lt;/a&gt; that you will enjoy reading.&lt;/p&gt;

&lt;p&gt;Whenever I see a new website, I immediately head over to Google's pagespeed insight to find out the speed of the page. I was astonished by the speed that the page was able to achieve.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vXmWyeaC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/UsKCfsh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vXmWyeaC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/UsKCfsh.png" alt="PageSpeed numbers for a React based Gatsby blog"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Honestly speaking I was never able to reach to those numbers on the Jekyll blog.&lt;/p&gt;

&lt;p&gt;So, I started working on my personal Gatsby blog. This post will contain all the details which I happen to do to make this blog to the stage it is currently at.&lt;/p&gt;

&lt;p&gt;Also, currently I don't have the concept of private posts on this blog, I am unable to keep it private when it is clearly not finished. You have to bear with for this.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final result
&lt;/h2&gt;

&lt;p&gt;The final blog after this series is going to be &lt;a href="https://peaceful-mayer-ecb11a.netlify.com/"&gt;present on this URL&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;I don't think there are any prerequisites required to start a blog using Gatsby and Netlify CMS, It is fairly easy to set up if you know how to make changes and use basic GitHub.&lt;/p&gt;

&lt;p&gt;The basic things required for this specific version of post are:&lt;/p&gt;

&lt;h3&gt;
  
  
  Domain Name
&lt;/h3&gt;

&lt;p&gt;I already had a domain name which I was already using. It will still work if you don't have it though. 🙏&lt;/p&gt;

&lt;h3&gt;
  
  
  GitHub Account
&lt;/h3&gt;

&lt;p&gt;To follow this tutorial, you will need to have a GitHub account where the code for the blog can reside. There are other ways to keep the code, but I think GH provides the easiest way to maintain your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Netlify Account
&lt;/h3&gt;

&lt;p&gt;You will also need account on Netlify so that you can deploy and &lt;/p&gt;

&lt;p&gt;The rest of the stuff we are going to figure out during the course of the post.&lt;/p&gt;

&lt;p&gt;Let's begin&lt;/p&gt;

&lt;h2&gt;
  
  
  Choose a theme
&lt;/h2&gt;

&lt;p&gt;You can choose from a list of themes that are available. Most of them has &lt;code&gt;deploy on Netlify&lt;/code&gt; button which will deploy it directly on Netlify and can save the code to your Netlify Account. Next time you make any change to your code, netlify will automatically detect the change and deploy the change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G8bMcPtt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Ag2ERnJ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G8bMcPtt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/Ag2ERnJ.png" alt="Deploy Gatsby blog on Netlify using GitHub"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I choose the basic &lt;a href="https://www.netlify.com/with/gatsby/"&gt;gatsby-theme-blog&lt;/a&gt;. Just click on the deploy button and choose &lt;code&gt;Connect to GitHub&lt;/code&gt; the code and it will host the code on GitHub. Finally, choose the name of the Repository that you want to contain the code in.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making the changes
&lt;/h2&gt;

&lt;p&gt;Once your site is deployed on &lt;code&gt;something.netlify.com&lt;/code&gt;, you can clone the code locally and start making the changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/your_repo/repo_name
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more information on various &lt;a href="https://ranvir.xyz/blog/basic-commands-in-github/"&gt;git commands check this post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The first thing to change if the &lt;code&gt;gatsby-config.js&lt;/code&gt; file. Change &lt;code&gt;siteMetadata&lt;/code&gt; as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="nx"&gt;siteMetadata&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Tutorial blog`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Ranvir`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`Basic description of the blog`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Push the code and you will see the changes being deployed in your Netlify account. &lt;code&gt;https://app.netlify.com/sites/your_site_id/deploys&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oFa4oyfH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ACODtqR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oFa4oyfH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/ACODtqR.png" alt="Changes to GitHub code deploys blog on Netlify"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git push origin master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After some time the changes will be available on the current website.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Netlify CMS
&lt;/h2&gt;

&lt;p&gt;Adding Netlify CMS will help you to quickly make changes to your posts and deploy them with a click of a button. All your posts are at a single place.&lt;/p&gt;

&lt;p&gt;You can create and edit your posts easily using the Netlify CMS.&lt;/p&gt;

&lt;p&gt;For adding Netlify CMS and allowing editing using GitHub Login, you will have to add a &lt;code&gt;static&lt;/code&gt; directory to the root of your code.&lt;/p&gt;

&lt;p&gt;Add this to the &lt;code&gt;static/admin/config.yml&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;backend&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;github&lt;/span&gt;
  &lt;span class="na"&gt;repo&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;singh1114/tutorial&lt;/span&gt;
  &lt;span class="na"&gt;branch&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;master&lt;/span&gt;

&lt;span class="na"&gt;media_folder&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;static/img&lt;/span&gt;
&lt;span class="na"&gt;public_folder&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;/img&lt;/span&gt;

&lt;span class="na"&gt;collections&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;blog"&lt;/span&gt;
    &lt;span class="na"&gt;label&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Blog"&lt;/span&gt;
    &lt;span class="na"&gt;folder&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;content/posts"&lt;/span&gt;
    &lt;span class="na"&gt;create&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="na"&gt;slug&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;{{slug}}"&lt;/span&gt;
    &lt;span class="na"&gt;editor&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
     &lt;span class="na"&gt;preview&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;
    &lt;span class="na"&gt;fields&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Title"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;title"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;widget&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Publish&lt;/span&gt;&lt;span class="nv"&gt; &lt;/span&gt;&lt;span class="s"&gt;Date"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;date"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;widget&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;datetime"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Image"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;image"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;widget&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;required&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;false&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Tags"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;tags"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;widget&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;list"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;required&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;false&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Description"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;description"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;widget&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;string"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
      &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="pi"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;label&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Body"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;body"&lt;/span&gt;&lt;span class="pi"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;widget&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="s"&gt;markdown"&lt;/span&gt; &lt;span class="pi"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can change the fields using &lt;a href="https://www.netlifycms.org/docs/widgets/"&gt;widget guide&lt;/a&gt; according to your needs.&lt;/p&gt;

&lt;p&gt;After deploying this you can access the website's admin panel on &lt;code&gt;https://web.netlify.com/admin&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For turning on the GitHub login on your website's admin page, so that only you can edit the posts, you will have to turn on the OAuth access using your Netlify account.&lt;/p&gt;

&lt;p&gt;Just go to the &lt;code&gt;settings tab &amp;gt; Access Control &amp;gt; OAuth &amp;gt;Install Provider&lt;/code&gt;. For installing, you will need auth credentials which you can generate using the &lt;a href="https://github.com/settings/applications/"&gt;GitHub Application page&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add your netlify address as the home page URL and &lt;code&gt;https://api.netlify.com/auth/done&lt;/code&gt; as callback.&lt;/p&gt;

&lt;p&gt;Once done, you will be able to Login to the admin panel and make changes to the posts directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S7oqRSjn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/CEM2fBI.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S7oqRSjn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/CEM2fBI.png" alt="Admin panel for Gatsby blog on Netlify CMS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Redirects
&lt;/h2&gt;

&lt;p&gt;As I already told you that I had a domain name and I wanted the website to be hosted on that &lt;code&gt;https://blog.ranvir.xyz&lt;/code&gt; and not on &lt;code&gt;something.netlify.com&lt;/code&gt;. For this we will need to set up &lt;code&gt;CNAME&lt;/code&gt; record in your domain name provider.&lt;/p&gt;

&lt;p&gt;You can find more about what record to set with simple google search.&lt;/p&gt;

&lt;p&gt;In my case, where I was using a subdomain, I had to use &lt;code&gt;blog&lt;/code&gt; as a host and &lt;code&gt;something.netlify.com&lt;/code&gt; as value.&lt;/p&gt;

&lt;p&gt;After that is done you can redirect, &lt;code&gt;something.netlify.com&lt;/code&gt; to &lt;code&gt;blog.ranvir.xyz&lt;/code&gt; by creating a redirect file.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;static/_redirects&lt;/code&gt; file and add the following content to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Redirect default Netlify subdomain to primary domain
https://something.netlify.com/* http://blog.ranvir.xyz/:splat 301!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a &lt;code&gt;301&lt;/code&gt; permanent redirect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Google Analytics
&lt;/h2&gt;

&lt;p&gt;Adding analytics to your website can be very useful, you can check how many people are looking at your website at every given moment.&lt;/p&gt;

&lt;p&gt;You just have to install a basic &lt;code&gt;npm&lt;/code&gt; package and add the Google Analytics tracking ID to your code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; gatsby-plugin-google-analytics
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now change the code in the config file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`gatsby-plugin-google-analytics`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;trackingId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;UA-*******-*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can take the tracking ID from the Google Analytics dashboard.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>gatsby</category>
      <category>react</category>
      <category>netlifycms</category>
    </item>
    <item>
      <title>How to use FactoryBoy to create model instances in Django for testing</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Sat, 11 Jan 2020 21:57:17 +0000</pubDate>
      <link>https://dev.to/singh1114/how-to-use-factoryboy-to-create-model-instances-in-django-for-testing-36j3</link>
      <guid>https://dev.to/singh1114/how-to-use-factoryboy-to-create-model-instances-in-django-for-testing-36j3</guid>
      <description>&lt;p&gt;Original post: &lt;a href="https://ranvir.xyz/blog/how-to-use-factoryboy-to-create-model-instances-in-python-for-testing/"&gt;Using FactoryBoy to create model objects automatically for testing.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Testing is one of the basic and most important parts of the development process that is needed to be done every time you write or change any part of the code. It gives you the authority confidence in the code that you are writing. &lt;/p&gt;

&lt;p&gt;I have already discussed testing in one of my earlier posts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ranvir.xyz/blog/writing-unit-tests-for-the-models"&gt;Writing unit tests for Django models&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post, I will talk about the way in which we can write tests for models.&lt;/p&gt;

&lt;p&gt;For testing models, we need to create sample objects. 😱&lt;/p&gt;

&lt;p&gt;It's pretty hard to write the same code again and again to create the same objects.&lt;/p&gt;

&lt;p&gt;Today I am going to talk about using &lt;a href="https://github.com/FactoryBoy/factory_boy"&gt;FactoryBoy&lt;/a&gt; for testing Django application.&lt;/p&gt;

&lt;p&gt;The name &lt;strong&gt;FactoryBoy&lt;/strong&gt; is inspired by the Factory Girl testing tool for Ruby on Rails. Without Factory boy, you will have to write a lot of code for the creation of test cases.&lt;/p&gt;

&lt;p&gt;For writing tests, you have to spend a lot of time writing the setup method for the tests.&lt;/p&gt;

&lt;p&gt;Without the use of factories, this is how your tests will look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;django.test&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;TestCase&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;my_app.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DataBaseStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TestCase&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;setUp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# Creating all the models   
&lt;/span&gt;        &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;objects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;gender&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'male'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'ranvir'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;test_something&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# test all the things that you want to.
&lt;/span&gt;        &lt;span class="k"&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On the other hand, it is always nice to separate your setting up &lt;code&gt;method&lt;/code&gt; from the testing method. For this sometimes we have to write a long code at the beginning which helps us in the future by allowing us to write smaller code. &lt;/p&gt;

&lt;p&gt;We can write factories anywhere but it's always a good idea to write it in a separate file named factories.py inside the root directory of your app.&lt;/p&gt;

&lt;p&gt;Write following in the factories.py file inside your root directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;factory.django&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;  
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;my_app.models&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;  

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PersonFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;  
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'ranvir'&lt;/span&gt;  
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;  
    &lt;span class="n"&gt;gender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'male'&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can simply call these factories during the test generation and create as many instances of models as we want to. This is a pretty simple case but we really can do a lot more. We can create instances of varies types by making use of &lt;code&gt;FuzzyText&lt;/code&gt; and Sequence.&lt;/p&gt;

&lt;p&gt;With the use of &lt;code&gt;FuzzyText&lt;/code&gt;, you can generate random text making testing more awesome.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applications of FactoryBoy
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Getting random values
&lt;/h3&gt;

&lt;p&gt;We can get random values for the purpose we specify to the &lt;code&gt;Faker&lt;/code&gt; method. The values created are far more realistic than any other random methods that we use otherwise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;factory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Faker&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PersonFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Faker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
    &lt;span class="n"&gt;gender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'male'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a list of &lt;a href="https://faker.readthedocs.io/en/stable/providers.html"&gt;available providers by faker library&lt;/a&gt; which can be used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reproducing random values
&lt;/h3&gt;

&lt;p&gt;Sometimes our tests fail for some random value and not for all of them. So, &lt;code&gt;factory&lt;/code&gt; have introduced a concept using which we can create the same value again and again.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;factory.random&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;reseed_random&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PersonFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;reseed_random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'Bill Clinton'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
    &lt;span class="n"&gt;gender&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'male'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using dependent values of other fields
&lt;/h3&gt;

&lt;p&gt;There are some cases in which you want to use the values which are dependent on the random values of the other fields. For example, full name can be the combination of first and last name.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;factory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Faker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;LazyAttribute&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PersonFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;
    &lt;span class="n"&gt;first_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Faker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'first_name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;second_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Faker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'last_name'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;full_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LazyAttribute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'{0} {1}'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_name&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using sequences for unique fields.
&lt;/h3&gt;

&lt;p&gt;With random function, there is a chance that after some time the values will start repeating, and if your test suite has grown out of that threshold size you will have to use sequences in your unique fields.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;factory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PersonFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'person{0}@ranvir.xyz'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Foreign Keys
&lt;/h3&gt;

&lt;p&gt;If your field is a foreign key, you can directly use that whole factory altogether.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;factory&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PersonFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;
    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'person{0}@ranvir.xyz'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SocietyFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;DjangoModelFactory&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;  
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Meta&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;  
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;
    &lt;span class="n"&gt;society_number&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Sequence&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Society {0}'&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SubFactory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PersonFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Testing is also considered as the sign of a good programmer. There are a ton of benefits of testing which were discussed in the &lt;a href="https://ranvir.xyz/blog/writing-unit-tests-for-the-models/"&gt;post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope you guys liked the idea of the post. We really can do a lot of stuff using this library. Do share your views regarding this post if you have used factories in the past or want to use it in your project. 😇&lt;/p&gt;

</description>
      <category>testing</category>
      <category>python</category>
      <category>django</category>
    </item>
    <item>
      <title>HTML presentation framework reveal.js and why I am a big fan?</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Tue, 31 Dec 2019 11:54:10 +0000</pubDate>
      <link>https://dev.to/singh1114/html-presentation-framework-reveal-js-and-why-i-am-a-big-fan-lja</link>
      <guid>https://dev.to/singh1114/html-presentation-framework-reveal-js-and-why-i-am-a-big-fan-lja</guid>
      <description>&lt;p&gt;This post was first posted on &lt;a href="https://ranvir.xyz/blog/making-a-presentation-in-reveal-js/"&gt;my blog.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reveal.js is an HTML presentation framework that can help you to create awesome presentations for your talk or simple knowledge transfer session in your class.&lt;/p&gt;

&lt;p&gt;I have created a number of presentations using reveal.js.&lt;/p&gt;

&lt;p&gt;Here are the links:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ranvir.xyz/presentations/project1.html#/"&gt;https://ranvir.xyz/presentations/project1.html#/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ranvir.xyz/presentations/slide2.html#/"&gt;https://ranvir.xyz/presentations/slide2.html#/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ranvir.xyz/presentations/mathjax.html#/"&gt;https://ranvir.xyz/presentations/mathjax.html#/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://ranvir.xyz/presentations/csi.html#/"&gt;https://ranvir.xyz/presentations/csi.html#/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prologue (How I came to know about reveal.js)
&lt;/h2&gt;

&lt;p&gt;So, during my time at college, I had to deliver a presentation to a group of students and teachers from a special student cell regarding one of the emerging technologies of those times.&lt;/p&gt;

&lt;p&gt;My mentor at that time suggested me to use reveal for creating the presentation. I was skeptical in the beginning but as soon as I got the grip of it, I loved it. &lt;/p&gt;

&lt;p&gt;I went home and thought that it would be a difficult task but in the end, I ended up doing it just in two hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up reveal.js
&lt;/h2&gt;

&lt;p&gt;All you need to run reveal powered presentation in your local system is to have javascript up and running and if you reading this post, you definitely have it running.&lt;/p&gt;

&lt;p&gt;All you have to do is to go to reveal.js GitHub page and download the latest version of the code on the website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/hakimel/reveal.js/"&gt;Here is the link for reveal.js GitHub repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The downloaded file would be a zip file. If in windows, open the file simply by double-clicking it.&lt;/p&gt;

&lt;p&gt;If in Linux use these commands in the terminal to open the file. First of all download the unzipper by using this command.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sudo apt-get install unzip&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then use this code to unzip the file by giving the name of the destination folder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unzip file.zip -d destination_folder&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Otherwise, if you prefer, you can &lt;code&gt;fork&lt;/code&gt; the code from the &lt;a href="https://revealjs.com/"&gt;https://revealjs.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After unzip open the folder and you will find an &lt;code&gt;index.html&lt;/code&gt; file inside.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating presentations using reveal.js
&lt;/h2&gt;

&lt;p&gt;Open this file in your text editor. Inside the body, tags delete every section.&lt;/p&gt;

&lt;p&gt;Now create each section using the beginning and ending section tag. Each section is equivalent to one slide.&lt;/p&gt;

&lt;p&gt;Here is the reference of one equivalent slide.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;You can do a whole lot of things using reveal.js presentation creator. You can create a style of your own if you want to.&lt;/p&gt;

&lt;h2&gt;
  
  
  Epilogue (Delivering the reveal.js presentation)
&lt;/h2&gt;

&lt;p&gt;So, there were somewhere around 100-200 students and teachers in the room where I was delivering the presentation. The stage was at a good height and I was going to show a good number of charts in my presentation.&lt;/p&gt;

&lt;p&gt;Before jumping on to the stage, I hosted the presentation on to GitHub pages website and created a &lt;a href="https://tinyurl.com/"&gt;short link&lt;/a&gt; using one of the services out there. Finally, I shared the link with everyone in the room.&lt;/p&gt;

&lt;p&gt;Everyone opened the link on their phones and were able to look at the chart more clearly.&lt;/p&gt;

</description>
      <category>html</category>
      <category>javascript</category>
      <category>presentations</category>
    </item>
    <item>
      <title>What do you use to create presentations for talks?</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Thu, 19 Dec 2019 23:20:15 +0000</pubDate>
      <link>https://dev.to/singh1114/what-do-you-use-to-create-presentations-for-talks-3bhh</link>
      <guid>https://dev.to/singh1114/what-do-you-use-to-create-presentations-for-talks-3bhh</guid>
      <description>&lt;p&gt;I personally prefer to use google slides or &lt;a href="https://ranvir.xyz/blog/making-a-presentation-in-reveal-js/"&gt;reveal.js&lt;/a&gt; for creating my presentations for any type of talk.&lt;/p&gt;

&lt;p&gt;I have seen people using a combination of vim and emacs for delivering their presentations.&lt;/p&gt;

&lt;p&gt;Just wanted to know what is your choice for the task.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>How to break out of SDE-I role</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Tue, 17 Dec 2019 20:01:55 +0000</pubDate>
      <link>https://dev.to/singh1114/how-to-break-out-of-sde-i-role-2b7f</link>
      <guid>https://dev.to/singh1114/how-to-break-out-of-sde-i-role-2b7f</guid>
      <description>&lt;p&gt;A few days ago, I posted a new blog on &lt;a href="https://ranvir.xyz/blog/"&gt;my personal blog.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ranvir.xyz/blog/breaking-out-of-the-sde-1-role/"&gt;Breaking out of Software Developer-I role as a web developer&lt;/a&gt;. Please head over to the post if you want to read the detailed description.&lt;/p&gt;

&lt;p&gt;Here are some of the key points that I want to share here as well. &lt;strong&gt;Experienced Devs&lt;/strong&gt;, please share your checklist as well will add it there.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Learn Database designing&lt;/li&gt;
&lt;li&gt;Learn some DevOps&lt;/li&gt;
&lt;li&gt;Learn how to debug&lt;/li&gt;
&lt;li&gt;Learn to write tests&lt;/li&gt;
&lt;li&gt;Learn about scaling issues&lt;/li&gt;
&lt;li&gt;Learn a little bit about security&lt;/li&gt;
&lt;li&gt;Learn about caching&lt;/li&gt;
&lt;li&gt;Learn to review code properly&lt;/li&gt;
&lt;li&gt;Maintain your personal websites&lt;/li&gt;
&lt;li&gt;Help out new joinings and business guys&lt;/li&gt;
&lt;li&gt;Ask for the promotion, when you feel like you are ready to step up.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>discuss</category>
      <category>career</category>
      <category>webdev</category>
      <category>backend</category>
    </item>
    <item>
      <title>How to link to your old posts like a pro in GitHub Pages blog</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Sun, 01 Dec 2019 09:16:21 +0000</pubDate>
      <link>https://dev.to/singh1114/how-to-link-to-your-old-posts-like-a-pro-in-github-pages-blog-4j9h</link>
      <guid>https://dev.to/singh1114/how-to-link-to-your-old-posts-like-a-pro-in-github-pages-blog-4j9h</guid>
      <description>&lt;p&gt;This post was originally shared on &lt;a href="https://ranvir.xyz/blog/how-to-link-to-your-old-posts-like-a-pro-in-github-pages-blog/"&gt;my blog.&lt;/a&gt; Please read it there for a better experience.&lt;/p&gt;

&lt;p&gt;Linking to your old posts is very good for the SEO of your blog and help you not to write the same things again and again. It also helps you to interconnect to your old posts increasing the traffic to your website.&lt;/p&gt;

&lt;p&gt;After writing a good number of posts on &lt;a href="https://dev.to/singh1114"&gt;dev.to&lt;/a&gt;, I started liking the options they provide to share the old posts or other dev.to posts in a well-formated way.&lt;/p&gt;

&lt;p&gt;I wanted to create the same experience on my personal blog as well.&lt;/p&gt;

&lt;p&gt;As I use GitHub Pages ( Jekyll) to build my blogging website, I was sure that it won't be a huge task to do this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rvs84mLg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/H1tqz8l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rvs84mLg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/H1tqz8l.png" alt="Dev to linking to posts" title="Dev to linking to posts"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have a lot of separate widgets on my blog. When someone clicks on any of the posts, all those widgets come together and are shown to the user as a single unit.&lt;/p&gt;

&lt;p&gt;Some examples of these widgets are:&lt;/p&gt;

&lt;h3&gt;
  
  
  Social sharing buttons
&lt;/h3&gt;

&lt;p&gt;The social sharing buttons on my blog pages are included as a separate widget. This helps your readers to share your posts on social media.&lt;/p&gt;

&lt;p&gt;BTW, please share this post on any of the social media and help us get more readers.&lt;/p&gt;

&lt;h3&gt;
  
  
  Read time widget
&lt;/h3&gt;

&lt;p&gt;The read time for any post on my blog is again a separate widget. I use it to show the amount of time it will take to read the post.&lt;/p&gt;

&lt;p&gt;Now that we know how widgets work in Jekyll blog, let's try to create something similar to what dev.to have.&lt;/p&gt;

&lt;p&gt;Let me know if you want to know more about them in the comments on &lt;a href="https://ranvir.xyz/blog/how-to-link-to-your-old-posts-like-a-pro-in-github-pages-blog/"&gt;my blog.&lt;/a&gt; I will share the details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final result after creating
&lt;/h2&gt;

&lt;p&gt;Let me a link to the social sharing post that I created some time back to show you how the final result will look like.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hDN4Pd47--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/0ScinKp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hDN4Pd47--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/0ScinKp.png" alt="ranvir.xyz linking to old post" title="ranvir.xyz linking to old post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How we created amazing old post linking widget for our Jekyll blog
&lt;/h2&gt;

&lt;p&gt;The first step was to create the include file so that we keep the structure of our code clean. The only requirement for this is that we need to pass some parameters so that we can find out which post is being linked to.&lt;/p&gt;

&lt;p&gt;The only thing which is unique for my blog is the URL. Although I know the URLs can change but this is the only way I can accomplish this task.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating the include file
&lt;/h3&gt;

&lt;p&gt;Add the following code to &lt;code&gt;_includes/linked_post.html&lt;/code&gt; file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We loop through all the posts on the site and check which post has the same URL as passed to the include statement which I will share very soon.&lt;/p&gt;

&lt;p&gt;Finally, we have just shown the heading and along with the date of the post. We have also added the read time widget and post categories on my blog but for that, we will need to include two more widgets so, for simplicity, I have omitted it in the above code.&lt;/p&gt;

&lt;p&gt;The code above would be sufficient to help you create something of your own. Still, if you are stuck somewhere, do let me know in the comments below.&lt;/p&gt;

&lt;h3&gt;
  
  
  CSS for the widget
&lt;/h3&gt;

&lt;p&gt;I have added the CSS separately in the &lt;code&gt;style.scss&lt;/code&gt; file. Here is the CSS code for this. You can add it anywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;old&lt;/span&gt; &lt;span class="nt"&gt;code&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
&lt;span class="nc"&gt;.linked_post_div&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#ccc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;15px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.linked_post_div&lt;/span&gt; &lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;margin-top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  How to use this widget.
&lt;/h3&gt;

&lt;p&gt;All you have to do is something like this.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Nice right, I hope it helps you to create links to your old posts. Please share this post on social media and let me know in the comments below.&lt;/p&gt;

&lt;p&gt;Also, if you found something wrong do let me know. Thanks for reading.&lt;/p&gt;

</description>
      <category>jekyll</category>
      <category>opensource</category>
      <category>writing</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Integration testing of APIs in Spring-Boot( Java) with Junit</title>
      <dc:creator>Ranvir Singh</dc:creator>
      <pubDate>Sun, 24 Nov 2019 19:39:20 +0000</pubDate>
      <link>https://dev.to/singh1114/integrity-testing-of-apis-in-spring-boot-java-with-junit-2mck</link>
      <guid>https://dev.to/singh1114/integrity-testing-of-apis-in-spring-boot-java-with-junit-2mck</guid>
      <description>&lt;p&gt;This post was initially shared on my blog. Read the post &lt;a href="https://ranvir.xyz/blog/testing-in-spring-boot-java-with-junit-for-beginners/" rel="noopener noreferrer"&gt;testing in spring boot with JUnit for beginners&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Shipping our product without testing is like selling a car without turning on the engine. We have to run a few tests again and again while the development of the product and if those tests are not automated, we will have a hard time testing our application.&lt;/p&gt;

&lt;p&gt;I have written a post on the &lt;a href="https://ranvir.xyz/blog/writing-unit-tests-for-the-models/" rel="noopener noreferrer"&gt;importance of writing tests&lt;/a&gt; against your codebase.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FBEIqT5f.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FBEIqT5f.jpg" title="Spring boot logo" alt="Spring boot logo"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This post is in continuation of the last post we shared related to Spring boot in which we shared on how to write &lt;a href="https://ranvir.xyz/blog/building-restful-apis-with-java-spring-boot-framework-for-beginners/" rel="noopener noreferrer"&gt;Rest APIs in Spring Boot.&lt;/a&gt;. Please go through that post if you want to learn on how to create rest APIs using Spring Boot.&lt;/p&gt;

&lt;p&gt;In this post, we are going to discuss how we can test our spring boot application from start to end without any mocking.&lt;/p&gt;

&lt;p&gt;First of all, we have to add the &lt;code&gt;test&lt;/code&gt; libraries to the &lt;code&gt;pom.xml&lt;/code&gt;, so that maven can install them for us.&lt;/p&gt;

&lt;p&gt;Here is the code to add testing libraries to &lt;code&gt;pom.xml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;        ...
        &lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.springframework.boot&lt;span class="nt"&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;spring-boot-starter-test&lt;span class="nt"&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
        ...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Using separate config for testing purposes
&lt;/h2&gt;

&lt;p&gt;We don't want to add the data to the same database as of the production one. So, we will start by creating a separate configuration for handling the test cases. Also, its always a good practice to have separate configurations anyway.&lt;/p&gt;

&lt;p&gt;Create a new resource file, &lt;code&gt;src/main/resources/application-test.properties&lt;/code&gt; and add the following data to it.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; We will have to create the database in our local system to make it work.&lt;/p&gt;

&lt;p&gt;Now that we have a separate configuration file, we have to export it as an App configuration so that we can use it in the tests itself.&lt;/p&gt;

&lt;p&gt;Add the following code to the file, &lt;code&gt;src/main/java/io/singh1114/springboottut/AppConfigurationTest.java&lt;/code&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  Writing Controller test for Spring Boot API.
&lt;/h2&gt;

&lt;p&gt;Now since we are going to test the application from end to end, let's create a controller test.&lt;/p&gt;

&lt;p&gt;Add the following code to &lt;code&gt;src/test/java/io/singh1114/springboottut/SchoolControllerTest.java&lt;/code&gt;&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Let's go through the file and understand this.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@ActiveProfiles&lt;/code&gt;, is used to let the framework know that we are going to use the given profile. As we have already declared the &lt;code&gt;test&lt;/code&gt; Profile in the &lt;code&gt;Configuration&lt;/code&gt; file, we can use it here directly.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@RunWith&lt;/code&gt; and &lt;code&gt;@SpringBootTest&lt;/code&gt; is used to start a simple server to carry out the tests using the &lt;code&gt;SpringBootFramework&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running on a random port
&lt;/h2&gt;

&lt;p&gt;With &lt;code&gt;SpringBootTest.WebEnvironment.RANDOM_PORT&lt;/code&gt; we are telling the framework to launch the application on a Random PORT.&lt;/p&gt;

&lt;h2&gt;
  
  
  Removing any change made to the database after test completion.
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;@Transactional&lt;/code&gt; will help us to clean the database once tests are completed. We don't want to keep adding stuff to the database and want to clean it after every test run.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the API endpoint
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@LocalServerPort&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;createURLWithPort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"http://localhost:"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;port&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These are used to create the URL for a given endpoint to hit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the School Object
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nd"&gt;@Autowired&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;SchoolRepository&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
        &lt;span class="nc"&gt;School&lt;/span&gt; &lt;span class="n"&gt;testSchool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;School&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"First Location"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Mr. Ranvir"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"California"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;save&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;testSchool&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is used to create the object in the database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Sending the request and getting the response
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;    &lt;span class="nc"&gt;TestRestTemplate&lt;/span&gt; &lt;span class="n"&gt;restTemplate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TestRestTemplate&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

    &lt;span class="nc"&gt;HttpHeaders&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpHeaders&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
        &lt;span class="nc"&gt;HttpEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HttpEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="nc"&gt;ResponseEntity&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;restTemplate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;exchange&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;createURLWithPort&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"/schools"&lt;/span&gt;&lt;span class="o"&gt;),&lt;/span&gt;
                &lt;span class="nc"&gt;HttpMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;GET&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="o"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Checking if the response is equal to the object created
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;JSONAssert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;expected&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getBody&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are checking if the body of the response is equal to the expected response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Run the tests
&lt;/h2&gt;

&lt;p&gt;Use can use the following command to run the tests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mvn &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FdyU6gXj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgur.com%2FdyU6gXj.png" title="Spring boot test passing" alt="Spring boot test passing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you liked the post. Please share your views in the comment section below.&lt;/p&gt;

</description>
      <category>java</category>
      <category>productivity</category>
      <category>testing</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
