From 2dd05873932de71289e24b6b4038038759172876 Mon Sep 17 00:00:00 2001 From: Aldarien Date: Wed, 11 Sep 2024 22:41:17 -0300 Subject: [PATCH] Pruebas --- Dockerfile | 2 +- Gemfile | 4 +- Gemfile.lock | 13 +----- app/controllers/ruta_controller.rb | 19 ++++++++ app/models/entrega.rb | 1 + app/models/retiro.rb | 1 + app/models/ruta.rb | 2 + app/models/viaje.rb | 3 ++ config/database.yml | 49 ++++++++++++++------ config/routes.rb | 2 + db/migrate/20240911231722_create_ruta.rb | 1 - db/migrate/20240911231807_create_viajes.rb | 5 +- db/migrate/20240911233401_create_entregas.rb | 3 +- db/migrate/20240911233430_create_retiros.rb | 3 +- db/schema.rb | 40 ++++++++++++++++ test/controllers/ruta_controller_test.rb | 13 ++++++ test/models/ruta_test.rb | 8 ++++ 17 files changed, 131 insertions(+), 38 deletions(-) create mode 100644 app/controllers/ruta_controller.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/ruta_controller_test.rb diff --git a/Dockerfile b/Dockerfile index 6010d3f..7658900 100644 --- a/Dockerfile +++ b/Dockerfile @@ -27,7 +27,7 @@ FROM base AS build # Install packages needed to build gems RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y build-essential git pkg-config && \ + apt-get install --no-install-recommends -y build-essential default-libmysqlclient-dev git pkg-config && \ rm -rf /var/lib/apt/lists /var/cache/apt/archives # Install application gems diff --git a/Gemfile b/Gemfile index db688ce..bce1304 100644 --- a/Gemfile +++ b/Gemfile @@ -2,8 +2,8 @@ source "https://rubygems.org" # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails", "~> 7.2.1" -# Use sqlite3 as the database for Active Record -gem "sqlite3", ">= 1.4" +# Use mysql2 as the database for Active Record +gem "mysql2", "~> 0.5" # Use the Puma web server [https://github.com/puma/puma] gem "puma", ">= 5.0" # Build JSON APIs with ease [https://github.com/rails/jbuilder] diff --git a/Gemfile.lock b/Gemfile.lock index 031dcae..9c0146e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -111,6 +111,7 @@ GEM mini_mime (1.1.5) minitest (5.25.1) msgpack (1.7.2) + mysql2 (0.5.6) net-imap (0.4.16) date net-protocol @@ -216,16 +217,6 @@ GEM rubocop-rails ruby-progressbar (1.13.0) securerandom (0.3.1) - sqlite3 (2.0.4-aarch64-linux-gnu) - sqlite3 (2.0.4-aarch64-linux-musl) - sqlite3 (2.0.4-arm-linux-gnu) - sqlite3 (2.0.4-arm-linux-musl) - sqlite3 (2.0.4-arm64-darwin) - sqlite3 (2.0.4-x86-linux-gnu) - sqlite3 (2.0.4-x86-linux-musl) - sqlite3 (2.0.4-x86_64-darwin) - sqlite3 (2.0.4-x86_64-linux-gnu) - sqlite3 (2.0.4-x86_64-linux-musl) stringio (3.1.1) thor (1.3.2) timeout (0.4.1) @@ -259,10 +250,10 @@ DEPENDENCIES bootsnap brakeman debug + mysql2 (~> 0.5) puma (>= 5.0) rails (~> 7.2.1) rubocop-rails-omakase - sqlite3 (>= 1.4) tzinfo-data BUNDLED WITH diff --git a/app/controllers/ruta_controller.rb b/app/controllers/ruta_controller.rb new file mode 100644 index 0000000..7ddd037 --- /dev/null +++ b/app/controllers/ruta_controller.rb @@ -0,0 +1,19 @@ +class RutaController < ApplicationController + def get + Ruta.find(params[:ruta]) + end + + def add + @ruta = Ruta.create + for data_viaje in params[:viajes] do + viaje = @ruta.viajes.create(direccion_partida: data_viaje[:direccion_partida], direccion_llegada: data_viaje[:direccion_llegada], kms: data_viaje[:kms]) + for data_entrega in data_viaje[:entregas] do + viaje.entregas.create(nombre_recibe: data_entrega[:nombre_recibe], carga: data_entrega[:carga]) + end + for data_retiro in data_viaje[:retiros] do + viaje.retiros.create(nombre_entrega: data_retiro[:nombre_recibe], carga: data_retiro[:carga]) + end + end + @ruta + end +end diff --git a/app/models/entrega.rb b/app/models/entrega.rb index 071d9a9..26fc844 100644 --- a/app/models/entrega.rb +++ b/app/models/entrega.rb @@ -1,2 +1,3 @@ class Entrega < ApplicationRecord + belongs_to :viaje end diff --git a/app/models/retiro.rb b/app/models/retiro.rb index 3d196d5..4137a6f 100644 --- a/app/models/retiro.rb +++ b/app/models/retiro.rb @@ -1,2 +1,3 @@ class Retiro < ApplicationRecord + belongs_to :viaje end diff --git a/app/models/ruta.rb b/app/models/ruta.rb index 37df4e7..6e7a611 100644 --- a/app/models/ruta.rb +++ b/app/models/ruta.rb @@ -1,4 +1,6 @@ class Ruta < ApplicationRecord + has_many :viajes + def direccion_inicio @viajes.first.direccion_partida end diff --git a/app/models/viaje.rb b/app/models/viaje.rb index 62e6669..00843ce 100644 --- a/app/models/viaje.rb +++ b/app/models/viaje.rb @@ -1,2 +1,5 @@ class Viaje < ApplicationRecord + belongs_to :ruta + has_many :entregas + has_many :retiros end diff --git a/config/database.yml b/config/database.yml index 01bebb5..1503396 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,32 +1,51 @@ -# SQLite. Versions 3.8.0 and up are supported. -# gem install sqlite3 +# MySQL. Versions 5.5.8 and up are supported. # -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem "sqlite3" +# Install the MySQL driver +# gem install mysql2 +# +# Ensure the MySQL gem is defined in your Gemfile +# gem "mysql2" +# +# And be sure to use new-style password hashing: +# https://dev.mysql.com/doc/refman/5.7/en/password-hashing.html # default: &default - adapter: sqlite3 + adapter: mysql2 + encoding: utf8mb4 pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - timeout: 5000 + username: <%= ENV.fetch("DB_USER") {"root"} %> + password: <%= ENV.fetch("DB_PASSWORD") {""} %> + database: <%= ENV.fetch("DB_NAME") %> + host: <%= ENV.fetch("DB_HOST") { "localhost" } %> development: <<: *default - database: storage/development.sqlite3 # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: storage/test.sqlite3 - -# SQLite3 write its data on the local filesystem, as such it requires -# persistent disks. If you are deploying to a managed service, you should -# make sure it provides disk persistence, as many don't. +# As with config/credentials.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password or a full connection URL as an environment +# variable when you boot the app. For example: +# +# DATABASE_URL="mysql2://myuser:mypass@localhost/somedatabase" +# +# If the connection URL is provided in the special DATABASE_URL environment +# variable, Rails will automatically merge its configuration values on top of +# the values provided in this file. Alternatively, you can specify a connection +# URL environment variable explicitly: +# +# production: +# url: <%= ENV["MY_APP_DATABASE_URL"] %> +# +# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full overview on how database connection configuration can be specified. # -# Similarly, if you deploy your application as a Docker container, you must -# ensure the database is located in a persisted volume. production: <<: *default - # database: path/to/persistent/storage/production.sqlite3 diff --git a/config/routes.rb b/config/routes.rb index a125ef0..65daa13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + get "/ruta", to: 'ruta#get' + post "/ruta", to: 'ruta#add' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. diff --git a/db/migrate/20240911231722_create_ruta.rb b/db/migrate/20240911231722_create_ruta.rb index a4cc96f..12c478d 100644 --- a/db/migrate/20240911231722_create_ruta.rb +++ b/db/migrate/20240911231722_create_ruta.rb @@ -1,7 +1,6 @@ class CreateRuta < ActiveRecord::Migration[7.2] def change create_table :ruta do |t| - t.has_many :viajes t.timestamps end end diff --git a/db/migrate/20240911231807_create_viajes.rb b/db/migrate/20240911231807_create_viajes.rb index 94a22a4..64ed57e 100644 --- a/db/migrate/20240911231807_create_viajes.rb +++ b/db/migrate/20240911231807_create_viajes.rb @@ -1,12 +1,9 @@ class CreateViajes < ActiveRecord::Migration[7.2] def change create_table :viajes do |t| - t.belongs_to :ruta t.string :direccion_partida t.string :direccion_llegada - t.int :kms - t.has_many :entregas - t.has_many :retiros + t.integer :kms t.timestamps end end diff --git a/db/migrate/20240911233401_create_entregas.rb b/db/migrate/20240911233401_create_entregas.rb index f96bf7a..d75cb80 100644 --- a/db/migrate/20240911233401_create_entregas.rb +++ b/db/migrate/20240911233401_create_entregas.rb @@ -1,9 +1,8 @@ class CreateEntregas < ActiveRecord::Migration[7.2] def change create_table :entregas do |t| - t.belongs_to :viaje t.string :nombre_recibe # Quien recibe - t.string :carga # Detalles de la carga, serializado + t.text :carga # Detalles de la carga, serializado t.timestamps end end diff --git a/db/migrate/20240911233430_create_retiros.rb b/db/migrate/20240911233430_create_retiros.rb index 15f1b38..e728779 100644 --- a/db/migrate/20240911233430_create_retiros.rb +++ b/db/migrate/20240911233430_create_retiros.rb @@ -1,9 +1,8 @@ class CreateRetiros < ActiveRecord::Migration[7.2] def change create_table :retiros do |t| - t.belongs_to :viaje t.string :nombre_entrega # Quien entrega - t.string :carga # Detalles de la carga, serializado + t.text :carga # Detalles de la carga, serializado t.timestamps end end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..c09a67e --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,40 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.2].define(version: 2024_09_11_233430) do + create_table "entregas", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.string "nombre_recibe" + t.text "carga" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "retiros", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.string "nombre_entrega" + t.text "carga" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "ruta", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "viajes", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.string "direccion_partida" + t.string "direccion_llegada" + t.integer "kms" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end +end diff --git a/test/controllers/ruta_controller_test.rb b/test/controllers/ruta_controller_test.rb new file mode 100644 index 0000000..8317e96 --- /dev/null +++ b/test/controllers/ruta_controller_test.rb @@ -0,0 +1,13 @@ +require "test_helper" + +class RutaControllerTest < ActionDispatch::IntegrationTest + test "should get get" do + get ruta_get_url, params: {ruta: 1} + assert_response :success + end + + test "should get add" do + post ruta_add_url, params: {viajes: [{direccion_partida: "Partida", direccion_llegada: "Llegada", kms: 10, entregas: [{nombre_recibe: "Recibe", carga: "{\"cantidad\": 10, \"descripcion\": \"Cajas\"}"}], retiros: [{nombre_entrega: "Entrega", carga: "{\"cantidad\": 2, \"descripcion\": \"Cajas\"}"}]}]} + assert_response :success + end +end diff --git a/test/models/ruta_test.rb b/test/models/ruta_test.rb index 771225c..87e5d82 100644 --- a/test/models/ruta_test.rb +++ b/test/models/ruta_test.rb @@ -4,4 +4,12 @@ class RutaTest < ActiveSupport::TestCase # test "the truth" do # assert true # end + test "create" do + assert Ruta.create + end + + test "get" do + ruta = Ruta.find(1) + assert Ruta.id === 1 + end end