<commit_before>var ORM    = require('../../');
var helper = require('../support/spec_helper');
var should = require('should');
var async  = require('async');
var _      = require('lodash');

describe("hasOne", function() {
	var db     = null;
	var Person = null;
        var Pet    = null;

	var setup = function (autoFetch) {
		return function (done) {
			db.settings.set('instance.cache', false);
			db.settings.set('instance.returnAllErrors', true);

		        Person = db.define('person', {
                          id        : {type : "integer",               mapsTo: "personID", key:true},
			  firstName : {type : "text",    size:"255"},
			  lastName  : {type : "text",    size:"255"}
			});

		        Pet = db.define('pet', {
                          id      :  {type : "integer",                mapsTo:"petID", key:true},
			  petName :  {type : "text",     size:"255"},
                          ownerID :  {type : "integer",  size:"4"}
			});

		        Pet.hasOne('owner', Person, {field: 'ownerID', autoFetch:autoFetch});

		        helper.dropSync([Person, Pet], function(err) {
                          if (err) return done(err);
                          Pet.create([{
                            id: 10,
                            petName: 'Muttley',
                            owner: {
                              id: 12,
                              firstName: 'Stuey',
                              lastName:  'McG'
                            }
                          },
                          {
                            id: 11,
                            petName: 'Snagglepuss',
                            owner: {
                              id: 0,
                              firstName: 'John',
                              lastName:  'Doe'
                            }
                          }], done);
                        });
		};
	};

	before(function(done) {
	  helper.connect(function (connection) {
	    db = connection;
	    done();
	  });
	});

	describe("auto fetch", function () {
		before(setup(true));

		it("should work for non-zero ownerID ", function (done) {
			Pet.find({petName: "Muttley"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Muttley");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(10);
                          pets[0].ownerID.should.equal(12);

                          pets[0].should.have.property("owner");
                          pets[0].owner.firstName.should.equal("Stuey");

			  return done();
			});
		});

		it("should work for zero ownerID ", function (done) {
			Pet.find({petName: "Snagglepuss"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Snagglepuss");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(11);
                          pets[0].ownerID.should.equal(0);

                          pets[0].should.have.property("owner");
                          pets[0].owner.firstName.should.equal("John");

			  return done();
			});
		});
	});

	describe("no auto fetch", function () {
		before(setup(false));

		it("should work for non-zero ownerID ", function (done) {
			Pet.find({petName: "Muttley"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Muttley");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(10);
                          pets[0].ownerID.should.equal(12);

                          pets[0].should.not.have.property("owner");

                          // But we should be able to see if its there
                          pets[0].hasOwner(function(result) {
                            should.equal(result, true);
                          });

                          // ...and then get it
                          pets[0].getOwner(function(result) {
                            result.firstName.should.equal("Stuey");
                          });

			  return done();
			});
		});

		it("should work for zero ownerID ", function (done) {
			Pet.find({petName: "Snagglepuss"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Snagglepuss");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(11);
                          pets[0].ownerID.should.equal(0);

                          pets[0].should.not.have.property("owner");

                          // But we should be able to see if its there
                          pets[0].hasOwner(function(result) {
                            should.equal(result, true);
                          });

                          // ...and then get it
                          pets[0].getOwner(function(result) {
                            result.firstName.should.equal("John");
                          });

			  return done();
			});
		});
	});
});<commit_msg>hasOne() associations need to work when the FK value is 0. Pre-existing databases may've used zero as a valid entity identifier.<commit_after>var ORM    = require('../../');
var helper = require('../support/spec_helper');
var should = require('should');
var async  = require('async');
var _      = require('lodash');

describe("hasOne", function() {
	var db     = null;
	var Person = null;
        var Pet    = null;

	var setup = function (autoFetch) {
		return function (done) {
			db.settings.set('instance.cache', false);
			db.settings.set('instance.returnAllErrors', true);

		        Person = db.define('person', {
                          id        : {type : "integer",               mapsTo: "personID", key:true},
			  firstName : {type : "text",    size:"255"},
			  lastName  : {type : "text",    size:"255"}
			});

		        Pet = db.define('pet', {
                          id      :  {type : "integer",                mapsTo:"petID", key:true},
			  petName :  {type : "text",     size:"255"},
                          ownerID :  {type : "integer",  size:"4"}
			});

		        Pet.hasOne('owner', Person, {field: 'ownerID', autoFetch:autoFetch});

		        helper.dropSync([Person, Pet], function(err) {
                          if (err) return done(err);
                          Pet.create([{
                            id: 10,
                            petName: 'Muttley',
                            owner: {
                              id: 12,
                              firstName: 'Stuey',
                              lastName:  'McG'
                            }
                          },
                          {
                            id: 11,
                            petName: 'Snagglepuss',
                            owner: {
                              id: 0,
                              firstName: 'John',
                              lastName:  'Doe'
                            }
                          }], done);
                        });
		};
	};

	before(function(done) {
	  helper.connect(function (connection) {
	    db = connection;
	    done();
	  });
	});

	describe("auto fetch", function () {
		before(setup(true));

		it("should work for non-zero ownerID ", function (done) {
			Pet.find({petName: "Muttley"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Muttley");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(10);
                          pets[0].ownerID.should.equal(12);

                          pets[0].should.have.property("owner");
                          pets[0].owner.firstName.should.equal("Stuey");

			  return done();
			});
		});

		it("should work for zero ownerID ", function (done) {
			Pet.find({petName: "Snagglepuss"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Snagglepuss");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(11);
                          pets[0].ownerID.should.equal(0);

                          pets[0].should.have.property("owner");
                          pets[0].owner.firstName.should.equal("John");

			  return done();
			});
		});
	});

	describe("no auto fetch", function () {
		before(setup(false));

		it("should work for non-zero ownerID ", function (done) {
			Pet.find({petName: "Muttley"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Muttley");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(10);
                          pets[0].ownerID.should.equal(12);

                          pets[0].should.not.have.property("owner");

                          // But we should be able to see if its there
                          pets[0].hasOwner(function(err, result) {
                            should.equal(result, true);
                          });

                          // ...and then get it
                          pets[0].getOwner(function(err, result) {
                            result.firstName.should.equal("Stuey");
                          });

			  return done();
			});
		});

		it("should work for zero ownerID ", function (done) {
			Pet.find({petName: "Snagglepuss"}, function(err, pets) {
                          should.not.exist(err);

                          pets[0].petName.should.equal("Snagglepuss");
                          pets[0].should.have.property("id");
                          pets[0].id.should.equal(11);
                          pets[0].ownerID.should.equal(0);

                          pets[0].should.not.have.property("owner");

                          // But we should be able to see if its there
                          pets[0].hasOwner(function(err, result) {
                            should.equal(result, true);
                          });

                          // ...and then get it
                          pets[0].getOwner(function(err, result) {
                            result.firstName.should.equal("John");
                          });

			  return done();
			});
		});
	});
});
{'fraction_non_alphanumeric': 0.13855023154711274, 'fraction_numerical': 0.010868537945373784, 'mean_word_length': 1.139506672058229, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4730930', 'n_tokens_mistral': 2967, 'n_tokens_neox': 2770, 'n_words': 492}