-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
69 lines (50 loc) · 2.09 KB
/
app.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
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const holidayDestinationRepo = require('./repos/holidayDestinationRepo');
const data = require('./holidayDestination.json')
const url = 'mongodb://localhost:27017';
const dbName = 'holidayDestination';
async function main() {
const client = new MongoClient(url);
await client.connect();
const admin= client.db(dbName).admin();
console.log(await admin.listDatabases());
try {
const results = await holidayDestinationRepo.loadData(data);
assert.equal(data.length, results.insertedCount);
const getData = await holidayDestinationRepo.get();
assert.equal(data.length, getData.length);
// Add new item
const newItem = {
"Title": "Barbados",
"Description": "Barbados is a lovely island located in the Caribbean Sea Windward islands.",
"ImageUrl": "https://live.staticflickr.com/4284/35447883490_ba332d5bc1_n.jpg"
}
const addedItem = await holidayDestinationRepo.add(newItem);
assert(addedItem._id)
const addedItemQuery = await holidayDestinationRepo.getById(addedItem._id);
assert.deepEqual(addedItemQuery, newItem)
//Update item
const updatedItem = await holidayDestinationRepo.update(addedItem._id, {
"Title": "Barbados NEW",
"Description": "Barbados NEW is a lovely island located in the Caribbean Sea Windward islands.",
"ImageUrl": "https://live.staticflickr.com/2937/32911101244_3eff17c051_n.jpg"
});
// assert
assert.equal(updatedItem.Description, "Barbados NEW is a lovely island located in the Caribbean Sea Windward islands.");
// Delete existing item (last added)
const removed = await holidayDestinationRepo.remove(addedItem._id);
// assert
assert(removed);
const deletedItem = await holidayDestinationRepo.getById(addedItem._id);
assert.equal(deletedItem, null);
} catch (error) {
console.log(error)
} finally {
const admin = client.db(dbName).admin();
await client.db(dbName).dropDatabase();
console.log(await admin.listDatabases());
client.close();
}
}
main();