<commit_before>util = require 'util'
express = require 'express'
_ = require 'underscore'
Backbone = require 'backbone'
RestController = require 'backbone-rest'

createMockModel = (mock_model_types, model_type) ->
  return mock_model_type if mock_model_type = mock_model_types[model_type.model_name]

  compiled_schema = model_type.schema()
  schema = _.clone(compiled_schema.raw)
  for key, relation of compiled_schema.relations
    do (key, relation) ->
      schema[key] = ->
        field = compiled_schema.raw[key]
        field = field() if _.isFunction(field)
        field = _.clone(field)
        relation = model_type.relation(key)
        field[1] = createMockModel(mock_model_types, relation.reverse_model_type)

        if relation.join_table
          mock_join_table = createMockModel(mock_model_types, relation.join_table)
          mock_model_types[mock_join_table.model_name] = mock_join_table
          join_table_info = {join_table: createMockModel(mock_model_types, relation.join_table)}
          if field.length is 2 then field.push(join_table_info) else _.extend(field[2], join_table_info)
        return field

  class MockModel extends Backbone.Model
    _orm_never_cache: true # so the correct models are found in the cache
    @model_name: model_type.model_name
    @schema: schema
    sync: require('backbone-orm/memory_sync')(MockModel)

  mock_model_types[model_type.model_name] = MockModel

  # configure the mock request
  app = express(); app.use(express.bodyParser())
  url = _.result(model_type.prototype, 'url')
  controller = new RestController(app, {model_type: MockModel, route: url}) # implicit knowledge of backbone-rest tests
  sync = model_type::sync('sync')
  sync = sync.wrapped_sync_fn('sync') if sync.wrapped_sync_fn # cache
  sync.request = require('supertest')(app)

  return MockModel


module.exports = (model_types, callback) ->
  mock_model_types = {}

  # initialize all to ensure the reverse relationships are configured
  model_type.schema() for model_type in model_types

  # create mock models
  createMockModel(mock_model_types, model_type) for model_type in model_types

  # ensure all models are initialized
  mock_model_type.schema() for key, mock_model_type of mock_model_types
  callback()<commit_msg>Bug fix for rest<commit_after>util = require 'util'
express = require 'express'
_ = require 'underscore'
Backbone = require 'backbone'
RestController = require 'backbone-rest'

createMockModel = (mock_model_types, model_type) ->
  return mock_model_type if mock_model_type = mock_model_types[model_type.model_name]

  compiled_schema = model_type.schema()
  schema = _.clone(compiled_schema.raw)
  for key, relation of compiled_schema.relations
    do (key, relation) ->
      schema[key] = ->
        field = compiled_schema.raw[key]
        field = field() if _.isFunction(field)
        field = _.clone(field)
        relation = model_type.relation(key)
        field[1] = createMockModel(mock_model_types, relation.reverse_model_type)

        if relation.join_table
          mock_join_table = createMockModel(mock_model_types, relation.join_table)
          mock_model_types[mock_join_table.model_name] = mock_join_table
          join_table_info = {join_table: createMockModel(mock_model_types, relation.join_table)}
          if field.length is 2 then field.push(join_table_info) else _.extend(field[2], join_table_info)
        return field

  url = _.result(model_type.prototype, 'url')
  class MockModel extends Backbone.Model
    _orm_never_cache: true # so the correct models are found in the cache
    @model_name: model_type.model_name
    url: url
    @schema: schema
    sync: require('backbone-orm/memory_sync')(MockModel)

  mock_model_types[model_type.model_name] = MockModel

  # configure the mock request
  app = express(); app.use(express.bodyParser())
  controller = new RestController(app, {model_type: MockModel, route: url}) # implicit knowledge of backbone-rest tests
  sync = model_type::sync('sync')
  sync = sync.wrapped_sync_fn('sync') if sync.wrapped_sync_fn # cache
  sync.request = require('supertest')(app)

  return MockModel


module.exports = (model_types, callback) ->
  mock_model_types = {}

  # initialize all to ensure the reverse relationships are configured
  model_type.schema() for model_type in model_types

  # create mock models
  createMockModel(mock_model_types, model_type) for model_type in model_types

  # ensure all models are initialized
  mock_model_type.schema() for key, mock_model_type of mock_model_types
  callback()
{'fraction_non_alphanumeric': 0.08905013192612138, 'fraction_numerical': 0.0013192612137203166, 'mean_word_length': 4.351764705882353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1179541', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1354, 'n_words': 334}