Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo utils tests #38

Merged
merged 4 commits into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/external-db-mongo/lib/connection_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const DatabaseOperations = require('./mongo_operations')
const { notConnectedPool } = require('./mongo_utils')

const init = async (cfg) => {
const uri = `mongodb://${cfg.user}:${cfg.password}@${cfg.host}/${cfg.db}`
const uri = `mongodb://${cfg.user}:${cfg.password}@${cfg.host||cfg.cloudSqlConnectionName}/${cfg.db}` // TODO: in some cases the prefix should be mongodb+srv://
const client = new MongoClient(uri)

const { pool, cleanup } = await client.connect()
Expand Down
3 changes: 1 addition & 2 deletions packages/external-db-mongo/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const SchemaProvider = require('./mongo_schema_provider')
const DataProvider = require('./mongo_data_provider')
const FilterParser = require('./sql_filter_transformer')
const SchemaColumnTranslator = require('./sql_schema_translator')
const init = require('./connection_provider')
const DatabaseOperations = require('./mongo_operations')

const driver = () => require('../tests/drivers/sql_filter_transformer_test_support')
const opsDriver = () => require('../tests/drivers/db_operations_test_support')

module.exports = { SchemaProvider, DataProvider, FilterParser, SchemaColumnTranslator, driver, init, opsDriver, DatabaseOperations }
module.exports = { SchemaProvider, DataProvider, FilterParser, driver, init, opsDriver, DatabaseOperations }
16 changes: 8 additions & 8 deletions packages/external-db-mongo/lib/mongo_data_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class DataProvider {
}

async insert(collectionName, items) {
const rss = await this.client.db()
.collection(collectionName)
.insertMany(items)
return rss.insertedCount
const result = await this.client.db()
.collection(collectionName)
.insertMany(items)
return result.insertedCount
}

async update(collectionName, items) {
Expand All @@ -38,10 +38,10 @@ class DataProvider {
}

async delete(collectionName, itemIds) {
const rss = await this.client.db()
const result = await this.client.db()
.collection(collectionName)
.remove({ _id: { $in: itemIds } })
return rss.deletedCount
return result.deletedCount
}

async truncate(collectionName) {
Expand All @@ -53,15 +53,15 @@ class DataProvider {
async aggregate(collectionName, filter, aggregation) {
const { fieldsStatement, havingFilter } = this.filterParser.parseAggregation(aggregation.processingStep, aggregation.postFilteringStep)
const { filterExpr } = this.filterParser.transform(filter)
const rs = await this.client.db()
const result = await this.client.db()
.collection(collectionName)
.aggregate( [ { $match: filterExpr },
fieldsStatement,
havingFilter
] )
.toArray()

return rs.map( unpackIdFieldForItem )
return result.map( unpackIdFieldForItem )
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/external-db-mongo/lib/mongo_schema_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class SchemaProvider {
}

async list() {
await this.ensureSystemTableExists()

const resp = await this.client.db()
.collection(SystemTable)
.find({})
Expand Down Expand Up @@ -99,6 +101,14 @@ class SchemaProvider {
.collection(SystemTable)
.findOne({ _id: collectionName })
}

async ensureSystemTableExists() {
const systemTable = await this.client.db().listCollections({name:SystemTable}).toArray()
if (!systemTable.length) {
await this.client.db().createCollection(SystemTable)
await this.create('wix_collection', [{ name: 'a', type: 'text' }, { name: 'b', type: 'text' }])
}
}
}


Expand Down
35 changes: 35 additions & 0 deletions packages/external-db-mongo/lib/mongo_utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const { unpackIdFieldForItem } = require('./mongo_utils')

describe('Mongo Utils', () => {
test.only('Item with _id object field that contains _id field (Item._id._id) will return the object with _id field not as object', () => {
const item = {
_id: { _id: 1 },
b: 2
}
expect(unpackIdFieldForItem(item)).toEqual({ _id: 1, b: 2 })
})

test.only('Item with _id object field that does not contains _id field will return the object without _id field', () => {
const item = {
_id: { a: 1 },
b: 2
}
expect(unpackIdFieldForItem(item)).toEqual({ a: 1, b: 2 })
})

test.only('Item with _id field that is not an object will return as the same item', () => {
const item = {
_id: 1,
b: 2
}
expect(unpackIdFieldForItem(item)).toEqual({ _id: 1, b: 2 })
})

test.only('Item without _id field will return as the same item', () => {
const item = {
a: 1,
b: 2
}
expect(unpackIdFieldForItem(item)).toEqual({ a: 1, b: 2 })
})
})
24 changes: 0 additions & 24 deletions packages/external-db-mongo/lib/sql_exception_translator.js

This file was deleted.

127 changes: 0 additions & 127 deletions packages/external-db-mongo/lib/sql_schema_translator.js

This file was deleted.

Loading