<commit_before>// TODO:
// - add tests for getChanges with different since configurations

function runBackendTests(backend, QUnit) {

  // Document API
  // --------------------

  QUnit.test("Test if seed db has a valid document test-doc", function(assert) {
    var done = assert.async();
    backend.getDocument('test-doc', function(err, doc) {
      assert.ok(doc, 'valid doc snapshot expected');
      assert.ok(doc.data, 'should have document data attached');
      assert.equal(doc.version, 1, 'doc version should be 1');
      assert.equal(doc.userId, '1', 'userId should be "1"');
      done();
    });
  });

  QUnit.test("Test if seed db test-doc has the right version", function(assert) {
    var done = assert.async();
    backend.getVersion('test-doc', function(err, version) {
      assert.notOk(err, 'Should not error');
      assert.equal(version, 1, 'Document version should equals 1');
      done();
    });
  });

  QUnit.test("Test if seed db test-doc has valid changes", function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'test-doc',
      sinceVersion: 0
    };
    backend.getChanges(args, function(err, result) {
      assert.notOk(err, 'Should not error');
      assert.equal(result.changes.length, 1, 'Should be only one change');
      assert.equal(result.currentVersion, 1, 'Document version should equals 1');
      done();
    });
  });

  QUnit.test('Create a new document', function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'new-doc',
      schemaName: 'prose-article',
      userId: '2'
    };
    backend.createDocument(args, function(err, doc) {
      assert.ok(doc, 'valid doc snapshot expected');
      done();
    });
  });

  QUnit.test('Delete document', function(assert) {
    var done = assert.async();
    backend.deleteDocument('test-doc', function(err) {
      assert.ok(!err, 'Should delete a document');

      backend.getDocument('test-doc', function(err, doc) {
        assert.ok(err, 'Should print an error that document does not exist');
        assert.isNullOrUndefined(doc, 'doc should be undefined');

        // Test if there are still changes for that doc after deletion
        var args = {
          documentId: 'test-doc',
          sinceVersion: 0
        };
        backend.getChanges(args, function(err, result) {
          assert.ok(err, 'Should print an error that document does not exist');
          done();
        });
      });
    });
  });

  QUnit.test('Should not allow adding a change to non existing changeset', function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'some-non-existent-doc',
      change: {'some': 'change'},
      userId: '1'
    };
    backend.addChange(args, function(err) {
      assert.ok(err, 'Adding change to non existent doc should error');
      done();
    });
  });

  QUnit.test('Add a change to an existing doc', function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'test-doc',
      change: {'some': 'change'},
      userId: '1'
    };
    backend.addChange(args, function(err, version) {
      assert.notOk(err, 'Should not error');
      assert.equal(version, 2, 'Version should have been incremented by 1');

      var args = {
        documentId: 'test-doc',
        sinceVersion: 0
      };

      backend.getChanges(args, function(err, result) {
        assert.equal(result.changes.length, 2, 'There should be two changes in the db');
        assert.equal(result.currentVersion, 2, 'New version should be 2');
        done();
      });
    });
  });

  QUnit.test('List documents', function(assert) {
    var done = assert.async();
    backend.listDocuments({}, function(err, documents) {
      assert.notOk(err, 'Should not error');
      assert.equal(documents.length, 1, 'There should be one document returned');
      assert.equal(documents[0].userId, '1', 'First doc should have userId "1"');
      assert.equal(documents[0].documentId, 'test-doc', 'documentId should be "test-doc"');
      done();
    });
  });

  QUnit.test('List documents with matching filter', function(assert) {
    var done = assert.async();
    backend.listDocuments({userId: '1'}, function(err, documents) {
      assert.notOk(err, 'Should not error');
      assert.equal(documents.length, 1, 'There should be one document returned');
      assert.equal(documents[0].userId, '1', 'First doc should have userId "1"');
      assert.equal(documents[0].documentId, 'test-doc', 'documentId should be "test-doc"');
      done();
    });
  });

  QUnit.test('List documents with filter that does not match', function(assert) {
    var done = assert.async();
    backend.listDocuments({userId: 'userx'}, function(err, documents) {
      assert.notOk(err, 'Should not error');
      assert.equal(documents.length, 0, 'There should be no matches');
      done();
    });
  });
<commit_msg>Group backend tests.<commit_after>// TODO:
// - add tests for getChanges with different since configurations

function runBackendTests(backend, QUnit) {

  // Document API
  // --------------------

  QUnit.test("Test if seed db has a valid document test-doc", function(assert) {
    var done = assert.async();
    backend.getDocument('test-doc', function(err, doc) {
      assert.ok(doc, 'valid doc snapshot expected');
      assert.ok(doc.data, 'should have document data attached');
      assert.equal(doc.version, 1, 'doc version should be 1');
      assert.equal(doc.userId, '1', 'userId should be "1"');
      done();
    });
  });

  QUnit.test('Create a new document', function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'new-doc',
      schemaName: 'prose-article',
      userId: '2'
    };
    backend.createDocument(args, function(err, doc) {
      assert.ok(doc, 'valid doc snapshot expected');
      done();
    });
  });

  QUnit.test('Delete document', function(assert) {
    var done = assert.async();
    backend.deleteDocument('test-doc', function(err) {
      assert.ok(!err, 'Should delete a document');

      backend.getDocument('test-doc', function(err, doc) {
        assert.ok(err, 'Should print an error that document does not exist');
        assert.isNullOrUndefined(doc, 'doc should be undefined');

        // Test if there are still changes for that doc after deletion
        var args = {
          documentId: 'test-doc',
          sinceVersion: 0
        };
        backend.getChanges(args, function(err, result) {
          assert.ok(err, 'Should print an error that document does not exist');
          done();
        });
      });
    });
  });

  QUnit.test('List documents', function(assert) {
    var done = assert.async();
    backend.listDocuments({}, function(err, documents) {
      assert.notOk(err, 'Should not error');
      assert.equal(documents.length, 1, 'There should be one document returned');
      assert.equal(documents[0].userId, '1', 'First doc should have userId "1"');
      assert.equal(documents[0].documentId, 'test-doc', 'documentId should be "test-doc"');
      done();
    });
  });

  QUnit.test('List documents with matching filter', function(assert) {
    var done = assert.async();
    backend.listDocuments({userId: '1'}, function(err, documents) {
      assert.notOk(err, 'Should not error');
      assert.equal(documents.length, 1, 'There should be one document returned');
      assert.equal(documents[0].userId, '1', 'First doc should have userId "1"');
      assert.equal(documents[0].documentId, 'test-doc', 'documentId should be "test-doc"');
      done();
    });
  });

  QUnit.test('List documents with filter that does not match', function(assert) {
    var done = assert.async();
    backend.listDocuments({userId: 'userx'}, function(err, documents) {
      assert.notOk(err, 'Should not error');
      assert.equal(documents.length, 0, 'There should be no matches');
      done();
    });
  });

  // Changes Store API (Required by CollabEngine)
  // --------------------

  QUnit.test("Test if seed db test-doc has the right version", function(assert) {
    var done = assert.async();
    backend.getVersion('test-doc', function(err, version) {
      assert.notOk(err, 'Should not error');
      assert.equal(version, 1, 'Document version should equals 1');
      done();
    });
  });

  QUnit.test("Test if seed db test-doc has valid changes", function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'test-doc',
      sinceVersion: 0
    };
    backend.getChanges(args, function(err, result) {
      assert.notOk(err, 'Should not error');
      assert.equal(result.changes.length, 1, 'Should be only one change');
      assert.equal(result.currentVersion, 1, 'Document version should equals 1');
      done();
    });
  });

  QUnit.test('Should not allow adding a change to non existing changeset', function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'some-non-existent-doc',
      change: {'some': 'change'},
      userId: '1'
    };
    backend.addChange(args, function(err) {
      assert.ok(err, 'Adding change to non existent doc should error');
      done();
    });
  });

  QUnit.test('Add a change to an existing doc', function(assert) {
    var done = assert.async();
    var args = {
      documentId: 'test-doc',
      change: {'some': 'change'},
      userId: '1'
    };
    backend.addChange(args, function(err, version) {
      assert.notOk(err, 'Should not error');
      assert.equal(version, 2, 'Version should have been incremented by 1');
      var args = {
        documentId: 'test-doc',
        sinceVersion: 0
      };

      backend.getChanges(args, function(err, result) {
        assert.equal(result.changes.length, 2, 'There should be two changes in the db');
        assert.equal(result.currentVersion, 2, 'New version should be 2');
        done();
      });
    });
  });

{'fraction_non_alphanumeric': 0.15273425654503184, 'fraction_numerical': 0.006469220661073486, 'mean_word_length': 2.84830805134189, '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': '4715067', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2869, 'n_words': 834}