<commit_before>import Ember from 'ember';
import { module, test, createStore, registerModels } from '../helpers/setup';
import { Model, prefix, attr } from 'sofa';

const {
  RSVP: { resolve }
} = Ember;

let store;
let db;

let Duck = Model.extend({
  id: prefix('the-duck:'),
  name: attr('string'),
});

let Fish = Model.extend({
});

module('database-push', () => {
  registerModels({ Duck, Fish });
  store = createStore();
  db = store.get('db.main');
  db.set('modelNames', [ 'duck', 'fish' ]);
});

test('deserialize without expected model name', assert => {
  let model = db.push({ _id: 'the-duck:yellow', _rev: '1-asd', type: 'duck', name: '<NAME>' });
  assert.deepEqual(model.get('state'), {
    "error": null,
    "isDeleted": false,
    "isDirty": false,
    "isError": false,
    "isLoaded": true,
    "isLoading": false,
    "isNew": false,
    "isSaving": false
  });
  assert.deepEqual(model.getProperties('id', 'rev', 'type', 'name'), {
    "id": "yellow",
    "name": "<NAME>",
    "rev": "1-asd",
    "type": "duck"
  });
});

test('deserialize wrong type', assert => {
  db.push({ _id: 'the-duck:yellow', type: 'duck', name: 'Yellow Duck' });
  return resolve().then(() => {
    db.push({ _id: 'the-duck:yellow', type: 'fish', name: '<NAME>' });
  }).then(() => {
    assert.ok(false, 'should reject');
  }, err => {
    assert.deepEqual(err.toJSON(), {
      "error": "inivalid_document",
      "reason": "document 'the-duck:yellow' is expected to be 'duck' not 'fish'"
    });
  });
});

test('deserialize wrong expected modelClass', assert => {
  return resolve().then(() => {
    db.push({ _id: 'the-duck:yellow', type: 'duck', name: '<NAME>' }, { model: 'fish', optional: false });
  }).then(() => {
    assert.ok(false, 'should reject');
  }, err => {
    assert.deepEqual(err.toJSON(), {
      "error": "invalid_document",
      "reason": "document 'the-duck:yellow' is expected to be 'fish' not 'duck'"
    });
  });
});

test('push deleted with known doc', assert => {
  let model = db.push({ _id: 'the-duck:yellow', type: 'duck', name: '<NAME>' });
  let got = db.push({ _id: 'the-duck:yellow', _deleted: true });
  assert.ok(model === got);
  assert.ok(got);
  assert.deepEqual(model.get('state'), {
    "error": null,
    "isDeleted": true,
    "isDirty": false,
    "isError": false,
    "isLoaded": true,
    "isLoading": false,
    "isNew": false,
    "isSaving": false
  });
  assert.ok(db.get('_modelIdentity').deleted['the-duck:yellow']);
  assert.ok(!db.get('_modelIdentity').saved['the-duck:yellow']);
  assert.ok(db.get('_modelIdentity').all.length === 0);
});

test('push deleted with unknown doc', assert => {
  let model = db.push({ _id: 'the-duck:yellow', _deleted: true });
  assert.ok(!model);
});

test('ressurect deleted', assert => {
  db.push({ _id: 'the-duck:yellow', type: 'duck' });
  db.push({ _id: 'the-duck:yellow', _deleted: true });
  let model = db.push({ _id: 'the-duck:yellow', type: 'duck', name: '<NAME>' });
  assert.ok(model);
  assert.deepEqual(model.get('state'), {
    "error": null,
    "isDeleted": false,
    "isDirty": false,
    "isError": false,
    "isLoaded": true,
    "isLoading": false,
    "isNew": false,
    "isSaving": false
  });
  assert.ok(!db.get('_modelIdentity').deleted['the-duck:yellow']);
  assert.ok(db.get('_modelIdentity').saved['the-duck:yellow']);
  assert.ok(db.get('_modelIdentity').all[0].docId === 'the-duck:yellow');
});
<commit_msg>typo fix<commit_after>import Ember from 'ember';
import { module, test, createStore, registerModels } from '../helpers/setup';
import { Model, prefix, attr } from 'sofa';

const {
  RSVP: { resolve }
} = Ember;

let store;
let db;

let Duck = Model.extend({
  id: prefix('the-duck:'),
  name: attr('string'),
});

let Fish = Model.extend({
});

module('database-push', () => {
  registerModels({ Duck, Fish });
  store = createStore();
  db = store.get('db.main');
  db.set('modelNames', [ 'duck', 'fish' ]);
});

test('deserialize without expected model name', assert => {
  let model = db.push({ _id: 'the-duck:yellow', _rev: '1-asd', type: 'duck', name: 'Yellow Duck' });
  assert.deepEqual(model.get('state'), {
    "error": null,
    "isDeleted": false,
    "isDirty": false,
    "isError": false,
    "isLoaded": true,
    "isLoading": false,
    "isNew": false,
    "isSaving": false
  });
  assert.deepEqual(model.getProperties('id', 'rev', 'type', 'name'), {
    "id": "yellow",
    "name": "<NAME>",
    "rev": "1-asd",
    "type": "duck"
  });
});

test('deserialize wrong type', assert => {
  db.push({ _id: 'the-duck:yellow', type: 'duck', name: 'Yellow Duck' });
  return resolve().then(() => {
    db.push({ _id: 'the-duck:yellow', type: 'fish', name: 'Yellow Duck' });
  }).then(() => {
    assert.ok(false, 'should reject');
  }, err => {
    assert.deepEqual(err.toJSON(), {
      "error": "invalid_document",
      "reason": "document 'the-duck:yellow' is expected to be 'duck' not 'fish'"
    });
  });
});

test('deserialize wrong expected modelClass', assert => {
  return resolve().then(() => {
    db.push({ _id: 'the-duck:yellow', type: 'duck', name: '<NAME>' }, { model: 'fish', optional: false });
  }).then(() => {
    assert.ok(false, 'should reject');
  }, err => {
    assert.deepEqual(err.toJSON(), {
      "error": "invalid_document",
      "reason": "document 'the-duck:yellow' is expected to be 'fish' not 'duck'"
    });
  });
});

test('push deleted with known doc', assert => {
  let model = db.push({ _id: 'the-duck:yellow', type: 'duck', name: '<NAME>' });
  let got = db.push({ _id: 'the-duck:yellow', _deleted: true });
  assert.ok(model === got);
  assert.ok(got);
  assert.deepEqual(model.get('state'), {
    "error": null,
    "isDeleted": true,
    "isDirty": false,
    "isError": false,
    "isLoaded": true,
    "isLoading": false,
    "isNew": false,
    "isSaving": false
  });
  assert.ok(db.get('_modelIdentity').deleted['the-duck:yellow']);
  assert.ok(!db.get('_modelIdentity').saved['the-duck:yellow']);
  assert.ok(db.get('_modelIdentity').all.length === 0);
});

test('push deleted with unknown doc', assert => {
  let model = db.push({ _id: 'the-duck:yellow', _deleted: true });
  assert.ok(!model);
});

test('ressurect deleted', assert => {
  db.push({ _id: 'the-duck:yellow', type: 'duck' });
  db.push({ _id: 'the-duck:yellow', _deleted: true });
  let model = db.push({ _id: 'the-duck:yellow', type: 'duck', name: '<NAME>' });
  assert.ok(model);
  assert.deepEqual(model.get('state'), {
    "error": null,
    "isDeleted": false,
    "isDirty": false,
    "isError": false,
    "isLoaded": true,
    "isLoading": false,
    "isNew": false,
    "isSaving": false
  });
  assert.ok(!db.get('_modelIdentity').deleted['the-duck:yellow']);
  assert.ok(db.get('_modelIdentity').saved['the-duck:yellow']);
  assert.ok(db.get('_modelIdentity').all[0].docId === 'the-duck:yellow');
});

{'fraction_non_alphanumeric': 0.22703488372093023, 'fraction_numerical': 0.0011627906976744186, 'mean_word_length': 3.8870738636363638, 'pattern_counts': {'":': 64, '<': 13, '<?xml version=': 0, '>': 39, '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': '6591630', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2482, 'n_words': 545}