Best JavaScript code snippet using fast-check-monorepo
index.spec.ts
Source:index.spec.ts
1import { BadRequestException, NotFoundException } from '@nestjs/common'2import { Connection, createConnection } from 'typeorm'3import { config } from '@mangroves/jest-docker-mysql/lib/config'4import { JamcaaHelper } from '../../src'5import { TestEntity } from '../testing-module/test.entity'6jest.mock('@nestjs/common')7jest.unmock('typeorm')8const NOT_FOUND_EXCEPTION_MESSAGE = `${TestEntity.name} not found!`9const ALREADY_EXISTS_EXCEPTION_MESSAGE = `${TestEntity.name} already exists!`10const NOTHING_UPDATED_EXCEPTION_MESSAGE = 'Nothing updated!'11let connection: Connection12beforeAll(async () => {13 connection = await createConnection({14 type: 'mysql',15 host: config.db.host,16 port: config.db.port,17 username: config.db.user,18 password: config.db.pass,19 database: config.db.dbName,20 entities: [TestEntity],21 })22})23afterAll(async () => {24 await connection.query('DROP TABLE IF EXISTS test;')25 await connection.close()26})27describe('Default options [sql: default_options]', () => {28 let helper: JamcaaHelper<TestEntity, 'firstName' | 'lastName'>29 const operator = 'operator'30 const uniqueKeyConditions = {31 firstName: Math.random().toString(36),32 lastName: Math.random().toString(36),33 }34 beforeEach(() => {35 // Each test case uses a new database36 helper = new JamcaaHelper(TestEntity, ['firstName', 'lastName'])37 })38 describe('createInsertQuery', () => {39 it('insert a new record', async () => {40 const savedEntity = await helper.createInsertQuery(uniqueKeyConditions, operator)41 42 expect(savedEntity.id).toBeDefined()43 expect(savedEntity).toMatchObject<Partial<TestEntity>>({44 firstName: uniqueKeyConditions.firstName,45 lastName: uniqueKeyConditions.lastName,46 dataVersion: '1',47 deleteStatus: 0,48 creator: operator,49 updater: operator,50 })51 expect(savedEntity.createTime).toMatch(/^\d+$/)52 expect(savedEntity.createTime).toBe(savedEntity.updateTime)53 })54 55 it('insert an existing record', async () => {56 // Insert record first57 await helper.createInsertQuery(uniqueKeyConditions, operator)58 await expect(helper.createInsertQuery(uniqueKeyConditions, operator)).rejects.toThrow(new BadRequestException(ALREADY_EXISTS_EXCEPTION_MESSAGE))59 })60 61 it('reuse soft deleted data', async () => {62 const { id: softDeletedId } = await helper.createInsertQuery(uniqueKeyConditions, operator)63 await helper.createDeleteQuery(uniqueKeyConditions, operator)64 const savedEntity = await helper.createInsertQuery(uniqueKeyConditions, operator)65 66 expect(savedEntity.id).toBe(softDeletedId)67 })68 })69 describe('createListQuery', () => {70 it('list correct entities', async () => {71 const toBeInserted = [72 {73 firstName: 'Charlie',74 lastName: 'Brown',75 },76 {77 firstName: 'Snoppy',78 lastName: 'Dog',79 },80 {81 firstName: 'Charlie',82 lastName: 'Gray',83 },84 {85 firstName: 'Woodstock',86 lastName: 'Bird',87 },88 {89 firstName: 'Charlie',90 lastName: 'Green',91 },92 ]93 for (let i = 0; i < toBeInserted.length; i++) {94 await helper.createInsertQuery(toBeInserted[i], operator)95 }96 const [entities, count] = await helper.createListQuery()97 .filter((fq) => {98 fq.equals('firstName', 'Charlie')99 })100 .paginationQuery(1, 2)101 .getQueryBuilder()102 .getManyAndCount()103 104 expect(count).toBe(3)105 expect(Array.isArray(entities)).toBe(true)106 expect(entities.length).toBe(2)107 expect(entities[0]).toMatchObject(toBeInserted[0])108 expect(entities[1]).toMatchObject(toBeInserted[2])109 })110 it('list entities with max page size', async () => {111 const toBeInserted = [112 {113 firstName: 'Charlie',114 lastName: 'Brown',115 },116 {117 firstName: 'Snoppy',118 lastName: 'Dog',119 },120 {121 firstName: 'Charlie',122 lastName: 'Gray',123 },124 {125 firstName: 'Woodstock',126 lastName: 'Bird',127 },128 {129 firstName: 'Charlie',130 lastName: 'Green',131 },132 ]133 const helperWithLimit = new JamcaaHelper(TestEntity, ['firstName', 'lastName'], { maxUnspecifiedPageSize: 3 })134 for (let i = 0; i < toBeInserted.length; i++) {135 await helper.createInsertQuery(toBeInserted[i], operator)136 }137 const [entities, count] = await helper.createListQuery()138 .getQueryBuilder()139 .getManyAndCount()140 const [entitiesWithLimit, countWithLimit] = await helperWithLimit.createListQuery()141 .getQueryBuilder()142 .getManyAndCount()143 144 expect(count).toBe(countWithLimit)145 expect(count).toBe(5)146 expect(entities.length).toBe(5)147 expect(entitiesWithLimit.length).toBe(3)148 })149 })150 describe('createGetQuery', () => {151 it('not found', async () => {152 await expect(helper.createGetQuery(uniqueKeyConditions)).rejects.toThrow(new NotFoundException(NOT_FOUND_EXCEPTION_MESSAGE))153 })154 155 it('not found soft deleted record', async () => {156 await helper.createInsertQuery(uniqueKeyConditions, operator)157 await helper.createDeleteQuery(uniqueKeyConditions, operator)158 await expect(helper.createGetQuery(uniqueKeyConditions)).rejects.toThrow(new NotFoundException(NOT_FOUND_EXCEPTION_MESSAGE))159 })160 })161 describe('createUpdateQuery', () => {162 it('update updater and update time', async () => {163 await helper.createInsertQuery(uniqueKeyConditions, operator)164 const updateConditions = {165 firstName: Math.random().toString(36),166 lastName: Math.random().toString(36),167 }168 const mask = ['firstName', 'lastName']169 const allowedMask = mask170 const newOperator = 'new operator'171 const updatedEntity = await helper.createUpdateQuery(172 uniqueKeyConditions,173 updateConditions,174 mask,175 allowedMask,176 newOperator,177 )178 expect(updatedEntity.creator).not.toBe(updatedEntity.updater)179 expect(updatedEntity.updater).toBe(newOperator)180 expect(updatedEntity.firstName).toBe(updateConditions.firstName)181 expect(updatedEntity.lastName).toBe(updateConditions.lastName)182 expect(updatedEntity.dataVersion).toBe('2')183 })184 185 it('throw if not found', async () => {186 await helper.createInsertQuery(uniqueKeyConditions, operator)187 await helper.createDeleteQuery(uniqueKeyConditions, operator)188 const updateConditions = {189 firstName: Math.random().toString(36),190 lastName: Math.random().toString(36),191 }192 const mask = ['firstName', 'lastName']193 const allowedMask = mask194 await expect(helper.createUpdateQuery(195 uniqueKeyConditions,196 updateConditions,197 mask,198 allowedMask,199 operator,200 )).rejects.toThrow(new NotFoundException(NOT_FOUND_EXCEPTION_MESSAGE))201 })202 it('throw if update mask not allowed', async () => {203 await helper.createInsertQuery(uniqueKeyConditions, operator)204 const updateConditions = {205 firstName: Math.random().toString(36),206 lastName: Math.random().toString(36),207 }208 const mask = ['firstName', 'lastName']209 const allowedMask = ['firstName']210 await expect(helper.createUpdateQuery(211 uniqueKeyConditions,212 updateConditions,213 mask,214 allowedMask,215 operator,216 )).rejects.toThrow(new BadRequestException('lastName cannot be updated!'))217 })218 it('throw if data version error', async () => {219 await helper.createInsertQuery(uniqueKeyConditions, operator)220 const updateConditions = {221 firstName: Math.random().toString(36),222 lastName: Math.random().toString(36),223 dataVersion: '0',224 }225 const mask = ['firstName', 'lastName']226 const allowedMask = ['firstName', 'lastName']227 await expect(helper.createUpdateQuery(228 uniqueKeyConditions,229 updateConditions,230 mask,231 allowedMask,232 operator,233 )).rejects.toThrow(new BadRequestException('Data version error!'))234 })235 it('skip field when it is not in the partial entity but present in update mask', async () => {236 await helper.createInsertQuery(uniqueKeyConditions, operator)237 const updateConditions = {238 firstName: Math.random().toString(36),239 }240 const mask = ['firstName', 'lastName']241 const allowedMask = ['firstName', 'lastName']242 const updatedEntity = await helper.createUpdateQuery(243 uniqueKeyConditions,244 updateConditions,245 mask,246 allowedMask,247 operator,248 )249 expect(updatedEntity.firstName).toBe(updateConditions.firstName)250 expect(updatedEntity.lastName).toBeDefined()251 expect(updatedEntity.lastName).toBe(uniqueKeyConditions.lastName)252 })253 it('deep update', async () => {254 await helper.createInsertQuery({255 ...uniqueKeyConditions,256 personInfo: {257 age: 24,258 hobbies: {259 coffee: true,260 tea: true,261 },262 },263 }, operator)264 const updateConditions = {265 firstName: Math.random().toString(36),266 personInfo: {267 hobbies: {268 coffee: false,269 },270 },271 }272 const mask = ['firstName', 'lastName', 'personInfo.hobbies.coffee']273 const allowedMask = ['firstName', 'lastName', 'personInfo']274 const updatedEntity = await helper.createUpdateQuery(275 uniqueKeyConditions,276 updateConditions,277 mask,278 allowedMask,279 operator,280 )281 expect(updatedEntity.firstName).toBe(updateConditions.firstName)282 expect(updatedEntity.lastName).toBeDefined()283 expect(updatedEntity.lastName).toBe(uniqueKeyConditions.lastName)284 expect(updatedEntity.personInfo?.hobbies?.coffee).toBe(false)285 expect(updatedEntity.personInfo?.hobbies?.tea).toBe(true)286 })287 it('transform entity before updating', async () => {288 await helper.createInsertQuery({289 ...uniqueKeyConditions,290 personInfo: {291 age: 24,292 hobbies: {293 coffee: true,294 tea: true,295 },296 },297 }, operator)298 const updateConditions = {299 first_name: Math.random().toString(36),300 person_info: {301 hobbies: {302 coffee: false,303 },304 } as Record<string, any> | null,305 data_version: '1',306 }307 const mask = ['first_name', 'last_name', 'person_info.hobbies.coffee']308 const allowedMask = ['first_name', 'last_name', 'person_info']309 const updatedEntity = await helper.createUpdateQuery(310 uniqueKeyConditions,311 updateConditions,312 mask,313 allowedMask,314 operator,315 (entity) => ({316 first_name: entity.firstName,317 last_name: entity.lastName,318 person_info: entity.personInfo,319 data_version: entity.dataVersion,320 }),321 (dto) => ({322 firstName: dto.first_name,323 personInfo: dto.person_info,324 }),325 )326 expect(updatedEntity.firstName).toBe(updateConditions.first_name)327 expect(updatedEntity.lastName).toBeDefined()328 expect(updatedEntity.lastName).toBe(uniqueKeyConditions.lastName)329 expect(updatedEntity.personInfo?.hobbies?.coffee).toBe(false)330 expect(updatedEntity.personInfo?.hobbies?.tea).toBe(true)331 })332 it('throw if nothing updated', async () => {333 await helper.createInsertQuery({334 ...uniqueKeyConditions,335 personInfo: {336 age: 24,337 hobbies: {338 coffee: true,339 tea: true,340 },341 },342 }, operator)343 const updateConditions = {344 firstName: uniqueKeyConditions.firstName,345 personInfo: {346 hobbies: {347 coffee: true,348 },349 },350 }351 const mask = ['firstName', 'lastName', 'personInfo.hobbies.coffee']352 const allowedMask = ['firstName', 'lastName', 'personInfo']353 await expect(helper.createUpdateQuery(354 uniqueKeyConditions,355 updateConditions,356 mask,357 allowedMask,358 operator,359 )).rejects.toThrow(new BadRequestException(NOTHING_UPDATED_EXCEPTION_MESSAGE))360 })361 it('increase data version correctly if it is not transformed by transformFromEntity and transformToEntity', async () => {362 await helper.createInsertQuery(uniqueKeyConditions, operator)363 const mask = ['person_info']364 const allowedMask = mask365 const newOperator = 'new operator'366 let updatedEntity367 for (let i = 0; i < 5; i++) {368 updatedEntity = await helper.createUpdateQuery(369 uniqueKeyConditions,370 {371 person_info: {},372 },373 mask,374 allowedMask,375 newOperator,376 (entity) => ({ person_info: entity.personInfo }),377 (dto) => ({ personInfo: dto.person_info }),378 )379 }380 expect(updatedEntity?.dataVersion).toBe('6')381 })382 })383 describe('createDeleteQuery', () => {384 it('soft delete', async () => {385 await helper.createInsertQuery(uniqueKeyConditions, operator)386 await helper.createDeleteQuery(uniqueKeyConditions, operator)387 const deletedEntity = await helper.createGetQuery(uniqueKeyConditions, true)388 expect(deletedEntity).toBeDefined()389 expect(deletedEntity.deleteStatus).toBe(1)390 })391 })...
suffix-trie.ts
Source:suffix-trie.ts
1import {2 fastPathLookup,3 IPublicSuffix,4 ISuffixLookupOptions,5} from 'tldts-core';6import { exceptions, ITrie, rules } from './data/trie';7// Flags used to know if a rule is ICANN or Private8const enum RULE_TYPE {9 ICANN = 1,10 PRIVATE = 2,11}12interface IMatch {13 index: number;14 isIcann: boolean;15 isPrivate: boolean;16}17/**18 * Lookup parts of domain in Trie19 */20function lookupInTrie(21 parts: string[],22 trie: ITrie,23 index: number,24 allowedMask: number,25): IMatch | null {26 let result: IMatch | null = null;27 let node: ITrie | undefined = trie;28 while (node !== undefined) {29 // We have a match!30 if ((node.$ & allowedMask) !== 0) {31 result = {32 index: index + 1,33 isIcann: node.$ === RULE_TYPE.ICANN,34 isPrivate: node.$ === RULE_TYPE.PRIVATE,35 };36 }37 // No more `parts` to look for38 if (index === -1) {39 break;40 }41 const succ: {42 [label: string]: ITrie;43 } = node.succ;44 node = succ && (succ[parts[index]] || succ['*']);45 index -= 1;46 }47 return result;48}49/**50 * Check if `hostname` has a valid public suffix in `trie`.51 */52export default function suffixLookup(53 hostname: string,54 options: ISuffixLookupOptions,55 out: IPublicSuffix,56): void {57 if (fastPathLookup(hostname, options, out) === true) {58 return;59 }60 const hostnameParts = hostname.split('.');61 const allowedMask =62 (options.allowPrivateDomains === true ? RULE_TYPE.PRIVATE : 0) |63 (options.allowIcannDomains === true ? RULE_TYPE.ICANN : 0);64 // Look for exceptions65 const exceptionMatch = lookupInTrie(66 hostnameParts,67 exceptions,68 hostnameParts.length - 1,69 allowedMask,70 );71 if (exceptionMatch !== null) {72 out.isIcann = exceptionMatch.isIcann;73 out.isPrivate = exceptionMatch.isPrivate;74 out.publicSuffix = hostnameParts.slice(exceptionMatch.index + 1).join('.');75 return;76 }77 // Look for a match in rules78 const rulesMatch = lookupInTrie(79 hostnameParts,80 rules,81 hostnameParts.length - 1,82 allowedMask,83 );84 if (rulesMatch !== null) {85 out.isIcann = rulesMatch.isIcann;86 out.isPrivate = rulesMatch.isPrivate;87 out.publicSuffix = hostnameParts.slice(rulesMatch.index).join('.');88 return;89 }90 // No match found...91 // Prevailing rule is '*' so we consider the top-level domain to be the92 // public suffix of `hostname` (e.g.: 'example.org' => 'org').93 out.isIcann = false;94 out.isPrivate = false;95 out.publicSuffix = hostnameParts[hostnameParts.length - 1];...
Using AI Code Generation
1import { allowedMask } from 'fast-check';2import { allowedMask } from 'fast-check';3import { allowedMask } from 'fast-check';4import { allowedMask } from 'fast-check';5import { allowedMask } from 'fast-check';6import { allowedMask } from 'fast-check';7import { allowedMask } from 'fast-check';8import { allowedMask } from 'fast-check';9import { allowedMask } from 'fast-check';10import { allowedMask } from 'fast-check';11import { allowedMask } from 'fast-check';12import { allowedMask } from 'fast-check';13import { allowedMask } from 'fast-check';14import { allowedMask } from 'fast-check';15import { allowedMask } from 'fast-check';16import { allowedMask } from 'fast-check';17import { allowedMask } from 'fast-check';18import { allowedMask } from 'fast-check';19import { allowed
Using AI Code Generation
1import { allowedMask } from 'fast-check';2console.log(allowedMask());3import { allowedMask } from 'fast-check';4console.log(allowedMask());5import { allowedMask } from 'fast-check';6console.log(allowedMask());7import { allowedMask } from 'fast-check';8console.log(allowedMask());9import { allowedMask } from 'fast-check';10console.log(allowedMask());11import { allowedMask } from 'fast-check';12console.log(allowedMask());13import { allowedMask } from 'fast-check';14console.log(allowedMask());15import { allowedMask } from 'fast-check';16console.log(allowedMask());17import { allowedMask } from 'fast-check';18console.log(allowedMask());19import { allowedMask } from 'fast-check';20console.log(allowedMask());
Using AI Code Generation
1const { allowedMask } = require("fast-check");2const mask = allowedMask(5);3console.log(mask);4console.log(mask(5));5console.log(mask(6));6console.log(mask(7));7console.log(mask(8));8console.log(mask(9));9console.log(mask(10));10console.log(mask(11));11console.log(mask(12));12console.log(mask(13));13console.log(mask(14));14console.log(mask(15));15console.log(mask(16));16console.log(mask(17));17console.log(mask(18));18console.log(mask(19));19console.log(mask(20));20console.log(mask(21));21console.log(mask(22));22console.log(mask(23));23console.log(mask(24));24console.log(mask(25));25console.log(mask(26));26console.log(mask(27));27console.log(mask(28));28console.log(mask(29));29console.log(mask(30));30console.log(mask(31));31console.log(mask(32));32console.log(mask(33));33console.log(mask(34));34console.log(mask(35));35console.log(mask(36));36console.log(mask(37));37console.log(mask(38));38console.log(mask(39));39console.log(mask(40));40console.log(mask(41));41console.log(mask(42));42console.log(mask(43));43console.log(mask(44));44console.log(mask(45));45console.log(mask(46));46console.log(mask(47));47console.log(mask(48));48console.log(mask(49));49console.log(mask(50));50console.log(mask(51));51console.log(mask(52));52console.log(mask(53));53console.log(mask(54));54console.log(mask(55));55console.log(mask(56));56console.log(mask(57));57console.log(mask(58));58console.log(mask(59));59console.log(mask(60));60console.log(mask(61));61console.log(mask(62));62console.log(mask(63));63console.log(mask(64));64console.log(mask(65));65console.log(mask(66));66console.log(mask(67));67console.log(mask(68));68console.log(mask(69));69console.log(mask(70));70console.log(mask(71));71console.log(mask(72));72console.log(mask(73));73console.log(mask(74));74console.log(mask(75));75console.log(mask(76));76console.log(mask(77));77console.log(mask(78));78console.log(mask(79));79console.log(mask(80));80console.log(mask(81));81console.log(mask(82));82console.log(mask(83));
Using AI Code Generation
1const fc = require('fast-check');2const allowedMask = require('fast-check-monorepo').allowedMask;3const mask = allowedMask([4]);5fc.assert(fc.property(fc.string(), (s) => {6 return mask(s).split('').every((c) => c === ' ');7}));8const fc = require('fast-check');9const allowedMask = require('fast-check-monorepo').allowedMask;10const mask = allowedMask([11]);
Using AI Code Generation
1const fc = require('fast-check');2const allowedMask = require('fast-check-monorepo').allowedMask;3console.log(allowedMask(fc.nat(), fc.nat()));4const fc = require('fast-check');5const allowedMask = require('fast-check-monorepo').allowedMask;6console.log(allowedMask(fc.nat(), fc.nat()));7const fc = require('fast-check');8const allowedMask = require('fast-check-monorepo').allowedMask;9console.log(allowedMask(fc.nat(), fc.nat()));10const fc = require('fast-check');11const allowedMask = require('fast-check-monorepo').allowedMask;12console.log(allowedMask(fc.nat(), fc.nat()));13const fc = require('fast-check');14const allowedMask = require('fast-check-monorepo').allowedMask;15console.log(allowedMask(fc.nat(), fc.nat()));16const fc = require('fast-check');17const allowedMask = require('fast-check-monorepo').allowedMask;18console.log(allowedMask(fc.nat(), fc.nat()));19const fc = require('fast-check');20const allowedMask = require('fast-check-monorepo').allowedMask;21console.log(allowedMask(fc.nat(), fc.nat()));22const fc = require('fast-check');23const allowedMask = require('fast-check-monorepo').allowedMask;24console.log(allowedMask(fc.nat(), fc.nat()));25const fc = require('fast-check');26const allowedMask = require('fast-check-monorepo').allowedMask;27console.log(allowedMask(fc.nat(), fc.nat()));
Using AI Code Generation
1const { allowedMask } = require('fast-check');2const mask = allowedMask(/[a-z]/, 3);3const result = mask('abc');4assert.equal(result, '___');5assert.equal(mask('aBc'), '___');6assert.equal(mask('aBc123'), '___');7assert.equal(mask('123'), '___');8assert.equal(mask('12'), '12');9assert.equal(mask('1'), '1');10const { allowedMask } = require('fast-check');11const mask = allowedMask(/[a-z]/, 3);12const result = mask('abc');13assert.equal(result, '___');14assert.equal(mask('aBc'), '___');15assert.equal(mask('aBc123'), '___');16assert.equal(mask('123'), '___');17assert.equal(mask('12'), '12');18assert.equal(mask('1'), '1');19const { allowedMask } = require('fast-check');20const mask = allowedMask(/[a-z]/, 3);21const result = mask('abc');22assert.equal(result, '___');23assert.equal(mask('aBc'), '___');24assert.equal(mask('aBc123'), '___');25assert.equal(mask('123'), '___');26assert.equal(mask('12'), '12');27assert.equal(mask('1'), '1');28const { allowedMask } = require('fast-check');29const mask = allowedMask(/[a-z]/, 3);30const result = mask('abc');31assert.equal(result, '___');32assert.equal(mask('aBc'), '___');33assert.equal(mask('aBc123'), '___');34assert.equal(mask('123'), '___');35assert.equal(mask('12'), '12');36assert.equal(mask('1'), '1');37const { allowedMask } = require('fast-check');38const mask = allowedMask(/[a-z]/, 3);39const result = mask('abc');40assert.equal(result, '___');41assert.equal(mask('aBc'), '___');42assert.equal(mask('aBc123'), '___');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!