Skip to content

Commit

Permalink
Merge pull request #164 from contentful/fix/respect-load-all-entries-…
Browse files Browse the repository at this point in the history
…option

fix: fetch entries of all CTs when required by an intent
  • Loading branch information
TimBeyer authored Jan 4, 2019
2 parents 270ee30 + 03458a4 commit d721805
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/lib/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ export default class Fetcher implements APIFetcher {
}

const filter = {
'sys.contentType.sys.id[in]': ids.join(','),
'sys.archivedAt[exists]': 'false'
}

// If we want to load all entries, we do not need to add the filter specification
// that loads just the entries for related content types
// If we do, then we specify the list of CTs that we want entries for
if (!loadAllEntries) {
filter['sys.contentType.sys.id[in]'] = ids.join(',')
}

const entries = await this.fetchAllPaginatedItems<APIEntry>('/entries', filter)

return entries
Expand Down
65 changes: 61 additions & 4 deletions test/unit/lib/fetcher.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Fetcher', function () {
request
.withArgs({
method: 'GET',
url: '/entries?sys.contentType.sys.id[in]=newsArticle&sys.archivedAt[exists]=false&skip=0'
url: '/entries?sys.archivedAt[exists]=false&sys.contentType.sys.id[in]=newsArticle&skip=0'
})
.resolves({
skip: 0,
Expand All @@ -41,7 +41,7 @@ describe('Fetcher', function () {
request
.withArgs({
method: 'GET',
url: '/entries?sys.contentType.sys.id[in]=newsArticle&sys.archivedAt[exists]=false&skip=4'
url: '/entries?sys.archivedAt[exists]=false&sys.contentType.sys.id[in]=newsArticle&skip=4'
})
.resolves({
skip: 4,
Expand All @@ -57,11 +57,68 @@ describe('Fetcher', function () {

expect(request).to.have.been.calledWith({
method: 'GET',
url: '/entries?sys.contentType.sys.id[in]=newsArticle&sys.archivedAt[exists]=false&skip=0'
url: '/entries?sys.archivedAt[exists]=false&sys.contentType.sys.id[in]=newsArticle&skip=0'
})
expect(request).to.have.been.calledWith({
method: 'GET',
url: '/entries?sys.contentType.sys.id[in]=newsArticle&sys.archivedAt[exists]=false&skip=4'
url: '/entries?sys.archivedAt[exists]=false&sys.contentType.sys.id[in]=newsArticle&skip=4'
})

const result = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']
expect(entries).to.eql(result)
})

it('fetches all Entries of all CTs in the plan if an intent needs them all', async function () {
const intents = await buildIntents(function up (migration) {
migration.transformEntriesToType({
sourceContentType: 'sourceContentType',
targetContentType: 'targetContentType',
updateReferences: true,
removeOldEntries: false,
from: ['author', 'authorCity'],
identityKey: () => 'ID',
transformEntryForLocale: function () {
return {}
}
})
}, null, null)

const request = sinon.stub()
request
.withArgs({
method: 'GET',
url: '/entries?sys.archivedAt[exists]=false&skip=0'
})
.resolves({
skip: 0,
limit: 4,
total: 6,
items: ['item1', 'item2', 'item3', 'item4']
})
request
.withArgs({
method: 'GET',
url: '/entries?sys.archivedAt[exists]=false&skip=4'
})
.resolves({
skip: 4,
limit: 4,
total: 6,
items: ['item5', 'item6']
})

const intentList = new IntentList(intents)

const fetcher = new Fetcher(request)
const entries = await fetcher.getEntriesInIntents(intentList)

expect(request).to.have.been.calledWith({
method: 'GET',
url: '/entries?sys.archivedAt[exists]=false&skip=0'
})
expect(request).to.have.been.calledWith({
method: 'GET',
url: '/entries?sys.archivedAt[exists]=false&skip=4'
})

const result = ['item1', 'item2', 'item3', 'item4', 'item5', 'item6']
Expand Down
4 changes: 2 additions & 2 deletions test/unit/lib/migration-parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Migration parser', function () {
}
}

if (config.url.indexOf('/entries?sys.contentType.sys.id[in]=foo,cat&sys.archivedAt[exists]=false&skip=0') !== -1) {
if (config.url.indexOf('/entries?sys.archivedAt[exists]=false&sys.contentType.sys.id[in]=foo,cat&skip=0') !== -1) {
return {
total: 2,
skip: 0,
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('Migration parser', function () {
}
}

if (config.url === '/entries?sys.contentType.sys.id[in]=foo&sys.archivedAt[exists]=false&skip=0') {
if (config.url === '/entries?sys.archivedAt[exists]=false&sys.contentType.sys.id[in]=foo&skip=0') {
return {
total: 2,
skip: 0,
Expand Down

0 comments on commit d721805

Please sign in to comment.