Create sensor_readings table / model

pull/664/head
Rick Carlino 2018-02-15 11:26:31 -06:00
parent 18b29767f7
commit 119689d8ef
6 changed files with 56 additions and 1 deletions

View File

@ -15,6 +15,7 @@ class Device < ApplicationRecord
has_many :tools, dependent: :destroy
has_many :images, dependent: :destroy
has_many :webcam_feeds, dependent: :destroy
has_many :sensor_reading, dependent: :destroy
validates :timezone, inclusion: { in: TIMEZONES,
message: BAD_TZ,
allow_nil: true }

View File

@ -0,0 +1,3 @@
class SensorReading < ApplicationRecord
belongs_to :device
end

View File

@ -0,0 +1,14 @@
class CreateSensorReadings < ActiveRecord::Migration[5.1]
def change
create_table :sensor_readings do |t|
t.references :device, foreign_key: true
t.float :x
t.float :y
t.float :z
t.integer :value
t.integer :pin
t.timestamps
end
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20180215064728) do
ActiveRecord::Schema.define(version: 20180215171709) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -283,6 +283,18 @@ ActiveRecord::Schema.define(version: 20180215064728) do
t.index ["device_id"], name: "index_regimens_on_device_id"
end
create_table "sensor_readings", force: :cascade do |t|
t.bigint "device_id"
t.float "x"
t.float "y"
t.float "z"
t.integer "value"
t.integer "pin"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["device_id"], name: "index_sensor_readings_on_device_id"
end
create_table "sequence_dependencies", id: :serial, force: :cascade do |t|
t.string "dependency_type"
t.integer "dependency_id"
@ -407,6 +419,7 @@ ActiveRecord::Schema.define(version: 20180215064728) do
add_foreign_key "peripherals", "devices"
add_foreign_key "points", "devices"
add_foreign_key "primary_nodes", "sequences"
add_foreign_key "sensor_readings", "devices"
add_foreign_key "sequence_dependencies", "sequences"
add_foreign_key "tool_slots", "tools"
end

View File

@ -0,0 +1,10 @@
FactoryBot.define do
factory :sensor_reading do
device
x { rand(1..500) }
y { rand(1..500) }
z { rand(1..500) }
value { rand(0..1024) }
pin { rand(1..540) }
end
end

View File

@ -0,0 +1,14 @@
require "spec_helper"
RSpec.describe SensorReading, type: :model do
let(:sensor_reading) { FactoryBot.create(:sensor_reading) }
it "Has basic attributes" do
expect(sensor_reading.device).to be_kind_of(Device)
expect(sensor_reading.value).to be_kind_of(Integer)
expect(sensor_reading.pin).to be_kind_of(Integer)
expect(sensor_reading.x).to be_kind_of(Float)
expect(sensor_reading.y).to be_kind_of(Float)
expect(sensor_reading.z).to be_kind_of(Float)
end
end