<commit_before>'use strict'
const test = require('tape')

const Serializer = require('../../lib/utils/serializer')

test('Serializer.constructor', (t) => {
  t.throws(
    () => new Serializer({}),
    /provide `modelName`/,
    'requires `modelName`'
  )

  t.throws(
    () => new Serializer({modelName: 'some'}),
    /provide `attributes`/,
    'requires `attributes`'
  )

  t.throws(
    () => new Serializer({modelName: 'some', attributes: []}),
    /provide `attributesOfRelations`/,
    'requires `attributesOfRelations`'
  )

  const fn1 = function () {
    new Serializer({ // eslint-disable-line
      modelName: 'userAccount',
      attributes: ['name', 'userGroup'],
      attributesOfRelations: {
        rel1: ['one', 'two'],
        rel2: ['one1', 'two1']
      }
    })
  }

  t.doesNotThrow(fn1, 'when all parameters are provided')

  t.end()
})

const serializerOptions = Serializer.serializerOptions

test('Serializer.serializerOptions()', (t) => {
  const modelAttributes = ['name', 'hide', 'rel1', 'rel2']

  const attributesOfRelations = {
    rel1: ['enabled'],
    rel2: ['shortName']
  }

  let relatedIncluded = true
  const options1 = serializerOptions(modelAttributes, attributesOfRelations, relatedIncluded)
  t.deepEqual(
    options1,
    {
      attributes: ['name', 'hide', 'rel1', 'rel2'],
      keyForAttribute: 'camelCase',
      rel1: { attributes: ['enabled'], included: true, ref: 'id' },
      rel2: { attributes: ['shortName'], included: true, ref: 'id' }
    },
    'generates options with `included` on'
  )

  relatedIncluded = false
  const options2 = serializerOptions(modelAttributes, attributesOfRelations, relatedIncluded)
  t.deepEqual(
    options2,
    {
      attributes: ['name', 'hide', 'rel1', 'rel2'],
      keyForAttribute: 'camelCase',
      rel1: { attributes: ['enabled'], included: false, ref: 'id' },
      rel2: { attributes: ['shortName'], included: false, ref: 'id' }
    },
    'generates options with `included` off'
  )

  t.end()
})

test('Serializer serialization', (t) => {
  const serializer = new Serializer({
    modelName: 'user',
    attributes: ['name', 'group', 'rights'],
    attributesOfRelations: {
      group: ['name', 'hide'],
      rights: ['shortName']
    }
  })

  const data = [
    {
      id: '1', name: 'John',
      group: {id: '101', name: 'Admins', hide: false},
      rights: {id: '12', shortName: 'Full'}
    },
    {
      id: '2', name: 'Smith',
      group: {id: '102', name: 'Users', hide: true},
      rights: {id: '13', shortName: 'Part'}
    }
  ]

  t.deepEqual(
    serializer.withoutRelated(data),
    {
      data: [{
        attributes: { name: 'John' },
        id: '1',
        relationships: {
          group: { data: { id: '101', type: 'groups' } },
          rights: { data: { id: '12', type: 'rights' } }
        },
        type: 'users'
      }, {
        attributes: { name: 'Smith' },
        id: '2',
        relationships: {
          group: { data: { id: '102', type: 'groups' } },
          rights: { data: { id: '13', type: 'rights' } }
        },
        type: 'users'
      }]
    },
    'withoutRelated() does not include relations` data'
  )

  t.deepEqual(
    serializer.withRelated(data),
    {
      data: [{
        attributes: { name: 'John' },
        id: '1',
        relationships: {
          group: { data: { id: '101', type: 'groups' } },
          rights: { data: { id: '12', type: 'rights' } }
        },
        type: 'users'
      }, {
        attributes: { name: 'Smith' },
        id: '2',
        relationships: {
          group: { data: { id: '102', type: 'groups' } },
          rights: { data: { id: '13', type: 'rights' } }
        },
        type: 'users'
      }],
      included: [{
        attributes: { name: 'Admins', hide: false },
        id: '101',
        type: 'groups'
      }, {
        attributes: { shortName: 'Full' },
        id: '12',
        type: 'rights'
      }, {
        attributes: { name: 'Users', hide: true },
        id: '102',
        type: 'groups'
      }, {
        attributes: { shortName: 'Part' },
        id: '13',
        type: 'rights'
      }]
    },
    'withRelated() includes relations data'
  )

  t.end()
})<commit_msg>fix Serializer`s tests<commit_after>'use strict'
const test = require('tape')

const Serializer = require('../../lib/utils/serializer')

test('Serializer.constructor', (t) => {
  t.throws(
    () => new Serializer({}),
    /provide `modelName`/,
    'requires `modelName`'
  )

  t.throws(
    () => new Serializer({modelName: 'some'}),
    /provide `attributes`/,
    'requires `attributes`'
  )

  t.throws(
    () => new Serializer({modelName: 'some', attributes: []}),
    /provide `attributesOfRelations`/,
    'requires `attributesOfRelations`'
  )

  const fn1 = function () {
    new Serializer({ // eslint-disable-line
      modelName: 'userAccount',
      attributes: ['name', 'userGroup'],
      attributesOfRelations: {
        rel1: ['one', 'two'],
        rel2: ['one1', 'two1']
      }
    })
  }

  t.doesNotThrow(fn1, 'when all parameters are provided')

  t.end()
})

const serializerOptions = Serializer.serializerOptions

test('Serializer.serializerOptions()', (t) => {
  const modelAttributes = ['name', 'hide', 'rel1', 'rel2']

  const attributesOfRelations = {
    rel1: ['enabled'],
    rel2: ['shortName']
  }

  let relatedIncluded = true
  const options1 = serializerOptions(modelAttributes, attributesOfRelations, relatedIncluded)
  t.deepEqual(
    options1,
    {
      attributes: ['name', 'hide', 'rel1', 'rel2'],
      rel1: { attributes: ['enabled'], included: true, ref: 'id' },
      rel2: { attributes: ['shortName'], included: true, ref: 'id' }
    },
    'generates options with `included` on'
  )

  relatedIncluded = false
  const options2 = serializerOptions(modelAttributes, attributesOfRelations, relatedIncluded)
  t.deepEqual(
    options2,
    {
      attributes: ['name', 'hide', 'rel1', 'rel2'],
      rel1: { attributes: ['enabled'], included: false, ref: 'id' },
      rel2: { attributes: ['shortName'], included: false, ref: 'id' }
    },
    'generates options with `included` off'
  )

  t.end()
})

test('Serializer serialization', (t) => {
  const serializer = new Serializer({
    modelName: 'user',
    attributes: ['name', 'group', 'rights'],
    attributesOfRelations: {
      group: ['name', 'hide'],
      rights: ['shortName']
    }
  })

  const data = [
    {
      id: '1', name: 'John',
      group: {id: '101', name: 'Admins', hide: false},
      rights: {id: '12', shortName: 'Full'}
    },
    {
      id: '2', name: 'Smith',
      group: {id: '102', name: 'Users', hide: true},
      rights: {id: '13', shortName: 'Part'}
    }
  ]

  t.deepEqual(
    serializer.withoutRelated(data),
    {
      data: [{
        attributes: { name: 'John' },
        id: '1',
        relationships: {
          group: { data: { id: '101', type: 'groups' } },
          rights: { data: { id: '12', type: 'rights' } }
        },
        type: 'users'
      }, {
        attributes: { name: 'Smith' },
        id: '2',
        relationships: {
          group: { data: { id: '102', type: 'groups' } },
          rights: { data: { id: '13', type: 'rights' } }
        },
        type: 'users'
      }]
    },
    'withoutRelated() does not include relations` data'
  )

  t.deepEqual(
    serializer.withRelated(data),
    {
      data: [{
        attributes: { name: 'John' },
        id: '1',
        relationships: {
          group: { data: { id: '101', type: 'groups' } },
          rights: { data: { id: '12', type: 'rights' } }
        },
        type: 'users'
      }, {
        attributes: { name: 'Smith' },
        id: '2',
        relationships: {
          group: { data: { id: '102', type: 'groups' } },
          rights: { data: { id: '13', type: 'rights' } }
        },
        type: 'users'
      }],
      included: [{
        attributes: { name: 'Admins', hide: false },
        id: '101',
        type: 'groups'
      }, {
        attributes: { 'short-name': 'Full' },
        id: '12',
        type: 'rights'
      }, {
        attributes: { name: 'Users', hide: true },
        id: '102',
        type: 'groups'
      }, {
        attributes: { 'short-name': 'Part' },
        id: '13',
        type: 'rights'
      }]
    },
    'withRelated() includes relations data'
  )

  t.end()
})
{'fraction_non_alphanumeric': 0.18176393364363289, 'fraction_numerical': 0.016231053825038786, 'mean_word_length': 2.3109442907941524, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '4987377', 'n_tokens_mistral': 2832, 'n_tokens_neox': 2703, 'n_words': 660}