<commit_before>/* eslint-env jest */

import moment from 'moment'
import logger from '../../../utils/logger'
import { getMockUserContext, mockDate } from '../../test-utils'
import getCause from '../../cause/getCause'
import { DatabaseItemDoesNotExistException } from '../../../utils/exceptions'

jest.mock('../../../utils/logger')
jest.mock('../../cause/getCause')

const userContext = getMockUserContext()

beforeAll(() => {
  mockDate.on()
})

afterAll(() => {
  mockDate.off()
})

beforeEach(() => {
  jest.clearAllMocks()
})

describe('setUserCause', () => {
  it('sets users cause ID', async () => {
    const UserModel = require('../UserModel').default
    const updateQuery = jest.spyOn(UserModel, 'update')
    const causeId = 'abcd'
    const setUserCause = require('../setUserCause').default
    await setUserCause(userContext, userContext.id, causeId)
    expect(updateQuery).toHaveBeenCalledWith(userContext, {
      id: userContext.id,
      causeId,
      updated: moment.utc().toISOString(),
    })
  })

  it('throws if cause doesnt exist', async () => {
    const error = new DatabaseItemDoesNotExistException()
    getCause.mockImplementationOnce(() => {
      throw error
    })
    const setUserCause = require('../setUserCause').default
    await expect(
      setUserCause(userContext, userContext.id, 'abcd')
    ).rejects.toThrow(error)
  })

  it('throws if calling the DB throws', async () => {
    const UserModel = require('../UserModel').default
    const causeId = 'abcd'
    const mockErr = new Error('No good.')
    jest.spyOn(UserModel, 'update').mockImplementationOnce(() => {
      throw mockErr
    })
    const setUserCause = require('../setUserCause').default
    await expect(
      setUserCause(userContext, userContext.id, causeId)
    ).rejects.toThrow(mockErr)
  })

  it('logs an error if calling the DB throws', async () => {
    const UserModel = require('../UserModel').default
    const causeId = 'abcd'
    const mockErr = new Error('No good.')
    jest.spyOn(UserModel, 'update').mockImplementationOnce(() => {
      throw mockErr
    })
    const setUserCause = require('../setUserCause').default
<commit_msg>try adding assertion counts to fix timeout<commit_after>/* eslint-env jest */

import moment from 'moment'
import logger from '../../../utils/logger'
import { getMockUserContext, mockDate } from '../../test-utils'
import getCause from '../../cause/getCause'
import { DatabaseItemDoesNotExistException } from '../../../utils/exceptions'

jest.mock('../../../utils/logger')
jest.mock('../../cause/getCause')

const userContext = getMockUserContext()

beforeAll(() => {
  mockDate.on()
})

afterAll(() => {
  mockDate.off()
})

beforeEach(() => {
  jest.clearAllMocks()
})

describe('setUserCause', () => {
  it('sets users cause ID', async () => {
    expect.assertions(1)
    const UserModel = require('../UserModel').default
    const updateQuery = jest.spyOn(UserModel, 'update')
    const causeId = 'abcd'
    const setUserCause = require('../setUserCause').default
    await setUserCause(userContext, userContext.id, causeId)
    expect(updateQuery).toHaveBeenCalledWith(userContext, {
      id: userContext.id,
      causeId,
      updated: moment.utc().toISOString(),
    })
  })

  it('throws if cause doesnt exist', async () => {
    expect.assertions(1)
    const error = new DatabaseItemDoesNotExistException()
    getCause.mockImplementationOnce(() => {
      throw error
    })
    const setUserCause = require('../setUserCause').default
    await expect(
      setUserCause(userContext, userContext.id, 'abcd')
    ).rejects.toThrow(error)
  })

  it('throws if calling the DB throws', async () => {
    expect.assertions(1)
    const UserModel = require('../UserModel').default
    const causeId = 'abcd'
    const mockErr = new Error('No good.')
    jest.spyOn(UserModel, 'update').mockImplementationOnce(() => {
      throw mockErr
    })
    const setUserCause = require('../setUserCause').default
    await expect(
      setUserCause(userContext, userContext.id, causeId)
    ).rejects.toThrow(mockErr)
  })

  it('logs an error if calling the DB throws', async () => {
    expect.assertions(1)
    const UserModel = require('../UserModel').default
    const causeId = 'abcd'
    const mockErr = new Error('No good.')
    jest.spyOn(UserModel, 'update').mockImplementationOnce(() => {
      throw mockErr
    })
    const setUserCause = require('../setUserCause').default

{'fraction_non_alphanumeric': 0.16138458080467522, 'fraction_numerical': 0.0008990784445942908, 'mean_word_length': 3.36702649656526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5007561', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1340, 'n_words': 298}