Solr.spec.data.js 8.37 KB
define(function(require) {
    'use strict';
    var _ = require('underscore');
    var Util = require('./Util');
    var Facet = require('./Facet');
    var Facets = require('./Facets');
    var Solr = require('./Solr');

    function createSolrResponse(has) {
        var solrData = {};
        var facetCounts = Util.setField(solrData, 'facet_counts', {});
        var facetIntervals = Util.setField(facetCounts, 'facet_intervals', {});
        var facetRanges = Util.setField(facetCounts, 'facet_ranges', {});
        var facetFields = Util.setField(facetCounts, 'facet_fields', {});
        var facetQueries = Util.setField(facetCounts, 'facet_queries', {});
        if (has.stats) {
            solrData.stats = {};
            Util.setField(solrData.stats, 'stats_fields', {});
        }
        return solrData;
    }

    function makeSolrResponseMixedCase(solrResponse) {
        var facetCounts = Util.getField(solrResponse, 'facet_counts');
        return {
            facetRanges: Util.getField(facetCounts, 'facet_ranges'),
            facetFields: Util.getField(facetCounts, 'facet_fields'),
            facetQueries: Util.getField(facetCounts, 'facet_queries'),
            statsFields: Util.getField(solrResponse.stats, 'stats_fields'),
        };
    }

    function createStaticSolrResponse(has, toMerge) {
        var solrResponse = createSolrResponse({stats: true});
        var solrData = makeSolrResponseMixedCase(solrResponse);
        if (_.isFunction(toMerge)) {
            toMerge = toMerge(solrData);
        } else {
            _.each(solrData, function(value, key) {
                _.extend(value, toMerge[key]);
            });
        }
        return solrResponse;
    }

    function expectItem(item, key, value, checked) {
        expect(item.get('key')).toEqual(key);
        expect(item.get('value')).toEqual(value);
        expect(item.get('checked')).toEqual(checked);
    }

    var definedFacets = {
        model: [
            {
                formName: 'model',
                label: 'Model',
            }, {
                queryField: 'model_s',
                facetType: 'field',
                bins: {
                    misc: ['saturn', 'jeep'],
                    startsWithB: ['buick'],
                },
            },
        ],
        year: [
            {
                formName: 'year',
                orderByDirection: 'desc',
                label: 'Year',
            }, {
                queryField: 'year_dt',
                comparator: 'date',
                facetType: 'year-field',
                facetStats: true,
                formatter: function(item) {
                    var value = item.get('key');
                    if (value === 'before') {
                        return 'Older';
                    } 
                    if (value === 'after') {
                        return 'Newer';
                    }
                    var year = new Date(value).getUTCFullYear();
                    return year + ' - ' + (year + 2);
                },
            },
        ],
        dateOfBirth: [
            {
                formName: 'dateOfBirth',
                label: 'Birthday',
            }, {
                queryField: 'dateOfBirth_dt',
                comparator: 'date',
                facetType: 'year',
                facetStats: true,
                other: 'before',
                start: 'NOW/YEAR-25YEAR',
            },
        ],
        value: [
            {
                formName: 'value',
                label: 'Value',
            }, {
                queryField: 'value_d',
                comparator: 'number',
                facetType: 'field',
                facetStats: true,
                formatter: function(item) {
                    var value = parseInt(item.get('key'));
                    return '$' + value + ' - $' + (value + 1000);
                },
            },
        ],
        bodyType: [
            {
                label: 'Type'
            }, {
                facetType: 'field',
                labelMap: {
                    aluminum: 'Aluminum',
                    plastic: 'Plastic',
                    carbonComposite: 'Carbon Composite',
                    steel: 'Steel',
                },
            },
        ],
    };
    var facetsBuilder = function facetsBuilder(facetName) {
        if (facetName) {
            return new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
        }
        var result = {};
        _.each(_.keys(definedFacets), function(facetName) {
            result[facetName] = new Facet(definedFacets[facetName][0], definedFacets[facetName][1]);
        });
        return result;
    };

    function createSolrDataMerger(modelData) {
        return function(solrData) {
            _.each(solrData, function(solrValue, solrKey) {
                _.each(modelData, function(modelObj, modelKey) {
                    var modelValue = modelObj[solrKey];
                    if (modelValue) {
                        if (solrKey === 'facetQueries') {
                            _.extend(solrValue, modelValue);
                        } else {
                            solrValue[modelKey] = modelValue;
                        }
                    }
                });
            });
        };
    }

    var defaultModelData = {
        dateOfBirth: {
            facetRanges: {
                counts: [
                    '1974-01-01T00:00:00.000Z', 1,
                    '1972-01-01T00:00:00.000Z', 1,
                    '1970-01-01T00:00:00.000Z', 1,
                ],
                before: 5,
            },
            statsFields: {min: '2000', max: '2010'},
        },
        bodyType: {
            facetFields: [
                'aluminum', 14,
                'carbonComposite', 66,
                'steel', 2,
            ],
        },

        model: {
            facetFields: [
                'ford', 23,
                'chevy', 18,
                'toyota', 5,
                'saturn', 7,    // keep here to test the bin/exclude feature
                'jeep', 19,     // keep here to test the bin/exclude feature
                'buick', 0,     // keep here to test the bin/exclude feature
                'dodge', 0,
            ],
            facetQueries: {
                'model:misc': 26,
                'model:startsWithB': 0,
            },
        },
        value: {
            facetFields: [
                '23456', 1,
                '5678', 2,
                '1234', 3,
            ],
            statsFields: {min: '1234', max: '23456'},
        },
        year: {
            facetFields: [
                '2000-01-01T00:00:00.000Z', 1,
                '2001-01-01T00:00:00.000Z', 4,
                '2002-01-01T00:00:00.000Z', 0,
                '2003-01-01T00:00:00.000Z', 3,
                '2004-01-01T00:00:00.000Z', 0,
                '2005-01-01T00:00:00.000Z', 0,
                '2006-01-01T00:00:00.000Z', 0,
                '2007-01-01T00:00:00.000Z', 0,
                '2008-01-01T00:00:00.000Z', 7,
                '2009-01-01T00:00:00.000Z', 0,
                '2010-01-01T00:00:00.000Z', 1,
            ],
            statsFields: {min: '2000-01-01T00:00:00.000Z', max: '2010-01-01T00:00:00.000Z'},
        },
    };
    var responses = {
        sequential: [
            createStaticSolrResponse({stats: true}, createSolrDataMerger(defaultModelData)),
        ],
        suggestions: [
            createStaticSolrResponse({stats: true}, {
                facetFields: {
                    'suggestions:model': [
                        'toyota', 5,
                        'ford', 23,
                        'chevy', 18,
                        'nissan', 0,
                    ],
                },
            }),
        ],
    };

    var testSearchBuilder = function testSearchBuilder() {
        var TestSearch = Solr.extend({}, {
            selectUrl: '/solr/query/select',
            orderingItems: [
                {value: 'value_d asc', label: 'Value Ascending'},
                {value: 'year_dt asc', label: 'Year Ascending'},
            ],
            returnFields: [
                'model_s',
                'year_dt',
                'value_d',
                'dateOfBirth_dt',
            ],
            facets: facetsBuilder(),
        });
        return TestSearch;
    };
    return {
        testSearchBuilder: testSearchBuilder,
        expectItem: expectItem,
        facetsBuilder: facetsBuilder,
        responses: responses,
    };
});