npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-couch

v0.1.11

Published

An http server pretending to be couchdb, for unit testing.

Downloads

473

Readme

Build Status

Mock a CouchDB server for your unit tests.

Mock Couch will create an HTTP server that emulates the responses of a real CouchDB server. Since it is an actual HTTP server, no matter if you use libraries like cradle and nano, your code should work out of the box.

Mock Couch emit events, so you can listen to them to see the result of your test.

Installation

npm install --save-dev mock-couch

Changelog

  • 0.1.10
  • Updated restify version (credits to @gamegee)
  • 0.1.9
  • Specify the version of dependencies with npm-shrinkwrap.json
  • 0.1.8
  • Fix: _all_docs must return { key : thekey, error : "not_found" } for each missing key
  • Fix: _bulk_docs didn't converted the functions of the passed design docs.
  • 0.1.7
  • Fix: Do not preserve preserve any properties of the old document when updating (Credits to @reederz)
  • New feature: Now is possible to PUT/POST design documents (Credits to @dermidgen)
  • 0.1.6
  • Fix on instance_start_time (Credits to @Dainis)
  • Fix issue when calling addDB multiple times. (Credits to @Troy Cochran)
  • 0.1.5
  • Add support for a show function (Credits to @tekdel)
  • 0.1.4
  • The required minimal version of node is now 0.12
  • Support for the built-in _sum and _count reduce functions.
  • Now, mock-couch uses the Views Collation rules of CouchDB, by using couch-viewkey-compare. (Thanks to @monowerker for this module).
  • GET _uuids (Credits to @watchforstock)
  • Several fixes (Credits to @monowerkerds and @reederz)
  • 0.1.3
  • Several fixes (Thanks to @monowerker and @alexjeffburke)
  • Rewriting a lot of the code using Ramda (to make it more functional)
  • Support for linked documents in views (Credits to @monowerker)
  • Implementation of _changes (Credits to @conor-mac-aoidh)
  • 0.1.2
  • Added HEAD requests. (Credits to @davidwood)
  • Added option to prevent keep-alive connections. (Credits to @davidwood)
  • Now is possible to use the query option key on views.
  • Other fixes.
  • 0.1.1
  • The emitted events are now standarized. Read about the events on the documentation.
  • 0.1.0
  • Support for views (map and reduce functions). Create a _design/ document to use them. Read more about them here.
  • Documentation available on http://chris-l.github.io/mock-couch/
  • Added addDoc method to add a document from the node.js side. Contrary to adding a document with a PUT, this is sync and allows you to specify the _rev

Documentation

Visit the Mock Couch website.

Features

  • Implemented with restify.
  • Uses simple JavaScript objects as documents.
  • It emit events, so the tests can listen to them.
  • The mock_couch object has a databases public property, to examine how the databases are in any moment.
  • Several of the CouchDB REST methods. Right now it has:
  • GET one document
  • GET _all_docs, including:
    • include_docs=true
    • descending=true
    • startkey
    • endkey
    • also, using _all_docs with POST to specify the desired keys
  • GET the information of a database
  • GET _all_dbs
  • GET _uuids
  • GET views (like http://localhost:5984/database/_design/myviews/_view/someview/)
  • PUT one document
  • PUT a database
  • POST one document
  • POST to _bulk_docs multiple documents
  • DELETE one document
  • DELETE a database

Not yet implemented

  • deleting by setting the _deleted member
  • Attachment support
  • And a lot of other things!

Keep in mind that Mock Couch is not attempting to fully implement CouchDB, but only the features necessary for unit testing CouchDB based apps.

However, if there is a feature you need for your tests, feel free to add a feature request in the issues section!

Usage

Here is an example:

var mockCouch = require('mock-couch');

// myfun is the function that we want to test.
// It uses couchdb, either by using cradle, nano, or direct http requests
// This function takes an object as parameter, maybe runs some validations
// and if everything is ok, then save it on couchdb.
var myfun = require('somefunc');


describe('myfun', function() {

  beforeEach(function() {
    // Starting the server
    var couchdb = mockCouch.createServer();

    // Make sure you are either executing this test under a machine that does not have couchdb installed/enabled,
    // or that you are using a different port!
    // (which may require that you are able to specify the couchdb port on the function you are about to test)
    couchdb.listen(5984);

    // This creates a db for Mock Couch. The db is nothing but an array of objects.
    // If we provide an object with an _id property, it will use it. Otherwise, it will create a random one.
    couchdb.addDB('people', [ { name : 'one name', lastname : 'one lastname' }, { _id : '4568797890', name : 'second name', lastname : 'other lastname' } ]);
  });


  // Here is the test
  it('must add a person to couchdb', function(done) {

    // Now we add a listener that is expecting the data we are about to send.
    couchdb.on('POST', function(data) {
      expect(data.doc.name).toBe('reimu');
      expect(data.doc.lastname).toBe('hakurei');
      done();
    });

    // And here we are finally calling the function
    myfun({ name : 'reimu', lastname : 'hakurei' });

  });
});

If your testing requires the frequent setup and teardown of the mock server, it may be beneficial to prevent keep-alive connections. The server will always return a Connection: close header if constructed with a keepAlive option set to false.

var couchdb = mockCouch.createServer({ keepAlive: false });

Status

In this moment I think it could be considered beta; I don't expect any breaking changes.

License

MIT license

Contribution

Your feedback, pull requests, etc are welcomed! :)