<commit_before>"use strict";

var expect = require("expect.js");

var mockedOctocats = [
    {
        id : 1,
        name : "<NAME>",
        age : 12
    },
    {
        id : 2,
        name : "<NAME>",
        age : 10
    }
];

describe("Model-Services", function () {

    describe("Server", function () {

        var Octocat,
            testService,
            env;

        before(function () {

            Octocat = require("./../shared/Model/Octocat.class.js");
            var services = require("../../lib/shared/registries/serviceRegistry.js");
            services.getService = function () {
                return testService;
            };
        });

        describe("#findById", function () {

            beforeEach(function () {
                testService = {
                    read : function (ids, callback) {
                        callback({ status : "success", data : mockedOctocats[ids.octocat - 1] });
                    },
                    readCollection : function (ids, params, callback) {
                        callback({ status : "success", data : mockedOctocats });
                    }
                };
            });

            it("should work with findById(1, callback) ", function (done) {
                Octocat.findById(1, function (err, model) {
                    expect(err).to.be(null);
                    expect(model.get("name")).to.eql("<NAME>");
                    done();
                });
            });

            it("should fail if no server-service is defined", function (done) {

                testService = null;

                Octocat.findById(1, function (err, model) {
                    expect(err).to.be.an(Error);
                    done();
                });
            });

            it("should return an error if the response-processing fails (i.e. unknown fields) ", function (done) {

                testService.read = function (ids, callback) {

                    callback({
                        status : "success",
                        data : {
                            id : 2,
                            name : "<NAME>",
                            wrongField : 10
                        }
                    });
                };

                Octocat.findById(1, function (err, model) {
                    expect(err).to.be.an(Error);
                    done();
                });
            });
        });

        describe("#find", function () {

            beforeEach(function () {
                testService = {
                    readCollection : function (ids, params, callback) {
                        callback({ status : "success", data : mockedOctocats });
                    }
                };
            });

            it("should work if called with ids and params", function (done) {
                Octocat.find({}, { da : "ta" }, function (err, models) {
                    expect(err).to.be(null);
                    expect(models.get(0).get("name")).to.eql("Octo 1");
                    expect(models.get(1).get("name")).to.eql("Octo 2");
                    done();
                });
            });

            it("should fail if no server-service is defined", function (done) {

                testService = null;

                Octocat.find({}, function (err, models) {
                    expect(err).to.be.an(Error);
                    done();
                });
            });
        });
    });
});<commit_msg>- added Model-CRUD tests to ModelService.test.js<commit_after>"use strict";

var expect = require("expect.js");

var mockedOctocats = [
    {
        id : 1,
        name : "<NAME>",
        age : 12
    },
    {
        id : 2,
        name : "<NAME>",
        age : 10
    }
];

describe("Model-Services", function () {

    describe("Server", function () {

        var Octocat,
            testService,
            env;

        before(function () {

            Octocat = require("./../shared/Model/Octocat.class.js");
            var services = require("../../lib/shared/registries/serviceRegistry.js");
            services.getService = function () {
                return testService;
            };
        });

        describe("CRUD", function () {

            var octocat;

            beforeEach(function () {

                octocat = new Octocat();
                octocat.setService(testService);

                testService = {
                    create : function (ids, model, callback) {
                        callback({ status : "success", data : { name : model.get("name"), age : 10 }});
                    },
                    update : function (ids, model, callback) {
                        callback({ status : "success", data : { name : model.get("name"), age : 12 }});
                    },
                    destroy : function (ids, callback) {
                        callback({ status : "success" });
                    }
                };
            });

            describe("Error handling and format parsing (__processResponse)", function () {
                it("should fail if response is no valid object", function (done) {

                    testService.create = function (ids, model, callback) {
                        callback();
                    };

                    octocat = new Octocat();

                    octocat.save(function (err) {
                        expect(err).not.to.be(null);
                        done();
                    });
                });

                it("should fail if no service is defined for requests", function (done) {
                    octocat.setService({});
                    octocat.save(function (err) {
                        expect(err).not.to.be(null);
                        done();
                    });
                });

                it("should convert an error-response to an internal error", function (done) {
                    octocat.setService({
                        create : function (ids, model, callback) {
                            callback({ status : "error", message : "my error message" });
                        }
                    });
                    octocat.save(function (err) {
                        expect(err.message).to.contain("my error message");
                        done();
                    });
                });
            });

            describe("#save", function () {

                it("call the update service if ID is set and return successfully", function (done) {
                    octocat = new Octocat(2);
                    octocat.setService(testService);
                    octocat.set('name', 'Octocat');
                    octocat.set('age', 8);
                    expect(octocat.getId()).to.be(2);

                    octocat.save(function (err) {
                        expect(err).to.be(null);
                        expect(octocat.getId()).to.be(2);
                        expect(octocat.get("age")).to.be(12);
                        expect(octocat.get("name")).to.be("Octocat");
                        done();
                    });
                });

                it("call the create service if ID is not set and return successfully", function (done) {
                    octocat.set('name', 'Octocat');
                    expect(octocat.getId()).to.be(null);

                    octocat.save(function (err) {
                        expect(err).to.be(null);
                        expect(octocat.get("age")).to.be(10);
                        expect(octocat.get("name")).to.be("Octocat");
                        done();
                    });
                });

                it("should also work with sync services", function (done) {
                    octocat.setService({
                        create : function (ids, model) {
                            return { status : "success", data : { age : 10 } };
                        }
                    });
                    octocat.set('name', 'Octocat');
                    expect(octocat.getId()).to.be(null);

                    octocat.save(function (err) {
                        expect(err).to.be(null);
                        expect(octocat.get("age")).to.be(10);
                        expect(octocat.get("name")).to.be("Octocat");
                        done();
                    });
                });
            });

            describe("#destroy", function () {

                var mockedDeleteService = {
                    destroy : function (ids, callback) {
                        if (ids !== null) {
                            callback({ status : "success" });
                            return;
                        }
                        callback({ status : "error", message : "missing IDs" });
                    }
                };

                it("call the delete service if ID is set and return successfully", function (done) {
                    octocat = new Octocat(2);
                    octocat.setService(mockedDeleteService);

                    octocat.destroy(function (err) {
                        expect(err).to.be(null);
                        done();
                    });
                });

                it("should fail with a missing ID", function (done) {
                    octocat.setService(mockedDeleteService);
                    expect(octocat.getId()).to.be(null);

                    octocat.save(function (err) {
                        expect(err).to.be.an(Error);
                        done();
                    });
                });
            });
        });

        describe("Statics", function () {

            describe("#findById", function () {

                beforeEach(function () {
                    testService = {
                        read : function (ids, callback) {
                            callback({ status : "success", data : mockedOctocats[ids.octocat - 1] });
                        },
                        readCollection : function (ids, params, callback) {
                            callback({ status : "success", data : mockedOctocats });
                        }
                    };
                });

                it("should work with findById(1, callback) ", function (done) {
                    Octocat.findById(1, function (err, model) {
                        expect(err).to.be(null);
                        expect(model.get("name")).to.eql("Octo 1");
                        done();
                    });
                });

                it("should fail if no server-service is defined", function (done) {

                    testService = null;

                    Octocat.findById(1, function (err, model) {
                        expect(err).to.be.an(Error);
                        done();
                    });
                });

                it("should return an error if the response-processing fails (i.e. unknown fields) ", function (done) {

                    testService.read = function (ids, callback) {

                        callback({
                            status : "success",
                            data : {
                                id : 2,
                                name : "Octo 2",
                                wrongField : 10
                            }
                        });
                    };

                    Octocat.findById(1, function (err, model) {
                        expect(err).to.be.an(Error);
                        done();
                    });
                });
            });

            describe("#find", function () {

                beforeEach(function () {
                    testService = {
                        readCollection : function (ids, params, callback) {
                            callback({ status : "success", data : mockedOctocats });
                        }
                    };
                });

                it("should work if called with ids and params", function (done) {
                    Octocat.find({}, { da : "ta" }, function (err, models) {
                        expect(err).to.be(null);
                        expect(models.get(0).get("name")).to.eql("Octo 1");
                        expect(models.get(1).get("name")).to.eql("Octo 2");
                        done();
                    });
                });

                it("should fail if no server-service is defined", function (done) {

                    testService = null;

                    Octocat.find({}, function (err, models) {
                        expect(err).to.be.an(Error);
                        done();
                    });
                });
            });
        });
    });
});
{'fraction_non_alphanumeric': 0.12477509191895486, 'fraction_numerical': 0.004302589376515685, 'mean_word_length': 0.9863269111249223, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4989974', 'n_tokens_mistral': 3174, 'n_tokens_neox': 2868, 'n_words': 621}