This repository was archived by the owner on Oct 1, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase_tests.js
71 lines (67 loc) · 2.03 KB
/
database_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const chai = require("chai");
const expect = chai.expect;
const conf = require("../config/config");
// Create a new schema that accepts a 'name' object.
// 'name' is a required field
const testSchema = new Schema({
name: { type: String, required: true }
});
//Create a new collection called 'Name'
const Name = mongoose.model("Name", testSchema);
describe("Database Tests", function() {
//Before starting the test, create a sandboxed database connection
//Once a connection is established invoke done()
before(function(done) {
mongoose.connect(conf.testDbURL, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", function() {
console.log("We are connected to test database!");
done();
});
});
describe("Test Database", function() {
//Save object with 'name' value of 'Mike"
it("New name saved to test database", function(done) {
var testName = Name({
name: "Mike"
});
testName.save(done);
});
it("Don't save incorrect format to database", function(done) {
//Attempt to save with wrong info. An error should trigger
var wrongSave = Name({
notName: "Not Mike"
});
wrongSave.save(err => {
if (err) {
return done();
}
throw new Error("Should generate error!");
});
});
it("Should retrieve data from test database", function(done) {
//Look up the 'Mike' object previously saved.
Name.find({ name: "Mike" }, (err, name) => {
if (err) {
throw err;
}
if (name.length === 0) {
throw new Error("No data!");
}
done();
});
});
});
// After all tests are finished drop database and close connection
after(function(done) {
mongoose.connection.db.dropDatabase(function() {
mongoose.connection.close(done);
});
});
});