<commit_before>var env, User, Contact, Email, Phone, Message, Post, Comment;
var Book, Chapter, Page;
var get = Ember.get;
var resolve = Ember.RSVP.resolve;
var run = Ember.run;

var attr = DS.attr;
var hasMany = DS.hasMany;
var belongsTo = DS.belongsTo;

function stringify(string) {
  return function() { return string; };
}

module("integration/relationships/has_many - Has-Many Relationships", {
  setup: function() {
    User = DS.Model.extend({
      name: attr('string'),
      messages: hasMany('message', { polymorphic: true }),
      contacts: hasMany()
    });

    Contact = DS.Model.extend({
      user: belongsTo('user')
    });

    Email = Contact.extend({
      email: attr('string')
    });

    Phone = Contact.extend({
      number: attr('string')
    });

    Message = DS.Model.extend({
      user: belongsTo('user'),
      created_at: attr('date')
    });
    Message.toString = stringify('Message');

    Post = Message.extend({
      title: attr('string'),
      comments: hasMany('comment')
    });
    Post.toString = stringify('Post');

    Comment = Message.extend({
      body: DS.attr('string'),
      message: DS.belongsTo('post', { polymorphic: true })
    });
    Comment.toString = stringify('Comment');

    Book = DS.Model.extend({
      title: attr(),
      chapters: hasMany('chapter', { async: true })
    });
    Book.toString = stringify('Book');

    Chapter = DS.Model.extend({
      title: attr(),
      pages: hasMany('page')
    });
    Chapter.toString = stringify('Chapter');

    Page = DS.Model.extend({
      number: attr('number'),
      chapter: belongsTo('chapter')
    });
    Page.toString = stringify('Page');

    env = setupStore({
      user: User,
      contact: Contact,
      email: Email,
      phone: Phone,
      post: Post,
      comment: Comment,
      message: Message,
      book: Book,
      chapter: Chapter,
      page: Page
    });
  },

  teardown: function() {
    run(env.container, 'destroy');
  }
});

test("When a hasMany relationship is accessed, the adapter's findMany method should not be called if all the records in the relationship are already loaded", function() {
  expect(0);

  env.adapter.findMany = function() {
    ok(false, "The adapter's find method should not be called");
  };

  run(function() {
    env.store.push('post', { id: 1, comments: [1] });
    env.store.push('comment', { id: 1 });
    env.store.find('post', 1).then(function(post) {
      return post.get('comments');
    });<commit_msg>Add adapter.hasMany unique IDs test<commit_after>var env, User, Contact, Email, Phone, Message, Post, Comment;
var Book, Chapter, Page;
var get = Ember.get;
var resolve = Ember.RSVP.resolve;
var run = Ember.run;

var attr = DS.attr;
var hasMany = DS.hasMany;
var belongsTo = DS.belongsTo;

function stringify(string) {
  return function() { return string; };
}

module("integration/relationships/has_many - Has-Many Relationships", {
  setup: function() {
    User = DS.Model.extend({
      name: attr('string'),
      messages: hasMany('message', { polymorphic: true }),
      contacts: hasMany()
    });

    Contact = DS.Model.extend({
      user: belongsTo('user')
    });

    Email = Contact.extend({
      email: attr('string')
    });

    Phone = Contact.extend({
      number: attr('string')
    });

    Message = DS.Model.extend({
      user: belongsTo('user'),
      created_at: attr('date')
    });
    Message.toString = stringify('Message');

    Post = Message.extend({
      title: attr('string'),
      comments: hasMany('comment')
    });
    Post.toString = stringify('Post');

    Comment = Message.extend({
      body: DS.attr('string'),
      message: DS.belongsTo('post', { polymorphic: true })
    });
    Comment.toString = stringify('Comment');

    Book = DS.Model.extend({
      title: attr(),
      chapters: hasMany('chapter', { async: true })
    });
    Book.toString = stringify('Book');

    Chapter = DS.Model.extend({
      title: attr(),
      pages: hasMany('page')
    });
    Chapter.toString = stringify('Chapter');

    Page = DS.Model.extend({
      number: attr('number'),
      chapter: belongsTo('chapter')
    });
    Page.toString = stringify('Page');

    env = setupStore({
      user: User,
      contact: Contact,
      email: Email,
      phone: Phone,
      post: Post,
      comment: Comment,
      message: Message,
      book: Book,
      chapter: Chapter,
      page: Page
    });
  },

  teardown: function() {
    run(env.container, 'destroy');
  }
});

test("When a hasMany relationship is accessed, the adapter's findMany method should not be called if all the records in the relationship are already loaded", function() {
  expect(0);

  env.adapter.findMany = function() {
    ok(false, "The adapter's find method should not be called");
  };

  run(function() {
    env.store.push('post', { id: 1, comments: [1] });
    env.store.push('comment', { id: 1 });
    env.store.find('post', 1).then(function(post) {
      return post.get('comments');
    });
  });
});

test("adapter.findMany only gets unique IDs even if duplicate IDs are present in the hasMany relationship", function() {
  expect(2);

  env.adapter.findMany = function(store, type, ids, records) {
    equal(type, Chapter, 'type passed to adapter.findMany is correct');
    deepEqual(ids, ['2', '3'], 'ids passed to adapter.findMany are unique');

    return Ember.RSVP.resolve([
      { id: 2, title: 'Chapter One' },
      { id: 3, title: 'Chapter Two' }
    ]);
  };

  run(function() {
    env.store.push('book', { id: 1, chapters: [2, 3, 3] });
    env.store.find('book', 1).then(function(book) {
      return book.get('chapters');
    });
{'fraction_non_alphanumeric': 0.1614950634696756, 'fraction_numerical': 0.003526093088857546, 'mean_word_length': 2.9232365145228214, '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': '1089198', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1709, 'n_words': 451}