<commit_before>// Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
/* eslint func-names:0 handle-callback-err: 0 */
import { cloneDeep, range } from 'lodash';
import fetchMock from 'fetch-mock';

import * as ex from '../src/execution';
import { expectColumns, expectMetricDefinition, expectOrderBy, expectWhereCondition } from './helpers/execution';

describe('execution', () => {
    describe('with fake server', () => {
        let serverResponseMock;

        afterEach(() => {
            fetchMock.restore();
        });

        describe('Data Execution:', () => {
            beforeEach(() => {
                serverResponseMock = {
                    executionResult: {
                        columns: [
                            {
                                attributeDisplayForm: {
                                    meta: {
                                        identifier: 'attrId',
                                        uri: 'attrUri',
                                        title: 'Df Title'
                                    }
                                }
                            },
                            {
                                metric: {
                                    meta: {
                                        identifier: 'metricId',
                                        uri: 'metricUri',
                                        title: 'Metric Title'
                                    },
                                    content: {
                                        format: '#00'
                                    }
                                }
                            }
                        ],
                        headers: [
                            {
                                id: 'attrId',
                                title: 'Atribute Title',
                                type: 'attrLabel',
                                uri: 'attrUri'
                            },
                            {
                                id: 'metricId',
                                title: 'Metric Title',
                                type: 'metric',
                                uri: 'metricUri'
                            }
                        ],
                        tabularDataResult: '/gdc/internal/projects/myFakeProjectId/experimental/executions/23452345'
                    }
                };
            });

            describe('getData', () => {
                it('should resolve with JSON with correct data including headers', () => {
                    const responseMock = JSON.parse(JSON.stringify(serverResponseMock));

                    fetchMock.mock(
                        '/gdc/internal/projects/myFakeProjectId/experimental/executions',
                        { status: 200, body: JSON.stringify(responseMock) }
                    );
                    fetchMock.mock(
                        /\/gdc\/internal\/projects\/myFakeProjectId\/experimental\/executions\/(\w+)/,
                        { status: 201, body: JSON.stringify({ tabularDataResult: { values: ['a', 1] } }) }
                    );

                    return ex.getData('myFakeProjectId', ['attrId', 'metricId']).then((result) => {
                        expect(result.headers[0].id).to.be('attrId');
                        expect(result.headers[0].uri).to.be('attrUri');
                        expect(result.headers[0].type).to.be('attrLabel');
                        expect(result.headers[0].title).to.be('Atribute Title');
                        expect(result.headers[1].id).to.be('metricId');
                        expect(result.headers[1].uri).to.be('metricUri');
                        expect(result.headers[1].type).to.be('metric');
                        expect(result.headers[1].title).to.be('Metric Title');
                        expect(result.rawData[0]).to.be('a');
                        expect(result.rawData[1]).to.be(1);
                        expect(result.warnings).to.eql([]);
                    });
                });

                it('should resolve with JSON with correct warnings', () => {
                    const responseMock = JSON.parse(JSON.stringify(serverResponseMock));

                    fetchMock.mock(
                        '/gdc/internal/projects/myFakeProjectId/experimental/executions',
                        { status: 200, body: JSON.stringify(responseMock) }
                    );
                    fetchMock.mock(
                        /\/gdc\/internal\/projects\/myFakeProjectId\/experimental\/executions\/(\w+)/,
                        { status: 201, body: JSON.stringify({ tabularDataResult: { warnings: [1, 2, 3] } }) }
                    );

                    return ex.getData('myFakeProjectId', ['attrId', 'metricId']).then((result) => {
                        expect(result.warnings).to.eql([1, 2, 3]);
                    });
                });


                it('should not fail if tabular data result is missing', () => {
                    fetchMock.mock(
                        '/gdc/internal/projects/myFakeProjectId/experimental/executions',
                        { status: 200, body: JSON.stringify(serverResponseMock) }
                    );
                    fetchMock.mock(<commit_msg>RELATED: CL-10935 - Test coverage for extended execution results<commit_after>// Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
/* eslint func-names:0 handle-callback-err: 0 */
import { cloneDeep, range } from 'lodash';
import fetchMock from 'fetch-mock';

import * as ex from '../src/execution';
import { expectColumns, expectMetricDefinition, expectOrderBy, expectWhereCondition } from './helpers/execution';

describe('execution', () => {
    describe('with fake server', () => {
        let serverResponseMock;

        afterEach(() => {
            fetchMock.restore();
        });

        describe('Data Execution:', () => {
            beforeEach(() => {
                serverResponseMock = {
                    executionResult: {
                        columns: [
                            {
                                attributeDisplayForm: {
                                    meta: {
                                        identifier: 'attrId',
                                        uri: 'attrUri',
                                        title: 'Df Title'
                                    }
                                }
                            },
                            {
                                metric: {
                                    meta: {
                                        identifier: 'metricId',
                                        uri: 'metricUri',
                                        title: 'Metric Title'
                                    },
                                    content: {
                                        format: '#00'
                                    }
                                }
                            }
                        ],
                        headers: [
                            {
                                id: 'attrId',
                                title: 'Atribute Title',
                                type: 'attrLabel',
                                uri: 'attrUri'
                            },
                            {
                                id: 'metricId',
                                title: 'Metric Title',
                                type: 'metric',
                                uri: 'metricUri'
                            }
                        ],
                        tabularDataResult: '/gdc/internal/projects/myFakeProjectId/experimental/executions/23452345',
                        extendedTabularDataResult: '/gdc/internal/projects/myFakeProjectId/experimental/executions/extendedResults/23452345'
                    }
                };
            });

            describe('getData', () => {
                it('should resolve with JSON with correct data including headers', () => {
                    const responseMock = JSON.parse(JSON.stringify(serverResponseMock));

                    fetchMock.mock(
                        '/gdc/internal/projects/myFakeProjectId/experimental/executions',
                        { status: 200, body: JSON.stringify(responseMock) }
                    );
                    fetchMock.mock(
                        /\/gdc\/internal\/projects\/myFakeProjectId\/experimental\/executions\/(\w+)/,
                        { status: 201, body: JSON.stringify({ tabularDataResult: { values: ['a', 1] } }) }
                    );

                    return ex.getData('myFakeProjectId', ['attrId', 'metricId']).then((result) => {
                        expect(result.headers[0].id).to.be('attrId');
                        expect(result.headers[0].uri).to.be('attrUri');
                        expect(result.headers[0].type).to.be('attrLabel');
                        expect(result.headers[0].title).to.be('Atribute Title');
                        expect(result.headers[1].id).to.be('metricId');
                        expect(result.headers[1].uri).to.be('metricUri');
                        expect(result.headers[1].type).to.be('metric');
                        expect(result.headers[1].title).to.be('Metric Title');
                        expect(result.rawData[0]).to.be('a');
                        expect(result.rawData[1]).to.be(1);
                        expect(result.warnings).to.eql([]);
                    });
                });

                it('should resolve with JSON with correct warnings', () => {
                    const responseMock = JSON.parse(JSON.stringify(serverResponseMock));

                    fetchMock.mock(
                        '/gdc/internal/projects/myFakeProjectId/experimental/executions',
                        { status: 200, body: JSON.stringify(responseMock) }
                    );
                    fetchMock.mock(
                        /\/gdc\/internal\/projects\/myFakeProjectId\/experimental\/executions\/(\w+)/,
                        { status: 201, body: JSON.stringify({ tabularDataResult: { warnings: [1, 2, 3] } }) }
                    );

                    return ex.getData('myFakeProjectId', ['attrId', 'metricId']).then((result) => {
                        expect(result.warnings).to.eql([1, 2, 3]);
                    });
                });

                it('should retrieve extended results on settings.extended == true', () => {
                    const responseMock = JSON.parse(JSON.stringify(serverResponseMock));

                    fetchMock.mock(
                        '/gdc/internal/projects/myFakeProjectId/experimental/executions',
                        { status: 200, body: JSON.stringify(responseMock) }
                    );

                    fetchMock.mock(
                        /\/gdc\/internal\/projects\/myFakeProjectId\/experimental\/executions\/extendedResults\/(\w+)/,
                        {
                            status: 201,
                            body: JSON.stringify({
                                extendedTabularDataResult: {
                                    paging: { offset: 0, count: 1, total: 1, next: null },
                                    values: [[{ id: '1', name: 'a' }, '2']]
                                }
                            })
                        }
                    );

                    return ex.getData('myFakeProjectId', ['attrId', 'metricId'], {}, { extended: true }).then((result) => {
                        expect(result.rawData[0][0].id).to.be('1');
                        expect(result.rawData[0][0].name).to.be('a');
                        expect(result.rawData[0][1]).to.be('2');
                    });
                });

                it('should not fail if tabular data result is missing', () => {
                    fetchMock.mock(
                        '/gdc/internal/projects/myFakeProjectId/experimental/executions',
                        { status: 200, body: JSON.stringify(serverResponseMock) }
                    );
                    fetchMock.mock(
{'fraction_non_alphanumeric': 0.11615791615791615, 'fraction_numerical': 0.011233211233211233, 'mean_word_length': 0.9610534716679968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1742699', 'n_tokens_mistral': 2857, 'n_tokens_neox': 2567, 'n_words': 428}