diff --git a/test/asset/find-result-wrapper.js b/test/asset/find-result-wrapper.js deleted file mode 100755 index ff172d1a..00000000 --- a/test/asset/find-result-wrapper.js +++ /dev/null @@ -1,1114 +0,0 @@ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('../config.js'); -const Utils = require('../entry/utils.js'); - -let Stack; - -describe('Contentstack Asset Tests', () => { - // Initialize the Contentstack Stack Instance - beforeAll(() => { - return new Promise((resolve) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(resolve, 1000); - }); - }); - - describe('default .find() No fallback', () => { - const _in = ['ja-jp']; - let assets; - - // Setup - run the query once for all tests - beforeAll(async () => { - try { - assets = await Stack.Assets().Query().language('ja-jp').toJSON().find(); - } catch (error) { - console.error('Error in beforeAll:', error); - throw error; - } - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should not include count when not requested', async () => { - expect(assets[1]).toBeFalsy(); - }); - - test('should return assets only in the requested locale', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsInRequestedLocale = assets[0].every((asset) => { - return _in.indexOf(asset.publish_details.locale) !== -1; - }); - expect(allAssetsInRequestedLocale).toBe(true); - } else { - // Skip this test if no assets are returned - console.warn('No assets returned to verify locale'); - } - }); - - test('should have the correct structure for each asset', async () => { - if (assets && assets.length && assets[0].length) { - const firstAsset = assets[0][0]; - expect(firstAsset).toHaveProperty('uid'); - expect(firstAsset).toHaveProperty('title'); - expect(firstAsset).toHaveProperty('publish_details'); - expect(firstAsset.publish_details).toHaveProperty('locale'); - expect(firstAsset.publish_details.locale).toBe('ja-jp'); - } else { - // Skip this test if no assets are returned - console.warn('No assets returned to verify structure'); - } - }); - }); - - describe('default .find() with fallback', () => { - const _in = ['ja-jp', 'en-us']; - let assets; - - // Setup - run the query once for all tests - beforeAll(async () => { - try { - assets = await Stack.Assets() - .Query() - .language('ja-jp') - .includeFallback() - .toJSON() - .find(); - } catch (error) { - console.error('Error in beforeAll:', error); - throw error; - } - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should not include count when not requested', async () => { - expect(assets[1]).toBeFalsy(); - }); - - test('should return assets from both primary and fallback locales', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsInAllowedLocales = assets[0].every((asset) => { - return _in.indexOf(asset.publish_details.locale) !== -1; - }); - expect(allAssetsInAllowedLocales).toBe(true); - } else { - // Skip this test if no assets are returned - console.warn('No assets returned to verify locales with fallback'); - } - }); - - test('should include some assets in primary locale', async () => { - if (assets && assets.length && assets[0].length) { - const anyAssetsInPrimaryLocale = assets[0].some((asset) => { - return asset.publish_details.locale === 'ja-jp'; - }); - expect(anyAssetsInPrimaryLocale).toBe(true); - } else { - console.warn('No assets returned to verify primary locale presence'); - } - }); - - test('should have the correct structure for each asset', async () => { - if (assets && assets.length && assets[0].length) { - const firstAsset = assets[0][0]; - expect(firstAsset).toHaveProperty('uid'); - expect(firstAsset).toHaveProperty('title'); - expect(firstAsset).toHaveProperty('publish_details'); - expect(firstAsset.publish_details).toHaveProperty('locale'); - expect( - ['ja-jp', 'en-us'].includes(firstAsset.publish_details.locale) - ).toBe(true); - } else { - console.warn('No assets returned to verify structure'); - } - }); - }); - - describe('default .find()', () => { - let assets; - const field = 'updated_at'; - - // Setup - run the query once for all tests - beforeAll(async () => { - try { - const Query = Stack.Assets().Query(); - assets = await Query.toJSON().find(); - } catch (error) { - console.error('Error in beforeAll:', error); - throw error; - } - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should not include count when not requested', async () => { - expect(assets[1]).toBeFalsy(); - }); - - test('should return assets sorted by updated_at by default in descending order', async () => { - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const allAssetsSorted = assets[0].every((asset) => { - const isSorted = asset[field] <= prev; - prev = asset[field]; - return isSorted; - }); - expect(allAssetsSorted).toBe(true); - } else { - console.warn('No assets returned to verify sorting'); - } - }); - }); - - describe('sorting', () => { - test('.ascending()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - try { - const assets = await Query.ascending(field).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - prev = asset[field]; - return asset[field] >= prev; - }); - expect(_assets).toBe(true); - } - } catch (err) { - console.error('Error:', err); - fail('.ascending()'); - } - }); - - test('.descending()', async () => { - const Query = Stack.Assets().Query(); - const field = 'created_at'; - try { - const assets = await Query.descending(field).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - } catch (err) { - console.error('Error:', err); - fail('.descending()'); - } - }); - }); - - test('.addParam()', async () => { - const Query = Stack.Assets().Query(); - - try { - const assets = await Query.addParam('include_dimension', 'true') - .toJSON() - .find(); - expect(assets[0][0].hasOwnProperty('dimension')).toBeTruthy(); - } catch (err) { - console.error('Error:', err); - fail('.addParam()'); - } - }); - - describe('comparison', () => { - describe('.lessThan()', () => { - const field = 'file_size'; - const value = 5122; - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.lessThan(field, value).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets with file_size less than the specified value', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every( - (asset) => asset[field] < value - ); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn('No assets returned to verify lessThan condition'); - } - }); - }); - - describe('.lessThanOrEqualTo()', () => { - const field = 'file_size'; - const value = 5122; - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.lessThanOrEqualTo(field, value).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets with file_size less than or equal to the specified value', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every( - (asset) => asset[field] <= value - ); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn( - 'No assets returned to verify lessThanOrEqualTo condition' - ); - } - }); - }); - - test('.greaterThan()', async () => { - const Query = Stack.Assets().Query(); - const field = 'file_size'; - const value = 5122; - try { - const assets = await Query.greaterThan('file_size', value) - .ascending(field) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].slice(1).every((asset) => { - const flag = asset[field] > value; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - } catch (err) { - fail('.greaterThan()'); - } - }); - - test('.greaterThanOrEqualTo()', async () => { - const Query = Stack.Assets().Query(); - const field = 'file_size'; - const value = 5122; - try { - const assets = await Query.greaterThanOrEqualTo('file_size', 5122) - .descending(field) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] >= value; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - } catch (err) { - console.error('Error:', err); - fail('.greaterThanOrEqualTo()'); - } - }); - - describe('.notEqualTo()', () => { - const field = 'file_size'; - const value = 5122; - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.notEqualTo(field, value) - .descending(field) - .toJSON() - .find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets with file_size not equal to the specified value', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every( - (asset) => asset[field] !== value - ); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn('No assets returned to verify notEqualTo condition'); - } - }); - }); - - describe('.where()', () => { - const title = 'image1'; - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.where('title', title).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return exactly one asset matching the title', async () => { - expect(assets[0].length).toBe(1); - }); - - test('should return only assets with the specified title', async () => { - if (assets && assets.length && assets[0].length) { - const matchingTitle = assets[0].every( - (asset) => asset.title === title - ); - expect(matchingTitle).toBe(true); - } else { - console.warn('No assets returned to verify where condition'); - } - }); - }); - - describe('.equalTo() with boolean values', () => { - describe('when comparing with false', () => { - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.language('en-us') - .equalTo('is_dir', false) - .toJSON() - .find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return exactly 5 assets matching the condition', async () => { - expect(assets[0].length).toBe(5); - }); - - test('should return only assets with is_dir set to false', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every( - (asset) => asset.is_dir === false - ); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn('No assets returned to verify equalTo condition'); - } - }); - }); - - describe('when comparing with true', () => { - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.equalTo('is_dir', true).toJSON().find(); - }); - - test('should return an empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBe(0); - }); - }); - }); - }); - - describe('Array/Subset Tests', () => { - describe('.containedIn()', () => { - const _in = ['image1', 'image2']; - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.containedIn('title', _in).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets with titles contained in the specified array', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every((asset) => { - return _in.indexOf(asset.title) !== -1; - }); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn('No assets returned to verify containedIn condition'); - } - }); - - test('should include at least one asset with each of the specified titles', async () => { - if (assets && assets.length && assets[0].length) { - // Check if at least one asset exists for each title in the array - const foundTitles = _in.filter((title) => - assets[0].some((asset) => asset.title === title) - ); - expect(foundTitles.length).toBe(_in.length); - } else { - console.warn('No assets returned to verify all titles are present'); - } - }); - }); - - describe('.notContainedIn()', () => { - const _in = ['image1', 'image2']; - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.notContainedIn('title', _in).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets with titles not contained in the specified array', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every((asset) => { - return _in.indexOf(asset.title) === -1; - }); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn('No assets returned to verify notContainedIn condition'); - } - }); - - test('should not include any assets with the specified titles', async () => { - if (assets && assets.length && assets[0].length) { - const foundForbiddenTitles = assets[0].filter((asset) => - _in.includes(asset.title) - ); - expect(foundForbiddenTitles.length).toBe(0); - } else { - console.warn('No assets returned to verify excluded titles'); - } - }); - }); - }); - - describe('Element Existence Tests', () => { - test('.exists()', async () => { - const Query = Stack.Assets().Query(); - const queryField = 'is_dir'; - const field = 'updated_at'; - try { - const assets = await Query.exists(queryField).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - } catch (err) { - console.error('Error:', err); - fail('.exists()'); - } - }); - - test('.notExists()', async () => { - const Query = Stack.Assets().Query(); - const queryField = 'is_dir'; - const field = 'updated_at'; - try { - const assets = await Query.notExists(queryField).toJSON().find(); - - expect(assets[0].length).toBeFalsy(); - - if (assets && assets.length && assets[0].length) { - const prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - return asset[field] <= prev; - }); - expect(_assets).toBe(true); - } - } catch (err) { - console.error('Error:', err); - fail('.notExists()'); - } - }); - }); - - describe('Pagination Tests', () => { - test('.skip()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - try { - const allassets = await Query.toJSON().find(); - const assets = await Stack.Assets().Query().skip(1).toJSON().find(); - - expect(assets[0].length >= 2).toBeTruthy(); - expect(allassets[0].slice(1)).toEqual(assets[0]); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - } catch (err) { - console.error('Error:', err); - fail('.skip()'); - } - }); - - test('.limit()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - try { - const allassets = await Query.toJSON().find(); - const assets = await Stack.Assets().Query().limit(2).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - expect(allassets[0].slice(0, 2)).toEqual(assets[0]); - - if (assets && assets.length && assets[0] && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - } catch (err) { - console.error('Error:', err); - fail('.limit()'); - } - }); - - test('.count()', async () => { - const Query = Stack.Assets().Query(); - try { - const count = await Query.count().toJSON().find(); - expect(count).toBeTruthy(); - } catch (err) { - console.error('Error:', err); - fail('.count()'); - } - }); - }); - - describe('Logical Operators Tests', () => { - describe('.or() - Query Objects', () => { - let assets; - const title = 'image1'; - const isDir = true; - - beforeAll(async () => { - const Query1 = Stack.Assets().Query().where('title', title); - const Query2 = Stack.Assets().Query().where('is_dir', isDir); - const Query = Stack.Assets().Query(); - assets = await Query.or(Query1, Query2).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets matching at least one of the specified conditions', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every( - (asset) => asset.title === title || asset.is_dir === isDir - ); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn('No assets returned to verify OR condition'); - } - }); - - test('should include at least one asset matching the title condition', async () => { - if (assets && assets.length && assets[0].length) { - const anyAssetMatchesTitleCondition = assets[0].some( - (asset) => asset.title === title - ); - expect(anyAssetMatchesTitleCondition).toBe(true); - } else { - console.warn('No assets returned to verify first condition'); - } - }); - }); - - describe('.and() - Query Objects', () => { - let assets; - const title = 'image1'; - const isDir = true; - - beforeAll(async () => { - const Query1 = Stack.Assets().Query().where('title', title); - const Query2 = Stack.Assets().Query().where('is_dir', isDir); - const Query = Stack.Assets().Query(); - assets = await Query.and(Query1, Query2).toJSON().find(); - }); - - test('should return an empty array when conditions cannot be satisfied simultaneously', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeFalsy(); - }); - - test('should verify that no assets match both conditions', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every( - (asset) => asset.title === title && asset.is_dir === isDir - ); - expect(allAssetsMatchCondition).toBe(true); - } - }); - }); - - describe('.query() - Raw query', () => { - let assets; - const title = 'image2'; - const isDir = true; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.query({ - $or: [{ title }, { is_dir: isDir }] - }) - .toJSON() - .find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets matching at least one of the specified conditions', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsMatchCondition = assets[0].every( - (asset) => asset.title === title || asset.is_dir === isDir - ); - expect(allAssetsMatchCondition).toBe(true); - } else { - console.warn('No assets returned to verify raw query conditions'); - } - }); - - test('should include at least one asset matching the title condition', async () => { - if (assets && assets.length && assets[0].length) { - const anyAssetMatchesTitleCondition = assets[0].some( - (asset) => asset.title === title - ); - expect(anyAssetMatchesTitleCondition).toBe(true); - } else { - console.warn('No assets returned to verify first condition'); - } - }); - }); - }); - - describe('Tags Tests', () => { - describe('.tags() - empty results', () => { - let assets; - const tags = ['asset3']; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.tags(tags).toJSON().find(); - }); - - test('should return a properly structured response', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets.length).toBeGreaterThanOrEqual(1); - }); - - test('should return an empty array when no assets match the tags', async () => { - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBe(0); - }); - }); - - describe('.tags() - with results', () => { - let assets; - const field = 'tags'; - const tags = ['asset1', 'asset2']; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.tags(tags).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets.length).toBeGreaterThanOrEqual(1); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets with at least one matching tag', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveMatchingTags = assets[0].every((asset) => { - return Utils.arrayPresentInArray(tags, asset[field]); - }); - expect(allAssetsHaveMatchingTags).toBe(true); - } else { - console.warn('No assets returned to verify tags'); - } - }); - - test('should include assets with tags that overlap with the specified tags', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveOverlappingTags = assets[0].every((asset) => { - // Check that asset tags overlap with requested tags - return asset[field].some((tag) => tags.includes(tag)); - }); - expect(allAssetsHaveOverlappingTags).toBe(true); - } else { - console.warn('No assets returned to verify tag overlap'); - } - }); - }); - }); - - describe('Search Tests', () => { - describe('.search()', () => { - let assets; - const searchTerm = 'image1'; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.toJSON().search(searchTerm).find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return assets matching the search term', async () => { - if (assets && assets.length && assets[0].length) { - // Verify that each asset contains the search term in some field - // This is a simplified check since search can match across multiple fields - const anyAssetMatchesSearchTerm = assets[0].some( - (asset) => - asset.title.includes(searchTerm) || - (asset.description && asset.description.includes(searchTerm)) - ); - expect(anyAssetMatchesSearchTerm).toBe(true); - } else { - console.warn('No assets returned to verify search results'); - } - }); - }); - - describe('.regex()', () => { - let assets; - const field = 'title'; - const regex = { - pattern: '^image', - options: 'i' - }; - const regexpObj = new RegExp(regex.pattern, regex.options); - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.regex(field, regex.pattern, regex.options) - .toJSON() - .find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets.length).toBeGreaterThanOrEqual(1); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should return only assets with titles matching the regex pattern', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsTitlesMatchRegex = assets[0].every((asset) => { - return regexpObj.test(asset[field]); - }); - expect(allAssetsTitlesMatchRegex).toBe(true); - } else { - console.warn('No assets returned to verify regex match'); - } - }); - - test('should include assets whose titles start with "image"', async () => { - if (assets && assets.length && assets[0].length) { - const allTitlesStartWithImage = assets[0].every((asset) => - asset.title.toLowerCase().startsWith('image') - ); - expect(allTitlesStartWithImage).toBe(true); - } else { - console.warn('No assets returned to verify specific regex pattern'); - } - }); - }); - }); - - describe('Include Options', () => { - describe('.includeCount()', () => { - let assets; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.includeCount().toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should include count information in the result', async () => { - expect(assets[1]).toBeDefined(); - expect(assets[1]).toBeTruthy(); - }); - - test('should return count as a number', async () => { - expect(typeof assets[1]).toBe('number'); - }); - - test('should return count equal to the number of returned assets', async () => { - expect(assets[1]).toBeGreaterThanOrEqual(assets[0].length); - }); - }); - }); - - describe('Field Projections', () => { - describe('.only() - Single String Parameter', () => { - let assets; - const selectedField = 'title'; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.only(selectedField).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should include the selected field in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveSelectedField = assets[0].every( - (asset) => selectedField in asset - ); - expect(allAssetsHaveSelectedField).toBe(true); - } else { - console.warn('No assets returned to verify field projection'); - } - }); - - test('should include system fields along with the selected field', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveRequiredFields = assets[0].every( - (asset) => 'title' in asset && 'uid' in asset && 'url' in asset - ); - expect(allAssetsHaveRequiredFields).toBe(true); - } else { - console.warn('No assets returned to verify system fields'); - } - }); - - test('should limit the total number of fields in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveLimitedFields = assets[0].every( - (asset) => Object.keys(asset).length === 5 - ); - expect(allAssetsHaveLimitedFields).toBe(true); - } else { - console.warn('No assets returned to verify field count'); - } - }); - }); - - describe('.only() - Multiple String Parameters', () => { - let assets; - const selectedFields = ['BASE', 'title']; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.only(...selectedFields) - .toJSON() - .find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should include the title field in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveTitle = assets[0].every( - (asset) => 'title' in asset - ); - expect(allAssetsHaveTitle).toBe(true); - } else { - console.warn('No assets returned to verify field projection'); - } - }); - - test('should include system fields in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveSystemFields = assets[0].every( - (asset) => 'uid' in asset && 'url' in asset - ); - expect(allAssetsHaveSystemFields).toBe(true); - } else { - console.warn('No assets returned to verify system fields'); - } - }); - - test('should limit the total number of fields in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveLimitedFields = assets[0].every( - (asset) => Object.keys(asset).length === 5 - ); - expect(allAssetsHaveLimitedFields).toBe(true); - } else { - console.warn('No assets returned to verify field count'); - } - }); - }); - - describe('.only() - Array Parameter', () => { - let assets; - const selectedFields = ['title', 'filename']; - - beforeAll(async () => { - const Query = Stack.Assets().Query(); - assets = await Query.only(selectedFields).toJSON().find(); - }); - - test('should return a non-empty array of assets', async () => { - expect(assets).toBeDefined(); - expect(Array.isArray(assets)).toBe(true); - expect(assets[0]).toBeDefined(); - expect(assets[0].length).toBeTruthy(); - }); - - test('should include all the selected fields in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveSelectedFields = assets[0].every((asset) => - selectedFields.every((field) => field in asset) - ); - expect(allAssetsHaveSelectedFields).toBe(true); - } else { - console.warn('No assets returned to verify field projection'); - } - }); - - test('should include system fields in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveSystemFields = assets[0].every( - (asset) => 'uid' in asset && 'url' in asset - ); - expect(allAssetsHaveSystemFields).toBe(true); - } else { - console.warn('No assets returned to verify system fields'); - } - }); - - test('should limit the total number of fields in each asset', async () => { - if (assets && assets.length && assets[0].length) { - const allAssetsHaveLimitedFields = assets[0].every( - (asset) => Object.keys(asset).length === 5 - ); - expect(allAssetsHaveLimitedFields).toBe(true); - } else { - console.warn('No assets returned to verify field count'); - } - }); - }); - }); -}); diff --git a/test/asset/find.js b/test/asset/find.js deleted file mode 100755 index 09935d50..00000000 --- a/test/asset/find.js +++ /dev/null @@ -1,582 +0,0 @@ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('../config.js'); -const Utils = require('../entry/utils.js'); - -let Stack; - -describe('Contentstack Asset Tests', () => { - // Initialize the Contentstack Stack Instance - beforeAll(() => { - return new Promise((resolve) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(resolve, 1000); - }); - }); - - describe('Language and Fallback Tests', () => { - test('default .find() No fallback', async () => { - const _in = ['ja-jp']; - - const assets = await Stack.Assets() - .Query() - .language('ja-jp') - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - expect(assets[1]).toBeFalsy(); - - if (assets && assets.length && assets[0].length) { - const _assets = assets[0].every((asset) => { - return _in.indexOf(asset.publish_details.locale) !== -1; - }); - expect(_assets).toBe(true); - } - }); - - test('default .find() fallback', async () => { - const _in = ['ja-jp', 'en-us']; - - const assets = await Stack.Assets() - .Query() - .language('ja-jp') - .includeFallback() - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - expect(assets[1]).toBeFalsy(); - - if (assets && assets.length && assets[0].length) { - const _assets = assets[0].every((asset) => { - return _in.indexOf(asset.publish_details.locale) !== -1; - }); - expect(_assets).toBe(true); - } - }); - }); - - test('default .find()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - const assets = await Query.toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - expect(assets[1]).toBeFalsy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - describe('Sorting', () => { - test('.ascending()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - - const assets = await Query.ascending(field).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] >= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.descending()', async () => { - const Query = Stack.Assets().Query(); - const field = 'created_at'; - - const assets = await Query.descending(field).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - }); - - describe('Params', () => { - test('.addParam()', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.addParam('include_dimension', 'true') - .toJSON() - .find(); - expect(assets[0][0].hasOwnProperty('dimension')).toBeTruthy(); - }); - }); - - describe('Comparison', () => { - test('.lessThan()', async () => { - const Query = Stack.Assets().Query(); - const field = 'file_size'; - const value = 5122; - - const assets = await Query.lessThan('file_size', value).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].slice(1).every((asset) => { - const flag = asset[field] < value; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.lessThanOrEqualTo()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - - const assets = await Query.lessThanOrEqualTo('file_size', 5122) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.greaterThan()', async () => { - const Query = Stack.Assets().Query(); - const field = 'file_size'; - const value = 5122; - - const assets = await Query.greaterThan('file_size', value) - .ascending(field) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].slice(1).every((asset) => { - const flag = asset[field] > value; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.greaterThanOrEqualTo()', async () => { - const Query = Stack.Assets().Query(); - const field = 'file_size'; - const value = 5122; - - const assets = await Query.greaterThanOrEqualTo('file_size', value) - .descending(field) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] >= value; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.notEqualTo()', async () => { - const Query = Stack.Assets().Query(); - const field = 'file_size'; - const value = 5122; - - const assets = await Query.notEqualTo('file_size', value) - .descending(field) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] != value; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.where()', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.where('title', 'image1').toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - expect(assets[0].length).toBe(1); - }); - - test('.equalTo() compare boolean value (true)', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.language('en-us') - .equalTo('is_dir', false) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - expect(assets[0].length).toBe(5); - }); - - test('.equalTo() compare boolean value (false)', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.equalTo('is_dir', true).toJSON().find(); - - expect(assets[0].length).toBeFalsy(); - expect(assets[0].length).toBe(0); - }); - }); - - describe('Array/Subset Tests', () => { - test('.containedIn()', async () => { - const Query = Stack.Assets().Query(); - const _in = ['image1', 'image2']; - const field = 'updated_at'; - - const assets = await Query.containedIn('title', _in).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - const _assets = assets[0].every((asset) => { - return _in.indexOf(asset.title) != -1; - }); - expect(_assets).toBe(true); - } - }); - - test('.notContainedIn()', async () => { - const Query = Stack.Assets().Query(); - const _in = ['image1', 'image2']; - - const assets = await Query.notContainedIn('title', _in).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - }); - }); - - describe('Element Existence Tests', () => { - test('.exists()', async () => { - const Query = Stack.Assets().Query(); - const queryField = 'is_dir'; - const field = 'updated_at'; - - const assets = await Query.exists(queryField).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.notExists()', async () => { - const Query = Stack.Assets().Query(); - const queryField = 'is_dir'; - const field = 'updated_at'; - - const assets = await Query.notExists(queryField).toJSON().find(); - - expect(assets[0].length).toBeFalsy(); - - if (assets && assets.length && assets[0].length) { - const prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - return asset[field] <= prev; - }); - expect(_assets).toBe(true); - } - }); - }); - - describe('Pagination Tests', () => { - test('.skip()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - - const allassets = await Query.toJSON().find(); - const assets = await Stack.Assets().Query().skip(1).toJSON().find(); - - expect(assets[0].length >= 2).toBeTruthy(); - expect(allassets[0].slice(1)).toEqual(assets[0]); - - if (assets && assets.length && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.limit()', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - - const allassets = await Query.toJSON().find(); - const assets = await Stack.Assets().Query().limit(2).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - expect(allassets[0].slice(0, 2)).toEqual(assets[0]); - - if (assets && assets.length && assets[0] && assets[0].length) { - let prev = assets[0][0][field]; - const _assets = assets[0].every((asset) => { - const flag = asset[field] <= prev; - prev = asset[field]; - return flag; - }); - expect(_assets).toBe(true); - } - }); - - test('.count()', async () => { - const Query = Stack.Assets().Query(); - - const count = await Query.count().toJSON().find(); - expect(count).toBeTruthy(); - }); - }); - - describe('Logical Operators Tests', () => { - test('.or() - Query Objects', async () => { - const Query1 = Stack.Assets().Query().where('title', 'image1'); - const Query2 = Stack.Assets().Query().where('is_dir', true); - const Query = Stack.Assets().Query(); - - const assets = await Query.or(Query1, Query2).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - const _assets = assets[0].every((asset) => { - return ~(asset.title === 'source1' || asset.is_dir === true); - }); - expect(_assets).toBeTruthy(); - } - }); - - test('.and() - Query Objects', async () => { - const Query1 = Stack.Assets().Query().where('title', 'image1'); - const Query2 = Stack.Assets().Query().where('is_dir', true); - const Query = Stack.Assets().Query(); - - const assets = await Query.and(Query1, Query2).toJSON().find(); - - expect(assets[0].length).toBeFalsy(); - - if (assets && assets.length && assets[0].length) { - const _assets = assets[0].every((asset) => { - return ~(asset.title === 'image1' && asset.is_dir === true); - }); - expect(_assets).toBeTruthy(); - } - }); - - test('.query() - Raw query', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.query({ - $or: [{ title: 'image2' }, { is_dir: 'true' }] - }) - .toJSON() - .find(); - - expect(assets[0].length).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - const _assets = assets[0].every((asset) => { - return asset.title === 'image2' || asset.is_dir === false; - }); - expect(_assets).toBeTruthy(); - } - }); - }); - - describe('Tags Tests', () => { - test('.tags() - empty results', async () => { - const Query = Stack.Assets().Query(); - const tags = ['asset3']; - - const assets = await Query.tags(tags).toJSON().find(); - - expect(assets.length >= 1).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - expect(assets[0].length).toBe(0); - } - }); - - test('.tags() - with results', async () => { - const Query = Stack.Assets().Query(); - const field = 'tags'; - const tags = ['asset1', 'asset2']; - - const assets = await Query.tags(tags).toJSON().find(); - - expect(assets.length >= 1).toBeTruthy(); - - if (assets && assets.length && assets[0].length) { - const _assets = assets[0].every((asset) => { - return Utils.arrayPresentInArray(tags, asset[field]); - }); - expect(_assets).toBe(true); - } - }); - }); - - describe('Search Tests', () => { - test('.search()', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.toJSON().search('image1').find(); - expect(assets[0].length).toBeTruthy(); - }); - - test('.regex()', async () => { - const Query = Stack.Assets().Query(); - const field = 'title'; - const regex = { - pattern: '^image', - options: 'i' - }; - const regexpObj = new RegExp(regex.pattern, regex.options); - - const assets = await Query.regex(field, regex.pattern, regex.options) - .toJSON() - .find(); - - expect(assets.length >= 1).toBeTruthy(); - - const flag = assets[0].every((asset) => { - return regexpObj.test(asset[field]); - }); - expect(flag).toBeTruthy(); - }); - }); - - describe('Include Options', () => { - test('.includeCount()', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.includeCount().toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - expect(assets[1]).toBeTruthy(); - }); - }); - - describe('Field Projections', () => { - test('.only() - Single String Parameter', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.only('title').toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - const flag = assets[0].every((asset) => { - return ( - asset && - Object.keys(asset).length === 5 && - 'title' in asset && - 'uid' in asset && - 'url' in asset - ); - }); - expect(flag).toBeTruthy(); - }); - - test('.only() - Multiple String Parameter', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.only('BASE', 'title').toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - const flag = assets[0].every((asset) => { - return ( - asset && - Object.keys(asset).length === 5 && - 'title' in asset && - 'uid' in asset && - 'url' in asset - ); - }); - expect(flag).toBeTruthy(); - }); - - test('.only() - Array Parameter', async () => { - const Query = Stack.Assets().Query(); - - const assets = await Query.only(['title', 'filename']).toJSON().find(); - - expect(assets[0].length).toBeTruthy(); - - const flag = assets[0].every((asset) => { - return ( - asset && - Object.keys(asset).length === 5 && - 'title' in asset && - 'filename' in asset && - 'uid' in asset && - 'url' in asset - ); - }); - expect(flag).toBeTruthy(); - }); - }); -}); diff --git a/test/asset/image-transformation.js b/test/asset/image-transformation.js deleted file mode 100755 index 6859090f..00000000 --- a/test/asset/image-transformation.js +++ /dev/null @@ -1,154 +0,0 @@ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('./../config.js'); -const Utils = require('./../entry/utils.js'); - -const Regexp = new RegExp('\\\?', 'g'); - -let Stack; -let Asset; - -describe('Image Transformation Tests', () => { - // Setup - runs before all tests - beforeAll(done => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(done, 1000); - }); - - // Get assets for testing - describe('Get All Assets', () => { - beforeAll(async () => { - try { - const assets = await Stack.Assets().Query().toJSON().find(); - Asset = assets[0][0]; - } catch (error) { - console.error('error:', error); - throw new Error('Failed to get assets'); - } - }); - - test('Should have assets in the resultset', () => { - expect(Asset).toBeDefined(); - }); - }); - - describe('Valid URL: single parameter testing', () => { - let Image; - const Params = { - quality: 50 - }; - - beforeAll(() => { - const URL = Asset.url; - Image = Stack.imageTransform(URL, Params); - }); - - test('Should generate valid URL', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - - test('Should include quality parameter', () => { - expect(Image.includes('?quality=50')).toBe(true); - }); - - test('Should verify URL is valid again', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - }); - - describe('Valid URL: multiple parameter testing', () => { - let Image; - const Params = { - quality: 50, - auto: 'webp', - format: 'jpg' - }; - - beforeAll(() => { - const URL = Asset.url; - Image = Stack.imageTransform(URL, Params); - }); - - test('Should generate valid URL', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - - test('Should include quality parameter', () => { - expect(Image.includes('quality=50')).toBe(true); - }); - - test('Should include auto parameter', () => { - expect(Image.includes('auto=webp')).toBe(true); - }); - - test('Should include format parameter', () => { - expect(Image.includes('format=jpg')).toBe(true); - }); - - test('Should verify URL is valid again', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - }); - - describe('Invalid URL: single parameter testing', () => { - let Image; - const Params = { - quality: 50 - }; - - beforeAll(() => { - const URL = Asset.url + '?'; - Image = Stack.imageTransform(URL, Params); - }); - - test('Should generate valid URL', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - - test('Should include quality parameter', () => { - expect(Image.includes('quality=50')).toBe(true); - }); - - test('Should verify URL is valid again', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - }); - - describe('Invalid URL: multiple parameter testing', () => { - let Image; - const Params = { - quality: 50, - auto: 'webp', - format: 'jpg' - }; - - beforeAll(() => { - const URL = Asset.url + '?'; - Image = Stack.imageTransform(URL, Params); - }); - - test('Should generate valid URL', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - - test('Should include quality parameter', () => { - expect(Image.includes('quality=50')).toBe(true); - }); - - test('Should include auto parameter', () => { - expect(Image.includes('auto=webp')).toBe(true); - }); - - test('Should include format parameter', () => { - expect(Image.includes('format=jpg')).toBe(true); - }); - - test('Should verify URL is valid again', () => { - expect(Image.match(Regexp).length).toBe(1); - }); - }); -}); diff --git a/test/asset/spread.js b/test/asset/spread.js deleted file mode 100755 index bb377310..00000000 --- a/test/asset/spread.js +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Created by Aamod Pisat on 09-06-2017. - */ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('../config.js'); - -let Stack; - -describe('Contentstack Asset Tests', () => { - // Initialize the Contentstack Stack Instance - beforeAll(() => { - return new Promise((resolve) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(resolve, 1000); - }); - }); - - test('assets as first argument', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - - const result = await Query.limit(1).toJSON().find(); - - const assets = result[0]; // Using array destructuring - - expect(assets.length).toBeTruthy(); - - if (assets && assets.length) { - let prev = assets[0][field]; - const _assets = assets.every((asset) => { - prev = asset[field]; - return asset[field] <= prev; - }); - expect(_assets).toBe(true); - } - }); - - test('with assets and count argument', async () => { - const Query = Stack.Assets().Query(); - const field = 'updated_at'; - - const result = await Query.includeCount().toJSON().find(); - - const [assets, count] = result; // Using array destructuring - - expect(assets.length).toBeTruthy(); - expect(count).toBeTruthy(); - - if (assets && assets.length) { - let prev = assets[0][field]; - const _assets = assets.every((asset) => { - prev = asset[field]; - return asset[field] <= prev; - }); - expect(_assets).toBe(true); - } - }); -}); diff --git a/test/entry/find-result-wrapper.js b/test/entry/find-result-wrapper.js deleted file mode 100755 index db2c9343..00000000 --- a/test/entry/find-result-wrapper.js +++ /dev/null @@ -1,1269 +0,0 @@ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('../config.js'); -const Utils = require('./utils.js'); - -const contentTypes = init.contentTypes; -let Stack; -const error = null; - -describe('ContentStack SDK Tests', () => { - // Initialize the Contentstack Stack Instance - beforeAll(() => { - return new Promise((resolve) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(resolve, 1000); - }); - }); - - describe('default .find()', () => { - let entries; - const field = 'updated_at'; - - // Setup - run the query once for all tests - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.toJSON().find(); - }); - - test('should return a non-empty array of entries', async () => { - expect(entries).toBeDefined(); - expect(Array.isArray(entries)).toBe(true); - expect(entries[0]).toBeDefined(); - expect(entries[0].length).toBeTruthy(); - }); - - test('should not include count when not requested', async () => { - expect(entries[1]).toBeFalsy(); - }); - - test('should return entries sorted by updated_at in descending order by default', async () => { - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - prev = entry[field]; - return entry.updated_at <= prev; - }); - expect(_entries).toBe(true); - } else { - console.warn('Not enough entries returned to verify default sorting'); - } - }); - - test('should have entries with valid structure', async () => { - if (entries && entries.length && entries[0].length) { - const firstEntry = entries[0][0]; - expect(firstEntry).toHaveProperty('uid'); - expect(firstEntry).toHaveProperty('title'); - expect(firstEntry).toHaveProperty('updated_at'); - } else { - console.warn('No entries returned to verify structure'); - } - }); - }); - - describe('sorting', () => { - test('.ascending()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'updated_at'; - - const entries = await Query.ascending(field).toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - prev = entry[field]; - return entry[field] >= prev; - }); - expect(_entries).toBe(true); - } - }); - - test('.descending()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'created_at'; - - const entries = await Query.descending(field).toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - prev = entry[field]; - return entry[field] >= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - - describe('comparison', () => { - test('.lessThan()', async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - const value = 11; - const field = 'updated_at'; - - const entries = await Query.lessThan('num_field', value).toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].slice(1).every(function (entry) { - const flag = entry[field] < value; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.lessThanOrEqualTo()', async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - const field = 'updated_at'; - const value = 11; - - const entries = await Query.lessThanOrEqualTo('num_field', value) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - const flag = entry[field] <= prev; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.greaterThan()', async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - const field = 'num_field'; - const value = 11; - - const entries = await Query.greaterThan('num_field', value) - .ascending(field) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].slice(1).every(function (entry) { - const flag = entry[field] > value; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.greaterThanOrEqualTo()', async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - const field = 'num_field'; - const value = 11; - - const entries = await Query.greaterThanOrEqualTo('num_field', value) - .descending(field) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - const flag = entry[field] >= value; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.notEqualTo()', async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - const field = 'num_field'; - const value = 6; - - const entries = await Query.notEqualTo('num_field', value) - .descending(field) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - const flag = entry[field] != value; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - }); - - describe('array/subset', () => { - test('.containedIn()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const _in = ['source1', 'source2']; - const field = 'updated_at'; - - const entries = await Query.containedIn('title', _in).toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - const _entries = entries[0].every(function (entry) { - return _in.indexOf(entry.title) != -1; - }); - expect(_entries).toBe(true); - } - }); - - test('.notContainedIn()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const _in = ['sourceddd1', 'sourceddddd2']; - - const entries = await Query.notContainedIn('title', _in).toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('exists', () => { - test('.exists()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const queryField = 'boolean'; - const field = 'updated_at'; - - const entries = await Query.exists(queryField).toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - const flag = entry[field] <= prev; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.notExists()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const queryField = 'isspecial'; - const field = 'updated_at'; - - const entries = await Query.notExists(queryField).toJSON().find(); - - expect('entries' in entries).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - const prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - return entry[field] <= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - - describe('pagination', () => { - test('.skip()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'updated_at'; - - const allEntries = await Query.toJSON().find(); - - const entries = await Stack.ContentType(contentTypes.source) - .Query() - .skip(1) - .toJSON() - .find(); - - expect(entries[0].length).toBeGreaterThanOrEqual(2); - expect(allEntries[0].slice(1)).toEqual(entries[0]); - - if (entries && entries.length && entries[0].length) { - allEntries[0] = allEntries[0].slice(1); - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - const flag = entry[field] <= prev; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.limit()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'updated_at'; - - const allEntries = await Query.toJSON().find(); - - const entries = await Stack.ContentType(contentTypes.source) - .Query() - .limit(2) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - expect(allEntries[0].slice(0, 2)).toEqual(entries[0]); - - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - const flag = entry[field] <= prev; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.count()', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - - const entries = await Query.count().toJSON().find(); - - expect(entries[0]).toBeTruthy(); - }); - }); - - describe('logical', () => { - describe('.or() - Query Objects', () => { - let entries; - const titles = ['source1', 'source2']; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .containedIn('title', titles); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', true); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entries = await Query.or(Query1, Query2).toJSON().find(); - }); - - test('should return a non-empty array of entries', async () => { - expect(entries).toBeDefined(); - expect(Array.isArray(entries)).toBe(true); - expect(entries[0]).toBeDefined(); - expect(entries[0].length).toBeTruthy(); - }); - - test('should return entries matching at least one of the conditions', async () => { - if (entries && entries.length && entries[0].length) { - const allEntriesMatchAnyCondition = entries[0].every( - (entry) => titles.includes(entry.title) || entry.boolean === true - ); - expect(allEntriesMatchAnyCondition).toBe(true); - } else { - console.warn('No entries returned to verify OR condition'); - } - }); - - test('should include entries with title in the specified list', async () => { - if (entries && entries.length && entries[0].length) { - const hasEntryWithTitle = entries[0].some((entry) => - titles.includes(entry.title) - ); - expect(hasEntryWithTitle).toBe(true); - } else { - console.warn('No entries returned to verify first condition'); - } - }); - - test('should include entries with boolean field set to true', async () => { - if (entries && entries.length && entries[0].length) { - const hasEntryWithBoolean = entries[0].some( - (entry) => entry.boolean === true - ); - expect(hasEntryWithBoolean).toBe(true); - } else { - console.warn('No entries returned to verify second condition'); - } - }); - }); - - describe('.and() - Query Objects', () => { - let entries; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .where('title', 'source1'); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', true); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entries = await Query.and(Query1, Query2).toJSON().find(); - }); - - test('should return a non-empty array of entries', async () => { - expect(entries).toBeDefined(); - expect(Array.isArray(entries)).toBe(true); - expect(entries[0]).toBeDefined(); - expect(entries[0].length).toBeTruthy(); - }); - - test('should return only entries matching all specified conditions', async () => { - if (entries && entries.length && entries[0].length) { - const allEntriesMatchAllConditions = entries[0].every( - (entry) => entry.title === 'source1' && entry.boolean === true - ); - expect(allEntriesMatchAllConditions).toBe(true); - } else { - console.warn('No entries returned to verify AND condition'); - } - }); - - test('should include entries with title set to "source1"', async () => { - if (entries && entries.length && entries[0].length) { - const allEntriesHaveCorrectTitle = entries[0].every( - (entry) => entry.title === 'source1' - ); - expect(allEntriesHaveCorrectTitle).toBe(true); - } else { - console.warn('No entries returned to verify title condition'); - } - }); - - test('should include entries with boolean field set to true', async () => { - if (entries && entries.length && entries[0].length) { - const allEntriesHaveBooleanTrue = entries[0].every( - (entry) => entry.boolean === true - ); - expect(allEntriesHaveBooleanTrue).toBe(true); - } else { - console.warn('No entries returned to verify boolean condition'); - } - }); - }); - - describe('.query() - Raw query', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.query({ - $or: [{ title: 'source1' }, { boolean: true }] - }) - .toJSON() - .find(); - }); - - test('should return a non-empty array of entries', async () => { - expect(entries).toBeDefined(); - expect(Array.isArray(entries)).toBe(true); - expect(entries[0]).toBeDefined(); - expect(entries[0].length).toBeTruthy(); - }); - - test('should return entries matching at least one of the conditions in the raw query', async () => { - if (entries && entries.length && entries[0].length) { - const allEntriesMatchAnyCondition = entries[0].every( - (entry) => entry.title === 'source1' || entry.boolean === true - ); - expect(allEntriesMatchAnyCondition).toBe(true); - } else { - console.warn('No entries returned to verify raw query conditions'); - } - }); - - test('should include entries with title "source1"', async () => { - if (entries && entries.length && entries[0].length) { - const hasEntryWithTitle = entries[0].some( - (entry) => entry.title === 'source1' - ); - expect(hasEntryWithTitle).toBe(true); - } else { - console.warn( - 'No entries returned to verify first raw query condition' - ); - } - }); - - test('should include entries with boolean field set to true', async () => { - if (entries && entries.length && entries[0].length) { - const hasEntryWithBoolean = entries[0].some( - (entry) => entry.boolean === true - ); - expect(hasEntryWithBoolean).toBe(true); - } else { - console.warn( - 'No entries returned to verify second raw query condition' - ); - } - }); - }); - }); - - describe('custom query', () => { - test('.query() - Raw query with basic OR condition', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - - const entries = await Query.query({ - $or: [{ title: 'source1' }, { boolean: 'true' }] - }) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - const _entries = entries[0].every(function (entry) { - return entry.title === 'source1' || entry.boolean === true; - }); - expect(_entries).toBeTruthy(); - } - }); - - test('.query() - Raw query with AND condition', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - - const entries = await Query.query({ - $and: [{ title: 'source1' }, { boolean: true }] - }) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - const allMatchBothConditions = entries[0].every( - (entry) => entry.title === 'source1' && entry.boolean === true - ); - expect(allMatchBothConditions).toBeTruthy(); - }); - - test('.query() - Raw query with nested conditions', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - - const entries = await Query.query({ - $and: [ - { title: 'source1' }, - { $or: [{ boolean: true }, { url: { $exists: true } }] } - ] - }) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - const allMatchConditions = entries[0].every( - (entry) => - entry.title === 'source1' && - (entry.boolean === true || entry.url !== undefined) - ); - expect(allMatchConditions).toBeTruthy(); - }); - }); - - describe('tags', () => { - test('.tags() - Multiple tags filter', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'tags'; - const tags = ['tag1', 'tag2']; - - const entries = await Query.tags(tags).toJSON().find(); - - expect(entries.length).toBeGreaterThanOrEqual(1); - - if (entries && entries.length && entries[0].length) { - const _entries = entries[0].every(function (entry) { - return Utils.arrayPresentInArray(tags, entry[field]); - }); - expect(_entries).toBe(true); - } - }); - - test('.tags() - Single tag filter', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'tags'; - const tags = ['tag1']; - - const entries = await Query.tags(tags).toJSON().find(); - - expect(entries.length).toBeGreaterThanOrEqual(1); - - if (entries && entries.length && entries[0].length) { - const entriesWithTag = entries[0].every( - (entry) => entry[field] && entry[field].includes(tags[0]) - ); - expect(entriesWithTag).toBe(true); - } - }); - - test('.tags() - Empty results with non-existent tag', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const nonExistentTag = ['non_existent_tag_123456']; - - const entries = await Query.tags(nonExistentTag).toJSON().find(); - - // Should return an array but with empty results - expect(entries).toBeDefined(); - expect(Array.isArray(entries)).toBe(true); - expect(entries[0].length).toBe(0); - }); - }); - - describe('search', () => { - test('.search() - Exact match', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - - const entries = await Query.search('source1').toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - const hasMatchingEntries = entries[0].some( - (entry) => - entry.title === 'source1' || JSON.stringify(entry).includes('source1') - ); - expect(hasMatchingEntries).toBe(true); - }); - - test('.search() - Partial match', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - - const entries = await Query.search('source').toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - const hasMatchingEntries = entries[0].some( - (entry) => - (entry.title && entry.title.includes('source')) || - JSON.stringify(entry).includes('source') - ); - expect(hasMatchingEntries).toBe(true); - }); - - test('.search() - Case insensitive match', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - - const entries = await Query.search('SOURCE1').toJSON().find(); - - expect(entries[0].length).toBeTruthy(); - - const hasMatchingEntries = entries[0].some( - (entry) => - (entry.title && entry.title.toLowerCase() === 'source1') || - JSON.stringify(entry).toLowerCase().includes('source1') - ); - expect(hasMatchingEntries).toBe(true); - }); - }); - - describe('regex', () => { - test('.regex() - Basic pattern match', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'title'; - const regex = { - pattern: '^source', - options: 'i' - }; - const regexpObj = new RegExp(regex.pattern, regex.options); - - const entries = await Query.regex(field, regex.pattern, regex.options) - .toJSON() - .find(); - - expect(entries.length).toBeGreaterThanOrEqual(1); - - const flag = entries[0].every(function (entry) { - return regexpObj.test(entry[field]); - }); - expect(flag).toBeTruthy(); - }); - - test('.regex() - Specific suffix pattern', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'title'; - const regex = { - pattern: '1$', // Matches strings ending with 1 - options: '' - }; - const regexpObj = new RegExp(regex.pattern, regex.options); - - const entries = await Query.regex(field, regex.pattern, regex.options) - .toJSON() - .find(); - - expect(entries.length).toBeGreaterThanOrEqual(1); - - if (entries && entries[0].length) { - const matchesPattern = entries[0].every((entry) => - regexpObj.test(entry[field]) - ); - expect(matchesPattern).toBeTruthy(); - - const endsWithOne = entries[0].every( - (entry) => entry[field] && entry[field].endsWith('1') - ); - expect(endsWithOne).toBeTruthy(); - } - }); - - test('.regex() - With wildcard pattern', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const field = 'title'; - const regex = { - pattern: 'source.*', - options: 'i' - }; - const regexpObj = new RegExp(regex.pattern, regex.options); - - const entries = await Query.regex(field, regex.pattern, regex.options) - .toJSON() - .find(); - - expect(entries.length).toBeGreaterThanOrEqual(1); - - if (entries && entries[0].length) { - const matchesPattern = entries[0].every((entry) => - regexpObj.test(entry[field]) - ); - expect(matchesPattern).toBeTruthy(); - } - }); - }); - - describe('locale and fallback', () => { - test('find: with specific locale', async () => { - const locale = 'ja-jp'; - - const entries = await Stack.ContentType(contentTypes.source) - .Query() - .language(locale) - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - const allEntriesInRequestedLocale = entries[0].every( - (entry) => - entry.publish_details && entry.publish_details.locale === locale - ); - expect(allEntriesInRequestedLocale).toBe(true); - } - }); - - test('find: with fallback enabled for partially localized content', async () => { - const primaryLocale = 'ja-jp'; - const fallbackLocale = 'en-us'; - - const entries = await Stack.ContentType(contentTypes.source) - .Query() - .language(primaryLocale) - .includeFallback() - .toJSON() - .find(); - - expect(entries[0].length).toBeTruthy(); - - if (entries && entries.length && entries[0].length) { - const _entries = entries[0].every(function (entry) { - return [primaryLocale, fallbackLocale].includes( - entry.publish_details.locale - ); - }); - expect(_entries).toBe(true); - } - - if (entries && entries.length && entries[0].length > 1) { - const hasPrimaryLocaleEntries = entries[0].some( - (entry) => entry.publish_details.locale === primaryLocale - ); - - const hasFallbackLocaleEntries = entries[0].some( - (entry) => entry.publish_details.locale === fallbackLocale - ); - - expect(hasPrimaryLocaleEntries || hasFallbackLocaleEntries).toBe(true); - } - }); - - test('find: comparing results with and without fallback', async () => { - const locale = 'ja-jp'; - - const entriesWithoutFallback = await Stack.ContentType( - contentTypes.source - ) - .Query() - .language(locale) - .toJSON() - .find(); - - const entriesWithFallback = await Stack.ContentType(contentTypes.source) - .Query() - .language(locale) - .includeFallback() - .toJSON() - .find(); - - expect(entriesWithFallback[0].length).toBeGreaterThanOrEqual( - entriesWithoutFallback[0].length - ); - - if (entriesWithoutFallback && entriesWithoutFallback[0].length) { - const allInRequestedLocale = entriesWithoutFallback[0].every( - (entry) => entry.publish_details.locale === locale - ); - expect(allInRequestedLocale).toBe(true); - } - }); - }); - - describe('include reference', () => { - describe('.includeReference() - String', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeReference('reference').toJSON().find(); - }); - - test('should return entries with the reference field', () => { - expect(entries[0].length).toBeGreaterThan(0); - }); - - test('should include the reference field as an object', () => { - const allEntriesHaveReference = entries[0].every( - (entry) => - entry && - entry.reference && - typeof entry.reference === 'object' - ); - expect(allEntriesHaveReference).toBe(true); - }); - }); - - describe('.includeReference() - Array', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeReference(['reference', 'other_reference']) - .toJSON() - .find(); - }); - - test('should return entries with data', () => { - expect(entries[0].length).toBeGreaterThan(0); - }); - - test('should include the first reference field as an object', () => { - const allEntriesHaveFirstReference = entries[0].every( - (entry) => - entry && - entry.reference && - typeof entry.reference === 'object' - ); - expect(allEntriesHaveFirstReference).toBe(true); - }); - - test('should include the second reference field as an object', () => { - const allEntriesHaveSecondReference = entries[0].every( - (entry) => - entry && - entry.other_reference && - typeof entry.other_reference === 'object' - ); - expect(allEntriesHaveSecondReference).toBe(true); - }); - }); - }); - - describe('include count and schema', () => { - describe('.includeCount()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeCount().toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include count information', () => { - expect(entries[1]).toBeTruthy(); - }); - }); - - describe('.includeSchema()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeSchema().toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include schema information', () => { - expect(entries[1].length).toBeTruthy(); - }); - }); - - describe('.includeCount() and .includeSchema()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeCount().includeSchema().toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include schema information', () => { - expect(entries[1].length).toBeTruthy(); - }); - - test('should include count information', () => { - expect(entries[2]).toBeTruthy(); - }); - }); - }); - - describe('include contenttypes', () => { - describe('.includeContentType()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeContentType().toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include content type information', () => { - expect(entries[1]).toBeTruthy(); - }); - - test('should include content type title', () => { - expect(entries[1].title).toBeTruthy(); - }); - - test('should have the correct content type UID', () => { - expect(entries[1].uid).toBe(contentTypes.source); - }); - }); - - describe('.includeCount() and .includeContentType()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeCount() - .includeContentType() - .toJSON() - .find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include content type information', () => { - expect(entries[1]).toBeTruthy(); - }); - - test('should include content type title', () => { - expect(entries[1].title).toBeTruthy(); - }); - - test('should have the correct content type UID', () => { - expect(entries[1].uid).toBe(contentTypes.source); - }); - - test('should include count information', () => { - expect(entries[2]).toBeTruthy(); - }); - }); - - describe('.includeSchema() and .includeContentType()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeSchema() - .includeContentType() - .toJSON() - .find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include content type information', () => { - expect(entries[1]).toBeTruthy(); - }); - - test('should include content type title', () => { - expect(entries[1].title).toBeTruthy(); - }); - - test('should have the correct content type UID', () => { - expect(entries[1].uid).toBe(contentTypes.source); - }); - }); - - describe('.includeCount(), .includeSchema() and .includeContentType()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeCount() - .includeSchema() - .includeContentType() - .toJSON() - .find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include content type information', () => { - expect(entries[1]).toBeTruthy(); - }); - - test('should include content type title', () => { - expect(entries[1].title).toBeTruthy(); - }); - - test('should have the correct content type UID', () => { - expect(entries[1].uid).toBe(contentTypes.source); - }); - - test('should include count information', () => { - expect(entries[2]).toBeTruthy(); - }); - }); - }); - - describe('field projections', () => { - describe('.only() - Single String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.only('title').toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include only the title and uid fields', () => { - const correctFieldsOnly = entries[0].every( - (entry) => - entry && - Object.keys(entry).length === 2 && - 'title' in entry && - 'uid' in entry - ); - expect(correctFieldsOnly).toBeTruthy(); - }); - }); - - describe('.only() - Multiple String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.only('BASE', 'title').toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include only the title and uid fields', () => { - const correctFieldsOnly = entries[0].every( - (entry) => - entry && - Object.keys(entry).length === 2 && - 'title' in entry && - 'uid' in entry - ); - expect(correctFieldsOnly).toBeTruthy(); - }); - }); - - describe('.only() - Array Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.only(['title', 'url']).toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should include only the title, url, and uid fields', () => { - const correctFieldsOnly = entries[0].every( - (entry) => - entry && - Object.keys(entry).length === 3 && - 'title' in entry && - 'url' in entry && - 'uid' in entry - ); - expect(correctFieldsOnly).toBeTruthy(); - }); - }); - - describe('.except() - Single String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.except('title').toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should exclude the title field', () => { - const titleExcluded = entries[0].every( - (entry) => entry && !('title' in entry) - ); - expect(titleExcluded).toBeTruthy(); - }); - }); - - describe('.except() - Multiple String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.except('BASE', 'title').toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should exclude the title field', () => { - const titleExcluded = entries[0].every( - (entry) => entry && !('title' in entry) - ); - expect(titleExcluded).toBeTruthy(); - }); - }); - - describe('.except() - Array of String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.except(['title', 'file']).toJSON().find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should exclude the title field', () => { - const titleExcluded = entries[0].every( - (entry) => entry && !('title' in entry) - ); - expect(titleExcluded).toBeTruthy(); - }); - - test('should exclude the file field', () => { - const fileExcluded = entries[0].every( - (entry) => entry && !('file' in entry) - ); - expect(fileExcluded).toBeTruthy(); - }); - }); - - describe('.except() - For the reference - String', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeReference('reference') - .only('BASE', ['reference']) - .except('reference', 'title') - .toJSON() - .find(); - }); - - test('should return entries', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('should properly format entries with reference but without title in references', () => { - const correctFormat = entries[0].every((entry) => { - let hasCorrectReferenceFormat = false; - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - hasCorrectReferenceFormat = true; - hasCorrectReferenceFormat = entry.reference.every((reference) => { - return reference && !('title' in reference); - }); - } - - return ( - hasCorrectReferenceFormat && - entry && - Object.keys(entry).length === 2 && - 'reference' in entry && - 'uid' in entry - ); - }); - - expect(correctFormat).toBeTruthy(); - }); - }); - }); -}); diff --git a/test/entry/find.js b/test/entry/find.js deleted file mode 100755 index 5f15937b..00000000 --- a/test/entry/find.js +++ /dev/null @@ -1,1414 +0,0 @@ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('../config.js'); -const Utils = require('./utils.js'); - -const contentTypes = init.contentTypes; -let Stack; - -describe('ContentStack SDK Tests', () => { - // Setup - Initialize the Contentstack Stack Instance - beforeAll((done) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(done, 1000); - }); - - describe('Stack Initialization', () => { - test('early_access in stack initialization should add headers', () => { - const stack = Contentstack.Stack({ - ...init.stack, - early_access: ['newCDA', 'taxonomy'] - }); - expect(stack.headers['x-header-ea']).toBe('newCDA,taxonomy'); - }); - }); - - describe('Default Find', () => { - let entries; - const field = 'updated_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Count should not be present', () => { - expect(entries[1]).toBeFalsy(); - }); - - test('Entries should be sorted by default in descending order of updated_at', () => { - if (entries && entries.length && entries[0].length > 1) { - let prev = entries[0][0][field]; - const sortedCorrectly = entries[0].slice(1).every((entry) => { - const isValid = entry[field] <= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('Sorting', () => { - describe('.ascending()', () => { - let entries; - const field = 'updated_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.ascending(field).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Entries should be sorted in ascending order', () => { - if (entries && entries.length && entries[0].length > 1) { - let prev = entries[0][0][field]; - const sortedCorrectly = entries[0].slice(1).every((entry) => { - const isValid = entry[field] >= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('.descending()', () => { - let entries; - const field = 'created_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.descending(field).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Entries should be sorted in descending order', () => { - if (entries && entries.length && entries[0].length > 1) { - let prev = entries[0][0][field]; - const sortedCorrectly = entries[0].slice(1).every((entry) => { - const isValid = entry[field] <= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - }); - - describe('Parameters', () => { - describe('.addParam()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.addParam('include_count', 'true').toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Count should be present', () => { - expect(entries[1]).toBeTruthy(); - }); - }); - }); - - describe('Comparison', () => { - describe('.lessThan()', () => { - let entries; - const field = 'num_field'; - const value = 11; - - test('Should return entry in the resultset', async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - - const result = await Query.lessThan('num_field', value).toJSON().find(); - - entries = result; - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have num_field less than specified value', () => { - if (entries && entries.length && entries[0].length) { - const allLessThan = entries[0].every((entry) => entry[field] < value); - expect(allLessThan).toBe(true); - } - }); - }); - - describe('.lessThanOrEqualTo()', () => { - let entries; - const field = 'num_field'; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entries = await Query.lessThanOrEqualTo('num_field', value) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have num_field less than or equal to specified value', () => { - const allLessThanOrEqual = entries[0].every( - (entry) => entry[field] <= value - ); - expect(allLessThanOrEqual).toBe(true); - }); - - test('Entries should be sorted in descending order by default', () => { - const updatedAtField = 'updated_at'; - if (entries && entries.length && entries[0].length > 1) { - let prev = entries[0][0][updatedAtField]; - const sortedCorrectly = entries[0].slice(1).every((entry) => { - const isValid = entry[updatedAtField] <= prev; - prev = entry[updatedAtField]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('.greaterThan()', () => { - let entries; - const field = 'num_field'; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entries = await Query.greaterThan('num_field', value) - .ascending(field) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have num_field greater than specified value', () => { - const allGreaterThan = entries[0].every( - (entry) => entry[field] > value - ); - expect(allGreaterThan).toBe(true); - }); - - test('Entries should be sorted in ascending order', () => { - if (entries && entries.length && entries[0].length > 1) { - let prev = entries[0][0][field]; - const sortedCorrectly = entries[0].slice(1).every((entry) => { - const isValid = entry[field] >= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('.greaterThanOrEqualTo()', () => { - let entries; - const field = 'num_field'; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entries = await Query.greaterThanOrEqualTo('num_field', value) - .descending(field) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have num_field greater than or equal to specified value', () => { - const allGreaterThanOrEqual = entries[0].every( - (entry) => entry[field] >= value - ); - expect(allGreaterThanOrEqual).toBe(true); - }); - - test('Entries should be sorted in descending order', () => { - if (entries && entries.length && entries[0].length > 1) { - let prev = entries[0][0][field]; - const sortedCorrectly = entries[0].slice(1).every((entry) => { - const isValid = entry[field] <= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('.notEqualTo()', () => { - let entries; - const field = 'num_field'; - const value = 6; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entries = await Query.notEqualTo('num_field', value) - .descending(field) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have num_field not equal to specified value', () => { - const allNotEqual = entries[0].every((entry) => entry[field] !== value); - expect(allNotEqual).toBe(true); - }); - - test('Entries should be sorted in descending order', () => { - if (entries && entries.length && entries[0].length > 1) { - let prev = entries[0][0][field]; - const sortedCorrectly = entries[0].slice(1).every((entry) => { - const isValid = entry[field] <= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('.where() with boolean value (true)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.where('boolean', true).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Should return four entries in the resultset', () => { - expect(entries[0].length).toBe(4); - }); - - test('All entries should have boolean field set to true', () => { - const allTrue = entries[0].every((entry) => entry.boolean === true); - expect(allTrue).toBe(true); - }); - }); - - describe('.where() with boolean value (false)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.where('boolean', false).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Should return three entries in the resultset', () => { - expect(entries[0].length).toBe(3); - }); - - test('All entries should have boolean field set to false', () => { - const allFalse = entries[0].every((entry) => entry.boolean === false); - expect(allFalse).toBe(true); - }); - }); - - describe('.where() with empty string', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.where('title', '').toJSON().find(); - }); - - test('Should return zero entries in the resultset', () => { - expect(entries[0].length).toBe(0); - }); - }); - describe('.tags()', () => { - let entries; - const field = 'tags'; - const tags = ['tag1', 'tag2']; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.tags(tags).toJSON().find(); - }); - - test('Should return one or more entries in the resultset', () => { - expect(entries.length).toBeGreaterThanOrEqual(1); - }); - - test('All entries should have at least one of the specified tags', () => { - if (entries && entries.length && entries[0].length) { - const allHaveTags = entries[0].every((entry) => - Utils.arrayPresentInArray(tags, entry[field]) - ); - expect(allHaveTags).toBe(true); - } else { - // Skip this test if no entries were found - console.log('No entries found to check tags'); - } - }); - }); - }); - - describe('Array/Subset Tests', () => { - describe('.containedIn()', () => { - let entries; - const _in = ['source1', 'source2']; - const field = 'title'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.containedIn('title', _in).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Should return two entries in the resultset', () => { - expect(entries[0].length).toBe(2); - }); - - test('All entries should have title field contained in the specified values', () => { - if (entries && entries.length && entries[0].length) { - const allContained = entries[0].every((entry) => - _in.includes(entry[field]) - ); - expect(allContained).toBe(true); - } - }); - }); - - describe('.notContainedIn()', () => { - let entries; - const _in = ['source1', 'source2']; - const field = 'title'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.notContainedIn('title', _in).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Should return three entries in the resultset', () => { - expect(entries[0].length).toBe(5); - }); - - test('All entries should have title field not contained in the specified values', () => { - if (entries && entries.length && entries[0].length) { - const allNotContained = entries[0].every( - (entry) => !_in.includes(entry[field]) - ); - expect(allNotContained).toBe(true); - } - }); - }); - test('.exists() should return entries with the specified field', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const queryField = 'boolean'; - const field = 'updated_at'; - const entries = await Query.exists(queryField).toJSON().find(); - - // Check if entries are returned - expect(entries[0].length).toBeTruthy(); - - // Verify sorting order (descending on updated_at) - if (entries && entries.length && entries[0].length) { - let prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - const flag = entry[field] <= prev; - prev = entry[field]; - return flag; - }); - expect(_entries).toBe(true); - } - }); - - test('.notExists() should return entries without the specified field', async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - const queryField = 'isspecial'; - const field = 'updated_at'; - const entries = await Query.notExists(queryField).toJSON().find(); - - // Check if entries are returned - expect('entries' in entries).toBeTruthy(); - - // Verify sorting order if entries exist - if (entries && entries.length && entries[0].length) { - const prev = entries[0][0][field]; - const _entries = entries[0].every(function (entry) { - return entry[field] <= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - - describe('Pagination Tests', () => { - describe('.skip()', () => { - let allEntries; - let skippedEntries; - const field = 'updated_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - allEntries = await Query.toJSON().find(); - - const SkipQuery = Stack.ContentType(contentTypes.source).Query(); - skippedEntries = await SkipQuery.skip(1).toJSON().find(); - }); - - test('All entries should be present in the resultset', () => { - expect(allEntries[0].length).toBeTruthy(); - }); - - test('Skipped entries should be present in the resultset', () => { - expect(skippedEntries[0].length).toBeGreaterThanOrEqual(2); - }); - - test('Skipped entries should match all entries with first skipped', () => { - expect(skippedEntries[0]).toEqual(allEntries[0].slice(1)); - }); - - test('Skipped entries should maintain sorting order', () => { - if ( - skippedEntries && - skippedEntries.length && - skippedEntries[0].length > 1 - ) { - let prev = skippedEntries[0][0][field]; - const sortedCorrectly = skippedEntries[0].slice(1).every((entry) => { - const isValid = entry[field] <= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('.limit()', () => { - let allEntries; - let limitedEntries; - const field = 'updated_at'; - const limitNumber = 2; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - allEntries = await Query.toJSON().find(); - - const LimitQuery = Stack.ContentType(contentTypes.source).Query(); - limitedEntries = await LimitQuery.limit(limitNumber).toJSON().find(); - }); - - test('All entries should be present in the resultset', () => { - expect(allEntries[0].length).toBeTruthy(); - }); - - test('Limited entries should be present in the resultset', () => { - expect(limitedEntries[0].length).toBeTruthy(); - }); - - test('Limited entries should match first N entries from all entries', () => { - expect(limitedEntries[0]).toEqual(allEntries[0].slice(0, limitNumber)); - }); - - test('Limited entries should maintain sorting order', () => { - if ( - limitedEntries && - limitedEntries.length && - limitedEntries[0].length > 1 - ) { - let prev = limitedEntries[0][0][field]; - const sortedCorrectly = limitedEntries[0].slice(1).every((entry) => { - const isValid = entry[field] <= prev; - prev = entry[field]; - return isValid; - }); - expect(sortedCorrectly).toBe(true); - } - }); - }); - - describe('.count()', () => { - let count; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - count = await Query.count().toJSON().find(); - }); - - test('Entries present in the resultset', () => { - expect(count).toBeTruthy(); - }); - }); - }); - - describe('Logical Operations', () => { - describe('.or() - Query Objects', () => { - let entries; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .where('title', 'source2'); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', true); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entries = await Query.or(Query1, Query2).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Should return 1 entries in the resultset', () => { - expect(entries[0].length).toBe(5); - }); - - test('All entries should satisfy the OR condition', () => { - if (entries && entries.length && entries[0].length) { - const _entries = entries[0].every(function (entry) { - return ~(entry.title === 'source1' || entry.boolean === true); - }); - expect(_entries).toBe(true); - } - }); - }); - - describe('.and() - Query Objects', () => { - let entries; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .where('title', 'source1'); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', true); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entries = await Query.and(Query1, Query2).toJSON().find(); - }); - - test('Should return one entry in the resultset', () => { - expect(entries[0].length).toBe(1); - }); - - test('All entries should satisfy the AND condition', () => { - if (entries && entries.length && entries[0].length) { - const allMatchCondition = entries[0].every( - (entry) => entry.title === 'source1' && entry.boolean === true - ); - expect(allMatchCondition).toBe(true); - } - }); - }); - - describe('.query() - Raw query', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.query({ - $or: [{ title: 'source2' }, { boolean: 'true' }] - }) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('Should return two entries in the resultset', () => { - expect(entries[0].length).toBe(1); - }); - - test('All entries should satisfy the OR condition', () => { - if (entries && entries.length && entries[0].length) { - const allMatchCondition = entries[0].every( - (entry) => entry.title === 'source2' || entry.boolean === false - ); - expect(allMatchCondition).toBe(true); - } - }); - }); - - describe('Search Tests', () => { - describe('.search()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.toJSON().search('source2').find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - }); - - describe('Including Additional Data Tests', () => { - describe('.includeCount() and .includeContentType()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeCount() - .includeContentType() - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('ContentType should be present in the resultset', () => { - expect(entries[1]).toBeTruthy(); - }); - - test('ContentType title should exist', () => { - expect(entries[1].title).toBeDefined(); - }); - - test('ContentType uid should match requested content type', () => { - expect(entries[1].uid).toBe(contentTypes.source); - }); - - test('Count should be present in the resultset', () => { - expect(entries[2]).toBeTruthy(); - }); - }); - - describe('.includeEmbeddedItems()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeEmbeddedItems().toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('.includeSchema() and .includeContentType()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeSchema() - .includeContentType() - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('ContentType should be present in the resultset', () => { - expect(entries[1]).toBeTruthy(); - }); - - test('ContentType title should exist', () => { - expect(entries[1].title).toBeDefined(); - }); - - test('ContentType uid should match requested content type', () => { - expect(entries[1].uid).toBe(contentTypes.source); - }); - }); - - describe('.includeCount(), .includeSchema() and .includeContentType()', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeCount() - .includeSchema() - .includeContentType() - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('ContentType should be present in the resultset', () => { - expect(entries[1]).toBeTruthy(); - }); - - test('ContentType title should exist', () => { - expect(entries[1].title).toBeDefined(); - }); - - test('ContentType uid should match requested content type', () => { - expect(entries[1].uid).toBe(contentTypes.source); - }); - - test('Count should be present in the resultset', () => { - expect(entries[2]).toBeTruthy(); - }); - }); - }); - - describe('Localization Tests', () => { - describe('find: without fallback', () => { - let entries; - const _in = ['ja-jp']; - - beforeAll(async () => { - entries = await Stack.ContentType(contentTypes.source) - .Query() - .language('ja-jp') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have the correct locale', () => { - if (entries && entries[0].length) { - const allHaveCorrectLocale = entries[0].every((entry) => - _in.includes(entry.publish_details.locale) - ); - expect(allHaveCorrectLocale).toBe(true); - } - }); - }); - - describe('find: with fallback', () => { - let entries; - const _in = ['ja-jp', 'en-us']; - - beforeAll(async () => { - entries = await Stack.ContentType(contentTypes.source) - .Query() - .language('ja-jp') - .includeFallback() - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have locale from the allowed fallback list', () => { - if (entries && entries[0].length) { - const allHaveCorrectLocale = entries[0].every((entry) => - _in.includes(entry.publish_details.locale) - ); - expect(allHaveCorrectLocale).toBe(true); - } - }); - }); - }); - - describe('Global Field Tests', () => { - describe('.getContentTypes()', () => { - let entries; - - beforeAll(async () => { - entries = await Stack.getContentTypes({ - include_global_field_schema: true - }); - }); - - test('Global field schema should be present when applicable', () => { - for (let i = 0; i < entries.content_types[0].schema.length; i++) { - if ( - entries.content_types[0].schema[i].data_type === 'global_field' - ) { - expect(entries[1].schema[i].schema).toBeDefined(); - } - } - }); - }); - }); - - describe('Field Selection Tests', () => { - describe('.only() - Single String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.only('title').toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should contain only title and uid fields', () => { - const allHaveCorrectFields = entries[0].every( - (entry) => - Object.keys(entry).length === 2 && - 'title' in entry && - 'uid' in entry - ); - expect(allHaveCorrectFields).toBe(true); - }); - }); - - describe('.only() - Multiple String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.only('BASE', 'title').toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should contain only title and uid fields', () => { - const allHaveCorrectFields = entries[0].every( - (entry) => - Object.keys(entry).length === 2 && - 'title' in entry && - 'uid' in entry - ); - expect(allHaveCorrectFields).toBe(true); - }); - }); - - describe('.only() - Array Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.only(['title', 'url']).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should contain only title, url, and uid fields', () => { - const allHaveCorrectFields = entries[0].every( - (entry) => - Object.keys(entry).length === 3 && - 'title' in entry && - 'url' in entry && - 'uid' in entry - ); - expect(allHaveCorrectFields).toBe(true); - }); - }); - - describe('.only() - For the reference - String', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeReference('reference') - .only('BASE', ['reference']) - .only('reference', 'title') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should contain reference field', () => { - const allHaveReference = entries[0].every( - (entry) => 'reference' in entry - ); - expect(allHaveReference).toBe(true); - }); - }); - - describe('.only() - For the reference - Array', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeReference('reference') - .only('BASE', ['reference']) - .only('reference', ['title']) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should contain reference field', () => { - const allHaveReference = entries[0].every( - (entry) => 'reference' in entry - ); - expect(allHaveReference).toBe(true); - }); - }); - }); - - describe('Field Exclusion Tests', () => { - describe('.except() - Single String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.except('title').toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should not have title field', () => { - const allExcluded = entries[0].every( - (entry) => entry && !('title' in entry) - ); - expect(allExcluded).toBe(true); - }); - }); - - describe('.except() - Multiple String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.except('BASE', 'title').toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should not have title field', () => { - const allExcluded = entries[0].every( - (entry) => entry && !('title' in entry) - ); - expect(allExcluded).toBe(true); - }); - }); - - describe('.except() - Array of String Parameter', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.except(['title', 'file']).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should not have title and file fields', () => { - const allExcluded = entries[0].every( - (entry) => entry && !('title' in entry) && !('file' in entry) - ); - expect(allExcluded).toBe(true); - }); - }); - - describe('.except() - For the reference - String', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeReference('reference') - .only('BASE', ['reference']) - .except('reference', 'title') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have reference field', () => { - const allHaveReference = entries[0].every( - (entry) => entry && 'reference' in entry - ); - expect(allHaveReference).toBe(true); - }); - - test('All entries should have uid field', () => { - const allHaveUID = entries[0].every( - (entry) => entry && 'uid' in entry - ); - expect(allHaveUID).toBe(true); - }); - - test('All references should not have title field', () => { - let allReferencesExcluded = true; - - entries[0].forEach((entry) => { - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - entry.reference.forEach((reference) => { - if (reference && 'title' in reference) { - allReferencesExcluded = false; - } - }); - } - }); - - expect(allReferencesExcluded).toBe(true); - }); - }); - - describe('.except() - For the reference - Array', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entries = await Query.includeReference('reference') - .only('BASE', ['reference']) - .except('reference', ['title']) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - - test('All entries should have reference field', () => { - const allHaveReference = entries[0].every( - (entry) => entry && 'reference' in entry - ); - expect(allHaveReference).toBe(true); - }); - - test('All entries should have uid field', () => { - const allHaveUID = entries[0].every( - (entry) => entry && 'uid' in entry - ); - expect(allHaveUID).toBe(true); - }); - - test('All references should not have title field', () => { - let allReferencesExcluded = true; - - entries[0].forEach((entry) => { - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - entry.reference.forEach((reference) => { - if (reference && 'title' in reference) { - allReferencesExcluded = false; - } - }); - } - }); - - expect(allReferencesExcluded).toBe(true); - }); - }); - }); - - describe('Taxonomies Endpoint Tests', () => { - describe('Get Entries With One Term', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.Taxonomies(); - entries = await Query.where('taxonomies.one', 'term_one') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Any Term ($in)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.Taxonomies(); - entries = await Query.containedIn('taxonomies.one', [ - 'term_one', - 'term_two' - ]) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Any Term ($or)', () => { - let entries; - - beforeAll(async () => { - const Query1 = Stack.Taxonomies().where('taxonomies.one', 'term_one'); - const Query2 = Stack.Taxonomies().where('taxonomies.two', 'term_two'); - const Query = Stack.Taxonomies(); - - entries = await Query.or(Query1, Query2).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With All Terms ($and)', () => { - let entries; - - beforeAll(async () => { - const Query1 = Stack.Taxonomies().where('taxonomies.one', 'term_one'); - const Query2 = Stack.Taxonomies().where('taxonomies.two', 'term_two'); - const Query = Stack.Taxonomies(); - - entries = await Query.and(Query1, Query2).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Any Taxonomy Terms ($exists)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.Taxonomies(); - entries = await Query.exists('taxonomies.one').toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - }); - - describe('Content Type Taxonomies Query Tests', () => { - describe('Get Entries With One Term', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.where('taxonomies.one', 'term_one') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Any Term ($in)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.containedIn('taxonomies.one', [ - 'term_one', - 'term_two' - ]) - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Any Term ($or)', () => { - let entries; - - beforeAll(async () => { - const Query1 = Stack.ContentType('source') - .Query() - .where('taxonomies.one', 'term_one'); - const Query2 = Stack.ContentType('source') - .Query() - .where('taxonomies.two', 'term_two'); - const Query = Stack.ContentType('source').Query(); - - entries = await Query.or(Query1, Query2).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With All Terms ($and)', () => { - let entries; - - beforeAll(async () => { - const Query1 = Stack.ContentType('source') - .Query() - .where('taxonomies.one', 'term_one'); - const Query2 = Stack.ContentType('source') - .Query() - .where('taxonomies.two', 'term_two'); - const Query = Stack.ContentType('source').Query(); - - entries = await Query.and(Query1, Query2).toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Any Taxonomy Terms ($exists)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.exists('taxonomies.one').toJSON().find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Taxonomy Terms and Also Matching Its Children Term ($eq_below, level)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.equalAndBelow('taxonomies.one', 'term_one') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe("Get Entries With Taxonomy Terms Children's and Excluding the term itself ($below, level)", () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.below('taxonomies.one', 'term_one') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Taxonomy Terms and Also Matching Its Parent Term ($eq_above, level)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.equalAndAbove('taxonomies.one', 'term_one') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - - describe('Get Entries With Taxonomy Terms Parent and Excluding the term itself ($above, level)', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.above('taxonomies.one', 'term_one_child') - .toJSON() - .find(); - }); - - test('Should return entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - }); - describe('Variants Tests', () => { - describe('Variants in entry', () => { - let entries; - - beforeAll(async () => { - const Query = Stack.ContentType('source').Query(); - entries = await Query.variants(['variant_entry_1', 'variant_entry_2']) - .toJSON() - .find(); - }); - - test('Should return variant entries in the resultset', () => { - expect(entries[0].length).toBeTruthy(); - }); - }); - }); - }); -}); diff --git a/test/entry/findone-result-wrapper.js b/test/entry/findone-result-wrapper.js deleted file mode 100755 index fffcab65..00000000 --- a/test/entry/findone-result-wrapper.js +++ /dev/null @@ -1,895 +0,0 @@ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const Utils = require('./utils.js'); -const init = require('../config.js'); - -const contentTypes = init.contentTypes; - -let Stack; - -describe('FindOne Tests', () => { - // Setup - Initialize the Contentstack Stack Instance - beforeAll((done) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(done, 1000); - }); - - describe('Default FindOne', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.toJSON().findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - // SORTING TESTS - describe('Sorting', () => { - describe('Ascending', () => { - let entry; - const field = 'updated_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.ascending(field).toJSON().findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Descending', () => { - let entry; - const field = 'created_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.descending(field).toJSON().findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - }); - - // COMPARISON TESTS - describe('Comparison', () => { - describe('lessThan', () => { - let entry; - const field = 'num_field'; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entry = await Query.lessThan(field, value).toJSON().findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - - test('num_field should be less than specified value', () => { - expect(entry[field]).toBeLessThan(value); - }); - }); - - describe('lessThanOrEqualTo', () => { - let entry; - const field = 'num_field'; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entry = await Query.lessThanOrEqualTo(field, value).toJSON().findOne(); - }); - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - test('num_field should be less than or equal to specified value', () => { - expect(entry[field]).toBeLessThanOrEqual(value); - }); - }); - - describe('greaterThan', () => { - let entry; - const field = 'num_field'; - const value = 6; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entry = await Query.greaterThan(field, value) - .ascending(field) - .toJSON() - .findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - - test('num_field should be greater than specified value', () => { - expect(entry[field]).toBeGreaterThan(value); - }); - }); - - describe('greaterThanOrEqualTo', () => { - let entry; - const field = 'num_field'; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entry = await Query.greaterThanOrEqualTo(field, value) - .descending(field) - .toJSON() - .findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - - test('num_field should be greater than or equal to specified value', () => { - expect(entry[field]).toBeGreaterThanOrEqual(value); - }); - }); - - describe('notEqualTo', () => { - let entry; - const field = 'num_field'; - const value = 6; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entry = await Query.notEqualTo(field, value) - .descending(field) - .toJSON() - .findOne(); - }); - - test('num_field should not be equal to specified value', () => { - expect(entry[field]).not.toBe(value); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - }); - - // ARRAY/SUBSET TESTS - describe('Array/Subset', () => { - describe('containedIn', () => { - let entry; - const _in = ['source1', 'source2']; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.containedIn('title', _in).toJSON().findOne(); - }); - - test('Entry title should be in the specified values', () => { - expect(_in).toContain(entry.title); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('notContainedIn', () => { - let entry; - const _in = ['source1']; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.notContainedIn('title', _in).toJSON().findOne(); - }); - - test('Should either return an entry with matching criteria or an expected error', () => { - if (entry) { - expect(entry.title).toBeDefined(); - expect(_in).not.toContain(entry.title); - } else { - expect(error).toEqual({ - error_code: 141, - error_message: "The requested entry doesn't exist." - }); - } - }); - - test('If entry exists, it should have uid', () => { - if (entry) { - expect(entry.uid).toBeDefined(); - } - }); - - test('If entry exists, it should have locale', () => { - if (entry) { - expect(entry.locale).toBeDefined(); - } - }); - - test('If entry exists, it should have publish_details', () => { - if (entry) { - expect(entry.publish_details).toBeDefined(); - } - }); - }); - }); - - // ELEMENT EXISTS TESTS - describe('Element Existence', () => { - describe('exists', () => { - let entry; - const queryField = 'boolean'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.exists(queryField).toJSON().findOne(); - }); - - test('Entry should have the queried field', () => { - expect(typeof entry[queryField]).not.toBe('undefined'); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('notExists', () => { - let entry; - const queryField = 'isspecial'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.notExists(queryField).toJSON().findOne(); - }); - - test('Should handle either success or error case', () => { - if (entry) { - expect(typeof entry[queryField]).toBe('undefined'); - } else { - expect(error).toEqual({ - error_code: 141, - error_message: "The requested entry doesn't exist." - }); - } - }); - - test('If entry exists, it should have uid', () => { - if (entry) { - expect(entry.uid).toBeDefined(); - } - }); - - test('If entry exists, it should have locale', () => { - if (entry) { - expect(entry.locale).toBeDefined(); - } - }); - - test('If entry exists, it should have publish_details', () => { - if (entry) { - expect(entry.publish_details).toBeDefined(); - } - }); - }); - }); - describe('Pagination', () => { - describe('skip', () => { - let allEntries; - let skippedEntry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - allEntries = await Query.toJSON().find(); - - const skipQuery = Stack.ContentType(contentTypes.source).Query(); - skippedEntry = await skipQuery.skip(1).toJSON().findOne(); - }); - - test('Should have entries in the result set', () => { - expect(allEntries.length).toBeTruthy(); - }); - - test('Should get correct skipped entry', () => { - expect(skippedEntry).toEqual(allEntries[0][1]); - }); - }); - }); - - describe('Logical Operations', () => { - describe('OR Query Objects', () => { - let entry; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .containedIn('title', ['source1']); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', 'false'); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entry = await Query.or(Query1, Query2).toJSON().findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('AND Query Objects', () => { - let entry; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .containedIn('title', ['source1']); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', true); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entry = await Query.and(Query1, Query2).toJSON().findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Raw Query', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.query({ - $or: [{ title: 'source1' }, { boolean: 'false' }] - }) - .toJSON() - .findOne(); - }); - - test('Entry should satisfy OR condition', () => { - expect( - entry.title === 'source1' || entry.boolean === false - ).toBeTruthy(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - }); - - describe('Tags', () => { - let entry; - const tags = ['tag1', 'tag2']; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.tags(tags).toJSON().findOne(); - }); - - test('Tags specified should be found in the result', () => { - expect(Utils.arrayPresentInArray(tags, entry.tags) > 0).toBe(true); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Search', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.search('source1').toJSON().findOne(); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Regex', () => { - let entry; - const field = 'title'; - const regex = { - pattern: '^source', - options: 'i' - }; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.regex(field, regex.pattern, regex.options) - .toJSON() - .findOne(); - }); - - test('Entry field should match the regex pattern', () => { - const regExp = new RegExp(regex.pattern, regex.options); - expect(regExp.test(entry[field])).toBe(true); - }); - - test('Should return an entry with uid, locale, publish_details', () => { - expect(entry).toBeDefined(); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Localization', () => { - describe('Without Fallback', () => { - let entry; - const _in = ['ja-jp']; - - beforeAll(async () => { - entry = await Stack.ContentType(contentTypes.source) - .Query() - .language('ja-jp') - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have correct locale in publish_details', () => { - expect(_in).toContain(entry.publish_details.locale); - }); - }); - - describe('With Fallback', () => { - let entry; - const _in = ['ja-jp', 'en-us']; - - beforeAll(async () => { - entry = await Stack.ContentType(contentTypes.source) - .Query() - .language('ja-jp') - .includeFallback() - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have locale from allowed fallback list', () => { - expect(_in).toContain(entry.publish_details.locale); - }); - }); - }); - describe('Including References', () => { - describe('includeReference - String', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('All present references should be included as objects', () => { - expect( - entry && entry.reference && typeof entry.reference === 'object' - ).toBe(true); - }); - }); - - describe('includeReference - Array', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference(['reference', 'other_reference']) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('All present references should be included as objects', () => { - const condition = - entry && - entry.reference && - typeof entry.reference === 'object' && - entry.other_reference && - typeof entry.other_reference === 'object'; - expect(condition).toBe(true); - }); - }); - }); - - describe('Including Schema', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeSchema().toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - }); - - describe('Including ContentType', () => { - let entry; - let contentType; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - [entry, contentType] = await new Promise((resolve, reject) => { - Query.includeContentType() - .toJSON() - .findOne() - .then((entry, contentType) => resolve([entry, contentType]), reject); - }); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('ContentType should not be present', () => { - expect(typeof contentType).toBe('undefined'); - }); - }); - - describe('Including Schema and ContentType', () => { - let entry; - let contentType; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - [entry, contentType] = await new Promise((resolve, reject) => { - Query.includeSchema() - .includeContentType() - .toJSON() - .findOne() - .then((entry, contentType) => resolve([entry, contentType]), reject); - }); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('ContentType should not be present', () => { - expect(typeof contentType).toBe('undefined'); - }); - }); - - describe('Field Selection - Only', () => { - describe('only - Single String Parameter', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.only('title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should only contain title and uid fields', () => { - expect(Object.keys(entry).length).toBe(2); - expect(entry).toHaveProperty('title'); - expect(entry).toHaveProperty('uid'); - }); - }); - - describe('only - Multiple String Parameters', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.only('BASE', 'title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should only contain title and uid fields', () => { - expect(Object.keys(entry).length).toBe(2); - expect(entry).toHaveProperty('title'); - expect(entry).toHaveProperty('uid'); - }); - }); - - describe('only - Array Parameter', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.only(['title', 'url']).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should contain title, url, and uid fields', () => { - expect(Object.keys(entry).length).toBe(3); - expect(entry).toHaveProperty('title'); - expect(entry).toHaveProperty('url'); - expect(entry).toHaveProperty('uid'); - }); - }); - - describe('only - For reference - String', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', 'reference') - .only('reference', 'title') - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Reference fields should be properly filtered', () => { - let hasProperReferences = false; - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - hasProperReferences = entry.reference.every( - (ref) => ref && 'title' in ref && 'uid' in ref - ); - } else { - hasProperReferences = true; // No references or empty references is valid - } - expect(hasProperReferences).toBe(true); - }); - }); - - describe('only - For reference - Array', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', ['reference']) - .only('reference', ['title']) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('References should have only specified fields', () => { - let hasProperReferences = false; - if (entry && entry.reference) { - if (Array.isArray(entry.reference)) { - if (entry.reference.length === 0) { - hasProperReferences = true; - } else { - hasProperReferences = entry.reference.every( - (ref) => ref && 'title' in ref && 'uid' in ref - ); - } - } else { - hasProperReferences = true; - } - } else { - hasProperReferences = true; - } - expect(hasProperReferences).toBe(true); - }); - }); - }); - - describe('Field Selection - Except', () => { - describe('except - Single String Parameter', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.except('title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should not contain the excluded field', () => { - expect(entry).not.toHaveProperty('title'); - }); - }); - - describe('except - Multiple String Parameters', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.except('BASE', 'title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should not contain the excluded field', () => { - expect(entry).not.toHaveProperty('title'); - }); - }); - - describe('except - Array of String Parameters', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.except(['title', 'url']).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should not contain the first excluded field', () => { - expect(entry).not.toHaveProperty('title'); - }); - - test('Entry should not contain the second excluded field', () => { - expect(entry).not.toHaveProperty('url'); - }); - }); - - describe('except - For the reference - String', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', 'reference') - .except('reference', 'title') - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('References should not contain the excluded field', () => { - let hasProperExclusions = false; - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - hasProperExclusions = entry.reference.every( - (ref) => ref && !('title' in ref) - ); - } else { - // No references is valid for this test - hasProperExclusions = true; - } - expect(hasProperExclusions).toBe(true); - }); - }); - - describe('except - For the reference - Array', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', ['reference']) - .except('reference', ['title']) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('References should not contain the excluded field', () => { - let hasProperExclusions = false; - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - hasProperExclusions = entry.reference.every( - (ref) => ref && !('title' in ref) - ); - } else { - hasProperExclusions = true; - } - expect(hasProperExclusions).toBe(true); - }); - }); - }); -}); diff --git a/test/entry/findone.js b/test/entry/findone.js deleted file mode 100755 index 99d4b403..00000000 --- a/test/entry/findone.js +++ /dev/null @@ -1,876 +0,0 @@ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('../config.js'); - -const contentTypes = init.contentTypes; - -let Stack; - -describe('FindOne Tests', () => { - // Setup - Initialize the Contentstack Stack Instance - beforeAll((done) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(done, 1000); - }); - - describe('Default FindOne', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Sorting', () => { - describe('Ascending', () => { - let entry; - const field = 'created_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.ascending(field).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Descending', () => { - let entry; - const field = 'created_at'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.descending(field).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - }); - - describe('Comparison', () => { - describe('lessThan', () => { - let entry; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entry = await Query.lessThan('num_field', value).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('num_field should be less than specified value', () => { - expect(entry.num_field).toBeLessThan(value); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('lessThanOrEqualTo', () => { - let entry; - const value = 11; - - beforeAll(async () => { - const Query = Stack.ContentType( - contentTypes.numbers_content_type - ).Query(); - entry = await Query.lessThanOrEqualTo('num_field', value) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('num_field should be less than or equal to specified value', () => { - expect(entry.num_field).toBeLessThanOrEqual(value); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - }); - - describe('Array/Subset', () => { - describe('containedIn', () => { - let entry; - const _in = ['source1', 'source2']; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.containedIn('title', _in).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry title should be in the specified values', () => { - expect(_in).toContain(entry.title); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('notContainedIn', () => { - let entry; - const _in = ['source1', 'source2', 'source3', 'source4']; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.notContainedIn('title', _in).toJSON().findOne(); - }); - - test('Should either return an entry or an expected error', () => { - if (entry) { - expect(entry).toBeDefined(); - expect(_in).not.toContain(entry.title); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - } else { - expect(error).toEqual({ - error_code: 141, - error_message: "The requested entry doesn't exist." - }); - } - }); - }); - }); - - describe('Element Existence', () => { - describe('exists', () => { - let entry; - const queryField = 'boolean'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.exists(queryField).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have the queried field', () => { - expect(typeof entry[queryField]).not.toBe('undefined'); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('notExists', () => { - let entry; - const queryField = 'isspecial'; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.notExists(queryField).toJSON().findOne(); - }); - - test('Should either have entry without field or proper error', () => { - if (entry) { - expect(typeof entry[queryField]).toBe('undefined'); - expect(entry.uid).toBeDefined(); - expect(entry.locale).toBeDefined(); - expect(entry.publish_details).toBeDefined(); - } else { - expect(error).toEqual({ - error_code: 141, - error_message: "The requested entry doesn't exist." - }); - } - }); - }); - }); - - describe('Pagination', () => { - describe('skip', () => { - let allEntries; - let skippedEntry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - allEntries = await Query.toJSON().find(); - - const SkipQuery = Stack.ContentType(contentTypes.source).Query(); - skippedEntry = await SkipQuery.skip(1).toJSON().findOne(); - }); - - test('Should have entries in the result set', () => { - expect(allEntries.length).toBeTruthy(); - }); - - test('Should get correct skipped entry', () => { - expect(skippedEntry).toEqual(allEntries[0][1]); - }); - }); - }); - - describe('Logical Operations', () => { - describe('OR Query Objects', () => { - let entry; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .containedIn('title', ['source1', 'source2']); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', true); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entry = await Query.or(Query1, Query2).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should satisfy the OR condition', () => { - const condition = - entry.title === 'source1' || - entry.title === 'source2' || - entry.boolean === true; - expect(condition).toBeTruthy(); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('AND Query Objects', () => { - let entry; - - beforeAll(async () => { - const Query1 = Stack.ContentType(contentTypes.source) - .Query() - .where('title', 'source1'); - const Query2 = Stack.ContentType(contentTypes.source) - .Query() - .where('boolean', true); - const Query = Stack.ContentType(contentTypes.source).Query(); - - entry = await Query.and(Query1, Query2).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should satisfy the AND condition', () => { - const condition = entry.title === 'source1' && entry.boolean === true; - expect(condition).toBeTruthy(); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - - describe('Raw Query', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.query({ - $or: [{ title: 'source1' }, { boolean: 'false' }] - }) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should satisfy the OR condition in raw query', () => { - const condition = entry.title === 'source1' || entry.boolean === false; - expect(condition).toBeTruthy(); - }); - - test('Entry should have uid', () => { - expect(entry.uid).toBeDefined(); - }); - - test('Entry should have locale', () => { - expect(entry.locale).toBeDefined(); - }); - - test('Entry should have publish_details', () => { - expect(entry.publish_details).toBeDefined(); - }); - }); - }); - - describe('Localization', () => { - describe('Without Fallback', () => { - let entry; - const _in = ['ja-jp']; - - beforeAll(async () => { - entry = await Stack.ContentType(contentTypes.source) - .Query() - .language('ja-jp') - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have correct locale in publish_details', () => { - expect(_in).toContain(entry.publish_details.locale); - }); - }); - - describe('With Fallback', () => { - let entry; - const _in = ['ja-jp', 'en-us']; - - beforeAll(async () => { - entry = await Stack.ContentType(contentTypes.source) - .Query() - .language('ja-jp') - .includeFallback() - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should have locale from allowed fallback list', () => { - expect(_in).toContain(entry.publish_details.locale); - }); - }); - }); - describe('Including References', () => { - describe('includeReference - String', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('All present references should be included as objects', () => { - expect( - entry && entry.reference && typeof entry.reference === 'object' - ).toBe(true); - }); - }); - - describe('includeReference - Array', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference(['reference', 'other_reference']) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('All present references should be included as objects', () => { - const condition = - entry && - entry.reference && - typeof entry.reference === 'object' && - entry.other_reference && - typeof entry.other_reference === 'object'; - expect(condition).toBe(true); - }); - }); - }); - - describe('Including Schema', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeSchema().toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - }); - - describe('Including ContentType', () => { - let entry; - let contentType; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - [entry, contentType] = await new Promise((resolve, reject) => { - Query.includeContentType() - .toJSON() - .findOne() - .then((entry, contentType) => resolve([entry, contentType]), reject); - }); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('ContentType should not be present', () => { - expect(typeof contentType).toBe('undefined'); - }); - }); - - describe('Including Schema and ContentType', () => { - let entry; - let contentType; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - [entry, contentType] = await new Promise((resolve, reject) => { - Query.includeSchema() - .includeContentType() - .toJSON() - .findOne() - .then((entry, contentType) => resolve([entry, contentType]), reject); - }); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('ContentType should not be present', () => { - expect(typeof contentType).toBe('undefined'); - }); - }); - - describe('Field Selection - Only', () => { - describe('only - Single String Parameter', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.only('title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should only contain title and uid fields', () => { - expect(Object.keys(entry).length).toBe(2); - expect(entry).toHaveProperty('title'); - expect(entry).toHaveProperty('uid'); - }); - }); - - describe('only - Multiple String Parameters', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.only('BASE', 'title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should only contain title and uid fields', () => { - expect(Object.keys(entry).length).toBe(2); - expect(entry).toHaveProperty('title'); - expect(entry).toHaveProperty('uid'); - }); - }); - - describe('only - Array Parameter', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.only(['title', 'url']).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should contain title, url, and uid fields', () => { - expect(Object.keys(entry).length).toBe(3); - expect(entry).toHaveProperty('title'); - expect(entry).toHaveProperty('url'); - expect(entry).toHaveProperty('uid'); - }); - }); - - describe('only - For reference - String', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', 'reference') - .only('reference', 'title') - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('References should have only specified fields', () => { - let flag = false; - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - flag = entry.reference.every( - (reference) => - reference && 'title' in reference && 'uid' in reference - ); - } else { - flag = true; - } - expect(flag).toBe(true); - }); - }); - - describe('only - For reference - Array', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', ['reference']) - .only('reference', ['title']) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('References should have only specified fields', () => { - let flag = false; - if (entry && entry.reference) { - if (entry.reference.length) { - if (entry.reference.length === 0) { - flag = true; - } else { - flag = entry.reference.every( - (reference) => - reference && 'title' in reference && 'uid' in reference - ); - } - } else { - flag = true; - } - } else { - flag = true; - } - expect(flag).toBe(true); - }); - }); - }); - - describe('Field Selection - Except', () => { - describe('except - Single String Parameter', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.except('title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should not contain the title field', () => { - expect(entry).not.toHaveProperty('title'); - }); - }); - - describe('except - Multiple String Parameters', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.except('BASE', 'title').toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should not contain the title field', () => { - expect(entry).not.toHaveProperty('title'); - }); - }); - - describe('except - Array Parameter', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.except(['title', 'file']).toJSON().findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('Entry should not contain the title field', () => { - expect(entry).not.toHaveProperty('title'); - }); - - test('Entry should not contain the file field', () => { - expect(entry).not.toHaveProperty('file'); - }); - }); - - describe('except - For reference - String', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', 'reference') - .except('reference', 'title') - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('References should not contain the specified field', () => { - let flag = false; - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - flag = entry.reference.every( - (reference) => reference && !('title' in reference) - ); - } - expect(flag).toBeTruthy(); - }); - }); - - describe('except - For reference - Array', () => { - let entry; - - beforeAll(async () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - entry = await Query.includeReference('reference') - .only('BASE', ['reference']) - .except('reference', ['title']) - .toJSON() - .findOne(); - }); - - test('Should return an entry', () => { - expect(entry).toBeDefined(); - }); - - test('References should not contain the specified field', () => { - let flag = false; - if ( - entry && - entry.reference && - typeof entry.reference === 'object' - ) { - flag = entry.reference.every( - (reference) => reference && !('title' in reference) - ); - } - expect(flag).toBeTruthy(); - }); - }); - }); - - describe('HTTP Error Handling', () => { - describe('422 Unprocessable Entity Error', () => { - let success = false; - let error = null; - - beforeAll(async () => { - try { - const Query = Stack.ContentType('invalid_content_type').Query(); - await Query.toJSON().findOne(); - success = true; - } catch (err) { - error = err; - } - }); - - test('Should not succeed', () => { - expect(success).toBe(false); - }); - - test('Should return HTTP status 422', () => { - expect(error.http_code).toBe(422); - }); - - test('Should have appropriate error message', () => { - expect(error.http_message).toBeTruthy(); - }); - }); - - describe('412 Unauthorized Error', () => { - let success = false; - let error = null; - - beforeAll(async () => { - try { - Stack.headers = { authorization: 'InvalidAPIKey' }; // Simulating an invalid API key - const Query = Stack.ContentType(contentTypes.source).Query(); - await Query.toJSON().findOne(); - success = true; - } catch (err) { - error = err; - } finally { - // Reset headers for subsequent tests - Stack.headers = {}; - } - }); - - test('Should not succeed', () => { - expect(success).toBe(false); - }); - - test('Should return HTTP status 412', () => { - expect(error.http_code).toBe(412); - }); - - test('Should have appropriate error message', () => { - expect(error.http_message).toBeTruthy(); - }); - }); - }); -}); diff --git a/test/entry/spread.js b/test/entry/spread.js deleted file mode 100755 index c3985b49..00000000 --- a/test/entry/spread.js +++ /dev/null @@ -1,286 +0,0 @@ -/** - * Created by Aamod Pisat on 09-06-2017. - */ -'use strict'; -/* - * Module Dependencies. - */ -const Contentstack = require('../../dist/node/contentstack.js'); -const init = require('../config.js'); - -const contentTypes = init.contentTypes; - -let Stack; - -describe('Spread Method Tests', () => { - // Setup - Initialize the Contentstack Stack Instance - beforeAll((done) => { - Stack = Contentstack.Stack(init.stack); - Stack.setHost(init.host); - setTimeout(done, 1000); - }); - - describe('Entries as first argument', () => { - const field = 'updated_at'; - - test('Should have entries', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.limit(1) - .toJSON() - .find() - .spread(function success (entries) { - expect(entries.length).toBeTruthy(); - }); - }); - - test('Should maintain default sorting of descending updated_at', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.limit(1) - .toJSON() - .find() - .spread(function success (entries) { - if (entries && entries.length) { - let prev = entries[0][field]; - const _entries = entries.every((entry) => { - prev = entry[field]; - return entry[field] <= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - }); - - describe('With entries and count argument', () => { - const field = 'updated_at'; - - test('Should have entries as first parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .toJSON() - .find() - .spread((entries) => { - expect(entries.length).toBeTruthy(); - }); - }); - - test('Should have count as second parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .toJSON() - .find() - .spread((_, count) => { - expect(count).toBeTruthy(); - }); - }); - - test('Should maintain default sorting of descending updated_at', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .toJSON() - .find() - .spread((entries) => { - if (entries && entries.length) { - let prev = entries[0][field]; - const _entries = entries.every((entry) => { - prev = entry[field]; - return entry[field] <= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - }); - - describe('With entries, schema and count argument (includeSchema first)', () => { - const field = 'updated_at'; - - test('Should have entries as first parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeSchema() - .includeCount() - .toJSON() - .find() - .spread((entries) => { - expect(entries.length).toBeTruthy(); - }); - }); - - test('Should have schema as second parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeSchema() - .includeCount() - .toJSON() - .find() - .spread((_, schema) => { - expect(schema).toBeTruthy(); - }); - }); - - test('Should have count as third parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeSchema() - .includeCount() - .toJSON() - .find() - .spread((_, __, count) => { - expect(count).toBeTruthy(); - }); - }); - - test('Should maintain default sorting of descending updated_at', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeSchema() - .includeCount() - .toJSON() - .find() - .spread((entries) => { - if (entries && entries.length) { - let prev = entries[0][field]; - const _entries = entries.every((entry) => { - prev = entry[field]; - return entry[field] <= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - }); - - describe('With entries, content_type and count argument (includeContentType first)', () => { - const field = 'updated_at'; - - test('Should have entries as first parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeContentType() - .includeCount() - .toJSON() - .find() - .spread((entries) => { - expect(entries.length).toBeTruthy(); - }); - }); - - test('Should have contentType as second parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeContentType() - .includeCount() - .toJSON() - .find() - .spread((_, contentType) => { - expect(contentType).toBeTruthy(); - }); - }); - - test('Should have correct contentType uid', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeContentType() - .includeCount() - .toJSON() - .find() - .spread((_, contentType) => { - expect(contentType.uid).toBe(contentTypes.source); - }); - }); - - test('Should have count as third parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeContentType() - .includeCount() - .toJSON() - .find() - .spread((_, __, count) => { - expect(count).toBeTruthy(); - }); - }); - - test('Should maintain default sorting of descending updated_at', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeContentType() - .includeCount() - .toJSON() - .find() - .spread((entries) => { - if (entries && entries.length) { - let prev = entries[0][field]; - const _entries = entries.every((entry) => { - prev = entry[field]; - return entry[field] <= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - }); - - describe('With entries, content_type|schema and count argument', () => { - const field = 'updated_at'; - - test('Should have entries as first parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .includeSchema() - .includeContentType() - .toJSON() - .find() - .spread((entries) => { - expect(entries.length).toBeTruthy(); - }); - }); - - test('Should have contentType as second parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .includeSchema() - .includeContentType() - .toJSON() - .find() - .spread((_, contentType) => { - expect(contentType).toBeTruthy(); - }); - }); - - test('Should have correct contentType uid', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .includeSchema() - .includeContentType() - .toJSON() - .find() - .spread((_, contentType) => { - expect(contentType.uid).toBe(contentTypes.source); - }); - }); - - test('Should have count as third parameter', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .includeSchema() - .includeContentType() - .toJSON() - .find() - .spread((_, __, count) => { - expect(count).toBeTruthy(); - }); - }); - - test('Should maintain default sorting of descending updated_at', () => { - const Query = Stack.ContentType(contentTypes.source).Query(); - Query.includeCount() - .includeSchema() - .includeContentType() - .toJSON() - .find() - .spread((entries) => { - if (entries && entries.length) { - let prev = entries[0][field]; - const _entries = entries.every((entry) => { - prev = entry[field]; - return entry[field] <= prev; - }); - expect(_entries).toBe(true); - } - }); - }); - }); -}); diff --git a/test/entry/utils.js b/test/entry/utils.js deleted file mode 100755 index 31ef5da6..00000000 --- a/test/entry/utils.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; - -/** - * Module Dependencies. - */ -const _ = require('lodash'); - -const utils = {}; -module.exports = exports = utils; - -exports.calculateBinary = function (uid) { - let binary = 0; - const bits = uid.split('').slice(3); - for (let i = 0, _i = bits.length; i < _i; i++) { - binary += parseInt(bits[i].toString(), 16); - } - return binary; -}; - -exports.arrayPresentInArray = function (src, dest) { - return (_.intersection(src, dest).length); -}; - -exports.isEntriesPublished = function (entries, environment_uid, locale) { - const searchInPublishDetails = function (entry) { - let flag = false; - if (entry && entry._metadata && entry._metadata.publish_details && entry._metadata.publish_details.length) { - for (let i = 0, _i = entry._metadata.publish_details.length; i < _i; i++) { - if (entry._metadata.publish_details[i] && entry._metadata.publish_details[i].environment === environment_uid && entry._metadata.publish_details[i].locale === locale) { - if (entry._metadata.publish_details[i].scheduled && entry._metadata.publish_details[i].time) continue; - flag = true; - break; - } - } - } - return flag; - }; - - let _flag = true; - if (entries instanceof Array) { - for (let j = 0, _j = entries.length; j < _j; j++) { - if (typeof entries[j].toJSON === 'function' && typeof entries[j].get === 'function') entries[j] = entries[j].toJSON(); - _flag = searchInPublishDetails(entries[j]); - if (!_flag) break; - } - } else if (typeof entries === 'object') { - if (typeof entries.toJSON === 'function' && typeof entries.get === 'function') entries = entries.toJSON(); - _flag = searchInPublishDetails(entries); - } - return _flag; -}; diff --git a/test/integration/AssetTests/AssetQuery.test.js b/test/integration/AssetTests/AssetQuery.test.js index 313790c6..6d6fb1f1 100644 --- a/test/integration/AssetTests/AssetQuery.test.js +++ b/test/integration/AssetTests/AssetQuery.test.js @@ -518,4 +518,51 @@ describe('Asset Tests - Asset Queries', () => { } }); }); + + // ============================================================================= + // ASSET SPREAD METHOD / ARRAY DESTRUCTURING TESTS + // ============================================================================= + + describe('Asset Spread Method - Array Destructuring', () => { + const field = 'updated_at'; + + test('AssetSpread_AssetsAsFirstElement_ReturnsTruthyLength', async () => { + const Query = Stack.Assets().Query(); + const result = await Query.limit(1).toJSON().find(); + const assets = result[0]; + + expect(assets.length).toBeTruthy(); + + if (assets && assets.length) { + let prev = assets[0][field]; + const _assets = assets.every((asset) => { + prev = asset[field]; + return asset[field] <= prev; + }); + expect(_assets).toBe(true); + } + + console.log(`✅ Assets as first element: ${assets.length} asset(s) returned`); + }); + + test('AssetSpread_WithIncludeCount_AssetsAndCountDestructured', async () => { + const Query = Stack.Assets().Query(); + const result = await Query.includeCount().toJSON().find(); + const [assets, count] = result; + + expect(assets.length).toBeTruthy(); + expect(count).toBeTruthy(); + + if (assets && assets.length) { + let prev = assets[0][field]; + const _assets = assets.every((asset) => { + prev = asset[field]; + return asset[field] <= prev; + }); + expect(_assets).toBe(true); + } + + console.log(`✅ Assets + count destructured: ${assets.length} assets, total count=${count}`); + }); + }); }); diff --git a/test/integration/AssetTests/ImageTransformation.test.js b/test/integration/AssetTests/ImageTransformation.test.js index 2aa80891..5fa13225 100644 --- a/test/integration/AssetTests/ImageTransformation.test.js +++ b/test/integration/AssetTests/ImageTransformation.test.js @@ -733,6 +733,60 @@ describe('Image Transformation Tests', () => { }); }); + describe('URL Edge Cases - Valid/Invalid URL Question Mark Handling', () => { + let Asset; + const Regexp = new RegExp('\\?', 'g'); + + beforeAll(async () => { + const assets = await Stack.Assets().Query().toJSON().find(); + Asset = assets[0][0]; + }); + + test('ImageTransform_ValidURL_SingleParam_ExactlyOneQuestionMark', () => { + const Image = Stack.imageTransform(Asset.url, { quality: 50 }); + expect(Image.match(Regexp).length).toBe(1); + }); + + test('ImageTransform_ValidURL_SingleParam_QualityIncluded', () => { + const Image = Stack.imageTransform(Asset.url, { quality: 50 }); + expect(Image.includes('?quality=50')).toBe(true); + }); + + test('ImageTransform_ValidURL_MultipleParams_ExactlyOneQuestionMark', () => { + const Image = Stack.imageTransform(Asset.url, { quality: 50, auto: 'webp', format: 'jpg' }); + expect(Image.match(Regexp).length).toBe(1); + }); + + test('ImageTransform_ValidURL_MultipleParams_AllParamsIncluded', () => { + const Image = Stack.imageTransform(Asset.url, { quality: 50, auto: 'webp', format: 'jpg' }); + expect(Image.includes('quality=50')).toBe(true); + expect(Image.includes('auto=webp')).toBe(true); + expect(Image.includes('format=jpg')).toBe(true); + }); + + test('ImageTransform_InvalidURL_TrailingQuestion_SingleParam_ExactlyOneQuestionMark', () => { + const Image = Stack.imageTransform(Asset.url + '?', { quality: 50 }); + expect(Image.match(Regexp).length).toBe(1); + }); + + test('ImageTransform_InvalidURL_TrailingQuestion_SingleParam_QualityIncluded', () => { + const Image = Stack.imageTransform(Asset.url + '?', { quality: 50 }); + expect(Image.includes('quality=50')).toBe(true); + }); + + test('ImageTransform_InvalidURL_TrailingQuestion_MultipleParams_ExactlyOneQuestionMark', () => { + const Image = Stack.imageTransform(Asset.url + '?', { quality: 50, auto: 'webp', format: 'jpg' }); + expect(Image.match(Regexp).length).toBe(1); + }); + + test('ImageTransform_InvalidURL_TrailingQuestion_MultipleParams_AllParamsIncluded', () => { + const Image = Stack.imageTransform(Asset.url + '?', { quality: 50, auto: 'webp', format: 'jpg' }); + expect(Image.includes('quality=50')).toBe(true); + expect(Image.includes('auto=webp')).toBe(true); + expect(Image.includes('format=jpg')).toBe(true); + }); + }); + describe('Performance', () => { test('ImageTransform_SimpleTransform_FastExecution', async () => { const imageUID = TestDataHelper.getImageAssetUID(); diff --git a/test/integration/ErrorTests/ErrorHandling.test.js b/test/integration/ErrorTests/ErrorHandling.test.js index 4290b676..85504305 100644 --- a/test/integration/ErrorTests/ErrorHandling.test.js +++ b/test/integration/ErrorTests/ErrorHandling.test.js @@ -635,6 +635,71 @@ describe('Error Tests - Error Handling & Validation', () => { }, 15000); }); + // ============================================================================= + // HTTP STATUS CODE ERRORS (http_code / http_message) — from findone.js + // ============================================================================= + + describe('HTTP Status Code Errors (http_code / http_message)', () => { + describe('422 Unprocessable Entity via findOne', () => { + let success = false; + let error = null; + + beforeAll(async () => { + try { + const Query = Stack.ContentType('invalid_content_type').Query(); + await Query.toJSON().findOne(); + success = true; + } catch (err) { + error = err; + } + }); + + test('Should not succeed with invalid content type', () => { + expect(success).toBe(false); + }); + + test('Should return HTTP status 422', () => { + expect(error.http_code).toBe(422); + }); + + test('Should have a non-empty http_message', () => { + expect(error.http_message).toBeTruthy(); + }); + }); + + describe('412 Unauthorized via findOne with invalid API key', () => { + let success = false; + let error = null; + const contentTypes = { source: 'source' }; + + beforeAll(async () => { + const savedHeaders = { ...Stack.headers }; + try { + Stack.headers = { authorization: 'InvalidAPIKey' }; + const Query = Stack.ContentType(contentTypes.source).Query(); + await Query.toJSON().findOne(); + success = true; + } catch (err) { + error = err; + } finally { + Stack.headers = savedHeaders; + } + }); + + test('Should not succeed with invalid API key', () => { + expect(success).toBe(false); + }); + + test('Should return HTTP status 412', () => { + expect(error.http_code).toBe(412); + }); + + test('Should have a non-empty http_message', () => { + expect(error.http_message).toBeTruthy(); + }); + }); + }); + describe('Special Error Cases', () => { test('Error_VeryLongUID_HandlesGracefully', async () => { const veryLongUID = 'a'.repeat(1000); diff --git a/test/integration/SDKUtilityTests/UtilityMethods.test.js b/test/integration/SDKUtilityTests/UtilityMethods.test.js index b060c7dd..cdc36812 100644 --- a/test/integration/SDKUtilityTests/UtilityMethods.test.js +++ b/test/integration/SDKUtilityTests/UtilityMethods.test.js @@ -105,6 +105,87 @@ describe('SDK Utility Methods - Comprehensive Tests', () => { .catch(done); }); + test('Spread_WithIncludeSchema_SchemaAsSecondArg_CountAsThird', (done) => { + const contentTypeUID = TestDataHelper.getContentTypeUID('article', true); + + Stack.ContentType(contentTypeUID) + .Query() + .includeSchema() + .includeCount() + .toJSON() + .find() + .spread((entries, schema, count) => { + expect(entries).toBeDefined(); + expect(Array.isArray(entries)).toBe(true); + expect(entries.length).toBeGreaterThan(0); + + expect(schema).toBeTruthy(); + + expect(count).toBeDefined(); + expect(typeof count).toBe('number'); + expect(count).toBeGreaterThanOrEqual(entries.length); + + console.log(`✅ Spread includeSchema+includeCount: entries=${entries.length}, count=${count}`); + done(); + }) + .catch(done); + }); + + test('Spread_WithIncludeContentType_ContentTypeHasUID', (done) => { + const contentTypeUID = TestDataHelper.getContentTypeUID('article', true); + + Stack.ContentType(contentTypeUID) + .Query() + .includeContentType() + .includeCount() + .toJSON() + .find() + .spread((entries, ct, count) => { + expect(entries).toBeDefined(); + expect(Array.isArray(entries)).toBe(true); + expect(entries.length).toBeGreaterThan(0); + + if (ct) { + expect(ct.uid).toBe(contentTypeUID); + } + + expect(count).toBeDefined(); + expect(typeof count).toBe('number'); + + console.log(`✅ Spread includeContentType+includeCount: ct.uid=${ct && ct.uid}, count=${count}`); + done(); + }) + .catch(done); + }); + + test('Spread_WithIncludeSchemaAndContentType_AllThreeArgs', (done) => { + const contentTypeUID = TestDataHelper.getContentTypeUID('article', true); + + Stack.ContentType(contentTypeUID) + .Query() + .includeCount() + .includeSchema() + .includeContentType() + .toJSON() + .find() + .spread((entries, ct, count) => { + expect(entries).toBeDefined(); + expect(Array.isArray(entries)).toBe(true); + expect(entries.length).toBeGreaterThan(0); + + if (ct) { + expect(ct.uid).toBe(contentTypeUID); + } + + expect(count).toBeDefined(); + expect(typeof count).toBe('number'); + + console.log(`✅ Spread includeCount+includeSchema+includeContentType: entries=${entries.length}, count=${count}`); + done(); + }) + .catch(done); + }); + test('Spread_ErrorHandling_CatchesErrors', async () => { try { await Stack.ContentType('non_existent_ct_12345') @@ -218,6 +299,17 @@ describe('SDK Utility Methods - Comprehensive Tests', () => { } }); + test('EarlyAccess_NewCDAAndTaxonomy_HeaderPreservesOrder', () => { + const stack = Contentstack.Stack({ + ...config.stack, + early_access: ['newCDA', 'taxonomy'] + }); + + expect(stack.headers['x-header-ea']).toBe('newCDA,taxonomy'); + + console.log(`✅ early_access ['newCDA','taxonomy'] → header: ${stack.headers['x-header-ea']}`); + }); + test('EarlyAccess_NoEarlyAccess_NoHeader', () => { const stack = Contentstack.Stack(config.stack);