<commit_before># encoding: UTF-8
require 'spec_helper'
require 'yt/models/video'

describe Yt::Video, :partner do
  subject(:video) { Yt::Video.new id: id, auth: $content_owner }

  context 'given a video of a partnered channel', :partner do
    context 'managed by the authenticated Content Owner' do
      let(:id) { ENV['YT_TEST_PARTNER_VIDEO_ID'] }

      describe 'advertising options can be retrieved' do
        it { expect{video.advertising_options_set}.not_to raise_error }
      end

      describe 'earnings can be retrieved for a specific day' do
        context 'in which the video made any money' do
          let(:earnings) {video.earnings_on ENV['YT_TEST_PARTNER_VIDEO_DATE']}
          it { expect(earnings).to be_a Float }
        end

        context 'in the future' do
          let(:earnings) { video.earnings_on 5.days.from_now}
          it { expect(earnings).to be_nil }
        end
      end

      describe 'earnings can be retrieved for a single country' do
        let(:country_code) { 'US' }
        let(:earnings) { video.earnings since: date, by: by, in: location }
        let(:date) { 4.days.ago }

        context 'and grouped by day' do
          let(:by) { :day }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(earnings.keys.min).to eq date.to_date }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(earnings.keys.min).to eq date.to_date }
          end
        end

        context 'and grouped by country' do
          let(:by) { :country }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(earnings.keys).to eq [country_code] }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(earnings.keys).to eq [country_code] }
          end
        end
      end

      describe 'earnings can be retrieved for a range of days' do
        let(:date) { ENV['YT_TEST_PARTNER_VIDEO_DATE'] }

        specify 'with a given start and end (:since / :until option)' do
          expect(video.earnings(since: date, until: date).keys.min).to eq date.to_date
        end

        specify 'with a given start and end (:from / :to option)' do
          expect(video.earnings(from: date, to: date).keys.min).to eq date.to_date
        end
      end

      describe 'earnings can be grouped by range' do
        let(:range) { {since: ENV['YT_TEST_PARTNER_VIDEO_DATE']} }
        let(:keys) { range.values }

        specify 'with the :by option set to :range' do
          earnings = video.earnings range.merge by: :range
          expect(earnings.size).to be 1
          expect(earnings[:total]).to be_a Float
        end
      end

      describe 'earnings can be grouped by day' do
        let(:range) { {since: 4.days.ago.to_date, until: 3.days.ago.to_date} }
        let(:keys) { range.values }

        specify 'without a :by option (default)' do
          earnings = video.earnings range
          expect(earnings.keys).to eq range.values
        end

        specify 'with the :by option set to :day' do
          earnings = video.earnings range.merge by: :day
          expect(earnings.keys).to eq range.values
        end
      end

      describe 'earnings can be grouped by country' do
        let(:range) { {since: 4.days.ago, until: 3.days.ago} }

        specify 'with the :by option set to :country' do
          earnings = video.earnings range.merge by: :country
          expect(earnings.keys).to all(be_a String)
          expect(earnings.keys.map(&:length).uniq).to eq [2]
          expect(earnings.values).to all(be_a Float)
        end
      end

      describe 'views can be retrieved for a specific day' do
        context 'in which the video was partnered' do
          let(:views) { video.views_on 5.days.ago}
          it { expect(views).to be_an Integer }
        end

        context 'in which the video was not partnered' do
          let(:views) { video.views_on 20.years.ago}
          it { expect(views).to be_nil }
        end
      end


      describe 'views can be retrieved for a single country' do
        let(:country_code) { 'US' }
        let(:views) { video.views since: date, by: by, in: location }
        let(:date) { 4.days.ago }

        context 'and grouped by day' do
          let(:by) { :day }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(views.keys.min).to eq date.to_date }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(views.keys.min).to eq date.to_date }
          end
        end

        context 'and grouped by country' do
          let(:by) { :country }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(views.keys).to eq [country_code] }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(views.keys).to eq [country_code] }
          end
        end
      end

      describe 'views can be retrieved for a range of days' do
        let(:date) { 4.days.ago }

        specify 'with a given start (:since option)' do
          expect(video.views(since: date).keys.min).to eq date.to_date
        end

        specify 'with a given end (:until option)' do
          expect(video.views(until: date).keys.max).to eq date.to_date
        end

        specify 'with a given start (:from option)' do
          expect(video.views(from: date).keys.min).to eq date.to_date
        end

        specify 'with a given end (:to option)' do<commit_msg>Add test to show that `in: 'US', by: :state` works<commit_after># encoding: UTF-8
require 'spec_helper'
require 'yt/models/video'

describe Yt::Video, :partner do
  subject(:video) { Yt::Video.new id: id, auth: $content_owner }

  context 'given a video of a partnered channel', :partner do
    context 'managed by the authenticated Content Owner' do
      let(:id) { ENV['YT_TEST_PARTNER_VIDEO_ID'] }

      describe 'advertising options can be retrieved' do
        it { expect{video.advertising_options_set}.not_to raise_error }
      end

      describe 'earnings can be retrieved for a specific day' do
        context 'in which the video made any money' do
          let(:earnings) {video.earnings_on ENV['YT_TEST_PARTNER_VIDEO_DATE']}
          it { expect(earnings).to be_a Float }
        end

        context 'in the future' do
          let(:earnings) { video.earnings_on 5.days.from_now}
          it { expect(earnings).to be_nil }
        end
      end

      describe 'earnings can be retrieved for a single country' do
        let(:country_code) { 'US' }
        let(:earnings) { video.earnings since: date, by: by, in: location }
        let(:date) { 4.days.ago }

        context 'and grouped by day' do
          let(:by) { :day }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(earnings.keys.min).to eq date.to_date }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(earnings.keys.min).to eq date.to_date }
          end
        end

        context 'and grouped by country' do
          let(:by) { :country }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(earnings.keys).to eq [country_code] }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(earnings.keys).to eq [country_code] }
          end
        end
      end

      describe 'earnings can be retrieved for a range of days' do
        let(:date) { ENV['YT_TEST_PARTNER_VIDEO_DATE'] }

        specify 'with a given start and end (:since / :until option)' do
          expect(video.earnings(since: date, until: date).keys.min).to eq date.to_date
        end

        specify 'with a given start and end (:from / :to option)' do
          expect(video.earnings(from: date, to: date).keys.min).to eq date.to_date
        end
      end

      describe 'earnings can be grouped by range' do
        let(:range) { {since: ENV['YT_TEST_PARTNER_VIDEO_DATE']} }
        let(:keys) { range.values }

        specify 'with the :by option set to :range' do
          earnings = video.earnings range.merge by: :range
          expect(earnings.size).to be 1
          expect(earnings[:total]).to be_a Float
        end
      end

      describe 'earnings can be grouped by day' do
        let(:range) { {since: 4.days.ago.to_date, until: 3.days.ago.to_date} }
        let(:keys) { range.values }

        specify 'without a :by option (default)' do
          earnings = video.earnings range
          expect(earnings.keys).to eq range.values
        end

        specify 'with the :by option set to :day' do
          earnings = video.earnings range.merge by: :day
          expect(earnings.keys).to eq range.values
        end
      end

      describe 'earnings can be grouped by country' do
        let(:range) { {since: 4.days.ago, until: 3.days.ago} }

        specify 'with the :by option set to :country' do
          earnings = video.earnings range.merge by: :country
          expect(earnings.keys).to all(be_a String)
          expect(earnings.keys.map(&:length).uniq).to eq [2]
          expect(earnings.values).to all(be_a Float)
        end
      end

      describe 'views can be retrieved for a specific day' do
        context 'in which the video was partnered' do
          let(:views) { video.views_on 5.days.ago}
          it { expect(views).to be_an Integer }
        end

        context 'in which the video was not partnered' do
          let(:views) { video.views_on 20.years.ago}
          it { expect(views).to be_nil }
        end
      end


      describe 'views can be retrieved for a single country' do
        let(:country_code) { 'US' }
        let(:views) { video.views since: date, by: by, in: location }
        let(:date) { 4.days.ago }

        context 'and grouped by day' do
          let(:by) { :day }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(views.keys.min).to eq date.to_date }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(views.keys.min).to eq date.to_date }
          end
        end

        context 'and grouped by country' do
          let(:by) { :country }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(views.keys).to eq [country_code] }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(views.keys).to eq [country_code] }
          end
        end

        context 'and grouped by state' do
          let(:by) { :state }

          context 'with the :in option set to the country code' do
            let(:location) { country_code }
            it { expect(views.keys.map(&:length).uniq).to eq [2] }
          end

          context 'with the :in option set to {country: country code}' do
            let(:location) { {country: country_code} }
            it { expect(views.keys.map(&:length).uniq).to eq [2] }
          end
        end
      end

      describe 'views can be retrieved for a range of days' do
        let(:date) { 4.days.ago }

        specify 'with a given start (:since option)' do
          expect(video.views(since: date).keys.min).to eq date.to_date
        end

        specify 'with a given end (:until option)' do
          expect(video.views(until: date).keys.max).to eq date.to_date
        end

        specify 'with a given start (:from option)' do
          expect(video.views(from: date).keys.min).to eq date.to_date
        end

        specify 'with a given end (:to option)' do
{'fraction_non_alphanumeric': 0.10060298317994287, 'fraction_numerical': 0.0023801967629324024, 'mean_word_length': 2.030776628997355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2002323', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3639, 'n_words': 1238}