Skip to content

Commit 0b785ac

Browse files
authored
Merge pull request #1040 from unepwcmc/fix/cache-in-tests
Fix: cache in tests which validate cache behaviour
2 parents 384a956 + 2f2f2ea commit 0b785ac

13 files changed

Lines changed: 186 additions & 18 deletions

spec/services/species/common_names_export_spec.rb

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,40 +4,57 @@
44
subject do
55
Species::CommonNamesExport.new({})
66
end
7-
specify { expect(subject.path).to eq('public/downloads/common_names/') }
7+
8+
specify do
9+
expect(subject.path).to eq('public/downloads/common_names/')
10+
end
811
end
9-
describe :export do
12+
13+
describe :export, cache: true do
1014
context 'when no results' do
1115
subject do
1216
Species::CommonNamesExport.new({})
1317
end
14-
specify { expect(subject.export).to be_falsey }
18+
19+
specify do
20+
expect(subject.export).to be_falsey
21+
end
1522
end
23+
1624
context 'when results' do
1725
before(:each) do
18-
species = create_cites_eu_species
26+
create_cites_eu_species
27+
1928
FileUtils.mkpath(
2029
File.expand_path('spec/public/downloads/common_names')
2130
)
31+
2232
allow_any_instance_of(Species::CommonNamesExport).to receive(:path).
2333
and_return('spec/public/downloads/common_names/')
2434
end
35+
2536
after(:each) do
2637
FileUtils.remove_dir('spec/public/downloads/common_names', true)
2738
end
39+
2840
subject do
2941
Species::CommonNamesExport.new({})
3042
end
43+
3144
context 'when file not cached' do
3245
specify do
3346
subject.export
47+
3448
expect(File.file?(subject.file_name)).to be_truthy
3549
end
3650
end
51+
3752
context 'when file cached' do
3853
specify do
3954
FileUtils.touch(subject.file_name)
55+
4056
expect(subject).not_to receive(:to_csv)
57+
4158
subject.export
4259
end
4360
end

spec/services/species/documents_export_spec.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,41 @@
44
subject do
55
Species::DocumentsExport.new({})
66
end
7-
specify { expect(subject.path).to eq('public/downloads/documents/') }
7+
8+
specify do
9+
expect(subject.path).to eq('public/downloads/documents/')
10+
end
811
end
12+
913
SPEC_DOCUMENTS_DOWNLOAD_PATH = 'spec/public/downloads/documents'
10-
describe :export do
14+
15+
describe :export, cache: true do
1116
before(:each) do
1217
FileUtils.mkpath(
1318
File.expand_path("#{SPEC_DOCUMENTS_DOWNLOAD_PATH}")
1419
)
1520
allow_any_instance_of(Species::DocumentsExport).to receive(:path).
1621
and_return("#{SPEC_DOCUMENTS_DOWNLOAD_PATH}/")
1722
end
23+
1824
after(:each) do
1925
FileUtils.remove_dir("#{SPEC_DOCUMENTS_DOWNLOAD_PATH}", true)
2026
end
27+
2128
context 'when no results' do
2229
before(:each) do
2330
FileUtils.rm_rf(Dir.glob("#{SPEC_DOCUMENTS_DOWNLOAD_PATH}/*"))
2431
end
32+
2533
subject do
2634
Species::DocumentsExport.new({})
2735
end
36+
2837
specify 'when file not cached it should not be generated' do
2938
expect(subject.export).to be_falsey
3039
end
3140
end
41+
3242
context 'when results' do
3343
# Commented as was causing issues and tests are pending anyway
3444
# before(:each) {
@@ -43,6 +53,7 @@
4353
expect(File.file?(subject.file_name)).to be_truthy
4454
expect(File.size(subject.file_name)).to be > 0
4555
end
56+
4657
pending 'when file cached it should not be generated' do
4758
FileUtils.touch(subject.file_name)
4859
expect(subject).not_to receive(:to_csv)

spec/services/species/hybrid_prefix_matcher_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
}
1414
)
1515
end
16+
1617
specify { expect(subject.results).to include(@hybrid_ac) }
1718
end
19+
1820
context 'when trade internal visibility' do
1921
subject do
2022
Species::TaxonConceptPrefixMatcher.new(
@@ -25,8 +27,10 @@
2527
}
2628
)
2729
end
30+
2831
specify { expect(subject.results).to include(@hybrid_ac) }
2932
end
33+
3034
context 'when speciesplus visibility' do
3135
subject do
3236
Species::TaxonConceptPrefixMatcher.new(
@@ -36,6 +40,7 @@
3640
}
3741
)
3842
end
43+
3944
specify { expect(subject.results).to be_empty }
4045
end
4146
end

spec/services/species/listings_export_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
}
1010
)
1111
end
12+
1213
specify { expect(subject.path).to eq('public/downloads/cites_listings/') }
1314
end
14-
describe :export do
15+
16+
describe :export, cache: true do
1517
context 'when no results' do
1618
subject do
1719
Species::ListingsExportFactory.new(
@@ -22,8 +24,10 @@
2224
}
2325
)
2426
end
27+
2528
specify { expect(subject.export).to be_falsey }
2629
end
30+
2731
context 'when results' do
2832
before(:each) do
2933
FileUtils.mkpath(
@@ -32,9 +36,11 @@
3236
allow_any_instance_of(Species::ListingsExport).to receive(:path).
3337
and_return('spec/public/downloads/cites_listings/')
3438
end
39+
3540
after(:each) do
3641
FileUtils.remove_dir('spec/public/downloads/cites_listings', true)
3742
end
43+
3844
subject do
3945
Species::ListingsExportFactory.new(
4046
{
@@ -44,12 +50,14 @@
4450
}
4551
)
4652
end
53+
4754
context 'when file not cached' do
4855
specify do
4956
subject.export
5057
expect(File.file?(subject.file_name)).to be_truthy
5158
end
5259
end
60+
5361
context 'when file cached' do
5462
specify do
5563
FileUtils.touch(subject.file_name)
@@ -59,6 +67,7 @@
5967
end
6068
end
6169
end
70+
6271
describe :query do
6372
context 'when CITES' do
6473
context 'when Appendix I' do
@@ -70,6 +79,7 @@
7079
}
7180
)
7281
end
82+
7383
specify { expect(subject.query.to_a.size).to eq(1) }
7484

7585
context 'when Poland' do
@@ -82,6 +92,7 @@
8292
}
8393
)
8494
end
95+
8596
specify { expect(subject.query.to_a.size).to eq(0) }
8697
end
8798

@@ -95,9 +106,11 @@
95106
}
96107
)
97108
end
109+
98110
specify { expect(subject.query.to_a.size).to eq(1) }
99111
end
100112
end
113+
101114
context 'when higher taxon ids' do
102115
subject do
103116
Species::ListingsExportFactory.new(
@@ -107,15 +120,18 @@
107120
}
108121
)
109122
end
123+
110124
specify { expect(subject.query.to_a.size).to eq(1) }
111125
end
126+
112127
context 'when implicitly listed subspecies present' do
113128
before(:each) do
114129
create_cites_eu_subspecies(
115130
parent_id: @species.id
116131
)
117132
SapiModule::StoredProcedures.rebuild_cites_taxonomy_and_listings
118133
end
134+
119135
subject do
120136
Species::ListingsExportFactory.new(
121137
{
@@ -124,9 +140,11 @@
124140
}
125141
)
126142
end
143+
127144
specify { expect(subject.query.to_a.size).to eq(1) }
128145
end
129146
end
147+
130148
context 'when EU' do
131149
context 'when Annex A' do
132150
subject do
@@ -137,6 +155,7 @@
137155
}
138156
)
139157
end
158+
140159
specify { expect(subject.query.to_a.size).to eq(1) }
141160

142161
context 'when Spain' do
@@ -149,6 +168,7 @@
149168
}
150169
)
151170
end
171+
152172
specify { expect(subject.query.to_a.size).to eq(0) }
153173
end
154174

@@ -162,9 +182,11 @@
162182
}
163183
)
164184
end
185+
165186
specify { expect(subject.query.to_a.size).to eq(1) }
166187
end
167188
end
189+
168190
context 'when higher taxon ids' do
169191
subject do
170192
Species::ListingsExportFactory.new(
@@ -174,6 +196,7 @@
174196
}
175197
)
176198
end
199+
177200
specify { expect(subject.query.to_a.size).to eq(1) }
178201
end
179202
end

spec/services/species/orphaned_taxon_concepts_export_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,55 @@
44
subject do
55
Species::OrphanedTaxonConceptsExport.new({})
66
end
7+
78
specify { expect(subject.path).to eq('public/downloads/orphaned_taxon_concepts/') }
89
end
9-
describe :export do
10+
11+
describe :export, cache: true do
1012
context 'when no results' do
1113
subject do
1214
Species::OrphanedTaxonConceptsExport.new({})
1315
end
16+
1417
specify { expect(subject.export).to be_falsey }
1518
end
19+
1620
context 'when results' do
1721
before(:each) do
1822
tc = create(:taxon_concept)
23+
1924
tc.update_attribute(:parent_id, nil) # skipping validations
25+
2026
FileUtils.mkpath(
2127
File.expand_path('spec/public/downloads/orphaned_taxon_concepts')
2228
)
29+
2330
allow_any_instance_of(Species::OrphanedTaxonConceptsExport).to receive(:path).
2431
and_return('spec/public/downloads/orphaned_taxon_concepts/')
2532
end
33+
2634
after(:each) do
2735
FileUtils.remove_dir('spec/public/downloads/orphaned_taxon_concepts', true)
2836
end
37+
2938
subject do
3039
Species::OrphanedTaxonConceptsExport.new({})
3140
end
41+
3242
context 'when file not cached' do
3343
specify do
3444
subject.export
45+
3546
expect(File.file?(subject.file_name)).to be_truthy
3647
end
3748
end
49+
3850
context 'when file cached' do
3951
specify do
4052
FileUtils.touch(subject.file_name)
53+
4154
expect(subject).not_to receive(:to_csv)
55+
4256
subject.export
4357
end
4458
end

spec/services/species/search_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
require 'spec_helper'
22
describe Species::Search do
33
include_context 'Canis lupus'
4+
45
describe :results do
56
context 'when searching by scientific name' do
67
context 'when regular query' do
78
subject { Species::Search.new({ taxon_concept_query: 'canis' }).results }
89
specify { expect(subject).to include(@species) }
910
end
11+
1012
context 'when malicious query' do
1113
subject { Species::Search.new({ taxon_concept_query: 'canis\'' }).results }
1214
specify { expect(subject).to be_empty }
1315
end
16+
1417
context 'when leading whitespace' do
1518
subject { Species::Search.new({ taxon_concept_query: ' canis' }).results }
1619
specify { expect(subject).to include(@species) }
1720
end
21+
1822
context 'when trailing whitespace' do
1923
subject { Species::Search.new({ taxon_concept_query: 'canis ' }).results }
2024
specify { expect(subject).to include(@species) }

0 commit comments

Comments
 (0)