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

QUnit.module("integration - Dependent State", {
  setup: function() {
    Person = DS.Model.extend({
      title: DS.attr('string'),
      name: MF.fragment('name'),
      addresses: MF.fragmentArray('address'),
      titles: MF.array(),
      hobbies: MF.fragmentArray('hobby', { defaultValue: null })
    });

    Name = MF.Fragment.extend({
      first: DS.attr('string'),
      last: DS.attr('string')
    });

    Address = MF.Fragment.extend({
      street: DS.attr('string'),
      city: DS.attr('string'),
      region: DS.attr('string'),
      country: DS.attr('string')
    });

    Hobby = MF.Fragment.extend({
      name: DS.attr('string')
    });

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

    store = env.store;

    expectNoDeprecation();

    people = [
      {
        id: 1,
        name: {
          first: "Tyrion",
          last: "Lannister"
        },
        addresses: [
          {
            street: "1 Sky Cell",
            city: "Eyre",
            region: "Vale of Arryn",
            country: "Westeros"
          },
          {
            street: "1 Tower of the Hand",
            city: "King's Landing",
            region: "Crownlands",
            country: "Westeros"
          }
        ],
        titles: [
          "Hand of the King",
          "Master of Coin"
        ]
      }
    ];
  },

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

function pushPerson(id) {
  store.push({
    type: 'person',
    id: id,
    attributes: Ember.A(people).findBy('id', id)
  });
}

test("changing a fragment property dirties the fragment and owner record", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Jamie",
        last: "Lannister"
      }
    }
  });

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

    name.set('first', 'Cercei');

    ok(name.get('hasDirtyAttributes'), "fragment is dirty");
    ok(person.get('hasDirtyAttributes'), "owner record is dirty");
  });
});

test("setting a fragment property to an object literal dirties the fragment and owner record", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Visenya",
        last: "Targaryen"
      }
    }
  });

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

    person.set('name', {
      first: 'Rhaenys',
    });

    ok(name.get('hasDirtyAttributes'), "fragment is dirty");
    ok(person.get('hasDirtyAttributes'), "owner record is dirty");
  });
});

test("setting a fragment property with an object literal to the same value does not dirty the fragment or owner record", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Samwell",
        last: "Tarly"
      }
    }
  });

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

    person.set('name', {
      first: "Samwell",
      last: "Tarly"
    });

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("restoring a fragment property to its original state returns the fragment and owner record to a clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Hoster",
        last: "Tully"
      }
    }
  });

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

    name.set('first', 'Brynden');
    name.set('first', 'Hoster');

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("restoring a fragment property to its original state when the owner record was dirty returns the fragment to a clean state maintains the owner record's dirty state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Jorah",
        last: "Mormont"
      }
    }
  });

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

    // Dirty the owner record
    person.set('title', 'Lord Commander');

    name.set('first', 'Jeor');
    name.set('first', 'Jorah');

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(person.get('hasDirtyAttributes'), "owner record is still dirty");
  });
});

test("rolling back the owner record returns fragment and owner record to a clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Catelyn",
        last: "Stark"
      }
    }
  });

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

    name.set('last', 'Tully');

    person.rollbackAttributes();

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("a record can be rolled back multiple times", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Arya",
        last: "Stark"
      }
    }
  });

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

    name.set('last', '');
    person.rollbackAttributes();

    equal(name.get('last'), 'Stark', "fragment has correct values");
    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");

    name.set('last', '');
    person.rollbackAttributes();

    equal(name.get('last'), 'Stark', "fragment has correct values");
    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("rolling back a fragment returns the fragment and the owner record to a clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Sansa",
        last: "Stark"
      }
    }
  });

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

    // Dirty the fragment
    name.set('last', 'Lannister');

    name.rollbackAttributes();

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});
<commit_msg>Added failing tests for owner rollback bug<commit_after>var env, store, Person, Name, Address, Hobby, people;

QUnit.module("integration - Dependent State", {
  setup: function() {
    Person = DS.Model.extend({
      title: DS.attr('string'),
      name: MF.fragment('name'),
      addresses: MF.fragmentArray('address'),
      titles: MF.array(),
      hobbies: MF.fragmentArray('hobby', { defaultValue: null })
    });

    Name = MF.Fragment.extend({
      first: DS.attr('string'),
      last: DS.attr('string'),
      person: MF.fragmentOwner()
    });

    Address = MF.Fragment.extend({
      street: DS.attr('string'),
      city: DS.attr('string'),
      region: DS.attr('string'),
      country: DS.attr('string')
    });

    Hobby = MF.Fragment.extend({
      name: DS.attr('string')
    });

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

    store = env.store;

    expectNoDeprecation();

    people = [
      {
        id: 1,
        name: {
          first: "Tyrion",
          last: "Lannister"
        },
        addresses: [
          {
            street: "1 Sky Cell",
            city: "Eyre",
            region: "Vale of Arryn",
            country: "Westeros"
          },
          {
            street: "1 Tower of the Hand",
            city: "King's Landing",
            region: "Crownlands",
            country: "Westeros"
          }
        ],
        titles: [
          "Hand of the King",
          "Master of Coin"
        ]
      }
    ];
  },

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

function pushPerson(id) {
  store.push({
    type: 'person',
    id: id,
    attributes: Ember.A(people).findBy('id', id)
  });
}

test("changing a fragment property dirties the fragment and owner record", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Jamie",
        last: "Lannister"
      }
    }
  });

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

    name.set('first', 'Cercei');

    ok(name.get('hasDirtyAttributes'), "fragment is dirty");
    ok(person.get('hasDirtyAttributes'), "owner record is dirty");
  });
});

test("setting a fragment property to an object literal dirties the fragment and owner record", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Visenya",
        last: "Targaryen"
      }
    }
  });

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

    person.set('name', {
      first: 'Rhaenys',
    });

    ok(name.get('hasDirtyAttributes'), "fragment is dirty");
    ok(person.get('hasDirtyAttributes'), "owner record is dirty");
  });
});

test("setting a fragment property with an object literal to the same value does not dirty the fragment or owner record", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Samwell",
        last: "Tarly"
      }
    }
  });

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

    person.set('name', {
      first: "Samwell",
      last: "Tarly"
    });

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("restoring a fragment property to its original state returns the fragment and owner record to a clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Hoster",
        last: "Tully"
      }
    }
  });

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

    name.set('first', 'Brynden');
    name.set('first', 'Hoster');

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("restoring a fragment property to its original state when the owner record was dirty returns the fragment to a clean state maintains the owner record's dirty state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Jorah",
        last: "Mormont"
      }
    }
  });

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

    // Dirty the owner record
    person.set('title', 'Lord Commander');

    name.set('first', 'Jeor');
    name.set('first', 'Jorah');

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(person.get('hasDirtyAttributes'), "owner record is still dirty");
  });
});

test("rolling back the owner record returns fragment and owner record to a clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Catelyn",
        last: "Stark"
      }
    }
  });

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

    name.set('last', 'Tully');

    person.rollbackAttributes();

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("a record can be rolled back multiple times", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Arya",
        last: "Stark"
      }
    }
  });

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

    name.set('last', '');
    person.rollbackAttributes();

    equal(name.get('last'), 'Stark', "fragment has correct values");
    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");

    name.set('last', '');
    person.rollbackAttributes();

    equal(name.get('last'), 'Stark', "fragment has correct values");
    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("rolling back a fragment returns the fragment and the owner record to a clean state", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Sansa",
        last: "Stark"
      }
    }
  });

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

    // Dirty the fragment
    name.set('last', 'Lannister');

    name.rollbackAttributes();

    ok(!name.get('hasDirtyAttributes'), "fragment is clean");
    ok(!person.get('hasDirtyAttributes'), "owner record is clean");
  });
});

test("changing a fragment property then rolling back the owner record preserves the fragment's owner", function() {
  store.push({
    type: 'person',
    id: 1,
    attributes: {
      name: {
        first: "Arya",
        last: "Stark"
      }
    }
  });

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

    person.set('name', null);

    person.rollbackAttributes();

    equal(name.get('person'), person, "fragment owner is preserved");
  });
});

{'fraction_non_alphanumeric': 0.16417158485677272, 'fraction_numerical': 0.002878940549877645, 'mean_word_length': 2.5527997954487343, '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': '2769437', 'n_tokens_mistral': 4426, 'n_tokens_neox': 4284, 'n_words': 1109}