<commit_before>require 'rails_helper'

describe Api::MeasurementsController do
  let(:user) { FactoryGirl.create(:user) }

  let :valid_door_metadata do
    {
      measurement_type: 'door',
      'description': 'Some user description',
      'data': {
        'width': 1.04
      }
    }
  end

  let :valid_steps_metadata do
    {
      measurement_type: 'steps',
      'description': 'Some user description',
      'data': {
        'height': 0.11
      }
    }
  end

  let :valid_ramp_metadata do
    {
      measurement_type: 'ramp',
      'description': 'Some user description',
      'data': {
        'angle': 15.42
      }
    }
  end

  let :valid_toilet_metadata do
    {
      measurement_type: 'toilet',
      'description': 'Some user description',
      'data': {
        'area': 15.42
      }
    }
  end

  describe 'create action' do
    let(:poi) { Poi.first }

    def json_response
      JSON.parse(response.body)
    end

    before do
      FactoryGirl.create(:poi)
    end

    before :all do
      User.delete_all
    end

    describe 'image upload' do
      context 'with valid params' do
        before do
          post(:create, :node_id => poi.id, :api_key => user.authentication_token, :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'creates a photo associated with the poi' do
          expect(poi.photos.count).to eq 1
        end

        it "creates a photo with the current user assigned" do
          photo = poi.photos.first
          expect(photo.user).to eq user
        end

        describe 'the response' do
          it 'has status 201 Created' do
            expect(response.status).to eql 201
          end

          it 'returns id' do
            photo = poi.photos.first
            expect(json_response['id']).to eq photo.id
          end
        end
      end

      context 'with missing image' do
        before do
          post(:create, :node_id => poi.id, :api_key => user.authentication_token)
        end

        it 'returns 400 Bad Request' do
          expect(response.status).to eq 400
        end

        specify 'the error message indicates that the image is missing' do
          expect(json_response['error']).to eq 'photo is missing'
        end

        it 'does not save a new photo for the poi' do
          expect(poi.photos.count).to eq 0
        end
      end

      context 'with missing api key' do
        before do
          post(:create, :node_id => poi.id, :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'returns 401 Unauthorized' do
          expect(response.status).to eq 401
        end

        it 'does not save a new photo for the poi' do
          expect(poi.photos.count).to eq 0
        end
      end

      context 'with invalid api key' do
        before do
          post(:create, :node_id => poi.id, api_key: '12354645543534534', :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'does not save a new photo for the poi' do
          expect(poi.photos.count).to eq 0
        end

        it 'returns 401 Unauthorized' do
          expect(response.status).to eq 401
        end
      end

      context 'with non existant node' do
        before do
          post(:create, :node_id => 999999, :api_key => user.authentication_token, :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'returns 404' do
          expect(response.status).to eq 404
        end

        it 'does not save a new measurement' do
          expect(poi.photos.count).to eq 0
        end
      end
    end
  end

  describe 'add metadata for image' do
    let(:poi) { FactoryGirl.create(:poi, photos: [FactoryGirl.create(:photo, user: user)]) }
    let(:picture) { poi.photos.first }

    it 'returns 404 if the node is not available' do
      post(:add_metadata, :node_id => 404, :measurement_id => picture.id, :api_key => user.authentication_token, :metadata => valid_door_metadata)
      expect(response.status).to eq 404
    end

    describe 'add door metadata' do
      let(:picture) { poi.photos.first }
      before do
        post(:add_metadata, metadata: valid_door_metadata, :node_id => poi.id, :measurement_id => picture.id, :api_key => user.authentication_token)
      end

      it 'accepts valid json' do
        expect(response.status).to eq 201
      end

      describe 'metadata' do
        let(:measurement) { picture.measurements.first }
        let(:data_point) { measurement.datapoints.first }

        it 'stores exactly one data point' do
          expect(picture.measurements.length).to eq 1
        end

        it 'has meters as unit' do
          expect(data_point.unit).to eq 'meters'
        end

        it 'has correct value' do
          expect(data_point.value).to eq 1.04
        end

        it 'has correct name' do
          expect(data_point.property).to eq 'width'
        end
      end
    end

    describe 'add steps metadata' do
      it 'accepts valid json' do
        post(:add_metadata, :node_id => poi.id, :measurement_id => picture.id, :api_key => user.authentication_token, :metadata => valid_steps_metadata)
        expect(response.status).to eq 201
      end<commit_msg>add tests for steps metadata<commit_after>require 'rails_helper'

describe Api::MeasurementsController do
  let(:user) { FactoryGirl.create(:user) }

  let :valid_door_metadata do
    {
      measurement_type: 'door',
      'description': 'Some user description',
      'data': {
        'width': 1.04
      }
    }
  end

  let :valid_steps_metadata do
    {
      measurement_type: 'steps',
      'description': 'Some user description',
      'data': {
        'height': 0.11
      }
    }
  end

  let :valid_ramp_metadata do
    {
      measurement_type: 'ramp',
      'description': 'Some user description',
      'data': {
        'angle': 15.42
      }
    }
  end

  let :valid_toilet_metadata do
    {
      measurement_type: 'toilet',
      'description': 'Some user description',
      'data': {
        'area': 15.42
      }
    }
  end

  describe 'create action' do
    let(:poi) { Poi.first }

    def json_response
      JSON.parse(response.body)
    end

    before do
      FactoryGirl.create(:poi)
    end

    before :all do
      User.delete_all
    end

    describe 'image upload' do
      context 'with valid params' do
        before do
          post(:create, :node_id => poi.id, :api_key => user.authentication_token, :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'creates a photo associated with the poi' do
          expect(poi.photos.count).to eq 1
        end

        it "creates a photo with the current user assigned" do
          photo = poi.photos.first
          expect(photo.user).to eq user
        end

        describe 'the response' do
          it 'has status 201 Created' do
            expect(response.status).to eql 201
          end

          it 'returns id' do
            photo = poi.photos.first
            expect(json_response['id']).to eq photo.id
          end
        end
      end

      context 'with missing image' do
        before do
          post(:create, :node_id => poi.id, :api_key => user.authentication_token)
        end

        it 'returns 400 Bad Request' do
          expect(response.status).to eq 400
        end

        specify 'the error message indicates that the image is missing' do
          expect(json_response['error']).to eq 'photo is missing'
        end

        it 'does not save a new photo for the poi' do
          expect(poi.photos.count).to eq 0
        end
      end

      context 'with missing api key' do
        before do
          post(:create, :node_id => poi.id, :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'returns 401 Unauthorized' do
          expect(response.status).to eq 401
        end

        it 'does not save a new photo for the poi' do
          expect(poi.photos.count).to eq 0
        end
      end

      context 'with invalid api key' do
        before do
          post(:create, :node_id => poi.id, api_key: '12354645543534534', :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'does not save a new photo for the poi' do
          expect(poi.photos.count).to eq 0
        end

        it 'returns 401 Unauthorized' do
          expect(response.status).to eq 401
        end
      end

      context 'with non existant node' do
        before do
          post(:create, :node_id => 999999, :api_key => user.authentication_token, :photo => fixture_file_upload('/placeholder.jpg'))
        end

        it 'returns 404' do
          expect(response.status).to eq 404
        end

        it 'does not save a new measurement' do
          expect(poi.photos.count).to eq 0
        end
      end
    end
  end

  describe 'add metadata for image' do
    let(:poi) { FactoryGirl.create(:poi, photos: [FactoryGirl.create(:photo, user: user)]) }
    let(:picture) { poi.photos.first }

    it 'returns 404 if the node is not available' do
      post(:add_metadata, :node_id => 404, :measurement_id => picture.id, :api_key => user.authentication_token, :metadata => valid_door_metadata)
      expect(response.status).to eq 404
    end

    describe 'add door metadata' do
      let(:picture) { poi.photos.first }
      before do
        post(:add_metadata, metadata: valid_door_metadata, :node_id => poi.id, :measurement_id => picture.id, :api_key => user.authentication_token)
      end

      it 'accepts valid json' do
        expect(response.status).to eq 201
      end

      describe 'metadata' do
        let(:measurement) { picture.measurements.first }
        let(:data_point) { measurement.datapoints.first }

        it 'stores exactly one data point' do
          expect(picture.measurements.length).to eq 1
        end

        it 'has meters as unit' do
          expect(data_point.unit).to eq 'meters'
        end

        it 'has correct value' do
          expect(data_point.value).to eq 1.04
        end

        it 'has correct name' do
          expect(data_point.property).to eq 'width'
        end
      end
    end

    describe 'add steps metadata' do
      let(:picture) { poi.photos.first }

      before do
        post(:add_metadata, :node_id => poi.id, :measurement_id => picture.id, :api_key => user.authentication_token, :metadata => valid_steps_metadata)
      end

      it 'accepts valid json' do
        post(:add_metadata, :node_id => poi.id, :measurement_id => picture.id, :api_key => user.authentication_token, :metadata => valid_steps_metadata)
        expect(response.status).to eq 201
      end

      describe "metadata" do
        let(:measurement) { picture.measurements.first }
        let(:data_point) { measurement.datapoints.first }

        it 'stores exactly one data point' do
          expect(picture.measurements.length).to eq 1
        end

        it 'has meters as unit' do
          expect(data_point.unit).to eq 'meters'
        end

        it 'has correct value' do
          expect(data_point.value).to eq 0.11
        end

        it 'has correct name' do
          expect(data_point.property).to eq 'height'
        end
      end
{'fraction_non_alphanumeric': 0.09581320450885668, 'fraction_numerical': 0.016639828234031134, 'mean_word_length': 2.0394235997824905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, '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': '4664797', 'n_tokens_mistral': 3475, 'n_tokens_neox': 3257, 'n_words': 990}