<commit_before>expect = require('chai').expect

{Presenter} = require('../../src/yayson.coffee')()

describe 'Presenter', ->
  it 'handles null', ->
    json = Presenter.toJSON(null)
    expect(json).to.deep.equal
      data: null

  it 'create json structure of an object', ->
    obj = {id: 5, foo: 'bar'}
    json = Presenter.toJSON(obj)
    expect(json).to.deep.equal
      data:
        type: 'objects'
        id: '5'
        attributes:
          foo: 'bar'

  it 'create json structure of an array of objects', ->
    obj = [{id: 1, foo: 'bar'}, {id: 2, foo: 'baz'}]
    json = Presenter.toJSON(obj)
    expect(json).to.deep.equal
      data: [{
        type: 'objects'
        id: '1'
        attributes:
          foo: 'bar'
      },{
        type: 'objects'
        id: '2'
        attributes:
          foo: 'baz'
      }]

  it 'should not dup object', ->
    obj = [{id: 1}, {id: 1}]
    json = Presenter.toJSON(obj)
    expect(json).to.deep.equal
      data: [
        type: 'objects'
        id: '1'
        attributes: {}
      ]

  it 'should serialize relations', ->
    class MotorPresenter extends Presenter
      type: 'motors'
      relationships: ->
        car: CarPresenter

    class CarPresenter extends Presenter
      type: 'cars'
      relationships: ->
        motor: MotorPresenter

    motor =
      id: 2
      car: null

    car =
      id: 1
      motor: motor

    motor.car = car

    json = CarPresenter.toJSON(car)
    expect(json).to.deep.equal
      data:
        type: 'cars'
        id: '1'
        attributes: {}
        relationships:
          motor:
            data:
              type: 'motors'
              id: '2'
      included: [
        type: 'motors'
        id: '2'
        attributes: {}
        relationships:
          car:
            data:
              type: 'cars'
              id: '1'
      ]

  it 'should serialize relations array', ->
    class WheelPresenter extends Presenter
      type: 'wheels'
      relationships: ->
        bike: BikePresenter

    class BikePresenter extends Presenter
      type: 'bikes'
      relationships: ->
        wheel: WheelPresenter

    wheel =[
      {
        id: 2
        bike: null
      },
      {
        id: 3
        bike: null
      }
    ]

    bike = 
      id: 1
      wheel: wheel

    for w in wheel
      w.bike = bike

    json = BikePresenter.toJSON(bike)
    expect(json).to.deep.equal
      data:
        type: 'bikes'
        id: '1'
        attributes: {}
        relationships:
          wheel:
            data:[
              {
                type: 'wheels'
                id: '2'
              },
              {
                type: 'wheels'
                id: '3'
              }
            ]
      included: [
        {
          type: 'wheels'
          id: '2'
          attributes: {}
          relationships:
            bike:
              data:
                type: 'bikes'
                id: '1'
        },
        {
          type: 'wheels'
          id: '3'
          attributes: {}
          relationships:
            bike:
              data:
                type: 'bikes'
                id: '1'
        }
      ]

  it 'should include self link', ->
    class CarPresenter extends Presenter
      type: 'cars'
      selfLinks: (instance) ->
        '/cars/' + @id(instance)

    json = CarPresenter.render(id: 3)
    expect(json.data.links.self).to.eq '/cars/3'

  it 'should include self and related link', ->
    class CarPresenter extends Presenter
      type: 'cars'
      selfLinks: (instance) ->
        self: '/cars/linkage/' + @id(instance)
        related: '/cars/' + @id(instance)

    json = CarPresenter.render(id: 3)
    expect(json.data.links.self).to.eq '/cars/linkage/3'
    expect(json.data.links.related).to.eq '/cars/3'

  it 'should handle links in relationships', ->
    class CarPresenter extends Presenter
      type: 'cars'

      relationships: ->
        car: CarPresenter

      selfLinks: (instance) ->
        '/cars/' + @id(instance)

      links: (instance) ->
        car:
          self: @selfLinks(instance) + '/linkage/car'
          related: @selfLinks(instance) + '/car'

    json = CarPresenter.render(id: 3, car: id: 5)
    expect(json.data.links.self).to.eq '/cars/3'
    expect(json.data.relationships.car.links.self).to.eq '/cars/3/linkage/car'
    expect(json.data.relationships.car.links.related).to.eq '/cars/3/car'

  it 'should serialize in pure JS', ->
    `
    var EventPresenter = function () { Presenter.call(this); }
    EventPresenter.prototype = new Presenter()
    EventPresenter.prototype.attributes = function() {
      return {hej: 'test'}
    }
    var presenter = new EventPresenter()
    var json = presenter.toJSON({id: 1})
    `
    expect(json.data.attributes.hej).to.eq 'test'
<commit_msg>add test for links in relationships when no data is provided<commit_after>expect = require('chai').expect

{Presenter} = require('../../src/yayson.coffee')()

describe 'Presenter', ->
  it 'handles null', ->
    json = Presenter.toJSON(null)
    expect(json).to.deep.equal
      data: null

  it 'create json structure of an object', ->
    obj = {id: 5, foo: 'bar'}
    json = Presenter.toJSON(obj)
    expect(json).to.deep.equal
      data:
        type: 'objects'
        id: '5'
        attributes:
          foo: 'bar'

  it 'create json structure of an array of objects', ->
    obj = [{id: 1, foo: 'bar'}, {id: 2, foo: 'baz'}]
    json = Presenter.toJSON(obj)
    expect(json).to.deep.equal
      data: [{
        type: 'objects'
        id: '1'
        attributes:
          foo: 'bar'
      },{
        type: 'objects'
        id: '2'
        attributes:
          foo: 'baz'
      }]

  it 'should not dup object', ->
    obj = [{id: 1}, {id: 1}]
    json = Presenter.toJSON(obj)
    expect(json).to.deep.equal
      data: [
        type: 'objects'
        id: '1'
        attributes: {}
      ]

  it 'should serialize relations', ->
    class MotorPresenter extends Presenter
      type: 'motors'
      relationships: ->
        car: CarPresenter

    class CarPresenter extends Presenter
      type: 'cars'
      relationships: ->
        motor: MotorPresenter

    motor =
      id: 2
      car: null

    car =
      id: 1
      motor: motor

    motor.car = car

    json = CarPresenter.toJSON(car)
    expect(json).to.deep.equal
      data:
        type: 'cars'
        id: '1'
        attributes: {}
        relationships:
          motor:
            data:
              type: 'motors'
              id: '2'
      included: [
        type: 'motors'
        id: '2'
        attributes: {}
        relationships:
          car:
            data:
              type: 'cars'
              id: '1'
      ]

  it 'should serialize relations array', ->
    class WheelPresenter extends Presenter
      type: 'wheels'
      relationships: ->
        bike: BikePresenter

    class BikePresenter extends Presenter
      type: 'bikes'
      relationships: ->
        wheel: WheelPresenter

    wheel =[
      {
        id: 2
        bike: null
      },
      {
        id: 3
        bike: null
      }
    ]

    bike = 
      id: 1
      wheel: wheel

    for w in wheel
      w.bike = bike

    json = BikePresenter.toJSON(bike)
    expect(json).to.deep.equal
      data:
        type: 'bikes'
        id: '1'
        attributes: {}
        relationships:
          wheel:
            data:[
              {
                type: 'wheels'
                id: '2'
              },
              {
                type: 'wheels'
                id: '3'
              }
            ]
      included: [
        {
          type: 'wheels'
          id: '2'
          attributes: {}
          relationships:
            bike:
              data:
                type: 'bikes'
                id: '1'
        },
        {
          type: 'wheels'
          id: '3'
          attributes: {}
          relationships:
            bike:
              data:
                type: 'bikes'
                id: '1'
        }
      ]

  it 'should include self link', ->
    class CarPresenter extends Presenter
      type: 'cars'
      selfLinks: (instance) ->
        '/cars/' + @id(instance)

    json = CarPresenter.render(id: 3)
    expect(json.data.links.self).to.eq '/cars/3'

  it 'should include self and related link', ->
    class CarPresenter extends Presenter
      type: 'cars'
      selfLinks: (instance) ->
        self: '/cars/linkage/' + @id(instance)
        related: '/cars/' + @id(instance)

    json = CarPresenter.render(id: 3)
    expect(json.data.links.self).to.eq '/cars/linkage/3'
    expect(json.data.links.related).to.eq '/cars/3'

  it 'should handle links in relationships', ->
    class CarPresenter extends Presenter
      type: 'cars'

      relationships: ->
        car: CarPresenter

      selfLinks: (instance) ->
        '/cars/' + @id(instance)

      links: (instance) ->
        car:
          self: @selfLinks(instance) + '/linkage/car'
          related: @selfLinks(instance) + '/car'

    json = CarPresenter.render(id: 3, car: id: 5)
    expect(json.data.links.self).to.eq '/cars/3'
    expect(json.data.relationships.car.links.self).to.eq '/cars/3/linkage/car'
    expect(json.data.relationships.car.links.related).to.eq '/cars/3/car'

  it 'should handle links in relationships without data', ->
    class CarPresenter extends Presenter
      type: 'cars'

      relationships: ->
        car: CarPresenter

      selfLinks: (instance) ->
        '/cars/' + @id(instance)

      links: (instance) ->
        car:
          self: @selfLinks(instance) + '/linkage/car'
          related: @selfLinks(instance) + '/car'

    json = CarPresenter.render(id: 3)
    expect(json.data.links.self).to.eq '/cars/3'
    expect(json.data.relationships.car.links.self).to.eq '/cars/3/linkage/car'
    expect(json.data.relationships.car.links.related).to.eq '/cars/3/car'
    expect(json.data.relationships.car.data).to.eq undefined

  it 'should serialize in pure JS', ->
    `
    var EventPresenter = function () { Presenter.call(this); }
    EventPresenter.prototype = new Presenter()
    EventPresenter.prototype.attributes = function() {
      return {hej: 'test'}
    }
    var presenter = new EventPresenter()
    var json = presenter.toJSON({id: 1})
    `
    expect(json.data.attributes.hej).to.eq 'test'

{'fraction_non_alphanumeric': 0.12873008208594883, 'fraction_numerical': 0.007339449541284404, 'mean_word_length': 1.594838386369331, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 47, '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': '1979168', 'n_tokens_mistral': 3327, 'n_tokens_neox': 3191, 'n_words': 769}