<commit_before>_ = require('underscore')._
Q = require 'q'
BaseService              = require '../../lib/services/base'
CartService              = require '../../lib/services/carts'
CategoryService          = require '../../lib/services/categories'
ChannelService           = require '../../lib/services/channels'
CommentService           = require '../../lib/services/comments'
CustomObjectService      = require '../../lib/services/custom-objects'
CustomerService          = require '../../lib/services/customers'
CustomerGroupService     = require '../../lib/services/customer-groups'
InventoryService         = require '../../lib/services/inventories'
OrderService             = require '../../lib/services/orders'
ProductService           = require '../../lib/services/products'
ProductProjectionService = require '../../lib/services/product-projections'
ProductTypeService       = require '../../lib/services/product-types'
ReviewService            = require '../../lib/services/reviews'
ShippingMethodService    = require '../../lib/services/shipping-methods'
TaxCategoryService       = require '../../lib/services/tax-categories'

describe 'Service', ->

  ID = '1234-abcd-5678-efgh'

  _.each [
    {name: 'BaseService', service: BaseService, path: ''}
    {name: 'CartService', service: CartService, path: '/carts'}
    {name: 'CategoryService', service: CategoryService, path: '/categories'}
    {name: 'ChannelService', service: ChannelService, path: '/channels'}
    {name: 'CommentService', service: CommentService, path: '/comments'}
    {name: 'CustomObjectService', service: CustomObjectService, path: '/custom-objects'}
    {name: 'CustomerService', service: CustomerService, path: '/customers'}
    {name: 'CustomerGroupService', service: CustomerGroupService, path: '/customer-groups'}
    {name: 'InventoryService', service: InventoryService, path: '/inventory'}
    {name: 'OrderService', service: OrderService, path: '/orders'}
    {name: 'ProductService', service: ProductService, path: '/products'}
    {name: 'ProductProjectionService', service: ProductProjectionService, path: '/product-projections'}
    {name: 'ProductTypeService', service: ProductTypeService, path: '/product-types'}
    {name: 'ReviewService', service: ReviewService, path: '/reviews'}
    {name: 'ShippingMethodService', service: ShippingMethodService, path: '/shipping-methods'}
    {name: 'TaxCategoryService', service: TaxCategoryService, path: '/tax-categories'}
  ], (o) ->

    describe ":: #{o.name}", ->

      beforeEach ->
        @restMock =
          config: {}
          GET: (endpoint, callback) ->
          POST: -> (endpoint, payload, callback) ->
          PUT: ->
          DELETE: ->
          _preRequest: ->
          _doRequest: ->
        @service = new o.service @restMock

      afterEach ->
        @service = null
        @restMock = null

      it 'should have constants defined', ->
        expect(o.service.baseResourceEndpoint).toBe o.path

      it 'should not share variables between instances', ->
        base1 = new o.service @restMock
        base1._currentEndpoint = '/foo/1'
        base2 = new o.service @restMock
        expect(base2._currentEndpoint).toBe o.path

      it 'should initialize with Rest client', ->
        expect(@service).toBeDefined()
        expect(@service._currentEndpoint).toBe o.path

      it 'should reset default params', ->
        expect(@service._params).toEqual
          query:
            where: []
            operator: 'and'
            sort: []

      it 'should build endpoint with id', ->
        @service.byId(ID)
        expect(@service._currentEndpoint).toBe "#{o.path}/#{ID}"

      _.each ['byId', 'where', 'whereOperator', 'page', 'perPage'], (f) ->
        it "should chain '#{f}'", ->
          clazz = @service[f]()
          expect(clazz).toEqual @service

          promise = @service[f]().fetch()
          expect(Q.isPromise(promise)).toBe true

      it 'should add where predicates to query', ->
        @service.where('name(en="Foo")')
        expect(@service._params.query.where).toEqual ['name(en%3D%22Foo%22)']

        @service.where('variants is empty')
        expect(@service._params.query.where).toEqual ['name(en%3D%22Foo%22)', 'variants%20is%20empty']

      it 'should not add undefined where predicates', ->
        @service.where()
        expect(@service._params.query.where).toEqual []

      it 'should set query logical operator', ->
        @service.whereOperator('or')
        expect(@service._params.query.operator).toBe 'or'

        @service.whereOperator()
        expect(@service._params.query.operator).toBe 'and'

        @service.whereOperator('foo')
        expect(@service._params.query.operator).toBe 'and'

      it 'should add page number', ->
        @service.page(5)
        expect(@service._params.query.page).toBe 5

      it 'should add perPage number', ->
        @service.perPage(50)
        expect(@service._params.query.perPage).toBe 50

      it 'should build query string', ->
        queryString = @service
          .where('name(en="Foo")')
          .where('id="1234567890"')
          .whereOperator('or')
          .page(3)
          .perPage(25)
          .sort('attrib', false)
          ._queryString()

        expect(queryString).toBe 'where=name(en%3D%22Foo%22)%20or%20id%3D%221234567890%22&limit=25&offset=50&sort=attrib%20desc'

      it 'should reset params after resolving a promise', ->
        spyOn(@restMock, 'GET').andCallFake (endpoint, callback) -> callback(null, {statusCode: 200}, {foo: 'bar'})
        _service = @service.byId('123-abc')
        expect(@service._params.id).toBe '123-abc'
        _service.fetch().then (result) -> #noop
        expect(@service._params.id).not.toBeDefined()
<commit_msg>Add second sort criteria in spec.<commit_after>_ = require('underscore')._
Q = require 'q'
BaseService              = require '../../lib/services/base'
CartService              = require '../../lib/services/carts'
CategoryService          = require '../../lib/services/categories'
ChannelService           = require '../../lib/services/channels'
CommentService           = require '../../lib/services/comments'
CustomObjectService      = require '../../lib/services/custom-objects'
CustomerService          = require '../../lib/services/customers'
CustomerGroupService     = require '../../lib/services/customer-groups'
InventoryService         = require '../../lib/services/inventories'
OrderService             = require '../../lib/services/orders'
ProductService           = require '../../lib/services/products'
ProductProjectionService = require '../../lib/services/product-projections'
ProductTypeService       = require '../../lib/services/product-types'
ReviewService            = require '../../lib/services/reviews'
ShippingMethodService    = require '../../lib/services/shipping-methods'
TaxCategoryService       = require '../../lib/services/tax-categories'

describe 'Service', ->

  ID = '1234-abcd-5678-efgh'

  _.each [
    {name: 'BaseService', service: BaseService, path: ''}
    {name: 'CartService', service: CartService, path: '/carts'}
    {name: 'CategoryService', service: CategoryService, path: '/categories'}
    {name: 'ChannelService', service: ChannelService, path: '/channels'}
    {name: 'CommentService', service: CommentService, path: '/comments'}
    {name: 'CustomObjectService', service: CustomObjectService, path: '/custom-objects'}
    {name: 'CustomerService', service: CustomerService, path: '/customers'}
    {name: 'CustomerGroupService', service: CustomerGroupService, path: '/customer-groups'}
    {name: 'InventoryService', service: InventoryService, path: '/inventory'}
    {name: 'OrderService', service: OrderService, path: '/orders'}
    {name: 'ProductService', service: ProductService, path: '/products'}
    {name: 'ProductProjectionService', service: ProductProjectionService, path: '/product-projections'}
    {name: 'ProductTypeService', service: ProductTypeService, path: '/product-types'}
    {name: 'ReviewService', service: ReviewService, path: '/reviews'}
    {name: 'ShippingMethodService', service: ShippingMethodService, path: '/shipping-methods'}
    {name: 'TaxCategoryService', service: TaxCategoryService, path: '/tax-categories'}
  ], (o) ->

    describe ":: #{o.name}", ->

      beforeEach ->
        @restMock =
          config: {}
          GET: (endpoint, callback) ->
          POST: -> (endpoint, payload, callback) ->
          PUT: ->
          DELETE: ->
          _preRequest: ->
          _doRequest: ->
        @service = new o.service @restMock

      afterEach ->
        @service = null
        @restMock = null

      it 'should have constants defined', ->
        expect(o.service.baseResourceEndpoint).toBe o.path

      it 'should not share variables between instances', ->
        base1 = new o.service @restMock
        base1._currentEndpoint = '/foo/1'
        base2 = new o.service @restMock
        expect(base2._currentEndpoint).toBe o.path

      it 'should initialize with Rest client', ->
        expect(@service).toBeDefined()
        expect(@service._currentEndpoint).toBe o.path

      it 'should reset default params', ->
        expect(@service._params).toEqual
          query:
            where: []
            operator: 'and'
            sort: []

      it 'should build endpoint with id', ->
        @service.byId(ID)
        expect(@service._currentEndpoint).toBe "#{o.path}/#{ID}"

      _.each ['byId', 'where', 'whereOperator', 'page', 'perPage'], (f) ->
        it "should chain '#{f}'", ->
          clazz = @service[f]()
          expect(clazz).toEqual @service

          promise = @service[f]().fetch()
          expect(Q.isPromise(promise)).toBe true

      it 'should add where predicates to query', ->
        @service.where('name(en="Foo")')
        expect(@service._params.query.where).toEqual ['name(en%3D%22Foo%22)']

        @service.where('variants is empty')
        expect(@service._params.query.where).toEqual ['name(en%3D%22Foo%22)', 'variants%20is%20empty']

      it 'should not add undefined where predicates', ->
        @service.where()
        expect(@service._params.query.where).toEqual []

      it 'should set query logical operator', ->
        @service.whereOperator('or')
        expect(@service._params.query.operator).toBe 'or'

        @service.whereOperator()
        expect(@service._params.query.operator).toBe 'and'

        @service.whereOperator('foo')
        expect(@service._params.query.operator).toBe 'and'

      it 'should add page number', ->
        @service.page(5)
        expect(@service._params.query.page).toBe 5

      it 'should add perPage number', ->
        @service.perPage(50)
        expect(@service._params.query.perPage).toBe 50

      it 'should build query string', ->
        queryString = @service
          .where 'name(en="Foo")'
          .where 'id="1234567890"'
          .whereOperator 'or'
          .page 3
          .perPage 25
          .sort 'attrib', false
          .sort 'createdAt'
          ._queryString()

        expect(queryString).toBe 'where=name(en%3D%22Foo%22)%20or%20id%3D%221234567890%22&limit=25&offset=50&sort=attrib%20desc&sort=createdAt%20asc'

      it 'should reset params after resolving a promise', ->
        spyOn(@restMock, 'GET').andCallFake (endpoint, callback) -> callback(null, {statusCode: 200}, {foo: 'bar'})
        _service = @service.byId('123-abc')
        expect(@service._params.id).toBe '123-abc'
        _service.fetch().then (result) -> #noop
        expect(@service._params.id).not.toBeDefined()

{'fraction_non_alphanumeric': 0.15567054764984908, 'fraction_numerical': 0.014833980163863735, 'mean_word_length': 2.7262210796915167, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 59, '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': '7201988', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3187, 'n_words': 715}