<commit_before>describe 'ActiveResource', ->
  beforeEach ->
    jasmine.Ajax.install()

    window.onSuccess = jasmine.createSpy('onSuccess')
    window.onFailure = jasmine.createSpy('onFailure')
    window.onCompletion = jasmine.createSpy('onCompletion')

  afterEach ->
    jasmine.Ajax.uninstall()

  describe 'Interfaces::JsonApi', ->
    beforeEach ->
      @lib = ActiveResource.createResourceLibrary(
        'https://www.example.com/api/v1/'
        interface: ActiveResource.Interfaces.JsonApi
      )
      class @lib::Product extends @lib.Base
        @className = 'Product'
        @queryName = 'products'

        @hasMany 'orders'

      class @lib::Order extends @lib.Base
        @className = 'Order'
        @queryName = 'orders'

        @belongsTo 'product'

      @interface = @lib.interface

    describe '#get()', ->
      describe 'getting resources', ->
        beforeEach ->
          @interface
          .get(@lib::Product.links()['related'])
          .done(window.onSuccess)
          .fail(window.onFailure)

        describe 'on success', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.all.success)
            expect(window.onSuccess).toHaveBeenCalled()
            @collection = window.onSuccess.calls.mostRecent().args[0]

          it 'returns a collection', ->
            expect(@collection.isA?(ActiveResource::Collection)).toBeTruthy()

          it 'returns a collection of resources of the queried type', ->
            @collection.each (resource) =>
              expect(resource.isA?(@lib::Product)).toBeTruthy()

          it 'returns a collection of resources with links', ->
            @collection.each (resource) =>
              expect(resource.links()['self']).toBeDefined()

      describe 'getting a resource', ->
        beforeEach ->
          @interface
          .get(@lib::Product.links()['related'] + '1')
          .done(window.onSuccess)
          .fail(window.onFailure)

        describe 'on success', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.find.success)
            expect(window.onSuccess).toHaveBeenCalled()
            @resource = window.onSuccess.calls.mostRecent().args[0]

          it 'returns a resource of the queried type', ->
            expect(@resource.isA?(@lib::Product)).toBeTruthy()

          it 'returns a resource with a link', ->
            expect(@resource.links()['self']).toBeDefined()

        describe 'on failure', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.find.failure)
            expect(window.onFailure).toHaveBeenCalled()

          it 'returns a collection of errors', ->
            expect(window.onFailure.calls.mostRecent().args[0].klass()).toBe(ActiveResource::Collection)

          it 'returns a parameter error', ->
            error = window.onFailure.calls.mostRecent().args[0].first()
            expect(error.parameter).toEqual('id')

      describe 'using fields queryParam', ->
        beforeEach ->
          queryParams =
            fields:
              product: ['title','updatedAt'],
              orders: ['price','createdAt']

          @interface
          .get(@lib::Product.links()['related'], queryParams)
          .done(window.onSuccess)
          .fail(window.onFailure)

          @paramStr = decodeURIComponent(jasmine.Ajax.requests.mostRecent().url.split('?')[1])

        it 'builds a field set into the query URL', ->
          expect(@paramStr).toEqual('fields[product]=title,updated_at&fields[orders]=price,created_at')

      describe 'using include queryParam', ->
        beforeEach ->
          queryParams =
            include: ['merchant', 'attributeValues', { orders: 'transactions' }]

          @interface
          .get(@lib::Product.links()['related'], queryParams)
          .done(window.onSuccess)
          .fail(window.onFailure)

          @paramStr = decodeURIComponent(jasmine.Ajax.requests.mostRecent().url.split('?')[1])

        it 'builds an include tree into the query URL', ->
          expect(@paramStr).toEqual('include=merchant,attribute_values,orders.transactions')

      describe 'using sort queryParam', ->
        beforeEach ->
          queryParams =
            sort: { updatedAt: 'asc', createdAt: 'desc' }

          @interface
          .get(@lib::Product.links()['related'], queryParams)
          .done(window.onSuccess)
          .fail(window.onFailure)

          @paramStr = decodeURIComponent(jasmine.Ajax.requests.mostRecent().url.split('?')[1])

        it 'builds an include tree into the query URL', ->
          expect(@paramStr).toEqual('sort=updated_at,-created_at')

    describe '#post', ->
      describe 'persisting resource data', ->
        beforeEach ->
          @product = @lib::Product.build(title: 'Another title')
          @product.orders().assign([@lib::Order.build(id: 3)])

          @interface
          .post(@lib::Product.links()['related'], @product)
          .done(window.onSuccess)
          .fail(window.onFailure)

        it 'builds a resource document', ->
          resourceDocument =
            {
              data: {
                type: 'products',
                attributes: {
                  title: 'Another title'
                },
                relationships: {
                  orders: {
                    data: [
                      {
                        id: 3,
                        type: 'orders'
                      }
                    ]
                  }
                }
              }
            }
          expect(jasmine.Ajax.requests.mostRecent().data()).toEqual(resourceDocument)

        describe 'when persistence succeeds', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.save.success)
            @resource = window.onSuccess.calls.mostRecent().args[0]
<commit_msg>Add failing spec<commit_after>describe 'ActiveResource', ->
  beforeEach ->
    jasmine.Ajax.install()

    window.onSuccess = jasmine.createSpy('onSuccess')
    window.onFailure = jasmine.createSpy('onFailure')
    window.onCompletion = jasmine.createSpy('onCompletion')

  afterEach ->
    jasmine.Ajax.uninstall()

  describe 'Interfaces::JsonApi', ->
    beforeEach ->
      @lib = ActiveResource.createResourceLibrary(
        'https://www.example.com/api/v1/'
        interface: ActiveResource.Interfaces.JsonApi
      )
      class @lib::Product extends @lib.Base
        @className = 'Product'
        @queryName = 'products'

        @hasMany 'orders'

      class @lib::Order extends @lib.Base
        @className = 'Order'
        @queryName = 'orders'

        @belongsTo 'product'

      @interface = @lib.interface

    describe '#get()', ->
      describe 'getting resources', ->
        beforeEach ->
          @interface
          .get(@lib::Product.links()['related'])
          .done(window.onSuccess)
          .fail(window.onFailure)

        describe 'on success', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.all.success)
            expect(window.onSuccess).toHaveBeenCalled()
            @collection = window.onSuccess.calls.mostRecent().args[0]

          it 'returns a collection', ->
            expect(@collection.isA?(ActiveResource::Collection)).toBeTruthy()

          it 'returns a collection of resources of the queried type', ->
            @collection.each (resource) =>
              expect(resource.isA?(@lib::Product)).toBeTruthy()

          it 'returns a collection of resources with links', ->
            @collection.each (resource) =>
              expect(resource.links()['self']).toBeDefined()

      describe 'getting a resource', ->
        beforeEach ->
          @interface
          .get(@lib::Product.links()['related'] + '1')
          .done(window.onSuccess)
          .fail(window.onFailure)

        describe 'on success', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.find.success)
            expect(window.onSuccess).toHaveBeenCalled()
            @resource = window.onSuccess.calls.mostRecent().args[0]

          it 'returns a resource of the queried type', ->
            expect(@resource.isA?(@lib::Product)).toBeTruthy()

          it 'returns a resource with a link', ->
            expect(@resource.links()['self']).toBeDefined()

        describe 'on failure', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.find.failure)
            expect(window.onFailure).toHaveBeenCalled()

          it 'returns a collection of errors', ->
            expect(window.onFailure.calls.mostRecent().args[0].klass()).toBe(ActiveResource::Collection)

          it 'returns a parameter error', ->
            error = window.onFailure.calls.mostRecent().args[0].first()
            expect(error.parameter).toEqual('id')

      describe 'using fields queryParam', ->
        beforeEach ->
          queryParams =
            fields:
              product: ['title','updatedAt'],
              orders: ['price','createdAt']

          @interface
          .get(@lib::Product.links()['related'], queryParams)
          .done(window.onSuccess)
          .fail(window.onFailure)

          @paramStr = decodeURIComponent(jasmine.Ajax.requests.mostRecent().url.split('?')[1])

        it 'builds a field set into the query URL', ->
          expect(@paramStr).toEqual('fields[product]=title,updated_at&fields[orders]=price,created_at')

      describe 'using include queryParam', ->
        beforeEach ->
          queryParams =
            include: ['merchant', 'attributeValues', { orders: 'transactions' }]

          @interface
          .get(@lib::Product.links()['related'], queryParams)
          .done(window.onSuccess)
          .fail(window.onFailure)

          @paramStr = decodeURIComponent(jasmine.Ajax.requests.mostRecent().url.split('?')[1])

        it 'builds an include tree into the query URL', ->
          expect(@paramStr).toEqual('include=merchant,attribute_values,orders.transactions')

      describe 'using sort queryParam', ->
        beforeEach ->
          queryParams =
            sort: { updatedAt: 'asc', createdAt: 'desc' }

          @interface
          .get(@lib::Product.links()['related'], queryParams)
          .done(window.onSuccess)
          .fail(window.onFailure)

          @paramStr = decodeURIComponent(jasmine.Ajax.requests.mostRecent().url.split('?')[1])

        it 'builds an include tree into the query URL', ->
          expect(@paramStr).toEqual('sort=updated_at,-created_at')

    describe '#post', ->
      describe 'persisting resource data', ->
        beforeEach ->
          @product = @lib::Product.build(title: 'Another title')
          @product.orders().assign([@lib::Order.build(id: 3)])

          @product.timestamp = new Date()

          @interface
          .post(@lib::Product.links()['related'], @product)
          .done(window.onSuccess)
          .fail(window.onFailure)

        it 'builds a resource document', ->
          resourceDocument =
            {
              data: {
                type: 'products',
                attributes: {
                  title: 'Another title',
                  timestamp: @product.timestamp.toJSON()
                },
                relationships: {
                  orders: {
                    data: [
                      {
                        id: 3,
                        type: 'orders'
                      }
                    ]
                  }
                }
              }
            }
          expect(jasmine.Ajax.requests.mostRecent().data()).toEqual(resourceDocument)

        describe 'when persistence succeeds', ->
          beforeEach ->
            jasmine.Ajax.requests.mostRecent().respondWith(JsonApiResponses.Product.save.success)
            @resource = window.onSuccess.calls.mostRecent().args[0]

{'fraction_non_alphanumeric': 0.13307984790874525, 'fraction_numerical': 0.001983798975037196, 'mean_word_length': 2.0315710348283638, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 83, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '3844606', 'n_tokens_mistral': 3283, 'n_tokens_neox': 3156, 'n_words': 559}