<!DOCTYPE html>
<html lang="en">
	<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-11030746-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-11030746-2');
</script>
		<link href="http://gmpg.org/xfn/11" rel="profile">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta http-equiv="content-type" content="text/html; charset=utf-8">

		<!-- Enable responsiveness on mobile devices-->
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">

		<title>What are Git?</title>

		<!-- CSS -->
		<link href="//fonts.googleapis.com/" rel="dns-prefetch">
		<link href="//fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic|Abril+Fatface|PT+Sans:400,400italic,700&amp;subset=latin,latin-ext" rel="stylesheet">

		<link rel="stylesheet" href="https://tuvokki.github.io/theme/css/poole.css" />
		<link rel="stylesheet" href="https://tuvokki.github.io/theme/css/hyde.css" />
		<link rel="stylesheet" href="https://tuvokki.github.io/theme/css/syntax.css" />
		<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">

		<!-- RSS -->
		<link rel="alternate" type="application/rss+xml" title="RSS" href="/atom.xml">
	</head>

	<body class="theme-base-0d">
<div class="sidebar">
	<div class="container sidebar-sticky">
		<div class="sidebar-about">

			<h1>
				<a href="/">
					<img class="profile-picture" src="https://tuvokki.github.io/images/wouter-krijt.png">
					What are Git?
				</a>
			</h1>
			<p class="lead"></p>
			<p class="lead"> </p>
			<p></p>
		</div>
		<nav class="sidebar-nav">
			<a class="sidebar-nav-item" href="mailto:tuvokki@gmail.com">
				<i class="fa fa-envelope"></i>
			</a>
			<a class="sidebar-nav-item" href="https://github.com/tuvokki">
				<i class="fa fa-github"></i>
			</a>
			<a class="sidebar-nav-item" href="">
				<i class="fa fa-feed"></i>
			</a>
		</nav>
	</div>
</div>		<div class="content container">
<div class="post">
	<h1 class="post-title">test-that-data-into-mongo</h1>
	<span class="post-date">Tue 26 May 2015</span>
	<p>I'm currently using a test in my Python project to get some data into my Mongo DB. I like it this way, since a unit-test to do such a thing comes in handy in two ways.</p>
<ul>
<li>get you project started initially</li>
<li>migrate your project from D to TAP</li>
</ul>
<h2>The initial start</h2>
<p>Sometimes it is hard to start with a new project. Especially when you have one or more new technologies to include or integrate. I like to follow tutorials, and I never do that by the letter. I just go about my way. But deferring from that 'hello world'-copy-paste-away-thing leaves you with some sample data to be added to your cool, new technology to fetch. This is where a unit-test comes in handy. It gives you the insight into the basics of a technology without having to get all the i's dotted.</p>
<h2>That DTAP thing</h2>
<p>And when your new hipster product is ready to be deployed? When it is matured enough to even leave the <code>localhost</code> and go to a testserver? Then a unit test comes in handy to do exactly the same thing as it done before. Namely to insert data into the next step in the lifecycle of a program. In a repeatable way, using the technology that underlies the actual program you are developing.</p>
<h2>Example</h2>
<p>A while ago I started with <a href="https://github.com/tuvokki/data-api">an API implementation usng PyMongo, Flask and Flask-Classy</a>. So I needed to have some data in my project. I wrote the following unit-test:</p>
<p>{% highlight python %}
    import unittest
    from pymongo import MongoClient</p>
<div class="highlight"><pre><span></span><span class="k">class</span> <span class="n">TestMongoInsertFunctions</span>(<span class="n">unittest</span>.<span class="n">TestCase</span>):

  <span class="n">def</span> <span class="n">setUp</span>(<span class="k">self</span>):
    <span class="c1"># Get a reference to the MongoDB</span>
<span class="k">self</span>.<span class="n">client</span> = <span class="n">MongoClient</span>(<span class="s">&#39;mongodb://localhost:27017/&#39;</span>)
<span class="k">self</span>.<span class="n">db</span> = <span class="k">self</span>.<span class="n">client</span>[<span class="s">&#39;quotes&#39;</span>]
<span class="k">self</span>.<span class="n">collection</span> = <span class="k">self</span>.<span class="n">db</span>[<span class="s">&#39;simple&#39;</span>]

  <span class="n">def</span> <span class="n">testConn</span>(<span class="k">self</span>):
    <span class="n">quotes</span> = [
      {
        <span class="s">&#39;text&#39;</span>: <span class="s">&quot;Waar ik inmiddels behoorlijk zenuwachtig van wordt is het lot van Jip en Janneke.&quot;</span>,
        <span class="s">&#39;who&#39;</span>: <span class="s">&quot;Erik van Wunnik&quot;</span>
      },
      {
        <span class="s">&#39;text&#39;</span>: <span class="s">&quot;Since there is absolutely no logical reason to assume there is an afterlife, I decided to make the life I have now as much fun as possible.&quot;</span>,
        <span class="s">&#39;who&#39;</span>: <span class="s">&quot;Erik van Wunnik&quot;</span>
      },
</pre></div>


<p>{% endhighlight %}</p>
<p>and a whole bunch more (<a href="https://github.com/tuvokki/data-api/blob/master/mongo_add_quotes_to_db.py">see</a>) ...</p>
<p>{% highlight python %}</p>
<div class="highlight"><pre><span></span>      {
        <span class="s1">&#39;</span><span class="s">text</span><span class="s1">&#39;</span>: <span class="s2">&quot;</span><span class="s">The more future to come, the more customers to satisfy</span><span class="s2">&quot;</span>,
        <span class="s1">&#39;</span><span class="s">who</span><span class="s1">&#39;</span>: <span class="s2">&quot;</span><span class="s">Erik van Wunnik</span><span class="s2">&quot;</span>
      }
    ]
    <span class="k">for</span> <span class="nv">quote</span> <span class="nv">in</span> <span class="nv">quotes</span>:
      ## <span class="nv">print</span> <span class="nv">values</span>
      # <span class="k">for</span> <span class="nv">kk</span>, <span class="nv">vv</span> <span class="nv">in</span> <span class="nv">quote</span>.<span class="nv">iteritems</span><span class="ss">()</span>:
      #   <span class="nv">print</span> <span class="nv">kk</span>, <span class="s1">&#39;</span><span class="s">: </span><span class="s1">&#39;</span>, <span class="nv">vv</span>
      <span class="nv">quote_id</span> <span class="o">=</span> <span class="nv">self</span>.<span class="nv">collection</span>.<span class="nv">insert</span><span class="ss">(</span><span class="nv">quote</span><span class="ss">)</span>
      <span class="nv">print</span> <span class="s1">&#39;</span><span class="s">inserted: </span><span class="s1">&#39;</span>, <span class="nv">quote_id</span>

<span class="nv">suite</span> <span class="o">=</span> <span class="nv">unittest</span>.<span class="nv">TestLoader</span><span class="ss">()</span>.<span class="nv">loadTestsFromTestCase</span><span class="ss">(</span><span class="nv">TestMongoInsertFunctions</span><span class="ss">)</span>
<span class="nv">unittest</span>.<span class="nv">TextTestRunner</span><span class="ss">(</span><span class="nv">verbosity</span><span class="o">=</span><span class="mi">2</span><span class="ss">)</span>.<span class="nv">run</span><span class="ss">(</span><span class="nv">suite</span><span class="ss">)</span>
</pre></div>


<p>{% endhighlight %}</p>
<p>To run this, just execute this test and you'll have all these (and more) quotes in your database. Also you have some insight in how to use Mongo from Python. And you just do this same trick when you want to go to another box, platform or stack you want to migrate to.</p>
<h2>todo's</h2>
<p>And while this example is maybe proof of good intentions done wrong, you know where and what to improve. Any suggestions? Just <a href="https://github.com/tuvokki/data-api/issues/new">open an issue</a> of fork and pull this repo. You're always welcome to join in.</p>
</div>
		</div>
	</body>
</html>