workarea-reviews 3.0.8 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.eslintrc.json +35 -0
- data/.github/workflows/ci.yml +60 -0
- data/.gitignore +1 -2
- data/.rubocop.yml +3 -0
- data/.stylelintrc.json +8 -0
- data/CHANGELOG.md +24 -12
- data/Gemfile +4 -2
- data/Rakefile +4 -5
- data/app/controllers/workarea/admin/reports_controller.decorator +10 -0
- data/app/helpers/workarea/admin/reviews_helper.rb +6 -5
- data/app/helpers/workarea/storefront/reviews_helper.rb +1 -3
- data/app/helpers/workarea/storefront/reviews_schema_org_helper.rb +34 -0
- data/app/mailers/workarea/admin/status_report_mailer.decorator +2 -2
- data/app/models/workarea/insights/most_active_reviewers.rb +34 -0
- data/app/models/workarea/insights/most_reviewed_products.rb +33 -0
- data/app/models/workarea/insights/top_rated_products.rb +33 -0
- data/app/models/workarea/review.rb +5 -16
- data/app/queries/workarea/reports/reviews_by_product.rb +86 -0
- data/app/queries/workarea/reports/reviews_by_user.rb +71 -0
- data/app/seeds/{reviews.rb → workarea/review_seeds.rb} +17 -6
- data/app/view_models/workarea/admin/dashboards/reports_view_model.decorator +11 -0
- data/app/view_models/workarea/admin/reports/reviews_by_product_view_model.rb +27 -0
- data/app/view_models/workarea/storefront/review_view_model.rb +9 -3
- data/app/views/workarea/admin/dashboards/_reviews_by_product_card.html.haml +17 -0
- data/app/views/workarea/admin/insights/_most_active_reviewers.html.haml +22 -0
- data/app/views/workarea/admin/insights/_most_reviewed_products.html.haml +22 -0
- data/app/views/workarea/admin/insights/_top_rated_products.html.haml +22 -0
- data/app/views/workarea/admin/reports/reviews_by_product.html.haml +44 -0
- data/app/views/workarea/storefront/products/_rating.html.haml +1 -2
- data/app/views/workarea/storefront/products/_reviews.html.haml +5 -5
- data/app/views/workarea/storefront/products/_reviews_aggregate.html.haml +2 -2
- data/app/views/workarea/storefront/review_mailer/review_request.html.haml +2 -0
- data/app/views/workarea/storefront/review_requests/show.html.haml +2 -1
- data/config/initializers/append_points.rb +5 -0
- data/config/initializers/configuration.rb +3 -1
- data/config/initializers/scheduled_jobs.rb +8 -6
- data/config/locales/en.yml +46 -0
- data/config/routes.rb +4 -0
- data/lib/workarea/reviews/engine.rb +4 -0
- data/lib/workarea/reviews/version.rb +1 -1
- data/package.json +9 -0
- data/test/dummy/config/initializers/session_store.rb +3 -1
- data/test/helpers/workarea/storefront/reviews_helper_test.rb +1 -1
- data/test/integration/workarea/admin/reviews_integration_test.rb +5 -1
- data/test/models/workarea/insights/most_active_reviewers_test.rb +32 -0
- data/test/models/workarea/insights/most_reviewed_products_test.rb +32 -0
- data/test/models/workarea/insights/top_rated_products_test.rb +49 -0
- data/test/models/workarea/review_test.rb +29 -18
- data/test/queries/workarea/reports/reviews_by_product_test.rb +104 -0
- data/test/queries/workarea/reports/reviews_by_user_test.rb +102 -0
- data/test/system/workarea/admin/reviews_by_product_system_test.rb +60 -0
- data/test/view_models/workarea/storefront/review_view_model_test.rb +46 -0
- data/workarea-reviews.gemspec +1 -1
- data/yarn.lock +3265 -0
- metadata +33 -8
- data/app/controllers/workarea/admin/import_reviews_controller.rb +0 -35
- data/test/helpers/workarea/admin/reviews_helper_test.rb +0 -17
    
        data/config/routes.rb
    CHANGED
    
    
| @@ -5,7 +5,11 @@ module Workarea | |
| 5 5 | 
             
                  isolate_namespace Workarea::Reviews
         | 
| 6 6 |  | 
| 7 7 | 
             
                  config.to_prepare do
         | 
| 8 | 
            +
                    Admin::ApplicationController.helper(Admin::ReviewsHelper)
         | 
| 8 9 | 
             
                    Storefront::ApplicationController.helper(Storefront::ReviewsHelper)
         | 
| 10 | 
            +
                    Workarea::Storefront::ApplicationController.helper(
         | 
| 11 | 
            +
                      Workarea::Storefront::ReviewsSchemaOrgHelper
         | 
| 12 | 
            +
                    )
         | 
| 9 13 | 
             
                  end
         | 
| 10 14 | 
             
                end
         | 
| 11 15 | 
             
              end
         | 
    
        data/package.json
    ADDED
    
    
| @@ -4,7 +4,7 @@ module Workarea | |
| 4 4 | 
             
              module Storefront
         | 
| 5 5 | 
             
                class ReviewsHelperTest < ViewTest
         | 
| 6 6 | 
             
                  def test_rating_stars_displays_correct_rating
         | 
| 7 | 
            -
                    assert_match(/1 | 
| 7 | 
            +
                    assert_match(/1(?:\.0)? out of 5 stars/, rating_stars(1))
         | 
| 8 8 | 
             
                    assert_match(/2\.5 out of 5 stars/, rating_stars(2.5))
         | 
| 9 9 | 
             
                    assert_match(/4\.25 out of 5 stars/, rating_stars(4.251))
         | 
| 10 10 | 
             
                  end
         | 
| @@ -6,7 +6,11 @@ module Workarea | |
| 6 6 | 
             
                  include Admin::IntegrationTest
         | 
| 7 7 |  | 
| 8 8 | 
             
                  def test_updating_a_review
         | 
| 9 | 
            -
                    review = create_review( | 
| 9 | 
            +
                    review = create_review(
         | 
| 10 | 
            +
                      product_id: product.id,
         | 
| 11 | 
            +
                      rating: 4,
         | 
| 12 | 
            +
                      approved: false
         | 
| 13 | 
            +
                    )
         | 
| 10 14 |  | 
| 11 15 | 
             
                    put admin.review_path(review), params: {
         | 
| 12 16 | 
             
                      review: { body: 'foo bar', approved: true }
         | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Workarea
         | 
| 4 | 
            +
              module Insights
         | 
| 5 | 
            +
                class MostActiveReviewersTest < TestCase
         | 
| 6 | 
            +
                  setup :add_data, :time_travel
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def add_data
         | 
| 9 | 
            +
                    travel_to Time.zone.local(2018, 10, 27)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    5.times { create_review(email: 'foo@workarea.com', approved: true, verified: true) }
         | 
| 12 | 
            +
                    7.times { create_review(email: 'bar@workarea.com', approved: true) }
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  def time_travel
         | 
| 16 | 
            +
                    travel_to Time.zone.local(2018, 11, 1)
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  def test_generate_monthly!
         | 
| 20 | 
            +
                    MostActiveReviewers.generate_monthly!
         | 
| 21 | 
            +
                    assert_equal(1, MostActiveReviewers.count)
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                    reviewers = MostActiveReviewers.first
         | 
| 24 | 
            +
                    assert_equal(2, reviewers.results.size)
         | 
| 25 | 
            +
                    assert_equal('foo@workarea.com', reviewers.results.first['email'])
         | 
| 26 | 
            +
                    assert_in_delta(5, reviewers.results.first['reviews'])
         | 
| 27 | 
            +
                    assert_equal('bar@workarea.com', reviewers.results.second['email'])
         | 
| 28 | 
            +
                    assert_in_delta(7, reviewers.results.second['reviews'])
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
            end
         | 
| @@ -0,0 +1,32 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Workarea
         | 
| 4 | 
            +
              module Insights
         | 
| 5 | 
            +
                class MostReviewedProductsTest < TestCase
         | 
| 6 | 
            +
                  setup :add_data, :time_travel
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def add_data
         | 
| 9 | 
            +
                    travel_to Time.zone.local(2018, 10, 27)
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    6.times { create_review(product_id: 'foo', approved: true) }
         | 
| 12 | 
            +
                    12.times { create_review(product_id: 'bar', approved: true) }
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  def time_travel
         | 
| 16 | 
            +
                    travel_to Time.zone.local(2018, 11, 1)
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  def test_generate_monthly!
         | 
| 20 | 
            +
                    MostReviewedProducts.generate_monthly!
         | 
| 21 | 
            +
                    assert_equal(1, MostReviewedProducts.count)
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                    reviewed_products = MostReviewedProducts.first
         | 
| 24 | 
            +
                    assert_equal(2, reviewed_products.results.size)
         | 
| 25 | 
            +
                    assert_equal('bar', reviewed_products.results.first['product_id'])
         | 
| 26 | 
            +
                    assert_in_delta(12, reviewed_products.results.first['reviews'])
         | 
| 27 | 
            +
                    assert_equal('foo', reviewed_products.results.second['product_id'])
         | 
| 28 | 
            +
                    assert_in_delta(6, reviewed_products.results.second['reviews'])
         | 
| 29 | 
            +
                  end
         | 
| 30 | 
            +
                end
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
            end
         | 
| @@ -0,0 +1,49 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Workarea
         | 
| 4 | 
            +
              module Insights
         | 
| 5 | 
            +
                class TopRatedProductsTest < TestCase
         | 
| 6 | 
            +
                  setup :add_data, :time_travel
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def add_data
         | 
| 9 | 
            +
                    create_review(product_id: 'foo', rating: 3, created_at: Time.zone.local(2018, 10, 27))
         | 
| 10 | 
            +
                    create_review(product_id: 'foo', rating: 3, created_at: Time.zone.local(2018, 10, 28))
         | 
| 11 | 
            +
                    create_review(product_id: 'foo', rating: 3, created_at: Time.zone.local(2018, 10, 28))
         | 
| 12 | 
            +
                    create_review(product_id: 'foo', rating: 5, created_at: Time.zone.local(2018, 10, 29))
         | 
| 13 | 
            +
                    create_review(product_id: 'foo', rating: 3, created_at: Time.zone.local(2018, 10, 29))
         | 
| 14 | 
            +
                    create_review(product_id: 'foo', rating: 2, created_at: Time.zone.local(2018, 10, 29))
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    create_review(product_id: 'bar', rating: 3, created_at: Time.zone.local(2018, 10, 27))
         | 
| 17 | 
            +
                    create_review(product_id: 'bar', rating: 4, created_at: Time.zone.local(2018, 10, 27))
         | 
| 18 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 27))
         | 
| 19 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 28))
         | 
| 20 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 28))
         | 
| 21 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 28))
         | 
| 22 | 
            +
                    create_review(product_id: 'bar', rating: 4, created_at: Time.zone.local(2018, 10, 28))
         | 
| 23 | 
            +
                    create_review(product_id: 'bar', rating: 4, created_at: Time.zone.local(2018, 10, 29))
         | 
| 24 | 
            +
                    create_review(product_id: 'bar', rating: 4, created_at: Time.zone.local(2018, 10, 29))
         | 
| 25 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 29))
         | 
| 26 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 29))
         | 
| 27 | 
            +
                    create_review(product_id: 'bar', rating: 3, created_at: Time.zone.local(2018, 10, 29))
         | 
| 28 | 
            +
                  end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                  def time_travel
         | 
| 31 | 
            +
                    travel_to Time.zone.local(2018, 11, 1)
         | 
| 32 | 
            +
                  end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                  def test_generate_monthly!
         | 
| 35 | 
            +
                    TopRatedProducts.generate_monthly!
         | 
| 36 | 
            +
                    assert_equal(1, TopRatedProducts.count)
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                    rated_products = TopRatedProducts.first
         | 
| 39 | 
            +
                    assert_equal(2, rated_products.results.size)
         | 
| 40 | 
            +
                    assert_equal('bar', rated_products.results.first['product_id'])
         | 
| 41 | 
            +
                    assert_equal(4.33, rated_products.results.first['average_rating'].round(2))
         | 
| 42 | 
            +
                    assert_equal(3.73, rated_products.results.first['weighted_average_rating'].round(2))
         | 
| 43 | 
            +
                    assert_equal('foo', rated_products.results.second['product_id'])
         | 
| 44 | 
            +
                    assert_equal(3.17, rated_products.results.second['average_rating'].round(2))
         | 
| 45 | 
            +
                    assert_equal(3.06, rated_products.results.second['weighted_average_rating'].round(2))
         | 
| 46 | 
            +
                  end
         | 
| 47 | 
            +
                end
         | 
| 48 | 
            +
              end
         | 
| 49 | 
            +
            end
         | 
| @@ -2,6 +2,26 @@ require 'test_helper' | |
| 2 2 |  | 
| 3 3 | 
             
            module Workarea
         | 
| 4 4 | 
             
              class ReviewTest < Workarea::TestCase
         | 
| 5 | 
            +
                def product_id
         | 
| 6 | 
            +
                  'PROD1'
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def approved
         | 
| 10 | 
            +
                  @approved ||= create_review(
         | 
| 11 | 
            +
                    product_id: product_id,
         | 
| 12 | 
            +
                    rating: 4,
         | 
| 13 | 
            +
                    approved: true
         | 
| 14 | 
            +
                  )
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def unapproved
         | 
| 18 | 
            +
                  @unapproved ||= create_review(
         | 
| 19 | 
            +
                    product_id: product_id,
         | 
| 20 | 
            +
                    rating: 4,
         | 
| 21 | 
            +
                    approved: false
         | 
| 22 | 
            +
                  )
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
             | 
| 5 25 | 
             
                def test_find_for_product_returns_approved_reviews
         | 
| 6 26 | 
             
                  assert([approved], Review.find_for_product(product_id))
         | 
| 7 27 | 
             
                end
         | 
| @@ -69,26 +89,17 @@ module Workarea | |
| 69 89 | 
             
                  assert_equal(review.body.truncate(50), review.title)
         | 
| 70 90 | 
             
                end
         | 
| 71 91 |  | 
| 72 | 
            -
                 | 
| 73 | 
            -
             | 
| 74 | 
            -
             | 
| 75 | 
            -
                  ' | 
| 76 | 
            -
             | 
| 92 | 
            +
                def test_find_sorting_score
         | 
| 93 | 
            +
                  create_review(product_id: 'foo', rating: 4, approved: true)
         | 
| 94 | 
            +
                  create_review(product_id: 'foo', rating: 5, approved: true)
         | 
| 95 | 
            +
                  create_review(product_id: 'foo', rating: 5, approved: true)
         | 
| 96 | 
            +
                  assert_equal(3.38, Review.find_sorting_score('foo').round(2))
         | 
| 77 97 |  | 
| 78 | 
            -
             | 
| 79 | 
            -
                   | 
| 80 | 
            -
                    product_id: product_id,
         | 
| 81 | 
            -
                    rating: 4,
         | 
| 82 | 
            -
                    approved: true
         | 
| 83 | 
            -
                  )
         | 
| 84 | 
            -
                end
         | 
| 98 | 
            +
                  create_review(product_id: 'foo', rating: 1, approved: false)
         | 
| 99 | 
            +
                  assert_equal(3.38, Review.find_sorting_score('foo').round(2))
         | 
| 85 100 |  | 
| 86 | 
            -
             | 
| 87 | 
            -
                   | 
| 88 | 
            -
                    product_id: product_id,
         | 
| 89 | 
            -
                    rating: 4,
         | 
| 90 | 
            -
                    approved: false
         | 
| 91 | 
            -
                  )
         | 
| 101 | 
            +
                  create_review(product_id: 'foo', rating: 1, approved: true)
         | 
| 102 | 
            +
                  assert_equal(3.21, Review.find_sorting_score('foo').round(2))
         | 
| 92 103 | 
             
                end
         | 
| 93 104 | 
             
              end
         | 
| 94 105 | 
             
            end
         | 
| @@ -0,0 +1,104 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Workarea
         | 
| 4 | 
            +
              module Reports
         | 
| 5 | 
            +
                class ReviewsByProductTest < TestCase
         | 
| 6 | 
            +
                  setup :add_data, :time_travel
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def add_data
         | 
| 9 | 
            +
                    create_review(product_id: 'foo', rating: 4, created_at: Time.zone.local(2018, 10, 27), approved: true, verified: true)
         | 
| 10 | 
            +
                    create_review(product_id: 'foo', rating: 3, created_at: Time.zone.local(2018, 10, 28), approved: true, verified: true)
         | 
| 11 | 
            +
                    create_review(product_id: 'foo', rating: 4, created_at: Time.zone.local(2018, 10, 28), approved: true)
         | 
| 12 | 
            +
                    create_review(product_id: 'foo', rating: 4, created_at: Time.zone.local(2018, 10, 29), approved: true)
         | 
| 13 | 
            +
                    create_review(product_id: 'foo', rating: 3, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 14 | 
            +
                    create_review(product_id: 'foo', rating: 2, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    create_review(product_id: 'bar', rating: 3, created_at: Time.zone.local(2018, 10, 27), approved: true, verified: true)
         | 
| 17 | 
            +
                    create_review(product_id: 'bar', rating: 4, created_at: Time.zone.local(2018, 10, 28), approved: true)
         | 
| 18 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 28), approved: false)
         | 
| 19 | 
            +
                    create_review(product_id: 'bar', rating: 5, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 20 | 
            +
                    create_review(product_id: 'bar', rating: 3, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                  def time_travel
         | 
| 24 | 
            +
                    travel_to Time.zone.local(2018, 10, 30)
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  def test_grouping_and_summing
         | 
| 28 | 
            +
                    report = ReviewsByProduct.new
         | 
| 29 | 
            +
                    assert_equal(2, report.results.length)
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 32 | 
            +
                    assert_equal(6, foo['reviews'])
         | 
| 33 | 
            +
                    assert_equal(2, foo['verified'])
         | 
| 34 | 
            +
                    assert_equal(3.33, foo['average_rating'].round(2))
         | 
| 35 | 
            +
                    assert_equal(3.13, foo['weighted_average_rating'].round(2))
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                    bar = report.results.detect { |r| r['_id'] == 'bar' }
         | 
| 38 | 
            +
                    assert_equal(5, bar['reviews'])
         | 
| 39 | 
            +
                    assert_equal(1, bar['verified'])
         | 
| 40 | 
            +
                    assert_equal(4, bar['average_rating'])
         | 
| 41 | 
            +
                    assert_equal(3.33, bar['weighted_average_rating'].round(2))
         | 
| 42 | 
            +
                  end
         | 
| 43 | 
            +
             | 
| 44 | 
            +
                  def test_date_ranges
         | 
| 45 | 
            +
                    report = ReviewsByProduct.new
         | 
| 46 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 47 | 
            +
                    assert_equal(6, foo['reviews'])
         | 
| 48 | 
            +
             | 
| 49 | 
            +
                    report = ReviewsByProduct.new(starts_at: '2018-10-28', ends_at: '2018-10-28')
         | 
| 50 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 51 | 
            +
                    assert_equal(2, foo['reviews'])
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                    report = ReviewsByProduct.new(starts_at: '2018-10-28', ends_at: '2018-10-29')
         | 
| 54 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 55 | 
            +
                    assert_equal(5, foo['reviews'])
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                    report = ReviewsByProduct.new(starts_at: '2018-10-28')
         | 
| 58 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 59 | 
            +
                    assert_equal(5, foo['reviews'])
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                    report = ReviewsByProduct.new(ends_at: '2018-10-28')
         | 
| 62 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 63 | 
            +
                    assert_equal(3, foo['reviews'])
         | 
| 64 | 
            +
                  end
         | 
| 65 | 
            +
             | 
| 66 | 
            +
                  def test_sorting
         | 
| 67 | 
            +
                    report = ReviewsByProduct.new(sort_by: 'average_rating', sort_direction: 'asc')
         | 
| 68 | 
            +
                    assert_equal('foo', report.results.first['_id'])
         | 
| 69 | 
            +
             | 
| 70 | 
            +
                    report = ReviewsByProduct.new(sort_by: 'average_rating', sort_direction: 'desc')
         | 
| 71 | 
            +
                    assert_equal('bar', report.results.first['_id'])
         | 
| 72 | 
            +
             | 
| 73 | 
            +
                    report = ReviewsByProduct.new(sort_by: 'reviews', sort_direction: 'desc')
         | 
| 74 | 
            +
                    assert_equal('foo', report.results.first['_id'])
         | 
| 75 | 
            +
                  end
         | 
| 76 | 
            +
             | 
| 77 | 
            +
                  def test_filtering
         | 
| 78 | 
            +
                    report = ReviewsByProduct.new(results_filter: 'unapproved')
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 81 | 
            +
                    assert_equal(2, foo['reviews'])
         | 
| 82 | 
            +
                    assert_equal(2.5, foo['average_rating'].round(2))
         | 
| 83 | 
            +
                    assert_equal(2.92, foo['weighted_average_rating'].round(2))
         | 
| 84 | 
            +
             | 
| 85 | 
            +
                    bar = report.results.detect { |r| r['_id'] == 'bar' }
         | 
| 86 | 
            +
                    assert_equal(3, bar['reviews'])
         | 
| 87 | 
            +
                    assert_equal(4.33, bar['average_rating'].round(2))
         | 
| 88 | 
            +
                    assert_equal(3.31, bar['weighted_average_rating'].round(2))
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                    report = ReviewsByProduct.new(results_filter: 'approved')
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo' }
         | 
| 93 | 
            +
                    assert_equal(4, foo['reviews'])
         | 
| 94 | 
            +
                    assert_equal(3.75, foo['average_rating'].round(2))
         | 
| 95 | 
            +
                    assert_equal(3.21, foo['weighted_average_rating'].round(2))
         | 
| 96 | 
            +
             | 
| 97 | 
            +
                    bar = report.results.detect { |r| r['_id'] == 'bar' }
         | 
| 98 | 
            +
                    assert_equal(2, bar['reviews'])
         | 
| 99 | 
            +
                    assert_equal(3.5, bar['average_rating'])
         | 
| 100 | 
            +
                    assert_equal(3.08, bar['weighted_average_rating'].round(2))
         | 
| 101 | 
            +
                  end
         | 
| 102 | 
            +
                end
         | 
| 103 | 
            +
              end
         | 
| 104 | 
            +
            end
         | 
| @@ -0,0 +1,102 @@ | |
| 1 | 
            +
            require 'test_helper'
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            module Workarea
         | 
| 4 | 
            +
              module Reports
         | 
| 5 | 
            +
                class ReviewsByUserTest < TestCase
         | 
| 6 | 
            +
                  setup :add_data, :time_travel
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  def add_data
         | 
| 9 | 
            +
                    create_review(email: 'foo@workarea.com', user_id: nil, rating: 4, created_at: Time.zone.local(2018, 10, 27), approved: true, verified: true)
         | 
| 10 | 
            +
                    create_review(email: 'foo@workarea.com', user_id: '123', rating: 3, created_at: Time.zone.local(2018, 10, 28), approved: true, verified: true)
         | 
| 11 | 
            +
                    create_review(email: 'foo@workarea.com', user_id: '123', rating: 4, created_at: Time.zone.local(2018, 10, 28), approved: true)
         | 
| 12 | 
            +
                    create_review(email: 'foo@workarea.com', user_id: nil, rating: 4, created_at: Time.zone.local(2018, 10, 29), approved: true)
         | 
| 13 | 
            +
                    create_review(email: 'foo@workarea.com', user_id: nil, rating: 3, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 14 | 
            +
                    create_review(email: 'foo@workarea.com', user_id: nil, rating: 2, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    create_review(email: 'bar@workarea.com', user_id: nil, rating: 3, created_at: Time.zone.local(2018, 10, 27), approved: true, verified: true)
         | 
| 17 | 
            +
                    create_review(email: 'bar@workarea.com', user_id: '456', rating: 4, created_at: Time.zone.local(2018, 10, 28), approved: true)
         | 
| 18 | 
            +
                    create_review(email: 'bar@workarea.com', user_id: nil, rating: 5, created_at: Time.zone.local(2018, 10, 28), approved: false)
         | 
| 19 | 
            +
                    create_review(email: 'bar@workarea.com', user_id: nil, rating: 5, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 20 | 
            +
                    create_review(email: 'bar@workarea.com', user_id: nil, rating: 3, created_at: Time.zone.local(2018, 10, 29), approved: false)
         | 
| 21 | 
            +
                  end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                  def time_travel
         | 
| 24 | 
            +
                    travel_to Time.zone.local(2018, 10, 30)
         | 
| 25 | 
            +
                  end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
                  def test_grouping_and_summing
         | 
| 28 | 
            +
                    report = ReviewsByUser.new
         | 
| 29 | 
            +
                    assert_equal(2, report.results.length)
         | 
| 30 | 
            +
             | 
| 31 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 32 | 
            +
                    assert_equal(6, foo['reviews'])
         | 
| 33 | 
            +
                    assert_equal(2, foo['verified'])
         | 
| 34 | 
            +
                    assert_equal(3.33, foo['average_rating'].round(2))
         | 
| 35 | 
            +
                    assert_equal('123', foo['user_id'])
         | 
| 36 | 
            +
                    assert_equal(7.5, foo['activity_score'])
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                    bar = report.results.detect { |r| r['_id'] == 'bar@workarea.com' }
         | 
| 39 | 
            +
                    assert_equal(5, bar['reviews'])
         | 
| 40 | 
            +
                    assert_equal(1, bar['verified'])
         | 
| 41 | 
            +
                    assert_equal(4, bar['average_rating'])
         | 
| 42 | 
            +
                    assert_equal('456', bar['user_id'])
         | 
| 43 | 
            +
                    assert_equal(5.75, bar['activity_score'])
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
             | 
| 46 | 
            +
                  def test_date_ranges
         | 
| 47 | 
            +
                    report = ReviewsByUser.new
         | 
| 48 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 49 | 
            +
                    assert_equal(6, foo['reviews'])
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                    report = ReviewsByUser.new(starts_at: '2018-10-28', ends_at: '2018-10-28')
         | 
| 52 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 53 | 
            +
                    assert_equal(2, foo['reviews'])
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                    report = ReviewsByUser.new(starts_at: '2018-10-28', ends_at: '2018-10-29')
         | 
| 56 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 57 | 
            +
                    assert_equal(5, foo['reviews'])
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                    report = ReviewsByUser.new(starts_at: '2018-10-28')
         | 
| 60 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 61 | 
            +
                    assert_equal(5, foo['reviews'])
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                    report = ReviewsByUser.new(ends_at: '2018-10-28')
         | 
| 64 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 65 | 
            +
                    assert_equal(3, foo['reviews'])
         | 
| 66 | 
            +
                  end
         | 
| 67 | 
            +
             | 
| 68 | 
            +
                  def test_sorting
         | 
| 69 | 
            +
                    report = ReviewsByUser.new(sort_by: 'average_rating', sort_direction: 'asc')
         | 
| 70 | 
            +
                    assert_equal('foo@workarea.com', report.results.first['_id'])
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                    report = ReviewsByUser.new(sort_by: 'average_rating', sort_direction: 'desc')
         | 
| 73 | 
            +
                    assert_equal('bar@workarea.com', report.results.first['_id'])
         | 
| 74 | 
            +
             | 
| 75 | 
            +
                    report = ReviewsByUser.new(sort_by: 'reviews', sort_direction: 'desc')
         | 
| 76 | 
            +
                    assert_equal('foo@workarea.com', report.results.first['_id'])
         | 
| 77 | 
            +
                  end
         | 
| 78 | 
            +
             | 
| 79 | 
            +
                  def test_filtering
         | 
| 80 | 
            +
                    report = ReviewsByUser.new(results_filter: 'unapproved')
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 83 | 
            +
                    assert_equal(2, foo['reviews'])
         | 
| 84 | 
            +
                    assert_equal(2.5, foo['average_rating'].round(2))
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                    bar = report.results.detect { |r| r['_id'] == 'bar@workarea.com' }
         | 
| 87 | 
            +
                    assert_equal(3, bar['reviews'])
         | 
| 88 | 
            +
                    assert_equal(4.33, bar['average_rating'].round(2))
         | 
| 89 | 
            +
             | 
| 90 | 
            +
                    report = ReviewsByUser.new(results_filter: 'approved')
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                    foo = report.results.detect { |r| r['_id'] == 'foo@workarea.com' }
         | 
| 93 | 
            +
                    assert_equal(4, foo['reviews'])
         | 
| 94 | 
            +
                    assert_equal(3.75, foo['average_rating'].round(2))
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                    bar = report.results.detect { |r| r['_id'] == 'bar@workarea.com' }
         | 
| 97 | 
            +
                    assert_equal(2, bar['reviews'])
         | 
| 98 | 
            +
                    assert_equal(3.5, bar['average_rating'])
         | 
| 99 | 
            +
                  end
         | 
| 100 | 
            +
                end
         | 
| 101 | 
            +
              end
         | 
| 102 | 
            +
            end
         |