{"size": 6574, "ext": "js", "max_stars_repo_path": "modules/engine/test/find-all-test.js", "max_stars_repo_name": "jsa-research/ql.io", "max_stars_repo_stars_event_min_datetime": "2015-01-14T01:45:08.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-01T21:08:25.000Z", "max_issues_repo_path": "modules/engine/test/find-all-test.js", "max_issues_repo_name": "jsa-research/ql.io", "max_issues_repo_issues_event_min_datetime": "2015-03-02T11:38:28.000Z", "max_issues_repo_issues_event_max_datetime": "2018-03-27T18:52:10.000Z", "max_forks_repo_path": "modules/engine/test/find-all-test.js", "max_forks_repo_name": "jsa-research/ql.io", "max_forks_repo_forks_event_min_datetime": "2015-01-27T09:11:54.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-07T01:27:03.000Z"}
{"max_stars_count": 118, "max_issues_count": 2, "max_forks_count": 24, "avg_line_length": 34.7830687831, "max_line_length": 139, "alphanum_fraction": 0.350623669}
/*
 * Copyright 2011 eBay Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

var _    = require('underscore'),
    util = require('util');

var Engine = require('../lib/engine');

var cooked = {
    findall : {
        ports: [
            {
                port: 3000,
                status: 200,
                type: "application",
                subType: "json",
                payload: JSON.stringify(
                    {
                        "OrderTransactionArray": [
                            {
                                "OrderTransaction" : [
                                    {
                                        "ItemID" : "123456"
                                    }
                                ]
                            },
                            {
                                "OrderTransaction" : [
                                    {
                                        "ItemID" : "123456"
                                    }
                                ]
                            }
                        ]
                    }
                )
            },
            {
                port: 3026,
                status: 200,
                type: "application",
                subType: "json",
                payload: JSON.stringify(
                    {
                        "OrderTransactionArray": [
                            {
                                "OrderTransaction" : [
                                    {
                                        "ItemID" : "253888486629"
                                    }
                                ]
                            },
                            {
                                "OrderTransaction" : [
                                    {
                                        "ItemID" : "253888486629"
                                    }
                                ]
                            }
                        ]
                    }
                )
            },
            {
                port: 3028,
                status: 200,
                type: "application",
                subType: "json",
                payload: JSON.stringify(
                    [{ "ItemID" : "253888486629"}, {"ItemID" : "123456" }]
                )

            }
        ],
        script:   'create table payload1 on select get from "http://localhost:3000"\r\n'+
                  'create table payload2 on select get from "http://localhost:3026"\r\n'+
                  'create table payload3 on select get from "http://localhost:3028"\r\n'+
                  'Resp1 = select * from payload1\r\n'+
                  'Resp2 = select * from payload2\r\n'+
                  'Resp3 = select * from payload3 where ItemID in ("{Resp2.$..ItemID}", "{Resp1.$..ItemID}")\r\n'+
                  'i1 = "{Resp1.$..ItemID}";\r\n'+
                  'ids = "{Resp3.$..ItemID}";\r\n'+
                  'txb = "{Resp1.$..OrderTransaction}";\r\n'+
                  'txs = "{Resp2.$..OrderTransaction}";\r\n'+
                  'be = "{Resp1.Errors}";\r\n'+
                  'se = "{Resp2.Errors}"\r\n'+
                  'return {"i1": "{i1}","i2": "{Resp2.$..ItemID}","ids": "{ids}","txb": "{txb}","txs": "{txs}","be": "{be}","se": "{se}"}',

        udf: {
            test : function (test, err, result) {
                if(err) {
                    console.log(err.stack || err);
                    test.ok(false, 'Grrr');

                }
                else {
                    result = result.body;
                    test.ok(result.i1);
                    test.ok(result.i2);
                    test.ok(result.ids);
                    var i1 = _.sortBy(result.i1, function(id) {
                        return id;
                    });
                    i1 = _.unique(i1);
                    var i2 = _.sortBy(result.i2, function(id) {
                        return id;
                    });
                    i2 = _.unique(i2);
                    var ids = _.sortBy(result.ids, function(id) {
                        return id;
                    });
                    ids = _.unique(result.ids);
                    test.equals(i1.length + i2.length, ids.length);

                    test.equals(result.txb.length, 2);
                    test.equals(result.txs.length, 2);
                    test.ok(result.be === undefined);
                    test.ok(result.se === undefined);

                }
            }
        }

    },
    findallflatten : {
        ports: [
            {
                port: 3000,
                status: 200,
                type: "application",
                subType: "json",
                payload: JSON.stringify(
                    {
                        "Item" : [
                            {   "ItemID" : "110763457898",
                                "Title" : "American Motorcycle 1902 Established t-shirts"
                            },
                            {
                                "ItemID" : "110763457898",
                                "Title" : "American Motorcycle 1902 Established t-shirts"
                            }
                        ]
                    }
                )
            }
        ],
        script: 'create table buying on select get from "http://localhost:3000/"'+
                'GetMyeBayBuyingResponse = select * from buying;'+
                'return "{GetMyeBayBuyingResponse.$..Item}";',
        
        udf: {
            test : function (test, err, result) {
                if(err) {
                    console.log(err.stack || err);
                    test.ok(false, 'Grrr');

                }
                else {
                    result = result.body;
                    test.ok(_.isArray(result));
                    test.equals(result.length, 2);

                }
            }
        }

    }

}

module.exports = require('ql-unit').init({
    cooked: cooked,
    engine:new Engine({

    })
});