<commit_before>var attr = Ember.attr;

module("Ember.HasManyArray - non embedded objects saving");

test("new records should remain after parent is saved", function() {
  expect(3);
  var json = {
    id: 1,
    title: 'foo',
    comment_ids: []
  };

  var Comment = Ember.Model.extend({
    id: attr(),
    text: attr()
  });
  Comment.adapter = Ember.RESTAdapter.create();
  Comment.url = '/comments';

  var Article = Ember.Model.extend({
    id: attr(),
    title: attr(),
    comments: Ember.hasMany(Comment, { key: 'comment_ids' })
  });
  Article.adapter = Ember.RESTAdapter.create();
  Article.url = '/articles';
  Article.adapter._ajax = function() {
    return new Ember.RSVP.Promise(function(resolve) {
        resolve(json);
    });
  };
  
  var article = Article.create({
    title: 'bar'
  });

  var comment = Comment.create({
    text: 'comment text'
  });

  article.get('comments').addObject(comment);

  var promise = Ember.run(article, article.save);
  promise.then(function(record) {
    start();
    ok(record.get('comments.firstObject') === comment, "Comment is the same object");
    equal(record.get('comments.length'), 1, "Article should still have one comment after save");
    equal(record.get('comments.firstObject.text'), comment.get('text'), 'Comment is the same');
  });
  stop();
});<commit_msg>add test for saving has many relationship<commit_after>var attr = Ember.attr;

module("Ember.HasManyArray - non embedded objects saving");

test("new records should remain after parent is saved", function() {
  expect(3);
  var json = {
    id: 1,
    title: 'foo',
    comment_ids: []
  };

  var Comment = Ember.Model.extend({
    id: attr(),
    text: attr()
  });
  Comment.adapter = Ember.RESTAdapter.create();
  Comment.url = '/comments';

  var Article = Ember.Model.extend({
    id: attr(),
    title: attr(),
    comments: Ember.hasMany(Comment, { key: 'comment_ids' })
  });
  Article.adapter = Ember.RESTAdapter.create();
  Article.url = '/articles';
  Article.adapter._ajax = function() {
    return new Ember.RSVP.Promise(function(resolve) {
        resolve(json);
    });
  };
  
  var article = Article.create({
    title: 'bar'
  });

  var comment = Comment.create({
    text: 'comment text'
  });

  article.get('comments').addObject(comment);

  var promise = Ember.run(article, article.save);
  promise.then(function(record) {
    start();
    ok(record.get('comments.firstObject') === comment, "Comment is the same object");
    equal(record.get('comments.length'), 1, "Article should still have one comment after save");
    equal(record.get('comments.firstObject.text'), comment.get('text'), 'Comment is the same');
  });
  stop();
});

test("saving child objects", function() {
  expect(1);

  var commentJSON = {
    id: 2,
    title: 'bar',
  };

  var Comment = Ember.Model.extend({
    id: attr(),
    text: attr()
  });
  Comment.adapter = Ember.RESTAdapter.create();
  Comment.url = '/comments';
  Comment.adapter._ajax = function() {
    return new Ember.RSVP.Promise(function(resolve) {
        resolve(commentJSON);
    });
  };

  var Article = Ember.Model.extend({
    id: attr(),
    title: attr(),
    comments: Ember.hasMany(Comment, { key: 'comment_ids' })
  });
  Article.adapter = Ember.RESTAdapter.create();
  Article.url = '/articles';
  
  var article = Article.create();
  var comment = Comment.create();

  var comments = article.get("comments");
  comments.addObject(comment);

  var promise = Ember.run(comments, comments.save);
  promise.then(function(records) {
    start();
    var comment = records.get("firstObject");
    equal(comment.get("id"), 2, "Data from the response is loaded");
  });
  stop();
});
{'fraction_non_alphanumeric': 0.16725399837354296, 'fraction_numerical': 0.002439685551640011, 'mean_word_length': 3.4781553398058254, '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': '1116519', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1147, 'n_words': 275}