Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic unit tests for stock service #3087

Merged
merged 9 commits into from
Aug 30, 2018

Conversation

mbayopanda
Copy link
Collaborator

This PR add unit tests for some functions of the stock service and fix some export feature in stock registries.

Copy link
Collaborator

@jniles jniles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbayopanda, nice tests! Your testing logic is pretty good, but there is a lot of set up which makes the test a bit hard to read. I propose you set up all your data, either in the top of the test file or in a MockService, and then begin executing test logic.

It's also typically better to check against new objects/values instead of the original objects/values when you write a test. Imagine the following function:

const o = {
  id : 1,
  data : { hello : 'world' }
};

function getData(o) {
  return o.data;
}

Now imagine you wrote the following test:

const exampleO = {
  id : 2,
  data : { food : 'sandwich' },
}

const result = getData(exampleO);
expect(results).to.equal(exampleO.data);

This test looks okay, but in reality, it may not catch all the changes I could make to the object. Imagine that I begin refactoring and change the function getData to be:

function getData(o) {
  delete o.data;
  return undefined;
}

I'm pretty sure the tests above would still pass, even though the behavior is completely different! That is because delete will remove o.data, which is equal to undefined. I wouldn't need to update the tests at all, and that is a bad thing.

We want our tests to tell us when the API changes in important ways. For this reason, it is better to repeat yourself and say:

expect(result).to.equal({ food : 'sandwich' });

... which will yield the error expected undefined to equal {food : 'sandwich' }. It immediately tells you that I made a mistake somewhere in my code instead of silently breaking the behavior of the module.

@@ -241,7 +248,7 @@ function StockService(Api, Filters, AppCache, Periods, $httpParamSerializer, Lan

/** Get label for purchase Status */
function statusLabelMap(status) {
var keys = {
const keys = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are constants, can we move them outside of this function? I feel like you structured your test similar to this code, but the code isn't well structured in this case.

@@ -35,12 +35,12 @@
</li>
<li role="separator" class="divider"></li>
<li role="menuitem">
<a ng-href="/reports/stock/lots/?{{ StockLotsCtrl.download('lot', 'pdf') }}" download="{{ 'VOUCHER_REGISTRY.TITLE' | translate }}">
<a ng-href="/reports/stock/lots/?{{ StockLotsCtrl.getQueryString('lot', 'pdf') }}" download="{{ 'VOUCHER_REGISTRY.TITLE' | translate }}">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this download called 'VOUCHER_REGISTRY.TITLE'? I know if pre-existed your changes, but can you fix that?


const expectedLotsArray = [
{
uuid : mockStockFormStoreData[0].lots[0].uuid,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An important part of tests is readability - and this isn't very readable to me. I propose that we clean this up by creating some extra variables. Like so:

const lotA = {
  uuid : 'd03e7870-0c8e-47d4-a7a8-a17a9924b123',
  lot : 'A',
  quantity : 50,
  expiration_date : '2019-01-01',
};

const lotB = {
  uuid : 'd03e7870-0c8e-47d4-a7a8-a17a9924b124',
  lot : 'B',
  quantity : 30,
  expiration_date : '2018-12-01',
};

Now that you have those, you can do this:

// a singe inventory with two lots
const mockStockFormStoreData = [{
  inventory_uuid : 'd03e7870-0c8e-47d4-a7a8-a17a9924b3f4',
  unit_cost : 0.5,
  lots : [ lotA, lotB ],
}];

const processed = Stock.processLotsFromStore(mockStockFormStoreData, entityUuid);
const [first] = processed;

const result = {
  uuid : 'd03e7870-0c8e-47d4-a7a8-a17a9924b123',
  lot : 'A',
  quantity : 50,
  expiration_date : '2019-01-01',
  inventory_uuid : 'd03e7870-0c8e-47d4-a7a8-a17a9924b3f4',
  unit_cost : 0.5,
  origin_uuid : entityUuid,
};

expect(first).to.deep.equal(result);

it('#getQueryString() returns a query string with parameters for a request', () => {
const FILTER_KEY_MOVEMENT = 'movement';
const FILE_TYPE_CSV = 'csv';
const query = Stock.getQueryString(FILTER_KEY_MOVEMENT, FILE_TYPE_CSV);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be helpful for future programmers to have a comment telling us what string this should produce. Something like:

// returns the string "/stock/lots/?type=csv&limit=100"
const query = Stock.getQueryString(FILTER_KEY_MOVEMENT, FILE_TYPE_CSV);

limitParamExists = true;
}
}
expect(fileTypeRendererExists).to.be.equal(true);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually just use chai to do this - specifically the include() method. You would something like this work and save you from doing the for loop?

expect(query).to.include(`renderer=${FILE_TYPE_CSV}`);
expect(query).to.include('period=');
expect(query).to.include('limit=');

@@ -0,0 +1,126 @@
/* global inject, expect */

describe('StockService', StockServiceTests);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good first attempt!

I propose you make a few lots/inventory items in the MockDataService (if they don't exist already) and use those in your tests. That way, this code can focus on the test itself, and spend less time setting up the tests.

@jniles jniles added the tests label Aug 29, 2018
Copy link
Collaborator

@jniles jniles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! Nice and clean.

bors r+

const transfers = new Api('/stock/transfers');

// stock status label keys
const stockStatusLabelKeys = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍

bors bot added a commit that referenced this pull request Aug 30, 2018
3087: Basic unit tests for stock service r=jniles a=mbayopanda

This PR add unit tests for some functions of the stock service and fix some export feature in stock registries.

Co-authored-by: mbayopanda <[email protected]>
@bors
Copy link
Contributor

bors bot commented Aug 30, 2018

Build succeeded

@bors bors bot merged commit 56f7c60 into Third-Culture-Software:master Aug 30, 2018
mbayopanda pushed a commit to mbayopanda/bhima that referenced this pull request Dec 15, 2018
3015: Update karma to the latest version 🚀 r=jniles a=greenkeeper[bot]




## Version **3.0.0** of **karma** was just published.

<table>
  <tr>
    <th align=left>
      Dependency
    </th>
    <td>
      <a target=_blank href=https://github.com/karma-runner/karma>karma</a>
    </td>
  </tr>
  <tr>
      <th align=left>
       Current Version
      </th>
      <td>
        2.0.5
      </td>
    </tr>
  <tr>
    <th align=left>
      Type
    </th>
    <td>
      devDependency
    </td>
  </tr>
</table>



The version **3.0.0** is **not covered** by your **current version range**.

If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.

It might be worth looking into these changes and trying to get this project onto the latest version of karma.

If you have a solid test suite and good coverage, a passing build is a strong indicator that you can take advantage of these changes directly by merging the proposed change into your project. If the build fails or you don’t have such unconditional trust in your tests, this branch is a great starting point for you to work on the update.


---


<details>
<summary>Release Notes</summary>
<strong>v3.0.0</strong>

<h3>Bug Fixes</h3>
<ul>
<li><strong>config:</strong> wait 20s for browser activity. (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3087">#3087</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/88b977f">88b977f</a>)</li>
<li><strong>config:</strong> Wait 30s for browser activity per Travis. (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3091">#3091</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/f6d2f0e">f6d2f0e</a>)</li>
<li><strong>init:</strong> add "ChromeHeadless" to the browsers' options (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3096">#3096</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/56fda53">56fda53</a>)</li>
<li><strong>server:</strong> Exit clean on unhandledRejections. (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3092">#3092</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/02f54c6">02f54c6</a>), closes <a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3064">#3064</a></li>
<li><strong>travis:</strong> Up the socket timeout 2-&gt;20s. (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3103">#3103</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/732396a">732396a</a>), closes <a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3102">#3102</a></li>
<li><strong>travis:</strong> use the value not the key name. (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3097">#3097</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/90f5546">90f5546</a>)</li>
<li><strong>travis:</strong> validate TRAVIS_COMMIT if TRAVIS_PULL_REQUEST_SHA is not set. (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3094">#3094</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/fba5d36">fba5d36</a>)</li>
<li><strong>travis:</strong> Validate TRAVIS_PULL_REQUEST_SHA rather than TRAVIS_COMMIT. (<a href="https://urls.greenkeeper.io/karma-runner/karma/issues/3093">#3093</a>) (<a href="https://urls.greenkeeper.io/karma-runner/karma/commit/a58fa45">a58fa45</a>)</li>
</ul>
</details>

<details>
<summary>Commits</summary>
<p>The new version differs by 17 commits.</p>
<ul>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/a4d5bdcbe36cbe12974fa9936a224a7837c5f0a4"><code>a4d5bdc</code></a> <code>chore: release v3.0.0</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/75f466dc172223cd8aa6fd7eec9bee810c982341"><code>75f466d</code></a> <code>chore: release v2.0.6</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/5db939907cabae3d0ef77ec08ae04563177116c2"><code>5db9399</code></a> <code>chore: update contributors</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/eb3b1b4ce8e0545832676289deb6e48bde5465fd"><code>eb3b1b4</code></a> <code>chore(deps): update mime -&gt; 2.3.1 (Third-Culture-Software#3107)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/732396a087c6dddeea2cf7f7493bf148a508725d"><code>732396a</code></a> <code>fix(travis): Up the socket timeout 2-&gt;20s. (Third-Culture-Software#3103)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/173848e3a0659bceea9d87d4eeee6c8a11c0f1a1"><code>173848e</code></a> <code>Remove erroneous change log entries for 2.0.3</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/10025695c4af3ea983c2c1316ec4ef078e056ebc"><code>1002569</code></a> <code>chore(ci): drop node 9 from travis tests (Third-Culture-Software#3100)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/02f54c6c57f5a3e0be3a44e8e5ca1db98b8dbc8f"><code>02f54c6</code></a> <code>fix(server): Exit clean on unhandledRejections. (Third-Culture-Software#3092)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/0fdd8f9e91b6039c07c857e11a5d7f7b3205cf01"><code>0fdd8f9</code></a> <code>chore(deps): update socket.io -&gt; 2.1.1 (Third-Culture-Software#3099)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/90f5546a9f88199f8118eae506922d4e8ee38945"><code>90f5546</code></a> <code>fix(travis): use the value not the key name. (Third-Culture-Software#3097)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/fba5d365146bad122d54af75bf191ad0b6091dd0"><code>fba5d36</code></a> <code>fix(travis): validate TRAVIS_COMMIT if TRAVIS_PULL_REQUEST_SHA is not set. (Third-Culture-Software#3094)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/56fda53ec19a1a691cd80342fef9b23d9f9fe4d2"><code>56fda53</code></a> <code>fix(init): add "ChromeHeadless" to the browsers' options (Third-Culture-Software#3096)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/f6d2f0ea5a3323c5e359e26fe5be9fbf68db819f"><code>f6d2f0e</code></a> <code>fix(config): Wait 30s for browser activity per Travis. (Third-Culture-Software#3091)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/a58fa45c1df08ff4e74f9e75379f74c1311073c3"><code>a58fa45</code></a> <code>fix(travis): Validate TRAVIS_PULL_REQUEST_SHA rather than TRAVIS_COMMIT. (Third-Culture-Software#3093)</code></li>
<li><a href="https://urls.greenkeeper.io/karma-runner/karma/commit/88b977fcada5d08ae8d5bba9bc8eefc8404eff82"><code>88b977f</code></a> <code>fix(config): wait 20s for browser activity. (Third-Culture-Software#3087)</code></li>
</ul>
<p>There are 17 commits in total.</p>
<p>See the <a href="https://urls.greenkeeper.io/karma-runner/karma/compare/00189471d383600a95415ea526b152dd556ce751...a4d5bdcbe36cbe12974fa9936a224a7837c5f0a4">full diff</a></p>
</details>

<details>
  <summary>FAQ and help</summary>

  There is a collection of [frequently asked questions](https://greenkeeper.io/faq.html). If those don’t help, you can always [ask the humans behind Greenkeeper](https://github.com/greenkeeperio/greenkeeper/issues/new).
</details>

---


Your [Greenkeeper](https://greenkeeper.io) bot 🌴



Co-authored-by: greenkeeper[bot] <greenkeeper[bot]@users.noreply.github.com>
@mbayopanda mbayopanda deleted the test-stock-services branch February 12, 2019 17:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants