Angel \”Java\” Lopez on Blog

November 8, 2017

New Month’s Resolutions: November 2017

The end of year is near. Time to write the new month’s resolutions, but first, review the previous ones:

– Continue smartie [complete] see repo
– Continue simpleevm [pending]
– Continue BInte [pending]
– Continue Yasold [pending]
– Continue BlockchainJ [complete] see repo
– Continue BlockchainSharp [complete] see repo
– Continue ChineseP [pending]
– Continue TensorSharp [complete] see repo
– Continue SimpleTensor [pending]
– Continue Elmie [complete] see repo

I was also working on:

– Improve SimpleDSL [complete] see repo
– Improve SimpleGA [complete] see repo
– Start SoliditySamples [complete] see repo
– Improve RskApi [complete] see repo
– Improve RskApi [complete] see repo
– Start Dapplib [complete] see repo
– Start AprendiendoSolidity [complete] see repo
– Improve HuskyJS [complete] see repo
– Improve ClojureSamples [complete] see repo
– Improve SimpleGo [complete] see repo
– Improve SimpleGo [complete] see repo
– Start AprendiendoInteligenciaArtificial [complete] see repo
– Start AprendiendoMachineLearning [complete] see repo

My new month’s resolutions:

– Continue BlockchainJ
– Continue BlockchainSharp
– Continue ChineseP
– Continue TensorSharp
– Continue SimpleTensor
– Continue Elmie
– Continue AprendiendoSolidity
– Continue AprendiendoMachineLearning
– Prepare Machine Learning Mentorship
– Prepare Smart Contracts Programming Course
– Continue SimpleGo
– Continue AjGo

Stay tuned!

Angel “Java” Lopez
https://github.com/ajlopez
http://www.ajlopez.com
https://twitter.com/ajlopez

April 25, 2015

Liqueed Project (6)

Filed under: Express, JavaScript, Liqueed, NodeJs, Test-Driven Development — ajlopez @ 4:26 pm

Previous Post

Let review another tests from test/personapi.js in project:

https://github.com/liquid-co-ops/liqueed

There are API entrypoints that are called using PUT or POST. They receive data in the body of the sent message. Using Express middleware, that message (formatted as JSON) is analized, parsed and converted into a JavaScript object. That object resides in the body property of the received request parameter. As the API uses that field, we should provide it in the test. An example:

exports['login person'] = function (test) {
    test.async();
    
    var request = {
        body: {
            username: persons[1].username,
            password: persons[1].username
        }
    };

    var response = {
        send: function (model) {
            test.ok(model);
            test.equal(model.id, persons[1].id);
            test.equal(model.name, persons[1].name);
            test.equal(model.username, persons[1].username);
            
            test.done();
        }
    };
    
    controller.loginPerson(request, response);
};

As in the previous example, we use the send function in the response object, to check the result.

Another example, sending not only the body but also sending parameters:

exports['change password first person'] = function (test) {
    test.async();
    
    var request = {
		params : {
			id : persons[0].id.toString()
		},
        body: {
            password: 'new' + persons[0].username
        }
    };

    async()
    .then(function (data, next) {
        var response = {
            send: function (model) {
                test.ok(model);
                test.strictEqual(model, true);
                
                next(null, null);
            }
        };
        
        controller.updatePassword(request, response);
    })
    .then(function (data, next) {
        var request = {
            body: {
                username: persons[0].username,
                password: 'new' + persons[0].username
            }
        };

        var response = {
            send: function (model) {
                test.ok(model);
                test.equal(model.id, persons[0].id);
                test.equal(model.name, persons[0].name);
                test.equal(model.username, persons[0].username);
                
                test.done();
            }
        };
        
        controller.loginPerson(request, response);
    })
    .run();
};
 

Next posts: MVC controller actions tests, tests launching the HTTP server, tests using a DSL (Domain-Specific Language)

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

April 20, 2015

Liqueed Project (5)

Filed under: Express, Liqueed, NodeJs, Test-Driven Development — ajlopez @ 5:38 pm

Previous Post  
Next Post

Let review two more tests from test/personapi.js. The second test is actually:

exports['get persons'] = function (test) {
    test.async();
    
    var request = {};
    var response = {
        send: function (model) {
            test.ok(model);
            test.ok(Array.isArray(model));
            test.ok(model.length);
            test.ok(model[0].id);
            test.ok(model[0].name);
            test.done();
        }
    };
    
    controller.list(request, response);
};

After building the domain model in memory (the first test taks), now we can test a controller action method, named getPersons. It requires to receive two arguments, request and response object. In the above code, two stubs were built and used. The response object has a send function that  it should be invoked to pass the tests. It receives the model to send to the client, as a JavaScript object (in the real environment, the object is serialized to JSON via Express response send method).

The third test is:

exports['get first person'] = function (test) {
    test.async();
    
    var request = {
        params: {
            id: persons[0].id.toString()
        }
    };

    var response = {
        send: function (model) {
            test.ok(model);
            test.equal(model.id, persons[0].id);
            test.equal(model.name, persons[0].name);
            test.done();
        }
    };
    
    controller.get(request, response);
};

The action needs something from request object, an aditional argument having the person primary key to be retrieved. There is a send method in request object, to test the result.

The tests are not using HTTP nor GET method. They are directly testing the controller code. Indeed, that code was written AFTER the tests, and its first execution was from test launch, without the need of launch a web server.

Next posts: other tests for API methods, sending objects, and tests for MVC controllers

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

April 15, 2015

Liqueed Project (4)

Filed under: Express, JavaScript, Liqueed, NodeJs, Test-Driven Development — ajlopez @ 8:42 pm

Previous Post
Next Post

The project:

https://github.com/liquid-co-ops/liqueed

was built using TDD (Test-Driven Development) worflow. In the previous post I showed a logic service example, implemented using tests. Today, I want to show that the MVC controllers was also coded using TDD. The controllers are JavaScript modules that expose functions as actions to be routed by Express. The actions receive and return JSON objects. They are the basis for the exposed API, to be consumed by the clients (actually, only one client, a simple single page application). For example, controllers\personapi.js starts declaring:

'use strict';

var service = require('../services/person');

It consumes the person service module.

There are associated test at test\personapi.js, its initial imports:

'use strict';

var controller = require('../controllers/personapi');

var loaddata = require('../utils/loaddata');
var db = require('../utils/db');
var async = require('simpleasync');

In the previous post, I mentioned that the test granularity I prefer for JavaScript is the module, not the function. So, all the tests in the module are execute, in declaration order. The first test is in charge of inicialization the domain model:

var persons;

exports['clear and load data'] = function (test) {
    var personService = require('../services/person');

    test.async();
    
    async()
    .then(function (data, next) { db.clear(next); })
    .then(function (data, next) { loaddata(next); })
    .then(function (data, next) { personService.getPersons(next); })
    .then(function (data, next) {
        persons = data;
        test.ok(persons);
        test.ok(persons.length);
        test.done();
    })
    .run();
};

The new thing to understand is the use of the module simpleasync, pointed by the async variable. I wrote the module to chain  functions. Each function receives two arguments: data, the success result of the previous executed function in chain, or the initial value triggered in the run chain function. And next, a callback to be invoked by the function, to execute the rest of the chain. The callback receives two arguments: err and data. So it can be used as the callback of other functions. If err is not null, the next functions in chain is not executed and the function defined in the chain fail method is run (this option is not used in the above code). In the above example  personService.getPersons(next) invokes the retrieve of the person list, using next as callback. The next chained function receives the person list in the data argument, and then, it saves it in a module variable, ready to be used by the rest of the tests.

It is not using a database. It using an in-memory domain model. That is the default “persistence”, and it is used in many of the defined tests. The initial domain model is loaded from testdata.json using the loaddata function:

{
    "projects": [
        {
            "name": "FaceHub",
            "periods": [
                { 
                    "name": "January 2014", 
                    "date": "2014-01-31", 
                    "amount": 100,
                    "assignments": [
                        { "from": "Alice", 
                            "to": "Bob", 
                            "amount": 50, 
                            "note": "Arrive earlier" },
                        { "from": "Alice", 
                            "to": "Charlie", 
                            "amount": 50 , 
                            "note": "Arrive earlier" },
                        { "from": "Bob", 
                            "to": "Alice", 
                            "amount": 60 , 
                            "note": "Arrive earlier" },
                        { "from": "Bob", 
                            "to": "Charlie", 
                            "amount": 40 , 
                            "note": "Arrive earlier" },
                        { "from": "Charlie", 
                            "to": "Alice", 
                            "amount": 35 , 
                            "note": "Arrive earlier" },
                        { "from": "Charlie", 
                            "to": "Bob", 
                            "amount": 65 , 
                            "note": "Arrive earlier" }
                    ]
                },
                { "name": "February 2014", 
                    "date": "2014-02-28", 
                    "amount": 100 }
            ],
            "team": [ "Alice", "Bob", "Charlie" ],
//....

The module personapi.js exports some functions to be used as actions:

module.exports = {
    list: list,
    get: get,
    getProjects: getProjects,
    loginPerson: loginPerson,
    getPendingShareProjects:getPendingShareProjects,
    updatePassword: updatePassword
}

Topics for the next posts: more API tests, routing of actions, persistence, etc.

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

March 18, 2015

Liqueed Project (3)

Filed under: Express, JavaScript, Liqueed, NodeJs, Test-Driven Development — ajlopez @ 5:24 pm

Previous Post
Next Post

I’m a big supporter of TDD (Test-Driven Development), and its workflow for software development. The software is created based on the tests that describe the expected behaviour, using the simplest possible implementation. For Liqueed Project, TDD was adopted during the creation of most of the code.

An example: there is a file, services\person.js, that is a JavaScript module. It manages the logic of the persons. A person can participate in zero, one or more projects, have sharepoints, vote, etc. And the system keeps the list of persons, retrieve a person by id, by username, … Part of the logic is in the service module. It was written using test/person.js for the tests of the logic. One test, one impementation, refactor, and so on. At the beginning of the test mode, there are some requires:

'use strict';

var service = require('../services/person');
var pservice = require('../services/project');
var async = require('simpleasync');

The services persons and project are used. simpleasync is a simple module I wrote to chaining callbacks. It was an interesting experience, and I’m doing “dog-fooding” in many projects.

Then, two module variables are declared, to be reused in the tests. They are the ids of some persons:

var annaid;
var lauraid;

To run the tests, I’m using simpleunit, another module I wrote, again, to practice JavaScript, TDD and simplicity. It is inspired by nodeunit, but it is simpler. simpleunit executes the module exported functions, providing a test object, something alike to assert in NodeJs.

The first tests exercises the creation of a person:

exports['add person'] = function (test) {
    test.async();

    service.addPerson({ name: 'Anna' }, function (err, result) {
        test.ok(!err);
        test.ok(result);
        annaid = result;
        test.done();
    });
};

It has a callback, so the function could end BEFORE the creation of the person. So, the test.async() tells to simpleunit to wait until the test.done() is reached. The above test only check the return of an id for the new person, and the value is saved in a module variable. This is something to discuss, but I found this way an easy one. The second tests depends on the first one. In C#, in other technologies, I wrote more isolated tests. But for me, in JavaScript, the level of isolation is the module, not the test function. In a module, a test function could depend on the execution of a previous one.

The second test:

exports['get person by id'] = function (test) {
    test.async();

    service.getPersonById(annaid, function (err, result) {
        test.ok(!err);
        test.ok(result);
        test.equal(result.name, 'Anna');
        test.equal(result.id, annaid);
        test.equal(result.username, 'anna');
        test.done();
    });
};

Using the id generated by the first test, the person is retrieved, and the data is checked.

This test series doesn’t use a predefined domain, and it does not assume a previous well-knoen status. And these tests are implemented using an in-memory domain. In the initial iterations, this in-memory domain was the only implementation. Only after the implementation of the first big use case, we added a domain persisted in MongoDB.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

February 20, 2015

Liqueed Project (2)

Filed under: Express, JavaScript, Liqueed, NodeJs — ajlopez @ 8:07 pm

Previous Post
Next Post

Liqueed is a Node.js project, repository at

https://github.com/liquid-co-ops/liqueed

And it has a package.json file, the current content:

{
  "name": "liqueed",
  "version": "0.0.2alpha",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "test": "simpleunit ./test ./testserver"
  },
  "dependencies": {
    "express": "~4.2.0",
    "static-favicon": "~1.0.0",
    "morgan": "~1.0.0",
    "cookie-parser": "~1.0.1",
    "body-parser": "~1.0.0",
    "debug": "~0.7.4",
    "ejs": "~0.8.5",
    "express-ejs-layouts": "^1.1.0",
    "ostore": "0.0.4",
    "simplelists": "0.0.4",
    "simpleasync": "0.0.5",
    "mongodb": "~1.4.19",
    "bcrypt-nodejs": "0.0.3",
    "express-session": "~1.9.3"
  },
  "devDependencies": {
    "simpleunit": "0.0.5",
    "simplejquery": "0.0.1",
    "zombie": "2.5.1"
  }
}

It is a private application, so you cannot publish it to the public npm repository.

It is a typical Express application, the initial code was generated with the express-generator. The file bin\www contains the source code to launch the application. It uses express modules, like morgan, cookie-parser, body-parser, express-session, but there are others that were included to be used from the logic of application server:

– ostore: Simple JavaScript objects repository, in-memory, used in many test and in the initial version, implementing persistence in-memory, without the need for a database.

– mongodb: the current app uses MongoDB as NoSQL persistence

– simplelists: a simple utility library with list/array functions

– simpleasync: a library to chain asynchronous callsbacks.

– bcript-nodejs: encryption module written in JavaScript

In the tests (written using Test-Driven Design workflow), it uses:

– simpleunit: test library, based on internal use of assert, exporting the test functions.

There are new tests to exercise the Single Page Application of public\index.html, using:

– zombie: it visits a page as a browser

Topics for the next posts: application structure, written tests, exposed API, MVC admin pages, persistence, SPA client, etc.

Stay tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

January 23, 2015

Liqueed Project (1)

Filed under: Express, JavaScript, Liqueed, MongoDB, NodeJs, Open Source Projects — ajlopez @ 6:36 pm

Next Post

Today, I want to present the Liqueed Project, see repo at:

https://github.com/liquid-co-ops/liqueed

It is a web application based on an idea by @acyment, read post:

http://blog.agilar.org/index.php/2014/04/30/leancoops-first-draft/

The basic idea of the application is to help teams that are developing something in the way that Cyment suggested, on the issue of allocation of shares on the project. For several months, the application (code, backlog, ideas, implementation, hosting and others) has been putting together by a “liquid” team (with entry and exit of people)

In this series of posts starting today I want to discuss interesting technical issues raised by the project. For today, I commented that:

– It is a Node.js application, exposed to the web using Express. Bringing the programming language is JavaScript.

– In addition to some internal administration pages with MVC, has an API exposed, exchanging JSON.

‘- There’s an app Single Page which is what would have to use the end user to view projects, voting, distributions of shares and to enter new ratings

– Most of the code was written using the workflow of TDD (Test-Driven Development). Even the first code implemented the model in memory, allowing easier progress on the implementation of use cases, without bothering about persistence (even the SPA client can run without having a walk server)

– A few months ago, we added persistence with MongoDB. We could use another database, relational perhaps. We are not taking advantage of the facilities to handle documents MongoDB yet. Only chose it for its ubiquity in development platforms and different Node hosting services.

– Some weeks ago, we added Istambul for code coverage.

– When TDD tests for pure code began to be long, we created a textual DSL (Domain Specific Language) that allows us to write text files for more complicated functional tests.

– Begin to add tests of SPA (Single Page Application) using Zombie

And there are more details and topics to comment, in the upcoming posts.

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

October 2, 2014

Express: Links, News And Resources (9)

Filed under: Express, Links, NodeJs, Web Development — ajlopez @ 5:37 pm

Previous Post

Traackr/chuck-node
https://github.com/Traackr/chuck-node
Node.js boilerplate with Express, MongoDB, Moongose, Backbone

StrongLoop | Express 3.x to 4.x Migration Guide
http://strongloop.com/strongblog/express-3-to-4-migration-guide/

Pixelhandler’s Blog
http://pixelhandler.com/posts/develop-a-restful-api-using-nodejs-with-express-and-mongoose

Express.js 4, Node.js and MongoDB REST API Tutorial
http://webapplog.com/express-js-4-node-js-and-mongodb-rest-api-tutorial/

Locomotive – Powerful MVC web framework for Node.js.
http://locomotivejs.org/

javascript – Difference between res.send and res.json in Express.js – Stack Overflow
http://stackoverflow.com/questions/19041837/difference-between-res-send-and-res-json-in-express-js

joemalski / Out .js 4.0- installation-guide
https://github.com/joemalski/express.js-4.0-Installation-Guide

linnovate/mean
https://github.com/linnovate/mean
MongoDB, Express, AngularJs, NodeJs

MEAN.JS Generator
http://meanjs.org/generator.html

codexar / Turn
https://github.com/codexar/rode
Packet-Oriented Framework for ES6 and Express

aeos/nivlac
https://github.com/aeos/nivlac
An express.js Template language that uses dom manipulation instead of a markup language

MEAN.io – A Fullstack Javascript Framework
http://mean.io/#!/

evilpacket/helmet
https://github.com/evilpacket/helmet
Collection of middleware to implement various security headers for Express / Connect

Passwordless – A node.js/express module for token-based logins
https://passwordless.net/

#3 Beginning routing in Node.js – Learn All The Nodes
http://www.learnallthenodes.com/episodes/3-beginning-routing-in-nodejs

My Links
http://delicious.com/ajlopez/express

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

September 29, 2014

Express: Links, News And Resources (8)

Filed under: Express, Links, NodeJs, Web Development — ajlopez @ 5:08 pm

Previous Post
Next Post

ol4ok/hogan-express
https://github.com/vol4ok/hogan-express

aito0077/coperable-site
https://github.com/aito0077/coperable-site

herenow/light-router
https://github.com/herenow/light-router

Stand-up: Developing a node.js application using Monaco (Part 1) – Visual Studio Online “Monaco” – Site Home – MSDN Blogs
http://blogs.msdn.com/b/monaco/archive/2014/03/27/stand-up-developing-a-node-js-application-using-monaco-part-1.aspx

mikaa123/catnap
https://github.com/mikaa123/catnap
Catnap helps you REST

brn/typescript-middleware
https://github.com/brn/typescript-middleware
typescript compile server for [connect/express]-middleware

RhapsodyJS docs – Get started
http://rhapsodyjs.github.io/
RhapsodyJS is a HMVC framework that runs on top of Express 4, it also auto-generate, in build time, a RESTful API with your database-agnostic Models

TryGhost/Ghost
https://github.com/tryghost/Ghost
Just a blogging platform https://ghost.org

Ghost – Just a blogging platform
https://ghost.org/

Ghost: Just a Blogging Platform by John O’Nolan — Kickstarter
https://www.kickstarter.com/projects/johnonolan/ghost-just-a-blogging-platform

StrongLoop | Comparing Express, Restify, hapi and LoopBack for building RESTful APIs
http://strongloop.com/strongblog/compare-express-restify-hapi-loopback/

MEAN Stack – A Quick Start Guide | Flippin’ Awesome
http://flippinawesome.org/2014/04/21/mean-stack-a-quick-start-guide/

An Introduction to the MEAN Stack
http://www.sitepoint.com/introduction-mean-stack/

StrongLoop | Beyond Node.js Express: An Intro to Koa.js and a Preview of Zones
http://strongloop.com/strongblog/node-js-express-introduction-koa-js-zone/

Express – api reference
http://expressjs.com/4x/api.html

Build a RESTful API Using Node and Express 4 ♥ Scotch
http://scotch.io/tutorials/javascript/build-a-restful-api-using-node-and-express-4

LoopBack
http://loopback.io/

Express 4 — Javascript and the server — Medium
https://medium.com/javascript-and-the-server/aa6992b52bcd

Using Webix with NodeJS
http://webix.com/blog/using-webix-with-nodejs/

DailyJS: Node Tutorial Part 22: Backbone (Again)
http://dailyjs.com/2011/04/25/node-tutorial-21/

KrakenJS Suite
http://krakenjs.com/

12 Web Application Frameworks for Node.js | CodeCondo
http://codecondo.com/10-web-application-frameworks-for-node-js/

My Links
http://delicious.com/ajlopez/express

Stay tuned!

Angel “Java” Lopez
http://www.ajlopez.com
http://twitter.com/ajlopez

September 11, 2014

Express: Links, News And Resources (7)

Filed under: Express, JavaScript, Links, NodeJs, Web Development — ajlopez @ 3:44 pm

Previous Post
Next Post

More links at http://delicious.com/ajlopez/express

29 Node.js Frameworks for Fast Web Application Development
http://www.queness.com/post/16219/29-nodejs-frameworks-for-fast-javascript-development

Introduction to the MEAN Stack – Tuts Code Tutorial
http://code.tutsplus.com/tutorials/introduction-to-the-mean-stack–cms-19918

expressmvp
https://www.npmjs.org/package/expressmvp

Node.js Pinterest Clone Script – Myyna build your own pinterest
http://www.myyna.com/

A Sample App with Node.js, Express and MongoDB – Part 2 | Dev Notes
http://blog.ijasoneverett.com/2013/04/a-sample-app-with-node-js-express-and-mongodb-part-2/

The Ultimate Guide To Node.js Express by Greg Cerveny | Udemy
https://www.udemy.com/the-ultimate-guide-to-nodejs-express/

hacksparrow/multer · GitHub
https://github.com/hacksparrow/multer

zeMirco/lockit
https://github.com/zeMirco/lockit

An Introduction To Full-Stack JavaScript | Smashing Coding
http://coding.smashingmagazine.com/2013/11/21/introduction-to-full-stack-javascript/

KeystoneJS · Node.js cms and web application platform built on Express and MongoDB
http://keystonejs.com/

JedWatson/keystone · GitHub
https://github.com/JedWatson/keystone

My Links
http://delicious.com/ajlopez/express

Older Posts »

Create a free website or blog at WordPress.com.