Skip to content

Commit e5fb4f9

Browse files
authored
increasing coverage on transcript model (#181)
* increasing coverage on transcript model * renaming spec description
1 parent a45e825 commit e5fb4f9

File tree

6 files changed

+66
-42
lines changed

6 files changed

+66
-42
lines changed

app/models/institution.rb

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ class Institution < ApplicationRecord
2525

2626
validate :image_size_restriction
2727

28-
HUMANIZED_ATTRIBUTES = {
29-
:slug => "UID"
30-
}
28+
HUMANIZED_ATTRIBUTES = { slug: "UID" }
3129

3230
scope :order_asc, -> { order("LOWER(institutions.name)") }
3331

@@ -63,31 +61,29 @@ def self.state_library_nsw
6361

6462
def self.all_institution_disk_usage
6563
Rails.cache.fetch("Institution:disk_usage:all", expires_in: 48.hours) do
66-
Institution.all.map { |i| i.disk_usage }
67-
.inject({ image: 0, audio: 0, script: 0 }) do |memo, tu|
68-
memo[:image] += tu[:image]
69-
memo[:audio] += tu[:audio]
70-
memo[:script] += tu[:script]
71-
memo
72-
end
64+
Institution.all.map(&:disk_usage).inject({ image: 0, audio: 0, script: 0 }) do |memo, tu|
65+
memo[:image] += tu[:image]
66+
memo[:audio] += tu[:audio]
67+
memo[:script] += tu[:script]
68+
memo
69+
end
7370
end
7471
end
7572

7673
def duration
77-
Rails.cache.fetch("Institution:duration:#{self.id}", expires_in: 23.hours) do
74+
Rails.cache.fetch("Institution:duration:#{id}", expires_in: 23.hours) do
7875
collections.map(&:duration).inject(0) { |memo, duration| memo + duration }
7976
end
8077
end
8178

8279
def disk_usage
83-
Rails.cache.fetch("Institution:disk_usage:#{self.id}", expires_in: 23.hours) do
84-
collections.map(&:disk_usage)
85-
.inject({ image: 0, audio: 0, script: 0 }) do |memo, tu|
86-
memo[:image] += tu[:image]
87-
memo[:audio] += tu[:audio]
88-
memo[:script] += tu[:script]
89-
memo
90-
end
80+
Rails.cache.fetch("Institution:disk_usage:#{id}", expires_in: 23.hours) do
81+
collections.map(&:disk_usage).inject({ image: 0, audio: 0, script: 0 }) do |memo, tu|
82+
memo[:image] += tu[:image]
83+
memo[:audio] += tu[:audio]
84+
memo[:script] += tu[:script]
85+
memo
86+
end
9187
end
9288
end
9389

app/services/azure/speech_to_text_service.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def wav_file
4141
# Convert the file to what azure speech-to-text javascript SDK requires
4242
# @see speech-to-text.js
4343
def convert_audio_to_wav
44-
stdout, stderr, status =
45-
Open3.capture3("ffmpeg", "-i", file.to_s, "-ac", "1", "-ar", "16000", wav_file)
44+
_stdout, stderr, status = Open3.capture3("ffmpeg", "-i", file.to_s, "-ac", "1", "-ar", "16000", wav_file)
4645
raise Exception, stderr unless status.success?
46+
4747
Rails.logger.debug("--- convert_audio_to_wav ---")
48-
Rails.logger.debug(File.size wav_file) if File.exist? wav_file
48+
Rails.logger.debug(File.size(wav_file)) if File.exist? wav_file
4949
end
5050

5151
def transcripts_from_sdk
@@ -58,6 +58,7 @@ def transcripts_from_sdk
5858
Rails.logger.debug(stdout)
5959
Rails.logger.debug(stderr)
6060
raise Exception, stderr.presence || stdout unless status.success?
61+
6162
stdout
6263
end
6364

app/services/voice_base/import_srt_transcripts.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,18 @@ def update_from_voicebase(transcript, contents)
2222
update_records(transcript, contents.lines)
2323
end
2424

25-
2625
private
2726

2827
def call
2928
# Retrieve empty transcripts that have VoiceBase as their vendor.
3029
transcripts = Transcript.getForDownloadByVendor('voice_base', @project_id)
31-
puts "Retrieved #{transcripts.count} empty transcripts from collections \
32-
with VoiceBase as their vendor."
30+
Rails.logger.info("Retrieved #{transcripts.count} empty transcripts from collections with VoiceBase as their vendor.")
3331

3432
transcripts.find_each { |transcript| load_from_file(transcript) }
3533
end
3634

3735
def transcript_file_path(transcript)
38-
Rails.root.join('project', @project_id, 'transcripts', 'voice_base',
39-
"#{transcript.vendor_identifier}.srt")
36+
Rails.root.join('project', @project_id, 'transcripts', 'voice_base', "#{transcript.vendor_identifier}.srt")
4037
end
4138

4239
# Read lines from a single transcript.
@@ -45,8 +42,7 @@ def read_lines_from_file(transcript)
4542
if transcript.script.file.nil?
4643
fp = transcript_file_path(transcript)
4744
unless File.exist?(fp)
48-
puts "Couldn't find transcript in project folder for Transcript \
49-
##{transcript.id} at #{fp}."
45+
Rails.logger.info("Couldn't find transcript in project folder for Transcript ##{transcript.id} at #{fp}.")
5046
return []
5147
end
5248
File.read(fp).lines
@@ -70,12 +66,12 @@ def update_records(transcript, contents)
7066
debug = "Created #{transcript_lines.length} lines from \
7167
transcript #{transcript.uid}."
7268
end
73-
puts debug
69+
Rails.logger.info(debug)
7470
end
7571

7672
# Ingest processed lines into the transcript.
7773
def ingest_transcript_lines(transcript, transcript_lines)
78-
return nil if transcript_lines.nil? or transcript_lines.empty?
74+
return nil if transcript_lines.nil? || transcript_lines.empty?
7975

8076
TranscriptLine.where(transcript_id: transcript.id).destroy_all
8177
TranscriptLine.create!(transcript_lines)

config/environments/staging.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Rails.application.configure do
2-
ENV["DEFAULT_MAILER_HOST"] # Settings specified here will take precedence over those in config/application.rb.
2+
ENV["DEFAULT_MAILER_HOST"] # Settings specified here will take precedence over those in config/application.rb.
33

44
# Code is not reloaded between requests.
55
config.cache_classes = true

spec/features/admin/summary_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require 'rails_helper'
22

3-
RSpec.feature 'Summary Page' do
3+
RSpec.describe 'Summary Page', type: :feature do
44
let(:admin) { create(:user, :admin) }
55

66
describe 'the Summary page as an admin', js: true do

spec/models/transcripts_spec.rb renamed to spec/models/transcript_spec.rb

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
end
2525

2626
describe "scopes" do
27+
let(:user) { create(:user) }
2728
let!(:completed_trasncript) { create :transcript, percent_completed: 100 }
2829
let!(:reviewing_trasncript) { create :transcript, percent_reviewing: 37 }
2930
let!(:not_completed_trasncript) { create :transcript, percent_edited: 37 }
@@ -39,12 +40,24 @@
3940
it "gets pending" do
4041
expect(described_class.pending.to_a).to eq([not_completed_trasncript])
4142
end
43+
44+
it 'gets edited' do
45+
expect(described_class.getEdited).to eq([])
46+
end
47+
48+
it 'gets user edited' do
49+
expect(described_class.getByUserEdited(user.id)).to eq([])
50+
end
51+
end
52+
53+
it "returns .seconds_per_line" do
54+
expect(described_class.seconds_per_line).to eq(5)
4255
end
4356

4457
describe "validate uid does not change after create" do
4558
let(:vendor) { Vendor.create!(uid: "voice_base", name: "VoiceBase") }
4659
let(:transcript) do
47-
Transcript.new(
60+
described_class.new(
4861
uid: "transcript_test",
4962
vendor_id: vendor.id,
5063
)
@@ -64,6 +77,22 @@
6477
end
6578
end
6679

80+
describe "#transcription_conventions" do
81+
let(:transcript) { create(:transcript) }
82+
83+
it "returns conventions" do
84+
expect(transcript.transcription_conventions.size).to eq(8)
85+
end
86+
end
87+
88+
describe "#to_param" do
89+
let(:transcript) { create(:transcript) }
90+
91+
it "returns uid" do
92+
expect(transcript.to_param).to eq(transcript.uid)
93+
end
94+
end
95+
6796
describe "#speakers" do
6897
let(:vendor) { Vendor.create(uid: "voice_base", name: "VoiceBase") }
6998
let(:institution) { FactoryBot.create :institution }
@@ -78,7 +107,7 @@
78107
)
79108
end
80109
let(:transcript) do
81-
Transcript.create!(
110+
described_class.create!(
82111
uid: "test_transcript",
83112
vendor: vendor,
84113
collection: collection,
@@ -148,17 +177,21 @@
148177
describe "#get_for_home_page" do
149178
let(:collection) { create :collection, :published }
150179
let(:params) do
151-
{ collections: [collection.title], sort_by: sort_by,
152-
search: "", institution: nil, theme: [""] }
180+
{
181+
collections: [collection.title], sort_by: sort_by, search: "", institution: nil, theme: [""]
182+
}
153183
end
154184

155185
before do
156186
%w[B A].each do |title|
157-
create :transcript, :published,
187+
create(
188+
:transcript,
189+
:published,
158190
title: title,
159191
collection: collection,
160192
project_uid: "nsw-state-library-amplify",
161193
lines: 1
194+
)
162195
end
163196
end
164197

@@ -175,7 +208,7 @@
175208
let!(:sort_by) { "" } # blank means random
176209

177210
it "return random records" do
178-
expect(Transcript).to receive(:randomize_list)
211+
expect(described_class).to receive(:randomize_list)
179212
described_class.get_for_home_page(params)
180213
end
181214
end
@@ -216,9 +249,7 @@
216249
end
217250

218251
it "shows theme1 records" do
219-
expect(described_class.search({
220-
theme: ["theme1"],
221-
})).to eq([transcript2])
252+
expect(described_class.search({ theme: ["theme1"] })).to eq([transcript2])
222253
end
223254
end
224255
end

0 commit comments

Comments
 (0)