Add tests for trips controller and change map seach form

This commit is contained in:
Eugene Burmakin 2024-11-28 17:13:04 +01:00
parent 5c09e5b0f9
commit fda09248da
6 changed files with 111 additions and 82 deletions

View file

@ -8,33 +8,35 @@
<div class="w-full sm:w-2/12 md:w-1/12 lg:w-2/12">
<div class="flex flex-col space-y-2">
<%= f.label :start_at, class: "text-sm font-semibold" %>
<%= f.datetime_local_field :start_at, class: "rounded-md w-full", value: @start_at %>
<%= f.datetime_local_field :start_at, class: "input input-bordered hover:cursor-pointer hover:input-primary", value: @start_at %>
</div>
</div>
<div class="w-full sm:w-2/12 md:w-1/12 lg:w-2/12">
<div class="flex flex-col space-y-2">
<%= f.label :end_at, class: "text-sm font-semibold" %>
<%= f.datetime_local_field :end_at, class: "rounded-md w-full", value: @end_at %>
<%= f.datetime_local_field :end_at, class: "input input-bordered hover:cursor-pointer hover:input-primary", value: @end_at %>
</div>
</div>
<div class="w-full sm:w-6/12 md:w-2/12 lg:w-3/12">
<div class="flex flex-col space-y-2">
<%= f.submit "Search", class: "px-4 py-2 bg-blue-500 text-white rounded-md" %>
<%= f.submit "Search", class: "btn btn-primary hover:btn-info" %>
</div>
</div>
<div class="w-full sm:w-6/12 md:w-2/12">
<div class="flex flex-col space-y-2 text-center">
<%= link_to "Yesterday", map_path(start_at: Date.yesterday.beginning_of_day, end_at: Date.yesterday.end_of_day, import_id: params[:import_id]), class: "px-4 py-2 bg-gray-500 text-white rounded-md" %>
<%= link_to "Yesterday",
map_path(start_at: Date.yesterday.beginning_of_day, end_at: Date.yesterday.end_of_day, import_id: params[:import_id]),
class: "btn btn-neutral hover:btn-ghost" %>
</div>
</div>
<div class="w-full sm:w-6/12 md:w-3/12 lg:w-2/12">
<div class="flex flex-col space-y-2 text-center">
<%= link_to "Last 7 days", map_path(start_at: 1.week.ago.beginning_of_day, end_at: Time.current.end_of_day, import_id: params[:import_id]), class: "px-4 py-2 bg-gray-500 text-white rounded-md" %>
<%= link_to "Last 7 days", map_path(start_at: 1.week.ago.beginning_of_day, end_at: Time.current.end_of_day, import_id: params[:import_id]), class: "btn btn-neutral hover:btn-ghost" %>
</div>
</div>
<div class="w-full sm:w-6/12 md:w-3/12 lg:w-2/12">
<div class="flex flex-col space-y-2 text-center">
<%= link_to "Last month", map_path(start_at: 1.month.ago.beginning_of_day, end_at: Time.current.end_of_day, import_id: params[:import_id]), class: "px-4 py-2 bg-gray-500 text-white rounded-md" %>
<%= link_to "Last month", map_path(start_at: 1.month.ago.beginning_of_day, end_at: Time.current.end_of_day, import_id: params[:import_id]), class: "btn btn-neutral hover:btn-ghost" %>
</div>
</div>
</div>

View file

@ -2,7 +2,7 @@
<div id='years-nav'>
<div class="dropdown">
<div tabindex="0" role="button" class="btn m-1">Select year</div>
<div tabindex="0" role="button" class="btn">Select year</div>
<ul tabindex="0" class="dropdown-content z-[1] menu p-2 shadow bg-base-100 rounded-box w-52">
<% current_user.stats.years.each do |year| %>
<li><%= link_to year, map_url(year_timespan(year).merge(year: year, import_id: params[:import_id])) %></li>

View file

@ -1,7 +1,6 @@
<% content_for :title, @trip.name %>
<div class="container mx-auto px-4 py-8 max-w-4xl my-5">
<!-- Header Section -->
<div class="container mx-auto px-4 max-w-4xl my-5">
<div class="text-center mb-8">
<h1 class="text-4xl font-bold mb-2"><%= @trip.name %></h1>
<p class="text-md text-base-content/60">
@ -14,9 +13,8 @@
<% end %>
</div>
<!-- Map and Description Section -->
<div class="bg-base-100 mb-8">
<div class="p-6 grid grid-cols-1 md:grid-cols-2 gap-6">
<div class="bg-base-100 my-8 p-4">
<div class="grid grid-cols-1 md:grid-cols-2 gap-6">
<div class="w-full">
<div
id='map'

View file

@ -1,9 +1,21 @@
# frozen_string_literal: true
FactoryBot.define do
factory :trip do
name { "MyString" }
started_at { "2024-11-27 17:16:21" }
ended_at { "2024-11-27 17:16:21" }
notes { "MyText" }
user { nil }
user
name { FFaker::Lorem.word }
started_at { DateTime.new(2024, 11, 27, 17, 16, 21) }
ended_at { DateTime.new(2024, 11, 29, 17, 16, 21) }
notes { FFaker::Lorem.sentence }
trait :with_points do
after(:build) do |trip|
create_list(
:point, 25,
user: trip.user,
timestamp: trip.started_at + (1..1000).to_a.sample.minutes
)
end
end
end
end

View file

@ -1,80 +1,89 @@
# frozen_string_literal: true
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to test the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
RSpec.describe '/trips', type: :request do
let(:valid_attributes) do
{
name: 'Summer Vacation 2024',
started_at: Date.tomorrow,
ended_at: Date.tomorrow + 7.days,
notes: 'A wonderful week-long trip'
}
end
RSpec.describe "/trips", type: :request do
# This should return the minimal set of attributes required to create a valid
# Trip. As you add validations to Trip, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:invalid_attributes) do
{
name: '', # name can't be blank
start_date: nil, # dates are required
end_date: Date.yesterday # end date can't be before start date
}
end
let(:user) { create(:user) }
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
before do
stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags')
.to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {})
describe "GET /index" do
it "renders a successful response" do
Trip.create! valid_attributes
allow_any_instance_of(Trip).to receive(:photos).and_return([])
sign_in user
end
describe 'GET /index' do
it 'renders a successful response' do
get trips_url
expect(response).to be_successful
end
end
describe "GET /show" do
it "renders a successful response" do
trip = Trip.create! valid_attributes
describe 'GET /show' do
let(:trip) { create(:trip, :with_points, user:) }
it 'renders a successful response' do
get trip_url(trip)
expect(response).to be_successful
end
end
describe "GET /new" do
it "renders a successful response" do
describe 'GET /new' do
it 'renders a successful response' do
get new_trip_url
expect(response).to be_successful
end
end
describe "GET /edit" do
it "renders a successful response" do
trip = Trip.create! valid_attributes
describe 'GET /edit' do
let(:trip) { create(:trip, :with_points, user:) }
it 'renders a successful response' do
get edit_trip_url(trip)
expect(response).to be_successful
end
end
describe "POST /create" do
context "with valid parameters" do
it "creates a new Trip" do
expect {
describe 'POST /create' do
context 'with valid parameters' do
it 'creates a new Trip' do
expect do
post trips_url, params: { trip: valid_attributes }
}.to change(Trip, :count).by(1)
end.to change(Trip, :count).by(1)
end
it "redirects to the created trip" do
it 'redirects to the created trip' do
post trips_url, params: { trip: valid_attributes }
expect(response).to redirect_to(trip_url(Trip.last))
end
end
context "with invalid parameters" do
it "does not create a new Trip" do
expect {
context 'with invalid parameters' do
it 'does not create a new Trip' do
expect do
post trips_url, params: { trip: invalid_attributes }
}.to change(Trip, :count).by(0)
end.to change(Trip, :count).by(0)
end
it "renders a response with 422 status (i.e. to display the 'new' template)" do
@ -84,47 +93,55 @@ RSpec.describe "/trips", type: :request do
end
end
describe "PATCH /update" do
context "with valid parameters" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
describe 'PATCH /update' do
context 'with valid parameters' do
let(:new_attributes) do
{
name: 'Updated Trip Name',
notes: 'Changed trip notes'
}
end
let(:trip) { create(:trip, :with_points, user:) }
it "updates the requested trip" do
trip = Trip.create! valid_attributes
it 'updates the requested trip' do
patch trip_url(trip), params: { trip: new_attributes }
trip.reload
skip("Add assertions for updated state")
expect(trip.name).to eq('Updated Trip Name')
expect(trip.notes.body.to_plain_text).to eq('Changed trip notes')
expect(trip.notes).to be_an(ActionText::RichText)
end
it "redirects to the trip" do
trip = Trip.create! valid_attributes
it 'redirects to the trip' do
patch trip_url(trip), params: { trip: new_attributes }
trip.reload
expect(response).to redirect_to(trip_url(trip))
end
end
context "with invalid parameters" do
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
trip = Trip.create! valid_attributes
context 'with invalid parameters' do
let(:trip) { create(:trip, :with_points, user:) }
it 'renders a response with 422 status' do
patch trip_url(trip), params: { trip: invalid_attributes }
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
describe "DELETE /destroy" do
it "destroys the requested trip" do
trip = Trip.create! valid_attributes
expect {
describe 'DELETE /destroy' do
let!(:trip) { create(:trip, :with_points, user:) }
it 'destroys the requested trip' do
expect do
delete trip_url(trip)
}.to change(Trip, :count).by(-1)
end.to change(Trip, :count).by(-1)
end
it "redirects to the trips list" do
trip = Trip.create! valid_attributes
it 'redirects to the trips list' do
delete trip_url(trip)
expect(response).to redirect_to(trips_url)
end
end

View file

@ -137,8 +137,8 @@ RSpec.describe Immich::RequestPhotos do
).to_return(status: 200, body: immich_data, headers: {})
end
it 'returns only images' do
expect(service.map { _1['type'] }.uniq).to eq(['IMAGE'])
it 'returns images and videos' do
expect(service.map { _1['type'] }.uniq).to eq(['IMAGE', 'VIDEO'])
end
end