Fix tests

This commit is contained in:
Hongbo Wu
2022-08-05 16:07:49 +08:00
parent 22ce59a198
commit 8070273dad
11 changed files with 290 additions and 215 deletions

View File

@ -7,7 +7,7 @@ import { getRepository } from '../../entity/utils'
import { Integration, IntegrationType } from '../../entity/integration'
import { buildLogger } from '../../utils/logger'
import { syncWithIntegration } from '../../services/integrations'
import { getPageById, getPageByParam, searchPages } from '../../elastic/pages'
import { getPageById, searchPages } from '../../elastic/pages'
import { Page } from '../../elastic/types'
import { DateFilter } from '../../utils/search'
@ -63,19 +63,19 @@ export function integrationsServiceRouter() {
const action = req.params.action.toUpperCase()
if (action === 'SYNC_UPDATED') {
// get updated page by id
const id = data.id
let page: Page | undefined
let id = ''
switch (type) {
case EntityType.PAGE:
page = await getPageById(id)
id = data.id
break
case EntityType.HIGHLIGHT:
page = await getPageByParam({ 'highlights.id': id })
id = data.articleId
break
case EntityType.LABEL:
page = await getPageByParam({ 'labels.id': id })
id = data.pageId
break
}
const page = await getPageById(id)
if (!page) {
logger.info('No page found for id', id)
res.status(200).send('No page found')

View File

@ -0,0 +1,57 @@
import 'mocha'
import { expect } from 'chai'
import { Highlight, Page, PageContext } from '../../src/elastic/types'
import { createPubSubClient } from '../../src/datalayer/pubsub'
import { deletePage } from '../../src/elastic/pages'
import {
addHighlightToPage,
searchHighlights,
} from '../../src/elastic/highlights'
import { createTestElasticPage } from '../util'
describe('highlights in elastic', () => {
const userId = 'userId'
const ctx: PageContext = {
pubsub: createPubSubClient(),
refresh: true,
uid: userId,
}
describe('searchHighlights', () => {
const highlightId = 'highlightId'
let page: Page
before(async () => {
// create a testing page
page = await createTestElasticPage(userId)
const highlightData: Highlight = {
patch: 'test patch',
quote: 'test content',
shortId: 'test shortId',
id: highlightId,
userId: page.userId,
createdAt: new Date(),
updatedAt: new Date(),
}
await addHighlightToPage(page.id, highlightData, ctx)
})
after(async () => {
// delete the testing page
await deletePage(page.id, ctx)
})
it('searches highlights', async () => {
const [searchResults, count] = (await searchHighlights(
{
query: 'test',
},
page.userId
)) || [[], 0]
expect(count).to.eq(1)
expect(searchResults[0].id).to.eq(highlightId)
})
})
})

View File

@ -0,0 +1,141 @@
import 'mocha'
import { expect } from 'chai'
import {
ArticleSavingRequestStatus,
Highlight,
Label,
Page,
PageContext,
PageType,
} from '../../src/elastic/types'
import { createPubSubClient } from '../../src/datalayer/pubsub'
import { createPage, deletePage, getPageById } from '../../src/elastic/pages'
import { addLabelInPage, setLabelsForHighlight } from '../../src/elastic/labels'
import { addHighlightToPage } from '../../src/elastic/highlights'
describe('labels in elastic', () => {
const userId = 'userId'
const ctx: PageContext = {
pubsub: createPubSubClient(),
refresh: true,
uid: userId,
}
describe('addLabelInPage', () => {
let page: Page
let label: Label = {
id: 'Test label id',
name: 'test label',
color: '#07D2D1',
}
let newLabel: Label
before(async () => {
// create a testing page
page = {
id: '',
hash: 'test hash',
userId: userId,
pageType: PageType.Article,
title: 'test title',
content: '<p>test</p>',
slug: 'test slug',
createdAt: new Date(),
updatedAt: new Date(),
savedAt: new Date(),
readingProgressPercent: 100,
readingProgressAnchorIndex: 0,
url: 'https://blog.omnivore.app/p/getting-started-with-omnivore',
archivedAt: new Date(),
labels: [label],
state: ArticleSavingRequestStatus.Succeeded,
}
page.id = (await createPage(page, ctx))!
})
after(async () => {
// delete the testing page
await deletePage(page.id, ctx)
})
context('when the label not exist in the page', () => {
before(() => {
newLabel = {
id: 'new label id',
name: 'new label',
color: '#07D2D1',
}
})
it('adds the label to the page', async () => {
const result = await addLabelInPage(page.id, newLabel, ctx)
expect(result).to.be.true
const updatedPage = await getPageById(page.id)
expect(updatedPage?.labels).to.deep.include(label)
})
})
context('when the label exists in the page', () => {
before(() => {
newLabel = label
})
it('does not add the label to the page', async () => {
const result = await addLabelInPage(page.id, newLabel, ctx)
expect(result).to.be.false
})
})
})
describe('setLabelsForHighlight', () => {
const page = {
id: 'testPageId',
hash: 'test set labels for highlight hash',
userId: userId,
pageType: PageType.Article,
title: 'test set labels for highlight title',
content: '<p>test</p>',
slug: 'test set labels for highlight slug',
createdAt: new Date(),
savedAt: new Date(),
readingProgressPercent: 100,
readingProgressAnchorIndex: 0,
url: 'https://blog.omnivore.app/p/setting-labels-for-highlight',
state: ArticleSavingRequestStatus.Succeeded,
}
const highlightId = 'highlightId'
const label: Label = {
id: 'test label id',
name: 'test label',
color: '#07D2D1',
}
before(async () => {
// create a testing page
await createPage(page, ctx)
const highlightData: Highlight = {
patch: 'test set labels patch',
quote: 'test set labels quote',
shortId: 'test set labels shortId',
id: highlightId,
userId: page.userId,
createdAt: new Date(),
updatedAt: new Date(),
}
await addHighlightToPage(page.id, highlightData, ctx)
})
after(async () => {
await deletePage(page.id, ctx)
})
it('sets labels for highlights', async () => {
const result = await setLabelsForHighlight(highlightId, [label], ctx)
expect(result).to.be.true
})
})
})

View File

@ -3,8 +3,6 @@ import { expect } from 'chai'
import { InFilter, ReadFilter } from '../../src/utils/search'
import {
ArticleSavingRequestStatus,
Highlight,
Label,
Page,
PageContext,
PageType,
@ -21,13 +19,9 @@ import {
searchPages,
updatePage,
} from '../../src/elastic/pages'
import { addLabelInPage, setLabelsForHighlight } from '../../src/elastic/labels'
import {
addHighlightToPage,
searchHighlights,
} from '../../src/elastic/highlights'
import { createTestElasticPage } from '../util'
describe('elastic api', () => {
describe('pages in elastic', () => {
const userId = 'userId'
const ctx: PageContext = {
pubsub: createPubSubClient(),
@ -35,49 +29,6 @@ describe('elastic api', () => {
uid: userId,
}
let page: Page
before(async () => {
// create a testing page
page = {
id: '',
hash: 'test hash',
userId: userId,
pageType: PageType.Article,
title: 'test title',
content: '<p>test</p>',
slug: 'test slug',
createdAt: new Date(),
updatedAt: new Date(),
savedAt: new Date(),
readingProgressPercent: 100,
readingProgressAnchorIndex: 0,
url: 'https://blog.omnivore.app/p/getting-started-with-omnivore',
archivedAt: new Date(),
labels: [
{
id: 'Test label id',
name: 'test label',
color: '#ffffff',
createdAt: new Date(),
},
{
id: 'Test label id 2',
name: 'test label 2',
color: '#eeeeee',
createdAt: new Date(),
},
],
state: ArticleSavingRequestStatus.Succeeded,
}
page.id = (await createPage(page, ctx))!
})
after(async () => {
// delete the testing page
await deletePage(page.id, ctx)
})
describe('createPage', () => {
let newPageId: string
@ -108,6 +59,18 @@ describe('elastic api', () => {
})
describe('getPageByParam', () => {
let page: Page
before(async () => {
// create a testing page
page = await createTestElasticPage(userId)
})
after(async () => {
// delete the testing page
await deletePage(page.id, ctx)
})
it('gets a page by url', async () => {
const pageFound = await getPageByParam({
userId: page.userId,
@ -119,6 +82,18 @@ describe('elastic api', () => {
})
describe('getPageById', () => {
let page: Page
before(async () => {
// create a testing page
page = await createTestElasticPage(userId)
})
after(async () => {
// delete the testing page
await deletePage(page.id, ctx)
})
it('gets a page by id', async () => {
const pageFound = await getPageById(page.id)
expect(pageFound).not.undefined
@ -126,6 +101,18 @@ describe('elastic api', () => {
})
describe('updatePage', () => {
let page: Page
before(async () => {
// create a testing page
page = await createTestElasticPage(userId)
})
after(async () => {
// delete the testing page
await deletePage(page.id, ctx)
})
it('updates a page', async () => {
const newTitle = 'new title'
const updatedPageData: Partial<Page> = {
@ -140,6 +127,18 @@ describe('elastic api', () => {
})
describe('searchPages', () => {
let page: Page
before(async () => {
// create a testing page
page = await createTestElasticPage(userId)
})
after(async () => {
// delete the testing page
await deletePage(page.id, ctx)
})
it('searches pages', async () => {
const searchResults = await searchPages(
{
@ -148,7 +147,7 @@ describe('elastic api', () => {
inFilter: InFilter.ALL,
labelFilters: [],
readFilter: ReadFilter.ALL,
query: 'test',
query: page.content,
},
page.userId
)
@ -156,43 +155,12 @@ describe('elastic api', () => {
})
})
describe('addLabelInPage', () => {
context('when the label not exist in the page', () => {
it('adds the label to the page', async () => {
const newLabel = {
id: 'new label id',
name: 'new label',
color: '#07D2D1',
}
const result = await addLabelInPage(page.id, newLabel, ctx)
expect(result).to.be.true
const updatedPage = await getPageById(page.id)
expect(updatedPage?.labels).to.deep.include(newLabel)
})
})
context('when the label exists in the page', () => {
it('does not add the label to the page', async () => {
const newLabel = {
id: 'Test label id',
name: 'test label',
color: '#07D2D1',
}
const result = await addLabelInPage(page.id, newLabel, ctx)
expect(result).to.be.false
})
})
})
describe('countByCreatedAt', () => {
const createdAt = Date.now() - 60 * 60 * 24 * 1000
let page: Page
before(async () => {
const newPageData: Page = {
page = {
id: '',
hash: 'hash',
userId: userId,
@ -208,93 +176,16 @@ describe('elastic api', () => {
state: ArticleSavingRequestStatus.Succeeded,
}
await createPage(newPageData, ctx)
})
it('counts pages by createdAt', async () => {
const count = await countByCreatedAt(userId, createdAt, createdAt)
expect(count).to.eq(1)
})
})
describe('searchHighlights', () => {
const highlightId = 'highlightId'
before(async () => {
const highlightData: Highlight = {
patch: 'test patch',
quote: 'test content',
shortId: 'test shortId',
id: highlightId,
userId: page.userId,
createdAt: new Date(),
updatedAt: new Date(),
}
await addHighlightToPage(page.id, highlightData, ctx)
})
it('searches highlights', async () => {
const [searchResults, count] = (await searchHighlights(
{
query: 'test',
},
page.userId
)) || [[], 0]
expect(count).to.eq(1)
expect(searchResults[0].id).to.eq(highlightId)
})
})
describe('setLabelsForHighlight', () => {
const page = {
id: 'testPageId',
hash: 'test set labels for highlight hash',
userId: userId,
pageType: PageType.Article,
title: 'test set labels for highlight title',
content: '<p>test</p>',
slug: 'test set labels for highlight slug',
createdAt: new Date(),
savedAt: new Date(),
readingProgressPercent: 100,
readingProgressAnchorIndex: 0,
url: 'https://blog.omnivore.app/p/setting-labels-for-highlight',
state: ArticleSavingRequestStatus.Succeeded,
}
const highlightId = 'highlightId'
const label: Label = {
id: 'test label id',
name: 'test label',
color: '#07D2D1',
}
before(async () => {
// create a testing page
await createPage(page, ctx)
const highlightData: Highlight = {
patch: 'test set labels patch',
quote: 'test set labels quote',
shortId: 'test set labels shortId',
id: highlightId,
userId: page.userId,
createdAt: new Date(),
updatedAt: new Date(),
}
await addHighlightToPage(page.id, highlightData, ctx)
page.id = (await createPage(page, ctx))!
})
after(async () => {
await deletePage(page.id, ctx)
})
it('sets labels for highlights', async () => {
const result = await setLabelsForHighlight(highlightId, [label], ctx)
expect(result).to.be.true
it('counts pages by createdAt', async () => {
const count = await countByCreatedAt(userId, createdAt, createdAt)
expect(count).to.eq(1)
})
})

View File

@ -694,7 +694,7 @@ describe('Article API', () => {
let pageId = ''
before(async () => {
pageId = (await createTestElasticPage(user)).id!
pageId = (await createTestElasticPage(user.id)).id!
})
after(async () => {

View File

@ -104,7 +104,7 @@ describe('Highlights API', () => {
.send({ fakeEmail: user.email })
authToken = res.body.authToken
pageId = (await createTestElasticPage(user)).id
pageId = (await createTestElasticPage(user.id)).id
ctx = { pubsub: createPubSubClient(), uid: user.id }
})

View File

@ -246,7 +246,7 @@ describe('Labels API', () => {
before(async () => {
toDeleteLabel = await createTestLabel(user, 'page label', '#ffffff')
labelId = toDeleteLabel.id
page = await createTestElasticPage(user, [toDeleteLabel])
page = await createTestElasticPage(user.id, [toDeleteLabel])
})
after(async () => {
@ -267,7 +267,7 @@ describe('Labels API', () => {
let page: Page
before(async () => {
page = await createTestElasticPage(user)
page = await createTestElasticPage(user.id)
toDeleteLabel = await createTestLabel(
user,
'highlight label',
@ -340,7 +340,7 @@ describe('Labels API', () => {
const label1 = await createTestLabel(user, 'label_1', '#ffffff')
const label2 = await createTestLabel(user, 'label_2', '#eeeeee')
labels = [label1, label2]
page = await createTestElasticPage(user)
page = await createTestElasticPage(user.id)
})
after(async () => {
@ -497,7 +497,7 @@ describe('Labels API', () => {
let page: Page
before(async () => {
page = await createTestElasticPage(user, [toUpdateLabel])
page = await createTestElasticPage(user.id, [toUpdateLabel])
})
after(async () => {
@ -542,7 +542,7 @@ describe('Labels API', () => {
const label1 = await createTestLabel(user, 'label_1', '#ffffff')
const label2 = await createTestLabel(user, 'label_2', '#eeeeee')
labels = [label1, label2]
page = await createTestElasticPage(user)
page = await createTestElasticPage(user.id)
})
after(async () => {

View File

@ -38,7 +38,7 @@ describe('Reminders API', () => {
authToken = res.body.authToken
// create page, link and reminders test data
page = await createTestElasticPage(user)
page = await createTestElasticPage(user.id)
reminder = await createTestReminder(user, page.id)
})

View File

@ -24,7 +24,7 @@ describe('Report API', () => {
authToken = res.body.authToken
// create a page
page = await createTestElasticPage(user)
page = await createTestElasticPage(user.id)
})
after(async () => {

View File

@ -1,9 +1,5 @@
import { createTestUser, deleteTestUser } from '../db'
import {
createTestElasticPage,
graphqlRequest,
request,
} from '../util'
import { createTestElasticPage, graphqlRequest, request } from '../util'
import { expect } from 'chai'
import 'mocha'
import { User } from '../../src/entity/user'
@ -24,7 +20,7 @@ describe('Update API', () => {
.send({ fakeEmail: user.email })
authToken = res.body.authToken
page = await createTestElasticPage(user)
page = await createTestElasticPage(user.id)
})
after(async () => {
@ -34,8 +30,8 @@ describe('Update API', () => {
describe('update page', () => {
let query: string
let title = "New Title"
let description = "New Description"
let title = 'New Title'
let description = 'New Description'
beforeEach(() => {
query = `
@ -59,14 +55,14 @@ describe('Update API', () => {
}
}
`
})
it('should update page', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
const updatedPage = res?.body.data.updatePage.updatedPage
expect(updatedPage?.title).to.eql(title)
expect(updatedPage?.description).to.eql(description)
})
})
it('should update page', async () => {
const res = await graphqlRequest(query, authToken).expect(200)
const updatedPage = res?.body.data.updatePage.updatedPage
expect(updatedPage?.title).to.eql(title)
expect(updatedPage?.description).to.eql(description)
})
})
})

View File

@ -4,9 +4,8 @@ import { v4 } from 'uuid'
import { corsConfig } from '../src/utils/corsConfig'
import { ArticleSavingRequestStatus, Label, Page } from '../src/elastic/types'
import { PageType } from '../src/generated/graphql'
import { User } from '../src/entity/user'
import { createPubSubClient } from '../src/datalayer/pubsub'
import { createPage, getPageById } from '../src/elastic/pages'
import { createPage } from '../src/elastic/pages'
const { app, apollo } = createApp()
export const request = supertest(app)
@ -39,13 +38,13 @@ export const generateFakeUuid = () => {
}
export const createTestElasticPage = async (
user: User,
userId: string,
labels?: Label[]
): Promise<Page> => {
const page: Page = {
id: '',
hash: 'test hash',
userId: user.id,
userId,
pageType: PageType.Article,
title: 'test title',
content: '<p>test content</p>',
@ -59,19 +58,10 @@ export const createTestElasticPage = async (
state: ArticleSavingRequestStatus.Succeeded,
}
const pageId = await createPage(page, {
page.id = (await createPage(page, {
pubsub: createPubSubClient(),
refresh: true,
uid: user.id,
})
if (pageId) {
page.id = pageId
}
const res = await getPageById(page.id)
console.log('got page', res)
if (!res) {
throw new Error('Failed to create page')
}
return res
uid: userId,
}))!
return page
}