<commit_before>import 'babel-polyfill'
import _ from 'lodash'
import extract from 'extract-zip'
import Excel from 'exceljs'
import fs from 'fs'
import glob from 'glob'
import path from 'path'
import Promise from 'bluebird'
import test from 'tape'
import tempfile from 'tempfile'
import tempWrite from 'temp-write'

// eslint-disable-next-line max-len
import ProductTypeExport, { sortAttributes } from '../../src/product-type-export'
import * as utils from '../utils'

const extractArchive = Promise.promisify(extract)
const mockProductTypes = utils.generateTestProductTypes()
const mockAttributes = utils.getMockAttributes(mockProductTypes)

let OUTPUT_FOLDER
let client
let productTypeExport
let sphereClientConfig

const readXlsx = async function readXlsx (filePath) {
  const values = []
  const workbook = new Excel.Workbook()
  await workbook.xlsx.readFile(filePath)
  const worksheet = workbook.getWorksheet(1)
  worksheet.eachRow((row) => {
    let rowValues = row.values
    rowValues.shift()

    rowValues = rowValues.map(item => (_.isObject(item) ? null : item))
    values.push(rowValues)
  })

  return values
}

async function before () {
  sphereClientConfig = {
    config: await utils.getClientConfig(),
  }

  client = await utils.getClient()
  OUTPUT_FOLDER = tempfile()
  fs.mkdirSync(OUTPUT_FOLDER)

  productTypeExport = new ProductTypeExport({
    sphereClientConfig,
    config: {
      outputFolder: OUTPUT_FOLDER,
      exportFormat: 'xlsx',
    },
  })

  await utils.deleteAll('productTypes', client)
  return Promise.map(
    mockProductTypes,
    productType => client.productTypes.create(productType)
  )
}

test(`writeProductTypes to xlsx
  should write product type to attribute mapping xlsx file`, async (t) => {
    t.timeoutAfter(15000) // 15s
    const download = tempWrite.sync()
    const destination = tempWrite.sync()

    try {
      await before()
      await productTypeExport.downloadProductTypes(download)

      const {
        attributeNames,
      } = await productTypeExport.collectAttributes(download)
      productTypeExport.attributeNames = attributeNames

      const {
        productTypes,
      } = await productTypeExport.collectTypesAndAttributes(download)
      await productTypeExport.writeProductTypes(productTypes, destination)

      const data = await readXlsx(destination)
      const attributes = productTypeExport.attributeNames.join(',')
      const header = data[0].join(',')
      t.equal(
        header,
        `name,key,description,${attributes}`,
        `exported row ${header} is equal`
      )
      t.end()
    } catch (e) {
      t.end(e)
    }
  })

test(`productType xlsx export module
  should write to attributes with all properties to xlsx file`, async (t) => {
    t.timeoutAfter(15000) // 15s
    const download = tempWrite.sync()
    const destination = tempWrite.sync(null, 'output.xlsx')

    try {
      await before()
      await productTypeExport.downloadProductTypes(download)
      const {
        attributeNames,
        attributeKeys,
      } = await productTypeExport.collectAttributes(download)

      productTypeExport.attributeNames = sortAttributes(attributeNames)
      productTypeExport.attributeKeys = sortAttributes(attributeKeys)
      const {
        attributes,
      } = await productTypeExport.collectTypesAndAttributes(download)

      await productTypeExport.writeAttributes(attributes, destination)
      const data = await readXlsx(destination)
      const header = data[0]
      const getColIndex = key => header.indexOf(key)
      // check if all the product types have been exported

      productTypeExport.attributeNames.reduce((rowIndex, attrName) => {
        const attrDef = mockAttributes.find(mock => mock.name === attrName)
        const {
          name, type, label, attributeConstraint, inputHint, displayGroup,
          isRequired, isSearchable,
        } = attrDef
        const row = data[rowIndex]
        t.equal(row[0], name, `row ${name} is equal to name`)
        // if the type is a set, element type of the set needs to appended
        const typeName = type.name === 'set' ? `set:${
          type.elementType.name
        }` : type.name
        t.equal(row[1], typeName, `attribute ${typeName} type is equal`)
        // check for all the localizations of the label
        Object.keys(label).forEach((locale) => {
          t.equal(
            row[getColIndex(`label.${locale}`)],
            label[locale],
            `locale ${label[locale]} is equal`
          )<commit_msg>refactor(tests): Update tests for better readability<commit_after>import 'babel-polyfill'
import _ from 'lodash'
import extract from 'extract-zip'
import Excel from 'exceljs'
import fs from 'fs'
import glob from 'glob'
import path from 'path'
import Promise from 'bluebird'
import test from 'tape'
import tempfile from 'tempfile'
import tempWrite from 'temp-write'

// eslint-disable-next-line max-len
import ProductTypeExport, { sortAttributes } from '../../src/product-type-export'
import * as utils from '../utils'

const extractArchive = Promise.promisify(extract)
const mockProductTypes = utils.generateTestProductTypes()
const mockAttributes = utils.getMockAttributes(mockProductTypes)

let OUTPUT_FOLDER
let client
let productTypeExport
let sphereClientConfig

const readXlsx = async function readXlsx (filePath) {
  const values = []
  const workbook = new Excel.Workbook()
  await workbook.xlsx.readFile(filePath)
  const worksheet = workbook.getWorksheet(1)
  worksheet.eachRow((row) => {
    let rowValues = row.values
    rowValues.shift()

    rowValues = rowValues.map(item => (_.isObject(item) ? null : item))
    values.push(rowValues)
  })

  return values
}

async function before () {
  sphereClientConfig = {
    config: await utils.getClientConfig(),
  }

  client = await utils.getClient()
  OUTPUT_FOLDER = tempfile()
  fs.mkdirSync(OUTPUT_FOLDER)

  productTypeExport = new ProductTypeExport({
    sphereClientConfig,
    config: {
      outputFolder: OUTPUT_FOLDER,
      exportFormat: 'xlsx',
    },
  })

  await utils.deleteAll('productTypes', client)
  return Promise.map(
    mockProductTypes,
    productType => client.productTypes.create(productType)
  )
}

test(`writeProductTypes to xlsx
  should write product type to attribute mapping xlsx file`, async (t) => {
    t.timeoutAfter(15000) // 15s
    const productTypesFile = tempWrite.sync()
    const destination = tempWrite.sync()

    try {
      await before()
      await productTypeExport.downloadProductTypes(productTypesFile)

      const {
        attributeNames,
      } = await productTypeExport.collectAttributes(productTypesFile)
      productTypeExport.attributeNames = attributeNames

      const {
        productTypes,
      } = await productTypeExport.collectTypesAndAttributes(productTypesFile)
      await productTypeExport.writeProductTypes(productTypes, destination)

      const data = await readXlsx(destination)
      const attributes = productTypeExport.attributeNames.join(',')
      const header = data[0].join(',')
      t.equal(
        header,
        `name,key,description,${attributes}`,
        `exported row ${header} is equal`
      )
      t.end()
    } catch (e) {
      t.end(e)
    }
  })

test(`productType xlsx export module
  should write to attributes with all properties to xlsx file`, async (t) => {
    t.timeoutAfter(15000) // 15s
    const productTypesFile = tempWrite.sync()
    const destination = tempWrite.sync(null, 'output.xlsx')

    try {
      await before()
      await productTypeExport.downloadProductTypes(productTypesFile)
      const {
        attributeNames,
        attributeKeys,
      } = await productTypeExport.collectAttributes(productTypesFile)

      productTypeExport.attributeNames = sortAttributes(attributeNames)
      productTypeExport.attributeKeys = sortAttributes(attributeKeys)
      const {
        attributes,
      } = await productTypeExport.collectTypesAndAttributes(productTypesFile)

      await productTypeExport.writeAttributes(attributes, destination)
      const data = await readXlsx(destination)
      const header = data[0]
      const getColIndex = key => header.indexOf(key)
      // check if all the product types have been exported

      productTypeExport.attributeNames.reduce((rowIndex, attrName) => {
        const attrDef = mockAttributes.find(mock => mock.name === attrName)
        const {
          name, type, label, attributeConstraint, inputHint, displayGroup,
          isRequired, isSearchable,
        } = attrDef
        const row = data[rowIndex]
        t.equal(row[0], name, `row ${name} is equal to name`)
        // if the type is a set, element type of the set needs to appended
        const typeName = type.name === 'set' ? `set:${
          type.elementType.name
        }` : type.name
        t.equal(row[1], typeName, `attribute ${typeName} type is equal`)
        // check for all the localizations of the label
        Object.keys(label).forEach((locale) => {
          t.equal(
            row[getColIndex(`label.${locale}`)],
            label[locale],
            `locale ${label[locale]} is equal`
          )
{'fraction_non_alphanumeric': 0.10001093015630123, 'fraction_numerical': 0.004153459394469341, 'mean_word_length': 3.2282809611829943, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2268925', 'n_tokens_mistral': 2611, 'n_tokens_neox': 2575, 'n_words': 665}