-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from contentstack/feat/cs-43915-or-operator-q…
…uery-implementation changes in query operators implementation
- Loading branch information
Showing
6 changed files
with
121 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { stackInstance } from '../utils/stack-instance'; | ||
import { Entries } from '../../src/lib/entries'; | ||
import { TEntry } from './types'; | ||
import { QueryOperation } from 'src/lib/types'; | ||
import { Query } from 'src/lib/query'; | ||
|
||
const stack = stackInstance(); | ||
|
||
describe('Query Operators API test cases', () => { | ||
it('should get entries which matches the fieldUid and values', async () => { | ||
const query = await makeEntries('contenttype_uid').query().containedIn('title', ['value']).find<TEntry>() | ||
if (query.entries) { | ||
expect(query.entries[0]._version).toBeDefined(); | ||
expect(query.entries[0].title).toBeDefined(); | ||
expect(query.entries[0].uid).toBeDefined(); | ||
expect(query.entries[0].created_at).toBeDefined(); | ||
} | ||
}); | ||
|
||
it('should get entries which does not match the fieldUid and values', async () => { | ||
const query = await makeEntries('contenttype_uid').query().notContainedIn('title', ['test', 'test2']).find<TEntry>() | ||
if (query.entries) { | ||
expect(query.entries[0]._version).toBeDefined(); | ||
expect(query.entries[0].title).toBeDefined(); | ||
expect(query.entries[0].uid).toBeDefined(); | ||
expect(query.entries[0].created_at).toBeDefined(); | ||
} | ||
}); | ||
|
||
it('should get entries which does not match the fieldUid - notExists', async () => { | ||
const query = await makeEntries('contenttype_uid').query().notExists('multi_line').find<TEntry>() | ||
if (query.entries) { | ||
expect(query.entries[0]._version).toBeDefined(); | ||
expect(query.entries[0].title).toBeDefined(); | ||
expect(query.entries[0].uid).toBeDefined(); | ||
expect(query.entries[0].created_at).toBeDefined(); | ||
expect((query.entries[0] as any).multi_line).not.toBeDefined() | ||
} | ||
}); | ||
it('should return entries matching any of the conditions - or', async () => { | ||
const query1: Query = await makeEntries('contenttype_uid').query().containedIn('title', ['value']); | ||
const query2: Query = await makeEntries('contenttype_uid').query().where('title', QueryOperation.EQUALS, 'value2'); | ||
const query = await makeEntries('contenttype_uid').query().or(query1, query2).find<TEntry>(); | ||
|
||
if (query.entries) { | ||
expect(query.entries).toHaveLength(2); | ||
expect(query.entries[0]._version).toBeDefined(); | ||
expect(query.entries[0].locale).toBeDefined(); | ||
expect(query.entries[0].uid).toBeDefined(); | ||
expect(query.entries[0].title).toBe('value2'); | ||
expect(query.entries[1]._version).toBeDefined(); | ||
expect(query.entries[1].locale).toBeDefined(); | ||
expect(query.entries[1].uid).toBeDefined(); | ||
expect(query.entries[1].title).toBe('value'); | ||
} | ||
}); | ||
}); | ||
|
||
function makeEntries(contentTypeUid = ''): Entries { | ||
const entries = stack.ContentType(contentTypeUid).Entry(); | ||
return entries; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { AxiosInstance, httpClient } from '@contentstack/core'; | ||
import { ContentType } from '../../src/lib/content-type'; | ||
import MockAdapter from 'axios-mock-adapter'; | ||
import { MOCK_CLIENT_OPTIONS } from '../utils/constant'; | ||
import { Query } from 'src/lib/query'; | ||
import { QueryOperation } from 'src/lib/types'; | ||
|
||
|
||
describe('Query Operators API test cases', () => { | ||
let contentType: ContentType; | ||
let client: AxiosInstance; | ||
let mockClient: MockAdapter; | ||
|
||
beforeAll(() => { | ||
client = httpClient(MOCK_CLIENT_OPTIONS); | ||
mockClient = new MockAdapter(client as any); | ||
}); | ||
|
||
beforeEach(() => { | ||
contentType = new ContentType(client, 'contentTypeUid'); | ||
}); | ||
it('should get entries which matches the fieldUid and values', () => { | ||
const query = contentType.Entry().query().containedIn('fieldUID', ['value']); | ||
expect(query._parameters).toStrictEqual({'fieldUID': {'$in': ['value']}}); | ||
}); | ||
it('should get entries which does not match the fieldUid and values', () => { | ||
const query = contentType.Entry().query().notContainedIn('fieldUID', ['value', 'value2']); | ||
expect(query._parameters).toStrictEqual({'fieldUID': {'$nin': ['value', 'value2']}}); | ||
}); | ||
it('should get entries which does not match the fieldUid - notExists', () => { | ||
const query = contentType.Entry().query().notExists('fieldUID'); | ||
expect(query._parameters).toStrictEqual({'fieldUID': {'$exists': false}}); | ||
}); | ||
it('should return entries matching any of the conditions - or', async () => { | ||
const query1: Query = await contentType.Entry().query().containedIn('fieldUID', ['value']); | ||
const query2: Query = await contentType.Entry().query().where('fieldUID', QueryOperation.EQUALS, 'value2'); | ||
const query = await contentType.Entry().query().or(query1, query2); | ||
expect(query._parameters).toStrictEqual({ '$or': [ {'fieldUID': {'$in': ['value']}}, { 'fieldUID': 'value2' } ] }); | ||
}); | ||
}); |