<commit_before>var env, store, Person, Name, Address;

QUnit.module("integration/fragments - Persisting Records With Fragments", {
  setup: function() {
    Person = DS.Model.extend({
      name      : DS.hasOneFragment("name"),
      addresses : DS.hasManyFragments("address", { defaultValue: null }),
    });

    Name = DS.ModelFragment.extend({
      first  : DS.attr("string"),
      last   : DS.attr("string")
    });

    Address = DS.ModelFragment.extend({
      street  : DS.attr("string"),
      city    : DS.attr("string"),
      region  : DS.attr("string"),
      country : DS.attr("string")
    });

    env = setupEnv({
      person  : Person,
      name    : Name,
      address : Address
    });

    store = env.store;

    expectNoDeprecation();
  },

  teardown: function() {
    env = null;
    store = null;
    Name = null;
    Person = null;
    Address = null;
  }
});

test("persisting the owner record in a clean state maintains clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Tyrion",
        last: "Lannister"
      },
      addresses: [
        {
          street: "1 Sky Cell",
          city: "Eyre",
          region: "Vale of Arryn",
          country: "Westeros"
        }
      ]
    }
  });

  env.adapter.updateRecord = function() {
    return Ember.RSVP.resolve();
  };

  return store.find('person', 1).then(function(person) {
    return person.save();
  }).then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');

    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("persisting the owner record when a fragment is dirty moves owner record, fragment array, and all fragments into clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Eddard",
        last: "Stark"
      },
      addresses: [
        {
          street: "1 Great Keep",
          city: "Winterfell",
          region: "North",
          country: "Westeros"
        }
      ]
    }
  });

  env.adapter.updateRecord = function() {
    return Ember.RSVP.resolve();
  };

  return store.find('person', 1).then(function(person) {
    var name = person.get('name');
    var address = person.get('addresses.firstObject');

    name.set('first', 'Arya');
    address.set('street', '1 Godswood');

    return person.save();
  }).then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');
    var address = addresses.get('firstObject');

    equal(name.get('first'), 'Arya', "`DS.hasOneFragment` change is persisted");
    equal(address.get('street'), '1 Godswood', "`DS.hasManyFragments` change is persisted");
    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("persisting a new owner record moves the owner record, fragment array, and all fragments into clean state", function() {
  var data = {
    name: {
      first: "Daenerys",
      last: "Targaryen"
    },
    addresses: [
      store.createFragment('address', {
        street: "1 Stone Drum",
        city: "Dragonstone",
        region: "Crownlands",
        country: "Westeros"
      })
    ]
  };

  var person = store.createRecord('person');
  person.set('name', store.createFragment('name', data.name));
  person.set('addresses', data.addresses);

  env.adapter.createRecord = function() {
    var payload = Ember.copy(data, true);

    payload.id = 3;

    return Ember.RSVP.resolve(payload);
  };

  return person.save().then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');

    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("a new record can be persisted with null fragments", function() {
  var person = store.createRecord('person');

  equal(person.get('name'), null, "`DS.hasOneFragment` property is null");
  equal(person.get('addresses'), null, "`DS.hasManyFragments` property is null");

  env.adapter.createRecord = function() {
    var payload = { id: 1 };

    return Ember.RSVP.resolve(payload);
  };

  return person.save().then(function(person) {
    equal(person.get('name'), null, "`DS.hasOneFragment` property is still null");
    equal(person.get('addresses'), null, "`DS.hasManyFragments` property is still null");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("the adapter can update fragments on save", function() {
  var data = {
    name: {
      first: "Eddard",
      last: "Stark"
    },
    addresses: [
      {
        street: "1 Great Keep",
        city: "Winterfell",
        region: "North",
        country: "Westeros"
      }
    ]
  };

  store.push({
    type: 'person',
    id: 1,
    attributes: data
  });

  env.adapter.updateRecord = function() {
    var payload = Ember.copy(data, true);

    payload.id = 1;
    payload.name.first = 'Ned';
    payload.addresses[0].street = '1 Godswood';

    return Ember.RSVP.resolve(payload);
  };

  return store.find('person', 1).then(function(person) {
    return person.save();
  }).then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');

    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
    equal(name.get('first'), 'Ned', "`DS.hasOneFragment` fragment correctly updated");
    equal(addresses.get('firstObject.street'), '1 Godswood', "`DS.hasManyFragments` fragment correctly updated");
  });
});

test("existing fragments are updated on save", function() {
  var data = {
    name: {
      first: "Eddard",
      last: "Stark"
    },
    addresses: [
      {
        street: "1 Great Keep",
        city: "Winterfell",
        region: "North",
        country: "Westeros"
      }
    ]
  };

  store.push({
    type: 'person',
    id: 1,
    attributes: data
  });

  env.adapter.updateRecord = function() {
    var payload = Ember.copy(data, true);

    payload.id = 1;
    payload.name.first = 'Ned';
    payload.addresses.unshift({
      street: "1 Red Keep",
      city: "Kings Landing",
      region: "Crownlands",
      country: "Westeros"
    });

    return Ember.RSVP.resolve(payload);
  };

  var name, addresses, address;

  return store.find('person', 1).then(function(person) {
    name = person.get('name');
    addresses = person.get('addresses');
    address = addresses.get('firstObject');
    return person.save();
  }).then(function() {
    equal(name.get('first'), 'Ned', "`DS.hasOneFragment` fragment correctly updated");
    equal(address.get('street'), '1 Red Keep', "`DS.hasManyFragments` fragment correctly updated");
    equal(addresses.get('length'), 2, "`DS.hasManyFragments` fragment correctly updated");
  });
});

test("the adapter can update fragments on reload", function() {
  var data = {
    name: {
      first: "Brandon",
      last: "Stark"
    },
    addresses: [<commit_msg>Added failing test for fragment updating on record creation<commit_after>var env, store, Person, Name, Address;

QUnit.module("integration/fragments - Persisting Records With Fragments", {
  setup: function() {
    Person = DS.Model.extend({
      name      : DS.hasOneFragment("name"),
      addresses : DS.hasManyFragments("address", { defaultValue: null }),
    });

    Name = DS.ModelFragment.extend({
      first  : DS.attr("string"),
      last   : DS.attr("string")
    });

    Address = DS.ModelFragment.extend({
      street  : DS.attr("string"),
      city    : DS.attr("string"),
      region  : DS.attr("string"),
      country : DS.attr("string")
    });

    env = setupEnv({
      person  : Person,
      name    : Name,
      address : Address
    });

    store = env.store;

    expectNoDeprecation();
  },

  teardown: function() {
    env = null;
    store = null;
    Name = null;
    Person = null;
    Address = null;
  }
});

test("persisting the owner record in a clean state maintains clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Tyrion",
        last: "Lannister"
      },
      addresses: [
        {
          street: "1 Sky Cell",
          city: "Eyre",
          region: "Vale of Arryn",
          country: "Westeros"
        }
      ]
    }
  });

  env.adapter.updateRecord = function() {
    return Ember.RSVP.resolve();
  };

  return store.find('person', 1).then(function(person) {
    return person.save();
  }).then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');

    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("persisting the owner record when a fragment is dirty moves owner record, fragment array, and all fragments into clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Eddard",
        last: "Stark"
      },
      addresses: [
        {
          street: "1 Great Keep",
          city: "Winterfell",
          region: "North",
          country: "Westeros"
        }
      ]
    }
  });

  env.adapter.updateRecord = function() {
    return Ember.RSVP.resolve();
  };

  return store.find('person', 1).then(function(person) {
    var name = person.get('name');
    var address = person.get('addresses.firstObject');

    name.set('first', 'Arya');
    address.set('street', '1 Godswood');

    return person.save();
  }).then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');
    var address = addresses.get('firstObject');

    equal(name.get('first'), 'Arya', "`DS.hasOneFragment` change is persisted");
    equal(address.get('street'), '1 Godswood', "`DS.hasManyFragments` change is persisted");
    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("persisting a new owner record moves the owner record, fragment array, and all fragments into clean state", function() {
  var data = {
    name: {
      first: "Daenerys",
      last: "Targaryen"
    },
    addresses: [
      store.createFragment('address', {
        street: "1 Stone Drum",
        city: "Dragonstone",
        region: "Crownlands",
        country: "Westeros"
      })
    ]
  };

  var person = store.createRecord('person');
  person.set('name', store.createFragment('name', data.name));
  person.set('addresses', data.addresses);

  env.adapter.createRecord = function() {
    var payload = Ember.copy(data, true);

    payload.id = 3;

    return Ember.RSVP.resolve(payload);
  };

  return person.save().then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');

    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("a new record can be persisted with null fragments", function() {
  var person = store.createRecord('person');

  equal(person.get('name'), null, "`DS.hasOneFragment` property is null");
  equal(person.get('addresses'), null, "`DS.hasManyFragments` property is null");

  env.adapter.createRecord = function() {
    var payload = { id: 1 };

    return Ember.RSVP.resolve(payload);
  };

  return person.save().then(function(person) {
    equal(person.get('name'), null, "`DS.hasOneFragment` property is still null");
    equal(person.get('addresses'), null, "`DS.hasManyFragments` property is still null");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("the adapter can update fragments on save", function() {
  var data = {
    name: {
      first: "Eddard",
      last: "Stark"
    },
    addresses: [
      {
        street: "1 Great Keep",
        city: "Winterfell",
        region: "North",
        country: "Westeros"
      }
    ]
  };

  store.push({
    type: 'person',
    id: 1,
    attributes: data
  });

  env.adapter.updateRecord = function() {
    var payload = Ember.copy(data, true);

    payload.id = 1;
    payload.name.first = 'Ned';
    payload.addresses[0].street = '1 Godswood';

    return Ember.RSVP.resolve(payload);
  };

  return store.find('person', 1).then(function(person) {
    return person.save();
  }).then(function(person) {
    var name = person.get('name');
    var addresses = person.get('addresses');

    ok(!name.get('hasDirtyAttributes'), "`DS.hasOneFragment` fragment is clean");
    ok(!addresses.isAny('hasDirtyAttributes'), "all `DS.hasManyFragments` fragments are clean");
    ok(!addresses.get('hasDirtyAttributes'), "fragment array is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
    equal(name.get('first'), 'Ned', "`DS.hasOneFragment` fragment correctly updated");
    equal(addresses.get('firstObject.street'), '1 Godswood', "`DS.hasManyFragments` fragment correctly updated");
  });
});

test("existing fragments are updated on save", function() {
  var data = {
    name: {
      first: "Eddard",
      last: "Stark"
    },
    addresses: [
      {
        street: "1 Great Keep",
        city: "Winterfell",
        region: "North",
        country: "Westeros"
      }
    ]
  };

  store.push({
    type: 'person',
    id: 1,
    attributes: data
  });

  env.adapter.updateRecord = function() {
    var payload = Ember.copy(data, true);

    payload.id = 1;
    payload.name.first = 'Ned';
    payload.addresses.unshift({
      street: "1 Red Keep",
      city: "Kings Landing",
      region: "Crownlands",
      country: "Westeros"
    });

    return Ember.RSVP.resolve(payload);
  };

  var name, addresses, address;

  return store.find('person', 1).then(function(person) {
    name = person.get('name');
    addresses = person.get('addresses');
    address = addresses.get('firstObject');
    return person.save();
  }).then(function() {
    equal(name.get('first'), 'Ned', "`DS.hasOneFragment` fragment correctly updated");
    equal(address.get('street'), '1 Red Keep', "`DS.hasManyFragments` fragment correctly updated");
    equal(addresses.get('length'), 2, "`DS.hasManyFragments` fragment correctly updated");
  });
});

test("newly created fragments are updated on save", function() {
  var data = {
    name: {
      first: "Eddard",
      last: "Stark"
    },
    addresses: [
      {
        street: "1 Great Keep",
        city: "Winterfell",
        region: "North",
        country: "Westeros"
      }
    ]
  };

  env.adapter.createRecord = function() {
    var payload = Ember.copy(data, true);

    payload.id = 1;
    payload.name.first = 'Ned';
    payload.addresses.unshift({
      street: "1 Red Keep",
      city: "Kings Landing",
      region: "Crownlands",
      country: "Westeros"
    });

    return Ember.RSVP.resolve(payload);
  };

  var person = store.createRecord('person');
  var name = store.createFragment('name', data.name);
  var address = store.createFragment('address', address);

  person.set('name', name);
  person.set('addresses', [ address ]);

  var addresses = person.get('addresses');

  return person.save().then(function() {
    equal(name.get('first'), 'Ned', "`DS.hasOneFragment` fragment correctly updated");
    equal(address.get('street'), '1 Red Keep', "`DS.hasManyFragments` fragment correctly updated");
    equal(addresses.get('length'), 2, "`DS.hasManyFragments` fragment correctly updated");
  });
});

test("the adapter can update fragments on reload", function() {
  var data = {
    name: {
      first: "Brandon",
      last: "Stark"
    },
    addresses: [
{'fraction_non_alphanumeric': 0.16165020337013364, 'fraction_numerical': 0.0031958163858221964, 'mean_word_length': 3.0707190160832547, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6272039', 'n_tokens_mistral': 5453, 'n_tokens_neox': 5281, 'n_words': 1258}