Skip to content

Instantly share code, notes, and snippets.

@byverdu
Last active May 29, 2017 22:17
Show Gist options
  • Save byverdu/77a47253ec9512b9f254c4191c266840 to your computer and use it in GitHub Desktop.
Save byverdu/77a47253ec9512b9f254c4191c266840 to your computer and use it in GitHub Desktop.
/* eslint-disable import/no-extraneous-dependencies*/
// Test cases for Mongo Schemas
import { ImdbSchema } from '../../app/server/models/ImdbSchema';
import sampleData from '../sampleData';
let movie;
let Imdb;
let connection;
require( '../../app/server/db' );
const { imdbMovie, imdbSerie } = sampleData;
// Crea una conexion temporal a la DB
before(( done ) => {
connection = mongoose.createConnection( 'mongodb://localhost/imdbAppTest' );
Imdb = connection.model( 'Imdb', ImdbSchema );
movie = new Imdb( imdbMovie );
connection.once( 'open', () => done());
});
after(( done ) => {
connection.close(() => done());
mongoose.models = {};
mongoose.modelSchemas = {};
});
describe( 'Schema test cases', () => {
describe( 'Imdb shape', () => {
it( 'has a title property that is a String', () => {
expect( movie.title ).to.be.a( 'string' );
});
it( 'myRating property can be set', () => {
movie.setMyRating( '8.5' );
expect( movie.myRating ).to.equal( '8.5' );
});
// ... Resto de casos
});
describe( 'Saving and deleting documents for Imdb', () => {
it( 'A new movie can be saved to db', () => {
const newImdb = new Imdb( imdbMovie );
return newImdb.save().then(( response ) => {
expect( response.title ).to.equal( 'Rambo' );
Imdb.remove({ title: 'Rambo' }).exec();
});
});
it( 'A saved movie can be deleted from db', () => {
const rambo = new Imdb( imdbMovie );
rambo.save()
.then(() => {
Imdb.remove({ title: 'Rambo' }).exec();
});
return Imdb.findOne({ title: 'Rambo' }).then(( response ) => {
expect( response ).to.eql( null );
});
});
});
});
// imdbSchema.js
import mongoose from 'mongoose';
const Schema = mongoose.Schema;
mongoose.Promise = global.Promise;
const ImdbSchema = new Schema({
title: { type: String, default: '' },
type: { type: String, default: '' },
description: { type: String, default: '' },
poster: { type: String, default: '' },
rating: { type: String, default: '' },
myRating: { type: String, default: '' },
imdburl: { type: String, default: '' },
genre: { type: [String], default: [] }
});
ImdbSchema.methods.setMyRating = function ( rating ) { this.myRating = rating; };
export {
ImdbSchema
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment