Solr.spec.data.js 6.99 KB
define(function(require) {
    'use strict';
    var _ = require('underscore');
    var Util = require('./Util');
    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(solrData) {
        var facetCounts = Util.getField(solrData, 'facet_counts');
        return {
            facetCounts: facetCounts,
            facetRanges: Util.getField(facetCounts, 'facet_ranges'),
            facetFields: Util.getField(facetCounts, 'facet_fields'),
            facetQueries: Util.getField(facetCounts, 'facet_queries'),
            statsFields: Util.getField(solrData.stats, 'stats_fields'),
        };
    }

    function createStaticSolrResponse(has, toMerge) {
        var solrResponse = createSolrResponse({stats: true});
        var solrData = makeSolrResponseMixedCase(solrResponse);
        _.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 facetsBuilder = function facetsBuilder() {
        return {
            model: new Facets.Facet({
                formName: 'model',
                label: 'Model',
            },
            {
                queryField: 'model_s',
                facetType: 'field',
                bins: {
                    misc: ['saturn', 'jeep'],
                    startsWithB: ['buick'],
                },
            }),
            year: new Facets.Facet({
                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: new Facets.Facet({
                formName: 'dateOfBirth',
                label: 'Birthday',
            }, {
                queryField: 'dateOfBirth_dt',
                comparator: 'date',
                facetType: 'year',
                facetStats: true,
                other: 'before',
                start: 'NOW/YEAR-25YEAR',
            }),
            value: new Facets.Facet({
                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);
                },
            }),
        };
    };

    var responses = {
        sequential: [
            createStaticSolrResponse({stats: true}, {
                facetRanges: {
                    dateOfBirth: {
                        counts: [
                            '1974-01-01T00:00:00.000Z', 1,
                            '1972-01-01T00:00:00.000Z', 1,
                            '1970-01-01T00:00:00.000Z', 1,
                        ],
                        before: 5,
                    },
                },
                facetFields: {
                    model: [
                        '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,
                    ],
                    value: [
                        '23456', 1,
                        '5678', 2,
                        '1234', 3,
                    ],
                    year: [
                        '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,
                    ],
                },
                facetQueries: {
                    'model:misc': 26,
                    'model:startsWithB': 0,
                },
                statsFields: {
                    year: {min: '2000', max: '2010'},
                    dateOfBirth: {min: '2000', max: '2010'},
                    value: {min: '1234', max: '23456'},
                },
            }),
        ],
        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,
    };
});