Browse Source

fix find one to return empty object

Signed-off-by: Vijay Kumar Rathore <professional.vijay8492@gmail.com>
pull/2457/head
Vijay Kumar Rathore 2 years ago
parent
commit
cf76563629
  1. 40
      .github/workflows/unit-test.yml
  2. 16845
      packages/nocodb/package-lock.json
  3. 6
      packages/nocodb/package.json
  4. 80
      packages/nocodb/src/__tests__/unit/lib/meta/api/dataApis/dataAliasApis.test.ts
  5. 15
      packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts
  6. 2
      packages/nocodb/tsconfig.json

40
.github/workflows/unit-test.yml

@ -0,0 +1,40 @@
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Backend Unit Tests
on:
push:
branches: [ "develop" ]
pull_request:
branches: [ "develop" ]
jobs:
unit-tests:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: install dependencies nocodb-sdk
working-directory: ./packages/nocodb-sdk
run: npm ci
- name: build nocodb-sdk
working-directory: ./packages/nocodb-sdk
run: npm run build:main
- name: install dependencies nocodb
working-directory: ./packages/nocodb
run: npm ci
- name: run unit tests
working-directory: ./packages/nocodb
run: npm run unit-test

16845
packages/nocodb/package-lock.json generated

File diff suppressed because it is too large Load Diff

6
packages/nocodb/package.json

@ -17,6 +17,7 @@
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"lint": "eslint src --ext .ts",
"test": "cross-env TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/**/*.test.ts --recursive",
"unit-test": "cross-env TS_NODE_PROJECT=tsconfig.json mocha --require ts-node/register 'src/__tests__/unit/**/*.test.ts' --recursive --check-leaks --exit",
"local:test:graphql": "cross-env DATABASE_URL=mysql://root:password@localhost:3306/sakila TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/graphql.test.ts --recursive --timeout 10000 --exit",
"test:graphql": "cross-env TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/graphql.test.ts --recursive --timeout 10000 --exit",
"test:grpc": "cross-env TS_NODE_PROJECT=tsconfig.json mocha -r ts-node/register src/__tests__/grpc.test.ts --recursive --timeout 10000 --exit",
@ -197,6 +198,7 @@
"@types/minio": "^7.0.7",
"@types/mkdirp": "^1.0.2",
"@types/mocha": "^8.0.1",
"@types/node": "^18.0.0",
"@types/nodemailer": "^6.4.0",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.0.1",
@ -221,8 +223,10 @@
"npm-run-all": "^4.1.5",
"nyc": "^14.1.1",
"open-cli": "^5.0.0",
"prettier": "^1.19.1",
"prettier": "^2.7.1",
"raw-body": "^2.4.1",
"rewire": "^6.0.0",
"sinon": "^14.0.0",
"standard-version": "^8.0.1",
"supertest": "^4.0.2",
"terser-webpack-plugin": "^4.1.0",

80
packages/nocodb/src/__tests__/unit/lib/meta/api/dataApis/dataAliasApis.test.ts

@ -0,0 +1,80 @@
'use strict';
import rewire from 'rewire';
import sinon from 'sinon';
import { expect } from 'chai';
import Base from '../../../../../../lib/models/Base';
import Model from '../../../../../../lib/models/Model';
import NcConnectionMgrv2 from '../../../../../../lib/utils/common/NcConnectionMgrv2';
import * as getAstObject from '../../../../../../lib/db/sql-data-mapper/lib/sql/helpers/getAst';
const dataAliasApis = rewire(
'../../../../../../lib/meta/api/dataApis/dataAliasApis'
);
const getFindOne = dataAliasApis.__get__('getFindOne');
describe('getFindOne', () => {
const model = {
id: 'modelId',
base_id: 'baseId'
};
const view = {
id: 'viewId'
};
const base = {
id: 'baseId'
};
const dbDriver = {
id: 'dbDriverId'
};
const req = { query: {} };
const baseModel = {
findOne: sinon.fake.returns(undefined)
};
const baseGetFake = sinon.replace(Base, 'get', sinon.fake.returns(base));
const baseModelFake = sinon.replace(
Model,
'getBaseModelSQL',
sinon.fake.returns(baseModel)
);
sinon.replace(NcConnectionMgrv2, 'get', sinon.fake.returns(dbDriver));
const getAstFake = sinon.fake.returns({ id: 1 });
sinon.replace(getAstObject, 'default', getAstFake);
it('calls Base.get to find base', async () => {
await getFindOne(model, view, req);
expect(baseGetFake.calledWith(model.base_id)).to.be.true;
});
it('call Model.getBaseModelSQL to find baseModel', async () => {
await getFindOne(model, view, req);
expect(
baseModelFake.calledWith({
id: model.id,
viewId: view.id,
dbDriver: dbDriver
})
).to.be.true;
});
it('calls baseModel.findOne', async () => {
await getFindOne(model, view, req);
expect(baseModel.findOne.calledWith({ ...req.query })).to.be.true;
});
describe('when data is not found', () => {
it('should return empty object', async () => {
expect(await getFindOne(model, view, req)).to.be.empty;
});
});
describe('when data is found', () => {
it('returns data', async () => {
const findOneResult = {
id: 'dataId'
};
baseModel.findOne = sinon.fake.returns(findOneResult);
expect(await getFindOne(model, view, req)).eql(findOneResult);
});
});
});

15
packages/nocodb/src/lib/meta/api/dataApis/dataAliasApis.ts

@ -135,12 +135,15 @@ async function getFindOne(model, view: View, req) {
args.sortArr = JSON.parse(args.sortArrJson);
} catch (e) {}
return await nocoExecute(
await getAst({ model, query: args, view }),
await baseModel.findOne(args),
{},
{}
);
const data = await baseModel.findOne(args);
return data
? await nocoExecute(
await getAst({ model, query: args, view }),
data,
{},
{}
)
: {};
}
async function getDataGroupBy(model, view: View, req) {

2
packages/nocodb/tsconfig.json

@ -48,7 +48,7 @@
"es2017"
],
"types": [
"node"
"mocha", "node"
],
"typeRoots": [
"node_modules/@types",

Loading…
Cancel
Save