PromucFlow_constructor/app/client/src/WidgetQueryGenerators/MongoDB/index.test.ts
balajisoundar 4c938676bd
chore: Miscellaneous one click binding updates (#24957)
## Description
- Remove the primary column from the insert and update queries.
- Save/Discard button isSaveDisabled and isDiscardDisabled properties
should be in js mode.
- Don't create insert and update query if datasource is read only

#### PR fixes following issue(s)
Fixes https://github.com/appsmithorg/appsmith/issues/24858

#### Type of change

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
>
>
>
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [x] Manual
- [ ] Jest
- [x] Cypress
>
>
#### Test Plan
> Add Testsmith test cases links that relate to this PR
>
>
#### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
>
>
>
## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


#### QA activity:
- [ ] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [ ] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [ ] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-07-20 11:52:20 +05:30

332 lines
8.8 KiB
TypeScript

import { DatasourceConnectionMode } from "entities/Datasource";
import MongoDB from ".";
describe("Mongo WidgetQueryGenerator", () => {
const initialValues = {
actionConfiguration: {
formData: {
command: { data: "FIND" },
aggregate: { limit: { data: "10" } },
delete: { limit: { data: "SINGLE" } },
updateMany: { limit: { data: "SINGLE" } },
smartSubstitution: { data: true },
find: { data: "" },
insert: { data: "" },
count: { data: "" },
},
},
};
test("should build select form data correctly", () => {
const expr = MongoDB.build(
{
select: {
limit: "data_table.pageSize",
where: 'data_table.searchText||""',
offset: "(data_table.pageNo - 1) * data_table.pageSize",
orderBy: "data_table.sortOrder.column || 'genres'",
sortOrder: 'data_table.sortOrder.order == "desc" ? -1 : 1',
},
totalRecord: false,
},
{
tableName: "someTable",
datasourceId: "someId",
// ignore columns
aliases: [{ name: "someColumn1", alias: "someColumn1" }],
widgetId: "someWidgetId",
searchableColumn: "title",
columns: [],
primaryColumn: "",
connectionMode: DatasourceConnectionMode.READ_WRITE,
},
initialValues,
);
expect(expr).toEqual([
{
type: "select",
name: "Find_someTable",
dynamicBindingPathList: [
{
key: "formData.find.skip.data",
},
{
key: "formData.find.query.data",
},
{
key: "formData.find.sort.data",
},
{
key: "formData.find.limit.data",
},
],
payload: {
formData: {
collection: {
data: "someTable",
},
smartSubstitution: { data: true },
aggregate: { limit: { data: "10" } },
command: {
data: "FIND",
},
find: {
data: "",
limit: {
data: "{{data_table.pageSize}}",
},
query: {
data: '{{{ title: {$regex: data_table.searchText||""} }}}',
},
skip: {
data: "{{(data_table.pageNo - 1) * data_table.pageSize}}",
},
sort: {
data: "{{ data_table.sortOrder.column || 'genres' ? { [data_table.sortOrder.column || 'genres']: data_table.sortOrder.order == \"desc\" ? -1 : 1 ? 1 : -1 } : {}}}",
},
},
},
},
},
]);
});
test("should build select form data without write permissions", () => {
const expr = MongoDB.build(
{
select: {
limit: "data_table.pageSize",
where: 'data_table.searchText||""',
offset: "(data_table.pageNo - 1) * data_table.pageSize",
orderBy: "data_table.sortOrder.column || 'genres'",
sortOrder: 'data_table.sortOrder.order == "desc" ? -1 : 1',
},
totalRecord: false,
},
{
tableName: "someTable",
datasourceId: "someId",
// ignore columns
aliases: [{ name: "someColumn1", alias: "someColumn1" }],
widgetId: "someWidgetId",
searchableColumn: "title",
columns: [],
primaryColumn: "",
connectionMode: DatasourceConnectionMode.READ_ONLY,
},
initialValues,
);
expect(expr).toEqual([
{
type: "select",
name: "Find_someTable",
dynamicBindingPathList: [
{
key: "formData.find.skip.data",
},
{
key: "formData.find.query.data",
},
{
key: "formData.find.sort.data",
},
{
key: "formData.find.limit.data",
},
],
payload: {
formData: {
collection: {
data: "someTable",
},
smartSubstitution: { data: true },
aggregate: { limit: { data: "10" } },
command: {
data: "FIND",
},
find: {
data: "",
limit: {
data: "{{data_table.pageSize}}",
},
query: {
data: '{{{ title: {$regex: data_table.searchText||""} }}}',
},
skip: {
data: "{{(data_table.pageNo - 1) * data_table.pageSize}}",
},
sort: {
data: "{{ data_table.sortOrder.column || 'genres' ? { [data_table.sortOrder.column || 'genres']: data_table.sortOrder.order == \"desc\" ? -1 : 1 ? 1 : -1 } : {}}}",
},
},
},
},
},
]);
});
test("should build update form data correctly ", () => {
const expr = MongoDB.build(
{
update: {
value: "{rating : {$gte : 9}}",
where: "{ $inc: { score: 1 } }",
},
totalRecord: false,
},
{
tableName: "someTable",
datasourceId: "someId",
// ignore columns
aliases: [{ name: "someColumn1", alias: "someColumn1" }],
widgetId: "someWidgetId",
searchableColumn: "title",
columns: [],
primaryColumn: "",
connectionMode: DatasourceConnectionMode.READ_WRITE,
},
initialValues,
);
expect(expr).toEqual([
{
name: "Update_someTable",
type: "update",
dynamicBindingPathList: [
{
key: "formData.updateMany.query.data",
},
{
key: "formData.updateMany.update.data",
},
],
payload: {
formData: {
collection: {
data: "someTable",
},
command: {
data: "UPDATE",
},
aggregate: { limit: { data: "10" } },
smartSubstitution: { data: true },
updateMany: {
query: {
data: "{_id: ObjectId('{{{ $inc: { score: 1 } }._id}}')}",
},
limit: { data: "SINGLE" },
update: {
data: '{{{$set: _.omit({rating : {$gte : 9}}, "_id")}}}',
},
},
},
},
},
]);
});
test("should not build update form data without write permissions ", () => {
const expr = MongoDB.build(
{
update: {
value: "{rating : {$gte : 9}}",
where: "{ $inc: { score: 1 } }",
},
totalRecord: false,
},
{
tableName: "someTable",
datasourceId: "someId",
// ignore columns
aliases: [{ name: "someColumn1", alias: "someColumn1" }],
widgetId: "someWidgetId",
searchableColumn: "title",
columns: [],
primaryColumn: "",
connectionMode: DatasourceConnectionMode.READ_ONLY,
},
initialValues,
);
expect(expr).toEqual([]);
});
test("should build insert form data correctly ", () => {
const expr = MongoDB.build(
{
create: {
value: "insert_form.formData",
},
totalRecord: false,
},
{
tableName: "someTable",
datasourceId: "someId",
// ignore columns
aliases: [{ name: "someColumn1", alias: "someColumn1" }],
widgetId: "someWidgetId",
searchableColumn: "title",
columns: [],
primaryColumn: "",
connectionMode: DatasourceConnectionMode.READ_WRITE,
},
initialValues,
);
expect(expr).toEqual([
{
name: "Insert_someTable",
type: "create",
dynamicBindingPathList: [
{
key: "formData.insert.documents.data",
},
],
payload: {
formData: {
collection: {
data: "someTable",
},
aggregate: { limit: { data: "10" } },
smartSubstitution: { data: true },
command: {
data: "INSERT",
},
insert: {
data: "",
documents: {
data: "{{insert_form.formData}}",
},
},
},
},
},
]);
});
test("should not build insert form data without write permissions ", () => {
const expr = MongoDB.build(
{
create: {
value: "insert_form.formData",
},
totalRecord: false,
},
{
tableName: "someTable",
datasourceId: "someId",
// ignore columns
aliases: [{ name: "someColumn1", alias: "someColumn1" }],
widgetId: "someWidgetId",
searchableColumn: "title",
columns: [],
primaryColumn: "",
connectionMode: DatasourceConnectionMode.READ_ONLY,
},
initialValues,
);
expect(expr).toEqual([]);
});
});