<commit_before>// test/object-readarray-hook.js
//
// Testing DatabankObject readArray()
//
// Copyright 2012, StatusNet Inc.
//
// 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 assert = require('assert'),
    vows = require('vows'),
    Step = require('step'),
    databank = require('../databank'),
    Databank = databank.Databank,
    DatabankObject = require('../databankobject').DatabankObject,
    nicknames = ['george', 'jane', 'judy', 'elroy', 'astro', 'rosie'];

var objectReadArrayHookContext = function(driver, params) {

    var context = {};

    context["When we create a " + driver + " databank"] = {

        topic: function() {
            params.schema = {
                person: {
                    pkey: 'username'
                }
            };
            return Databank.get(driver, params);
        },
        'We can connect to it': {
            topic: function(bank) {
                bank.connect(params, this.callback);
            },
            teardown: function(bank) {
                bank.disconnect(function(err) {});
            },
            'without an error': function(err) {
                assert.ifError(err);
            },
            'and we can initialize the Person class': {
                topic: function(bank) {
                    var Person;

                    DatabankObject.bank = bank;

                    Person = DatabankObject.subClass('person');

                    return Person;
                },
                'which is valid': function(Person) {
                    assert.isFunction(Person);
                },
                'and we can create a few people': {
                    topic: function(Person) {
                        var cb = this.callback;

                        Step(
                            function() {
                                var i, group = this.group();
                                for (i = 0; i < nicknames.length; i++) {
                                    Person.create({username: nicknames[i]}, group());
                                }
                            },
                            function(err, people) {
                                cb(err, people);
                            }
                        );
                    },
                    teardown: function(people) {
                        var i;

                        for (i = 0; i < people.length; i++) {
                            people[i].del(function(err) {});
                        }
                    },
                    'it works': function(err, people) {
                        assert.ifError(err);
                        assert.isArray(people);
                    },
                    'and we read a few back': {
                        topic: function(people, Person) {
                            var cb = this.callback,
                                called = {
                                    before: 0,
                                    after: 0,
                                    people: {}
                                };

                            Person.beforeGet = function(username, callback) {
                                called.before++;
                                callback(null, username);
                            };

                            Person.prototype.afterGet = function(callback) {
                                called.after++;
                                this.addedByAfter = 23;
                                callback(null, this);
                            };

                            Person.readArray(nicknames, function(err, ppl) {
                                called.people = ppl;
                                cb(err, called);
                            });
                        },
                        'without an error': function(err, called) {
                            assert.ifError(err);
                            assert.isObject(called);
                        },
                        'and the before hook is called': function(err, called) {
                            assert.isObject(called);
                            assert.equal(called.before, nicknames.length);
                        },
                        'and the after hook is called': function(err, called) {
                            assert.equal(called.after, nicknames.length);
                        },
                        'and the after hook modification happened': function(err, called) {
                            var i = 0;
                            for (i = 0; i < called.people.length; i++) {
                                assert.isObject(called.people[i]);
                                assert.equal(called.people[i].addedByAfter, 23);
                            }
                        }
                    }
                }
            }
        }
    };

    return context;
};

module.exports = objectReadArrayHookContext;<commit_msg>use Appliance instead of Person in readarray hook test<commit_after>// test/object-readarray-hook.js
//
// Testing DatabankObject readArray()
//
// Copyright 2012, StatusNet Inc.
//
// 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 assert = require('assert'),
    vows = require('vows'),
    Step = require('step'),
    databank = require('../databank'),
    Databank = databank.Databank,
    DatabankObject = require('../databankobject').DatabankObject,
    names = ['blender', 'dishwasher', 'toaster', 'refrigerator', 'mixer', 'waffle iron'];

var objectReadArrayHookContext = function(driver, params) {

    var context = {};

    context["When we create a " + driver + " databank"] = {

        topic: function() {
            params.schema = {
                appliance: {
                    pkey: 'name'
                }
            };
            return Databank.get(driver, params);
        },
        'We can connect to it': {
            topic: function(bank) {
                bank.connect(params, this.callback);
            },
            teardown: function(bank) {
                bank.disconnect(function(err) {});
            },
            'without an error': function(err) {
                assert.ifError(err);
            },
            'and we can initialize the Appliance class': {
                topic: function(bank) {
                    var Appliance;

                    DatabankObject.bank = bank;

                    Appliance = DatabankObject.subClass('appliance');

                    return Appliance;
                },
                'which is valid': function(Appliance) {
                    assert.isFunction(Appliance);
                },
                'and we can create a few appliances': {
                    topic: function(Appliance) {
                        var cb = this.callback;

                        Step(
                            function() {
                                var i, group = this.group();
                                for (i = 0; i < names.length; i++) {
                                    Appliance.create({name: names[i]}, group());
                                }
                            },
                            function(err, appliances) {
                                cb(err, appliances);
                            }
                        );
                    },
                    teardown: function(appliances) {
                        var i;

                        for (i = 0; i < appliances.length; i++) {
                            appliances[i].del(function(err) {});
                        }
                    },
                    'it works': function(err, appliances) {
                        assert.ifError(err);
                        assert.isArray(appliances);
                    },
                    'and we read a few back': {
                        topic: function(appliances, Appliance) {
                            var cb = this.callback,
                                called = {
                                    before: 0,
                                    after: 0,
                                    appliances: {}
                                };

                            Appliance.beforeGet = function(name, callback) {
                                called.before++;
                                callback(null, name);
                            };

                            Appliance.prototype.afterGet = function(callback) {
                                called.after++;
                                this.addedByAfter = 23;
                                callback(null, this);
                            };

                            Appliance.readArray(names, function(err, ppl) {
                                called.appliances = ppl;
                                cb(err, called);
                            });
                        },
                        'without an error': function(err, called) {
                            assert.ifError(err);
                            assert.isObject(called);
                        },
                        'and the before hook is called': function(err, called) {
                            assert.isObject(called);
                            assert.equal(called.before, names.length);
                        },
                        'and the after hook is called': function(err, called) {
                            assert.equal(called.after, names.length);
                        },
                        'and the after hook modification happened': function(err, called) {
                            var i = 0;
                            for (i = 0; i < called.appliances.length; i++) {
                                assert.isObject(called.appliances[i]);
                                assert.equal(called.appliances[i].addedByAfter, 23);
                            }
                        }
                    }
                }
            }
        }
    };

    return context;
};

module.exports = objectReadArrayHookContext;
{'fraction_non_alphanumeric': 0.0957619477006312, 'fraction_numerical': 0.0032461677186654643, 'mean_word_length': 0.9650956768249468, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3290224', 'n_tokens_mistral': 2582, 'n_tokens_neox': 2380, 'n_words': 618}