Skip to content

Commit

Permalink
chore: Refactor some specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ribose-jeffreylau committed Nov 11, 2024
1 parent 0ebaff5 commit af13f6f
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 33 deletions.
3 changes: 1 addition & 2 deletions lib/paneron/register/raw/data_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def initialize(

@extension = extension
@item_classes = {}
@item_uuids = nil
@metadata = nil
@paneron_metadata = nil
self.data_set_name = new_data_set_name
Expand Down Expand Up @@ -235,7 +234,7 @@ def item_class_names
end

def item_uuids
item_classes.values.map(&:item_uuids).flatten
item_classes.values.map(&:item_uuids).to_set.flatten
end

# TODO: Add validation to register.yaml fields
Expand Down
16 changes: 8 additions & 8 deletions lib/paneron/register/raw/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ class Item
include Validatable
include RootFinder

attr_reader :item_id,
attr_reader :item_uuid,
:extension
attr_accessor :item_class

def initialize(
item_id,
item_uuid,
item_class_path = nil,
extension = "yaml",
item_class: nil
Expand All @@ -30,7 +30,7 @@ def initialize(
item_class
end

@item_id = item_id
@item_uuid = item_uuid
@extension = extension
@to_h = nil
@status = nil
Expand All @@ -39,7 +39,7 @@ def initialize(
end

def item_path
File.join(item_class_path, "#{item_id}.#{extension}")
File.join(item_class_path, "#{item_uuid}.#{extension}")
end

def item_class_path
Expand All @@ -56,8 +56,8 @@ def set(
self.date_accepted = date_accepted
end

def id
@item_id
def uuid
@item_uuid
end

def status
Expand Down Expand Up @@ -137,7 +137,7 @@ def self.validate_path(path)

def to_lutaml
Paneron::Register::Item.new(
id: item_id,
id: item_uuid,
data: data,
status: Paneron::Register::ItemStatus.new(state: status),
date_accepted: date_accepted.to_s,
Expand All @@ -146,7 +146,7 @@ def to_lutaml

def to_h
{
"id" => item_id,
"id" => item_uuid,
"data" => data,
"status" => status,
"dateAccepted" => date_accepted,
Expand Down
32 changes: 21 additions & 11 deletions lib/paneron/register/raw/item_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ def add_items(new_items)
new_items = [new_items] unless new_items.is_a?(Enumerable)
new_items.each do |item|
item.set_item_class(self)
@items[item.id] = item
item_uuids << item.id
@items[item.uuid] = item
end
end

Expand All @@ -129,21 +128,32 @@ def to_lutaml
)
end

def add_item(*new_items)
new_items = [new_items] unless new_items.is_a?(Enumerable)
new_items.each do |item|
item.set_item_class(self)
@items[item.item_uuid] = item
end
end

def spawn_item(item_uuid)
new_item =
@items[item_uuid] =
Paneron::Register::Raw::Item.new(
item_uuid,
item_class: self,
)
new_item = Paneron::Register::Raw::Item.new(
item_uuid,
item_class: self,
)

add_item(new_item)

item_uuids << item_uuid
new_item
end

def item_uuids
@item_uuids ||= Dir.glob(File.join(item_class_path, "*.#{extension}"))
.map { |file| File.basename(file, ".#{extension}") }
if @items.empty?
Dir.glob(File.join(item_class_path, "*.#{extension}"))
.map { |file| File.basename(file, ".#{extension}") }.to_set
else
@items.keys
end
end

def items(uuid = nil)
Expand Down
49 changes: 37 additions & 12 deletions spec/raw_data_set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,45 @@
ds
end

it "creates a new ItemClass object" do
expect do
raw_data_set.spawn_item_class("ic-a")
raw_data_set.spawn_item_class("ic-a")
raw_data_set.spawn_item_class("ic-a")
end.to change { raw_data_set.item_classes.length }.by(1)
describe "when adding the item class multiple times" do
let(:new_item_class_name) do
"ic-a"
end
let(:action) do
proc {
3.times do
raw_data_set.spawn_item_class(new_item_class_name)
end
}
end

it "creates a new ItemClass object" do
expect(&action).to change { raw_data_set.item_classes.length }.by(1)
end

it "creates a new ItemClass object and modifies #item_class_names" do
expect(&action).to change { raw_data_set.item_class_names.length }.by(1)
end
end

it "creates new ItemClass objects" do
expect do
raw_data_set.spawn_item_class("ic-a")
raw_data_set.spawn_item_class("ic-b")
raw_data_set.spawn_item_class("ic-c")
end.to change { raw_data_set.item_classes.length }.by(3)
describe "when adding different item classes" do
let(:new_item_class_names) do
["ic-a", "ic-b", "ic-c"]
end

let(:action) do
proc {
new_item_class_names.each { |ic| raw_data_set.spawn_item_class(ic) }
}
end

it "creates new ItemClass objects" do
expect(&action).to change { raw_data_set.item_classes.length }.by(3)
end

it "creates a new ItemClass object and modifies #item_class_names" do
expect(&action).to change { raw_data_set.item_class_names.length }.by(3)
end
end
end

Expand Down
50 changes: 50 additions & 0 deletions spec/raw_item_class_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# frozen_string_literal: true

RSpec.describe Paneron::Register::Raw::ItemClass do
let(:raw_data_set) do
Paneron::Register::Raw::Register.new(
"spec/fixtures/test-register",
).spawn_data_set("test-reg-a")
end

describe "#path_valid?" do
describe "with an invalid path" do
subject do
Expand Down Expand Up @@ -110,6 +116,50 @@
end

describe "#spawn_item" do
let(:raw_item_class) do
raw_data_set.spawn_item_class("asdfnonexist")
end

describe "when adding the item class multiple times" do
let(:new_item_uuid) do
"000a-1"
end
let(:action) do
proc {
3.times do
raw_item_class.spawn_item(new_item_uuid)
end
}
end

it "creates a new Item object" do
expect(&action).to change { raw_item_class.items.length }.by(1)
end

it "creates a new Item object and modifies #item_uuids" do
expect(&action).to change { raw_item_class.item_uuids.length }.by(1)
end
end

describe "when adding different item classes" do
let(:new_item_uuids) do
["ic-a", "ic-b", "ic-c"]
end

let(:action) do
proc {
new_item_uuids.each { |ic| raw_item_class.spawn_item(ic) }
}
end

it "creates new Item objects" do
expect(&action).to change { raw_item_class.items.length }.by(3)
end

it "creates a new Item object and modifies #item_uuids" do
expect(&action).to change { raw_item_class.item_uuids.length }.by(3)
end
end
it "creates a new Item object" do
expect do
item_class.spawn_item("000a")
Expand Down

0 comments on commit af13f6f

Please sign in to comment.