<commit_before>
const hooks = require('hooks');
const {MongoClient} = require('mongodb');


let db;


// Fixtures
const gist = {
  '_id': '42',
  'created_at': new Date().toISOString(),
  'description': 'Gist description...',
  'content': 'String content...',
}

const star = {
  'gist_id': '42',
  'starred': true,
}


// Setup database connection before Dredd starts testing
hooks.beforeAll((transactions, done) => {
  MongoClient.connect('mongodb://localhost/dredd-example', function(err, conn) {
    db = conn;
    done(err);
  });
});

// Close database connection after Dredd finishes testing
hooks.afterAll((transactions, done) => {
  db.close();
  done();
});


// Before each test let's fix the media type - unfortunately current support
// of Swagger in Dredd works only for application/json as of now
hooks.beforeEach((transaction, done) => {
  let contentType;

  if (transaction.expected.statusCode == '204') {
    transaction.request.headers['Accept'] = 'text/plain';
    delete transaction.expected.headers['Content-Type'];
  } else {
    transaction.request.headers['Accept'] = 'application/hal+json';
    transaction.expected.headers['Content-Type'] = 'application/hal+json';
  }
  done();
});

// After each test clear contents of the database (we want isolated tests)
hooks.afterEach((transaction, done) => {
  db.collection('gists').remove({}, (err) => {
    if (err) { return done(err) }

    db.collection('stars').remove({}, (err) => {
      done(err);
    });
  });
});


// To test work with Gists and Stars in isolation, we need to add some prior
// to certain HTTP transactions Dredd is about to make
hooks.before('/gists/{id} > GET', (transaction, done) => {
  db.collection('gists').insert(gist, (err) => {
    done(err);
  });
});

hooks.before('/gists/{id} > PATCH', (transaction, done) => {
  db.collection('gists').insert(gist, (err) => {
    done(err);
  });
});

hooks.before('/gists > GET', (transaction, done) => {
  db.collection('gists').insert(gist, (err) => {
    done(err);
  });
});

hooks.before('/gists/{id}/star > GET', (transaction, done) => {<commit_msg>Update transaction names in Swagger hooks<commit_after>
const hooks = require('hooks');
const {MongoClient} = require('mongodb');


let db;


// Fixtures
const gist = {
  '_id': '42',
  'created_at': new Date().toISOString(),
  'description': 'Gist description...',
  'content': 'String content...',
}

const star = {
  'gist_id': '42',
  'starred': true,
}


// Setup database connection before Dredd starts testing
hooks.beforeAll((transactions, done) => {
  MongoClient.connect('mongodb://localhost/dredd-example', function(err, conn) {
    db = conn;
    done(err);
  });
});

// Close database connection after Dredd finishes testing
hooks.afterAll((transactions, done) => {
  db.close();
  done();
});


// Before each test let's fix the media type - unfortunately current support
// of Swagger in Dredd works only for application/json as of now
hooks.beforeEach((transaction, done) => {
  let contentType;

  if (transaction.expected.statusCode == '204') {
    transaction.request.headers['Accept'] = 'text/plain';
    delete transaction.expected.headers['Content-Type'];
  } else {
    transaction.request.headers['Accept'] = 'application/hal+json';
    transaction.expected.headers['Content-Type'] = 'application/hal+json';
  }
  done();
});

// After each test clear contents of the database (we want isolated tests)
hooks.afterEach((transaction, done) => {
  db.collection('gists').remove({}, (err) => {
    if (err) { return done(err) }

    db.collection('stars').remove({}, (err) => {
      done(err);
    });
  });
});


// To test work with Gists and Stars in isolation, we need to add some prior
// to certain HTTP transactions Dredd is about to make
hooks.before('/gists/{id} > GET > 200 > application/json', (transaction, done) => {
  db.collection('gists').insert(gist, (err) => {
    done(err);
  });
});

hooks.before('/gists/{id} > PATCH > 200 > application/json', (transaction, done) => {
  db.collection('gists').insert(gist, (err) => {
    done(err);
  });
});

hooks.before('/gists > GET > 200 > application/json', (transaction, done) => {
  db.collection('gists').insert(gist, (err) => {
    done(err);
  });
});

hooks.before('/gists/{id}/star > GET > 200 > application/json', (transaction, done) => {
{'fraction_non_alphanumeric': 0.18513388734995384, 'fraction_numerical': 0.006001846722068329, 'mean_word_length': 3.9238636363636363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '5064613', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1378, 'n_words': 345}