<commit_before>require 'spec_helper'

describe Nacre::API::Product do
  let(:connection) { mock("connection") }

  before do
    Nacre::API::Product.stub(:connection).and_return(connection)
  end

  context "when instantiated with no data" do
    let(:model) {
      Nacre::API::Product.new
    }

    it 'should create an instance' do
      model.should be_a(Nacre::API::Product)
    end
  end

  context "when instantiated with valid data" do
    let(:valid_params) {}

    let(:model) {
      Nacre::API::Product.new(valid_params)
    }

    describe "initialization" do
      it 'should create an instance' do
        model.should be_a(Nacre::API::Product)
      end

      it 'should have a list of fields' do
        model.class.fields.should == [
          :id, :brandId, :productTypeId, :identity,
          :productGroupId, :stock, :financialDetails,
          :salesChannels, :composition, :variations
        ]
      end

      pending 'should contain the correct model data' do
        model.product_id.should == 1000
        model.sales_channel.should == 'Brightpearl'
        model.sku.should == ''
        model.ean.should be_nil
      end
    end

    describe "find" do
      before do
        response = mock("response")
        response.stub(:body).and_return(response_json)
        connection.should_receive(:get).
          with("/product-service/product/1008").
          any_number_of_times.
          and_return(response)
      end

      context "when an empty list comes back" do
        let(:response_json) { { "response" => [] }.to_json }

        it 'should return nil' do
          Nacre::API::Product.find(1008).should be_nil
        end
      end

      context "when one valid value comes back" do
        let(:results) { [] }
        let(:response_json) { IO.read("spec/fixtures/json/product.json") }

        it 'should return a valid Product' do
          product = Nacre::API::Product.find(1008)
          product.should be_a(Nacre::API::Product)
          product.id.should == 1008
          product.identity["sku"].should == "SKU0001"
          #product.productTypeId.should == 1  #TODO uncamelize this
        end
      end
    end

    describe "all" do
      context "when an empty list comes back" do
        it 'should return an empty array' do
          Nacre::API::ProductSearch.any_instance.should_receive(:results).and_return(Nacre::API::ProductSearchResults.new)
          all_products = Nacre::API::Product.all
          all_products.should be_a(Array)
          all_products.should be_empty
        end
      end

      context "when a list with actual values comes back" do
        let(:response_json) { IO.read("spec/fixtures/json/product.json") }

        it 'should return an array of Products' do
          search_results = Nacre::API::ProductSearchResults.new_from_json(
              IO.read("spec/fixtures/json/product_search_result.json")
          )
          Nacre::API::ProductSearch.any_instance.should_receive(:results).and_return(search_results)

          response = mock("response")
          response.stub(:body).and_return(response_json)
          connection.should_receive(:get).
            with("/product-service/product/1000,1001,1002,1003,1004,1005,1006").
            and_return(response)

          products = Nacre::API::Product.all
          products.should be_a(Array)
          products.length.should == 1

          product = products.first
          product.should be_a(Nacre::API::Product)
          product.id.should == 1008
          product.identity["sku"].should == "SKU0001"
          #product.productTypeId.should == 1  #TODO uncamelize this
        end
      end
    end

    describe "save"

    describe "delete"
  end
end<commit_msg>Changes tested product methods to match new uncamelised names<commit_after>require 'spec_helper'

describe Nacre::API::Product do
  let(:connection) { mock("connection") }

  before do
    Nacre::API::Product.stub(:connection).and_return(connection)
  end

  context "when instantiated with no data" do
    let(:model) {
      Nacre::API::Product.new
    }

    it 'should create an instance' do
      model.should be_a(Nacre::API::Product)
    end
  end

  context "when instantiated with valid data" do
    let(:valid_params) {}

    let(:model) {
      Nacre::API::Product.new(valid_params)
    }

    describe "initialization" do
      it 'should create an instance' do
        model.should be_a(Nacre::API::Product)
      end

      it 'should have a list of fields' do
        model.class.fields.should == [
          :id, :brandId, :productTypeId, :identity,
          :productGroupId, :stock, :financialDetails,
          :salesChannels, :composition, :variations
        ]
      end

      pending 'should contain the correct model data' do
        model.product_id.should == 1000
        model.sales_channel.should == 'Brightpearl'
        model.sku.should == ''
        model.ean.should be_nil
      end
    end

    describe "find" do
      before do
        response = mock("response")
        response.stub(:body).and_return(response_json)
        connection.should_receive(:get).
          with("/product-service/product/1008").
          any_number_of_times.
          and_return(response)
      end

      context "when an empty list comes back" do
        let(:response_json) { { "response" => [] }.to_json }

        it 'should return nil' do
          Nacre::API::Product.find(1008).should be_nil
        end
      end

      context "when one valid value comes back" do
        let(:results) { [] }
        let(:response_json) { IO.read("spec/fixtures/json/product.json") }

        it 'should return a valid Product' do
          product = Nacre::API::Product.find(1008)
          product.should be_a(Nacre::API::Product)
          product.id.should == 1008
          product.identity.sku.should == "SKU0001"
          product.product_type_id.should == 1
        end
      end
    end

    describe "all" do
      context "when an empty list comes back" do
        it 'should return an empty array' do
          Nacre::API::ProductSearch.any_instance.should_receive(:results).and_return(Nacre::API::ProductSearchResults.new)
          all_products = Nacre::API::Product.all
          all_products.should be_a(Array)
          all_products.should be_empty
        end
      end

      context "when a list with actual values comes back" do
        let(:response_json) { IO.read("spec/fixtures/json/product.json") }

        it 'should return an array of Products' do
          search_results = Nacre::API::ProductSearchResults.new_from_json(
              IO.read("spec/fixtures/json/product_search_result.json")
          )
          Nacre::API::ProductSearch.any_instance.should_receive(:results).and_return(search_results)

          response = mock("response")
          response.stub(:body).and_return(response_json)
          connection.should_receive(:get).
            with("/product-service/product/1000,1001,1002,1003,1004,1005,1006").
            and_return(response)

          products = Nacre::API::Product.all
          products.should be_a(Array)
          products.length.should == 1

          product = products.first
          product.should be_a(Nacre::API::Product)
          product.id.should == 1008
          product.identity.sku.should == "SKU0001"
          product.product_type_id.should == 1
        end
      end
    end

    describe "save"

    describe "delete"
  end
end
{'fraction_non_alphanumeric': 0.10735373054213634, 'fraction_numerical': 0.016908212560386472, 'mean_word_length': 2.3954441913439637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4435114', 'n_tokens_mistral': 2280, 'n_tokens_neox': 2122, 'n_words': 483}