aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2023-12-18 17:01:29 +0000
committerMatthew Lemon <y@yulqen.org>2023-12-18 17:01:29 +0000
commit7483d6b8652854dbab53337b57467f69c7a2cad4 (patch)
treeb0dac0a2fcc37567298d82e9325b346a8569a416
parentc504e1e907ea68e3b777db5d286974010d2361c0 (diff)
Adds some specs for testing events
Adds a method to Event to check its membership of a month in a particular year.
-rw-r--r--.rspec1
-rw-r--r--app/models/event.rb8
-rw-r--r--spec/models/event_spec.rb32
3 files changed, 41 insertions, 0 deletions
diff --git a/.rspec b/.rspec
index c99d2e7..5be63fc 100644
--- a/.rspec
+++ b/.rspec
@@ -1 +1,2 @@
--require spec_helper
+--format documentation
diff --git a/app/models/event.rb b/app/models/event.rb
index 1f722d3..9dea7b8 100644
--- a/app/models/event.rb
+++ b/app/models/event.rb
@@ -1,4 +1,12 @@
class Event < ApplicationRecord
validates :name, presence: true, length: { maximum: 50 }
validates :date, presence: true
+
+ def month_year_check(year, month)
+ if self.date.year == year && self.date.month == month
+ true
+ else
+ false
+ end
+ end
end
diff --git a/spec/models/event_spec.rb b/spec/models/event_spec.rb
index adf288a..5815ec2 100644
--- a/spec/models/event_spec.rb
+++ b/spec/models/event_spec.rb
@@ -1,4 +1,5 @@
require 'rails_helper'
+require 'date'
RSpec.describe Event, type: :model do
it "is invalid without a name" do
@@ -11,5 +12,36 @@ RSpec.describe Event, type: :model do
e = Event.new(date: nil, name: "Sludge")
expect(e).to_not be_valid
end
+
+ it "is valid with valid attributes" do
+ d = Date.new(2023, 1, 1)
+ e = Event.new(date: d, name: "Slish")
+ expect(e).to be_valid
+ end
+
+ it "is invalid with a name longer than 50 chars" do
+ d = Date.new(2023, 1, 1)
+ str = "a" * 51
+ e = Event.new(date: d, name: str)
+ expect(e).to_not be_valid
+ end
+ end
+
+ describe "events by month" do
+ it "knows it is a january 2024 event" do
+ d = Date.new(2024, 1, 10)
+ e = Event.new(date: d, name: "Nice January event")
+ expect(e.month_year_check(2024, 1) ).to eq(true)
+ end
+ it "knows it is a September 2028 event" do
+ d = Date.new(2028, 9, 10)
+ e = Event.new(date: d, name: "Nice September event")
+ expect(e.month_year_check(2028, 9) ).to eq(true)
+ end
+ it "knows it is not a September 2028 event" do
+ d = Date.new(2028, 10, 10)
+ e = Event.new(date: d, name: "Not a nice September event")
+ expect(e.month_year_check(2028, 9) ).to eq(false)
+ end
end
end