<commit_before>module("Model");

test("attr, attributes, changes, reset, save, destroy", function() {
  var Post = Model("post");

  same(new Post().attributes, {});

  var post = new Post({ title: "Foo", body: "..." });

  same(post.attributes, { title: "Foo", body: "..." });
  same(post.changes, {});
  same(post.attr(), { title: "Foo", body: "..." });

  post.attr("title", null);
  equals(post.attributes.title, "Foo", "attributes should be unchanged");
  equals(post.changes.title, null);
  equals(post.attr("title"), null, "null value should be read back as null");

  post.attr("title", "Foo");
  equals(post.attributes.title, "Foo");
  ok(!("title" in post.changes), "unchanged value shouldn't appear in changes");
  equals(post.attr("title"), "Foo");

  post.reset();
  same(post.attributes, { title: "Foo", body: "..." });
  same(post.changes, {});
  same(post.attr(), { title: "Foo", body: "..." });

  // Set attribute using attr.
  equals(post.attr("title", "Bar"), post, "returns self");

  // Check attributes and changes.
  equals(post.attr("title"), "Bar");
  same(post.attributes, { title: "Foo", body: "..." }, "attributes should be unchanged");
  same(post.changes, { title: "Bar" });
  same(post.attr(), { title: "Bar", body: "..." });

  equals(post.reset(), post, "returns self");

  equals(post.attr("title"), "Foo");
  same(post.changes, {});

  // Set again
  post.attr("title", "Bar");

  same(post.attributes, { title: "Foo", body: "..." });
  same(post.changes, { title: "Bar" });

  same(post.save(), post);

  same(post.attributes, { title: "Bar", body: "..." });
  same(post.changes, {});

  equals(post.attr({ title: "Foo", bar: "Bar" }), post, "returns self");

  same(post.attributes, { title: "Bar", body: "..." });
  same(post.changes, { title: "Foo", bar: "Bar" });

  same(post.save(function(success) {
    ok(success);
  }), post);

  same(post.attributes, { bar: "Bar", body: "...", title: "Foo" });
  same(post.changes, {});

  post.destroy(function(success) {
    ok(success);
  });
});

test("custom methods", function() {
  var Post = Model("post", {
    foo: function() {
      return "foo";
    }
  });

  var post = new Post();

  equals(post.foo(), "foo");
});

test("valid, validate, errors", function() {
  var Post = Model("post", {
    validate: function() {
      if (_.isEmpty(this.attr("body")))
        this.errors.add("body", "can't be blank");<commit_msg>Fix validation - fails in Chrome.<commit_after>module("Model");

test("attr, attributes, changes, reset, save, destroy", function() {
  var Post = Model("post");

  same(new Post().attributes, {});

  var post = new Post({ title: "Foo", body: "..." });

  same(post.attributes, { title: "Foo", body: "..." });
  same(post.changes, {});
  same(post.attr(), { title: "Foo", body: "..." });

  post.attr("title", null);
  equals(post.attributes.title, "Foo", "attributes should be unchanged");
  equals(post.changes.title, null);
  equals(post.attr("title"), null, "null value should be read back as null");

  post.attr("title", "Foo");
  equals(post.attributes.title, "Foo");
  ok(!("title" in post.changes), "unchanged value shouldn't appear in changes");
  equals(post.attr("title"), "Foo");

  post.reset();
  same(post.attributes, { title: "Foo", body: "..." });
  same(post.changes, {});
  same(post.attr(), { title: "Foo", body: "..." });

  // Set attribute using attr.
  equals(post.attr("title", "Bar"), post, "returns self");

  // Check attributes and changes.
  equals(post.attr("title"), "Bar");
  same(post.attributes, { title: "Foo", body: "..." }, "attributes should be unchanged");
  same(post.changes, { title: "Bar" });
  same(post.attr(), { title: "Bar", body: "..." });

  equals(post.reset(), post, "returns self");

  equals(post.attr("title"), "Foo");
  same(post.changes, {});

  // Set again
  post.attr("title", "Bar");

  same(post.attributes, { title: "Foo", body: "..." });
  same(post.changes, { title: "Bar" });

  same(post.save(), post);

  same(post.attributes, { title: "Bar", body: "..." });
  same(post.changes, {});

  equals(post.attr({ title: "Foo", bar: "Bar" }), post, "returns self");

  same(post.attributes, { title: "Bar", body: "..." });
  same(post.changes, { title: "Foo", bar: "Bar" });

  same(post.save(function(success) {
    ok(success);
  }), post);

  same(post.attributes, { bar: "Bar", body: "...", title: "Foo" });
  same(post.changes, {});

  post.destroy(function(success) {
    ok(success);
  });
});

test("custom methods", function() {
  var Post = Model("post", {
    foo: function() {
      return "foo";
    }
  });

  var post = new Post();

  equals(post.foo(), "foo");
});

test("valid, validate, errors", function() {
  var Post = Model("post", {
    validate: function() {
      if (!/\S/.test(this.attr("body") || ""))
        this.errors.add("body", "can't be blank");
{'fraction_non_alphanumeric': 0.24171299155857526, 'fraction_numerical': 0.0, 'mean_word_length': 3.7580803134182172, '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': '3596879', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1593, 'n_words': 381}