Giter Club home page Giter Club logo

currency-api's Introduction

Currency API

A simple project to show how to test a Node Express app using MNP - Mocha, Nock and Proxyquire. Code coverage is done with Istanbul (now called nyc). Rewire can be used in place of proxyquire to test private JS methods. This app is a very basic currency API.

Build Status Maintainability Test Coverage

Running app

You can see this app running on Zeit Now, each pull request will have it's own URL.

Run on Google cloud run

Run on Google Cloud

How it works

The GET api works in the following way:

  1. hit URL /api/convert/AUD/USD/2018-07-22.
  2. Checks if the currency exchange rate is in the DB, if yes returns it.
  3. If rate is not in the db it will query the currencyconverterapi.com free API to get the rate.
  4. Returns the rate back and saves it in the DB too.

DB script

To create the db and the table, run the following sql script.

CREATE DATABASE currency CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE TABLE IF NOT EXISTS `currency`.`exchange_rates` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `from_currency` CHAR(3) NOT NULL,
  `to_currency` CHAR(3) NOT NULL,
  `rate` DECIMAL(12,7) NOT NULL,
  `on_date` DATE NOT NULL,
  `created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `rate_on_date_UNIQUE` (`from_currency` ASC, `to_currency` ASC, `on_date` ASC))
ENGINE = InnoDB;

INSERT INTO `currency`.`exchange_rates` (`from_currency`, `to_currency`, `rate`, `on_date`) VALUES ('AUD', 'USD', '0.742719', '2018-07-22');

Configs

Configs for db like username, password etc are in the /src/config.js file.

Run

to run the app you can use docker-compose up the go to http://localhost:8080/api/convert/AUD/USD/2018-07-23 on the browser. It is using the db on remotemysql.com so no need to setup the db locally. If you want to set it up locally change the config in src/configs.js or put in environment variables.

Run tests

To run the tests inside the container run docker-compose run web npm t

To run tests just run npm t to watch test run npm t -- -w.

To watch specific test(s) run npm t -- -w -g "exchangeRates get or even npm t -- -w -g "should use default params if no params are provided and no results in db"

Code coverage

To get the code coverage with Istanbul/nyc execute : npm run test-cov. You should see the code coverage on the cli.

You can also check the code coverage on code climate.

Mutation testing

Has some mutation testing done with Stryker. Current coverage is ~88% with mostly log lines failing. To run the mutation tests run the following after installing stryker.

stryker run

or

npm run mutation-cov

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.