Skip to content

Commit

Permalink
#130: Write tests for Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
groenroos committed Oct 24, 2021
1 parent a19b798 commit 9e04890
Show file tree
Hide file tree
Showing 4 changed files with 489 additions and 50 deletions.
60 changes: 30 additions & 30 deletions lib/Storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ export default class Storage {
if (this.app.uploads) {
this.schema.uploads = uploadStructure;
}

/* Create dbs */
this.createDatabase();
}


Expand Down Expand Up @@ -112,6 +109,8 @@ export default class Storage {
}
}
}

await this.createDatabase();
}

return this.db;
Expand All @@ -122,36 +121,38 @@ export default class Storage {
* Connect to the database and create each collection defined in the scheme
*/
async createDatabase() {
await this.importDriver();

const dbConfig = this.app.config.db;
dbConfig.name = this.app.name || 'app';
dbConfig.dataLimit = this.app.config.dataLimit;
if (this.db === null) {
await this.importDriver();
} else {
const dbConfig = this.app.config.db;
dbConfig.name = this.app.name || 'app';
dbConfig.dataLimit = this.app.config.dataLimit;

await this.db.connect(dbConfig);
await this.db.connect(dbConfig);

/* Create each collection in the schema in the database */
for (const collection in this.schema) {
if (Object.prototype.hasOwnProperty.call(this.schema, collection)) {
const fields = this.schema[collection];
/* Create each collection in the schema in the database */
for (const collection in this.schema) {
if (Object.prototype.hasOwnProperty.call(this.schema, collection)) {
const fields = this.schema[collection];

try {
await this.db.createCollection(collection, fields);
} catch (error) {
console.warn(error);
}
try {
await this.db.createCollection(collection, fields);
} catch (error) {
console.warn(error);
}

/* Go through all the fields in the model */
for (const key in fields) {
/* Create indices for any fields marked unique or identifiable */
if (fields[key].unique || fields[key].identifiable) {
await this.db.createIndex(collection, { [key]: 'unique' });
/* Go through all the fields in the model */
for (const key in fields) {
/* Create indices for any fields marked unique or identifiable */
if (fields[key].unique || fields[key].identifiable) {
await this.db.createIndex(collection, { [key]: 'unique' });
}
}
}
}
}

console.log('CREATED DBS');
console.log('CREATED DBS');
}
}


Expand Down Expand Up @@ -195,7 +196,7 @@ export default class Storage {
const rules = this.getRules(collection);

/* If it doesn't exist, return null in strict mode */
if (!('field' in rules) && this.app.config.strict) {
if (!(field in rules) && this.app.config.strict) {
return null;
}

Expand Down Expand Up @@ -351,10 +352,9 @@ export default class Storage {
/* Specially format certain fields */
for (const field of Object.keys(rules)) {
const rule = rules[field];
const type = (rule.type || rule).toLowerCase();

/* Format reference fields */
if (type === 'reference' || type === 'id') {
if (rule.type === 'reference' || rule.type === 'id') {
if (conditions.references) {
conditions.references.push(field);
} else {
Expand All @@ -364,12 +364,12 @@ export default class Storage {

if (data[field]) {
/* Format date fields */
if (type === 'date') {
if (rule.type === 'date') {
data[field] = Number(moment(data[field]).format('x'));
}

/* Format boolean fields */
if (type === 'boolean') {
if (rule.type === 'boolean') {
data[field] = Boolean(data[field]);
}
}
Expand Down
25 changes: 25 additions & 0 deletions test/_utils/getFileObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url';
import mimeTypes from 'mime-types';


const __dirname = path.dirname(fileURLToPath(import.meta.url));


export default (filename, cb) => {
const filepath = path.join(__dirname, '../_data/files', filename);
const file = fs.readFileSync(filepath);
const stats = fs.statSync(filepath);
const mime = mimeTypes.lookup(filepath);

return {
name: filename,
data: file,
size: stats.size,
tempFilePath: filepath,
truncated: false,
mimetype: mime,
mv: cb ? cb : () => true
};
};
Loading

0 comments on commit 9e04890

Please sign in to comment.