多维表格
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1 lines
13 KiB

"use strict";(self.webpackChunknoco_docs=self.webpackChunknoco_docs||[]).push([[8149],{3905:(e,t,n)=>{n.d(t,{Zo:()=>c,kt:()=>k});var a=n(67294);function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function o(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function l(e,t){if(null==e)return{};var n,a,i=function(e,t){if(null==e)return{};var n,a,i={},r=Object.keys(e);for(a=0;a<r.length;a++)n=r[a],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(a=0;a<r.length;a++)n=r[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}var s=a.createContext({}),p=function(e){var t=a.useContext(s),n=t;return e&&(n="function"==typeof e?e(t):o(o({},t),e)),n},c=function(e){var t=p(e.components);return a.createElement(s.Provider,{value:t},e.children)},u="mdxType",d={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},m=a.forwardRef((function(e,t){var n=e.components,i=e.mdxType,r=e.originalType,s=e.parentName,c=l(e,["components","mdxType","originalType","parentName"]),u=p(n),m=i,k=u["".concat(s,".").concat(m)]||u[m]||d[m]||r;return n?a.createElement(k,o(o({ref:t},c),{},{components:n})):a.createElement(k,o({ref:t},c))}));function k(e,t){var n=arguments,i=t&&t.mdxType;if("string"==typeof e||i){var r=n.length,o=new Array(r);o[0]=m;var l={};for(var s in t)hasOwnProperty.call(t,s)&&(l[s]=t[s]);l.originalType=e,l[u]="string"==typeof e?e:i,o[1]=l;for(var p=2;p<r;p++)o[p]=n[p];return a.createElement.apply(null,o)}return a.createElement.apply(null,n)}m.displayName="MDXCreateElement"},90806:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>s,contentTitle:()=>o,default:()=>d,frontMatter:()=>r,metadata:()=>l,toc:()=>p});var a=n(87462),i=(n(67294),n(3905));const r={title:"Writing unit tests",description:"Overview to Unit Testing",tags:["Engineering"]},o=void 0,l={unversionedId:"engineering/unit-testing",id:"engineering/unit-testing",title:"Writing unit tests",description:"Overview to Unit Testing",source:"@site/docs/150.engineering/040.unit-testing.md",sourceDirName:"150.engineering",slug:"/engineering/unit-testing",permalink:"/engineering/unit-testing",draft:!1,editUrl:"https://github.com/nocodb/nocodb/tree/develop/packages/noco-docs/docs/docs/150.engineering/040.unit-testing.md",tags:[{label:"Engineering",permalink:"/tags/engineering"}],version:"current",sidebarPosition:40,frontMatter:{title:"Writing unit tests",description:"Overview to Unit Testing",tags:["Engineering"]},sidebar:"tutorialSidebar",previous:{title:"Development setup",permalink:"/engineering/development-setup"},next:{title:"Playwright E2E testing",permalink:"/engineering/playwright"}},s={},p=[{value:"Unit Tests",id:"unit-tests",level:2},{value:"Pre-requisites",id:"pre-requisites",level:3},{value:"Setup",id:"setup",level:3},{value:"Run Tests",id:"run-tests",level:3},{value:"Folder Structure",id:"folder-structure",level:3},{value:"Factory Pattern",id:"factory-pattern",level:3},{value:"Walk through of writing a Unit Test",id:"walk-through-of-writing-a-unit-test",level:3},{value:"Configure test",id:"configure-test",level:4},{value:"Test case",id:"test-case",level:4},{value:"Integrating the New Test Suite",id:"integrating-the-new-test-suite",level:4},{value:"Seeding Sample DB (Sakila)",id:"seeding-sample-db-sakila",level:3}],c={toc:p},u="wrapper";function d(e){let{components:t,...n}=e;return(0,i.kt)(u,(0,a.Z)({},c,n,{components:t,mdxType:"MDXLayout"}),(0,i.kt)("h2",{id:"unit-tests"},"Unit Tests"),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},"All individual unit tests are independent of each other. We don't use any shared state between tests."),(0,i.kt)("li",{parentName:"ul"},"Test environment includes ",(0,i.kt)("inlineCode",{parentName:"li"},"sakila")," sample database and any change to it by a test is reverted before running other tests."),(0,i.kt)("li",{parentName:"ul"},"While running unit tests, it tries to connect to mysql server running on ",(0,i.kt)("inlineCode",{parentName:"li"},"localhost:3306")," with username ",(0,i.kt)("inlineCode",{parentName:"li"},"root")," and password ",(0,i.kt)("inlineCode",{parentName:"li"},"password")," (which can be configured) and if not found, it will use ",(0,i.kt)("inlineCode",{parentName:"li"},"sqlite")," as a fallback, hence no requirement of any sql server to run tests.")),(0,i.kt)("h3",{id:"pre-requisites"},"Pre-requisites"),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},"MySQL is preferred - however tests can fallback on SQLite too")),(0,i.kt)("h3",{id:"setup"},"Setup"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-bash"},"pnpm --filter=-nocodb install\n\n# add a .env file\ncp tests/unit/.env.sample tests/unit/.env\n\n# open .env file\nopen tests/unit/.env\n")),(0,i.kt)("p",null,"Configure the following variables"),(0,i.kt)("blockquote",null,(0,i.kt)("p",{parentName:"blockquote"},"DB_HOST : host\nDB_PORT : port\nDB_USER : username\nDB_PASSWORD : password")),(0,i.kt)("h3",{id:"run-tests"},"Run Tests"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-bash"},"pnpm run test:unit\n")),(0,i.kt)("h3",{id:"folder-structure"},"Folder Structure"),(0,i.kt)("p",null,"The root folder for unit tests is ",(0,i.kt)("inlineCode",{parentName:"p"},"packages/nocodb/tests/unit")),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"rest")," folder contains all the test suites for rest apis."),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"model")," folder contains all the test suites for models."),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"factory")," folder contains all the helper functions to create test data."),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"init")," folder contains helper functions to configure test environment."),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"index.test.ts")," is the root test suite file which imports all the test suites."),(0,i.kt)("li",{parentName:"ul"},(0,i.kt)("inlineCode",{parentName:"li"},"TestDbMngr.ts")," is a helper class to manage test databases (i.e. creating, dropping, etc.).")),(0,i.kt)("h3",{id:"factory-pattern"},"Factory Pattern"),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},"Use factories for create/update/delete data. No data should be directly create/updated/deleted in the test."),(0,i.kt)("li",{parentName:"ul"},"While writing a factory make sure that it can be used with as less parameters as possible and use default values for other parameters."),(0,i.kt)("li",{parentName:"ul"},"Use named parameters for factories.",(0,i.kt)("pre",{parentName:"li"},(0,i.kt)("code",{parentName:"pre",className:"language-ts"},"createUser({ email, password})\n"))),(0,i.kt)("li",{parentName:"ul"},"Use one file per factory.")),(0,i.kt)("h3",{id:"walk-through-of-writing-a-unit-test"},"Walk through of writing a Unit Test"),(0,i.kt)("p",null,"We will create an ",(0,i.kt)("inlineCode",{parentName:"p"},"Table")," test suite as an example."),(0,i.kt)("h4",{id:"configure-test"},"Configure test"),(0,i.kt)("p",null,"We will configure ",(0,i.kt)("inlineCode",{parentName:"p"},"beforeEach")," which is called before each test is executed. We will use ",(0,i.kt)("inlineCode",{parentName:"p"},"init")," function from ",(0,i.kt)("inlineCode",{parentName:"p"},"nocodb/packages/nocodb/tests/unit/init/index.ts"),", which is a helper function which configures the test environment(i.e resetting state, etc.)."),(0,i.kt)("p",null,(0,i.kt)("inlineCode",{parentName:"p"},"init")," does the following things -"),(0,i.kt)("ul",null,(0,i.kt)("li",{parentName:"ul"},"It initializes a ",(0,i.kt)("inlineCode",{parentName:"li"},"Noco")," instance(reused in all tests)."),(0,i.kt)("li",{parentName:"ul"},"Restores ",(0,i.kt)("inlineCode",{parentName:"li"},"meta")," and ",(0,i.kt)("inlineCode",{parentName:"li"},"sakila")," database to its initial state."),(0,i.kt)("li",{parentName:"ul"},"Creates the root user."),(0,i.kt)("li",{parentName:"ul"},"Returns ",(0,i.kt)("inlineCode",{parentName:"li"},"context")," which has ",(0,i.kt)("inlineCode",{parentName:"li"},"auth token")," for the created user, node server instance(",(0,i.kt)("inlineCode",{parentName:"li"},"app"),"), and ",(0,i.kt)("inlineCode",{parentName:"li"},"dbConfig"),".")),(0,i.kt)("p",null,"We will use ",(0,i.kt)("inlineCode",{parentName:"p"},"createProject")," and ",(0,i.kt)("inlineCode",{parentName:"p"},"createProject")," factories to create a project and a table."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-typescript"},"let context;\n\nbeforeEach(async function () {\n context = await init();\n\n project = await createProject(context);\n table = await createTable(context, project);\n});\n")),(0,i.kt)("h4",{id:"test-case"},"Test case"),(0,i.kt)("p",null,"We will use ",(0,i.kt)("inlineCode",{parentName:"p"},"it")," function to create a test case. We will use ",(0,i.kt)("inlineCode",{parentName:"p"},"supertest")," to make a request to the server. We use ",(0,i.kt)("inlineCode",{parentName:"p"},"expect"),"(",(0,i.kt)("inlineCode",{parentName:"p"},"chai"),") to assert the response."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-typescript"},"it('Get table list', async function () {\n const response = await request(context.app)\n .get(`/api/v1/db/meta/projects/${project.id}/tables`)\n .set('xc-auth', context.token)\n .send({})\n .expect(200);\n\n expect(response.body.list).to.be.an('array').not.empty;\n});\n")),(0,i.kt)("admonition",{type:"info"},(0,i.kt)("p",{parentName:"admonition"},"We can also run individual test by using ",(0,i.kt)("inlineCode",{parentName:"p"},".only")," in ",(0,i.kt)("inlineCode",{parentName:"p"},"describe")," or ",(0,i.kt)("inlineCode",{parentName:"p"},"it")," function and the running the test command.")),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-typescript"},"it.only('Get table list', async () => {\n")),(0,i.kt)("h4",{id:"integrating-the-new-test-suite"},"Integrating the New Test Suite"),(0,i.kt)("p",null,"We create a new file ",(0,i.kt)("inlineCode",{parentName:"p"},"table.test.ts")," in ",(0,i.kt)("inlineCode",{parentName:"p"},"packages/nocodb/tests/unit/rest/tests")," directory."),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-typescript"},"import 'mocha';\nimport request from 'supertest';\nimport init from '../../init';\nimport { createTable, getAllTables } from '../../factory/table';\nimport { createProject } from '../../factory/project';\nimport { defaultColumns } from '../../factory/column';\nimport Model from '../../../../src/lib/models/Model';\nimport { expect } from 'chai';\n\nfunction tableTest() {\n let context;\n let project;\n let table;\n\n beforeEach(async function () {\n context = await init();\n\n project = await createProject(context);\n table = await createTable(context, project);\n });\n\n it('Get table list', async function () {\n const response = await request(context.app)\n .get(`/api/v1/db/meta/projects/${project.id}/tables`)\n .set('xc-auth', context.token)\n .send({})\n .expect(200);\n\n expect(response.body.list).to.be.an('array').not.empty;\n });\n}\n\nexport default function () {\n describe('Table', tableTests);\n}\n")),(0,i.kt)("p",null,"We can then import the ",(0,i.kt)("inlineCode",{parentName:"p"},"Table")," test suite to ",(0,i.kt)("inlineCode",{parentName:"p"},"Rest")," test suite in ",(0,i.kt)("inlineCode",{parentName:"p"},"packages/nocodb/tests/unit/rest/index.test.ts")," file(",(0,i.kt)("inlineCode",{parentName:"p"},"Rest")," test suite is imported in the root test suite file which is ",(0,i.kt)("inlineCode",{parentName:"p"},"packages/nocodb/tests/unit/index.test.ts"),")."),(0,i.kt)("h3",{id:"seeding-sample-db-sakila"},"Seeding Sample DB (Sakila)"),(0,i.kt)("pre",null,(0,i.kt)("code",{parentName:"pre",className:"language-typescript"},"\nfunction tableTest() {\n let context;\n let sakilaProject: Project;\n let customerTable: Model;\n\n beforeEach(async function () {\n context = await init();\n \n /******* Start : Seeding sample database **********/\n sakilaProject = await createSakilaProject(context);\n /******* End : Seeding sample database **********/\n \n customerTable = await getTable({project: sakilaProject, name: 'customer'})\n });\n\n it('Get table data list', async function () {\n const response = await request(context.app)\n .get(`/api/v1/db/data/noco/${sakilaProject.id}/${customerTable.id}`)\n .set('xc-auth', context.token)\n .send({})\n .expect(200);\n\n expect(response.body.list[0]['FirstName']).to.equal('MARY');\n });\n}\n")))}d.isMDXComponent=!0}}]);