|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const MongoClient = require('../../lib/mongo_client'); |
| 6 | +const GridFsBucket = require('../../lib/gridfs-stream'); |
| 7 | + |
| 8 | +const DB_NAME = 'perftest'; |
| 9 | +const COLLECTION_NAME = 'corpus'; |
| 10 | + |
| 11 | +const SPEC_DIRECTORY = path.resolve(__dirname, 'spec'); |
| 12 | + |
| 13 | +function loadSpecFile(filePath, encoding) { |
| 14 | + const fp = [SPEC_DIRECTORY].concat(filePath); |
| 15 | + return fs.readFileSync(path.join.apply(path, fp), encoding); |
| 16 | +} |
| 17 | + |
| 18 | +function loadSpecString(filePath) { |
| 19 | + return loadSpecFile(filePath, 'utf8'); |
| 20 | +} |
| 21 | + |
| 22 | +function makeClient() { |
| 23 | + this.client = new MongoClient('mongodb://localhost:27017'); |
| 24 | +} |
| 25 | + |
| 26 | +function connectClient() { |
| 27 | + return this.client.connect(); |
| 28 | +} |
| 29 | + |
| 30 | +function disconnectClient() { |
| 31 | + this.client.close(); |
| 32 | +} |
| 33 | + |
| 34 | +function initDb() { |
| 35 | + this.db = this.client.db(DB_NAME); |
| 36 | +} |
| 37 | + |
| 38 | +function dropDb() { |
| 39 | + return this.db.dropDatabase(); |
| 40 | +} |
| 41 | + |
| 42 | +function createCollection() { |
| 43 | + return this.db.createCollection(COLLECTION_NAME); |
| 44 | +} |
| 45 | + |
| 46 | +function initCollection() { |
| 47 | + this.collection = this.db.collection(COLLECTION_NAME); |
| 48 | +} |
| 49 | + |
| 50 | +function dropCollection() { |
| 51 | + return this.collection.drop(); |
| 52 | +} |
| 53 | + |
| 54 | +function initBucket() { |
| 55 | + this.bucket = new GridFsBucket(this.db); |
| 56 | +} |
| 57 | + |
| 58 | +function dropBucket() { |
| 59 | + return this.bucket && this.bucket.drop(); |
| 60 | +} |
| 61 | + |
| 62 | +function makeLoadJSON(name) { |
| 63 | + return function() { |
| 64 | + this.doc = JSON.parse(loadSpecString(['single_and_multi_document', name])); |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +module.exports = { |
| 69 | + makeClient, |
| 70 | + connectClient, |
| 71 | + disconnectClient, |
| 72 | + initDb, |
| 73 | + dropDb, |
| 74 | + createCollection, |
| 75 | + initCollection, |
| 76 | + dropCollection, |
| 77 | + makeLoadJSON, |
| 78 | + loadSpecFile, |
| 79 | + loadSpecString, |
| 80 | + initBucket, |
| 81 | + dropBucket |
| 82 | +}; |
0 commit comments