strain-code 0.4.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,7 +14,7 @@ class StrainCode::Number
14
14
  end
15
15
 
16
16
  def number
17
- catalogue ? [code, accession].compact.join(' ') : ori_number
17
+ catalogue ? [code.upcase, accession].compact.join(' ') : ori_number
18
18
  end
19
19
 
20
20
  def url
@@ -7,7 +7,7 @@ class StrainCode::Parser
7
7
  end
8
8
 
9
9
  def parse(numbers)
10
- clean(numbers).split(' = ').map do |number|
10
+ clean(numbers).split('=').map(&:strip).reject(&:empty?).map do |number|
11
11
  StrainCode::Number.new(number)
12
12
  end
13
13
  end
@@ -1,5 +1,5 @@
1
1
  class StrainCode
2
- VERSION = '0.4.3'
2
+ VERSION = '0.5.0'
3
3
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
4
4
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
5
5
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -0,0 +1,59 @@
1
+ require 'test_helper'
2
+
3
+ class CatalogueTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def test_data_path_exists
7
+ assert(File.directory?(StrainCode.data_path))
8
+ assert(File.file?(catalogues_path))
9
+ end
10
+
11
+ def test_every_code_resolves_to_its_catalogue
12
+ catalogues.each do |c|
13
+ c['codes'].each do |code|
14
+ cat = StrainCode.catalogue(code)
15
+ assert_kind_of(StrainCode::Catalogue, cat, code)
16
+ assert_equal(c['codes'], cat.codes, code)
17
+ assert_equal(c['name'], cat.name, code)
18
+ end
19
+ end
20
+ end
21
+
22
+ def test_lookup_is_case_insensitive_and_symbol_friendly
23
+ assert_same(StrainCode.catalogue('DSM'), StrainCode.catalogue('dsm'))
24
+ assert_same(StrainCode.catalogue('DSM'), StrainCode.catalogue(:dsm))
25
+ end
26
+
27
+ def test_unknown_or_empty_codes
28
+ assert_nil(StrainCode.catalogue('NOT-A-COLLECTION'))
29
+ assert_nil(StrainCode.catalogue(''))
30
+ assert_nil(StrainCode.catalogue(nil))
31
+ end
32
+
33
+ def test_catalogues_are_memoized
34
+ assert_same(StrainCode.catalogue('ATCC'), StrainCode.catalogue('ATCC'))
35
+ end
36
+
37
+ def test_attributes
38
+ cat = StrainCode.catalogue('CCAP')
39
+ assert_equal('GB', cat.country_code)
40
+ assert_equal(['/', '-'], cat.number_tr)
41
+ assert_match(/%s/, cat.url_pattern)
42
+ end
43
+
44
+ def test_underscore_fields_are_ignored
45
+ cat = StrainCode::Catalogue.new(
46
+ 'codes' => ['XYZ'], 'name' => 'Test', '_notes' => 'ignored',
47
+ '_whatever' => 1
48
+ )
49
+ assert_equal(['XYZ'], cat.codes)
50
+ assert_equal('Test', cat.name)
51
+ assert_false(cat.respond_to?(:_notes))
52
+ end
53
+
54
+ def test_unknown_public_fields_raise
55
+ assert_raise(NoMethodError) do
56
+ StrainCode::Catalogue.new('codes' => ['XYZ'], 'bogus' => 1)
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,159 @@
1
+ require 'test_helper'
2
+
3
+ # Integrity checks on data/catalogues.yaml
4
+ class CataloguesDataTest < Test::Unit::TestCase
5
+ include TestHelper
6
+
7
+ def test_yaml_is_parseable
8
+ assert_kind_of(Hash, source)
9
+ assert_kind_of(Array, catalogues)
10
+ assert_not_empty(catalogues)
11
+ end
12
+
13
+ def test_manifest_update_is_a_past_date
14
+ update = source.dig('manifest', 'update')
15
+ assert_kind_of(Date, update)
16
+ assert_operator(update, :<=, Date.today)
17
+ end
18
+
19
+ def test_entries_only_use_known_fields
20
+ catalogues.each do |c|
21
+ unknown = c.keys - FIELDS
22
+ assert_empty(unknown, "#{c['codes']}: unknown fields #{unknown}")
23
+ end
24
+ end
25
+
26
+ def test_public_fields_have_catalogue_accessors
27
+ FIELDS.reject { |f| f.start_with?('_') }.each do |f|
28
+ assert_respond_to(StrainCode::Catalogue.allocate, "#{f}=")
29
+ end
30
+ end
31
+
32
+ def test_fields_follow_conventional_order
33
+ catalogues.each do |c|
34
+ assert_equal(FIELDS & c.keys, c.keys, "#{c['codes']}: field order")
35
+ end
36
+ end
37
+
38
+ def test_codes_are_uppercase_sorted_and_parseable
39
+ catalogues.each do |c|
40
+ codes = c['codes']
41
+ assert_kind_of(Array, codes)
42
+ assert_not_empty(codes)
43
+ assert_equal(codes.sort, codes, "#{codes}: codes not sorted")
44
+ codes.each do |code|
45
+ assert_equal(code.upcase, code, "#{code}: not uppercase")
46
+ # Codes are separated from numbers by space, colon or underscore
47
+ assert_no_match(/[ :_]/, code, "#{code}: contains a separator")
48
+ end
49
+ end
50
+ end
51
+
52
+ def test_codes_are_unique_across_catalogues
53
+ dups = catalogues.flat_map { |c| c['codes'] }.tally.select { |_, n| n > 1 }
54
+ assert_empty(dups, "Duplicated codes: #{dups.keys}")
55
+ end
56
+
57
+ def test_entries_are_sorted_by_first_code
58
+ first = catalogues.map { |c| c['codes'].first }
59
+ assert_equal(first.sort, first)
60
+ end
61
+
62
+ def test_entries_are_under_their_letter_section
63
+ section = nil
64
+ File.readlines(catalogues_path).each_with_index do |line, i|
65
+ if line =~ /^ #--- ([A-Z])$/
66
+ section = $1
67
+ elsif line =~ /^ - codes: \[ (\S)/
68
+ assert_equal(section, $1, "Line #{i + 1}: entry under section #{section}")
69
+ end
70
+ end
71
+ end
72
+
73
+ def test_names_are_present
74
+ catalogues.each do |c|
75
+ assert_kind_of(String, c['name'], "#{c['codes']}: missing name")
76
+ assert_not_equal(c['name'], c['name_en'], "#{c['codes']}: redundant name_en")
77
+ end
78
+ end
79
+
80
+ def test_country_codes_look_like_iso_3166_alpha2
81
+ catalogues.each do |c|
82
+ next unless c.key?('country_code')
83
+
84
+ cc = c['country_code']
85
+ assert_match(/\A[A-Z]{2}\z/, cc, "#{c['codes']}: #{cc}")
86
+ # Common mistakes: UK (ISO is GB), EU (not a country)
87
+ assert_not_include(%w[UK EU], cc, "#{c['codes']}: #{cc}")
88
+ end
89
+ end
90
+
91
+ def test_urls_are_well_formed
92
+ catalogues.each do |c|
93
+ %w[url url_pattern].each do |f|
94
+ next unless c[f]
95
+
96
+ assert_match(%r{\Ahttps?://\S+\z}, c[f], "#{c['codes']}: #{f}")
97
+ end
98
+ end
99
+ end
100
+
101
+ def test_url_patterns_have_one_placeholder_and_escaped_percents
102
+ catalogues.each do |c|
103
+ next unless (pattern = c['url_pattern'])
104
+
105
+ # Remove escaped percents, then exactly one %s must remain
106
+ assert_equal(
107
+ ['%s'], pattern.gsub('%%', '').scan(/%./),
108
+ "#{c['codes']}: url_pattern must have one %s and escape other %"
109
+ )
110
+ assert_include(pattern % 'NUMBER', 'NUMBER')
111
+ end
112
+ end
113
+
114
+ def test_number_tr_is_a_pair_of_strings
115
+ catalogues.each do |c|
116
+ next unless (tr = c['number_tr'])
117
+
118
+ assert_kind_of(Array, tr)
119
+ assert_equal(2, tr.size, "#{c['codes']}: number_tr")
120
+ tr.each { |s| assert_kind_of(String, s) }
121
+ end
122
+ end
123
+
124
+ def test_examples_belong_to_their_catalogue
125
+ catalogues.each do |c|
126
+ (c['_examples'] || []).each do |ex|
127
+ number = StrainCode.number(ex)
128
+ assert_not_nil(number.catalogue, "#{ex}: catalogue not found")
129
+ assert_equal(c['codes'], number.catalogue.codes, "#{ex}: wrong catalogue")
130
+ next unless c['url_pattern']
131
+
132
+ assert_match(%r{\Ahttps?://\S+\z}, number.url, "#{ex}: bad URL")
133
+ end
134
+ end
135
+ end
136
+
137
+ def test_tested_dates_are_past_dates_on_entries_with_urls
138
+ catalogues.each do |c|
139
+ next unless c.key?('_tested')
140
+
141
+ assert_kind_of(Date, c['_tested'], "#{c['codes']}: _tested")
142
+ assert_operator(c['_tested'], :<=, Date.today, "#{c['codes']}: _tested")
143
+ assert(
144
+ c['url'] || c['url_pattern'],
145
+ "#{c['codes']}: _tested without url or url_pattern"
146
+ )
147
+ end
148
+ end
149
+
150
+ def test_url_patterns_have_examples
151
+ catalogues.each do |c|
152
+ next unless c['url_pattern']
153
+
154
+ assert_not_empty(
155
+ c['_examples'] || [], "#{c['codes']}: url_pattern without _examples"
156
+ )
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,80 @@
1
+ require 'test_helper'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ # Live checks of the catalogue websites. These hit external servers, so they
6
+ # only run when STRAIN_CODE_LIVE is set (e.g., `rake test:live`).
7
+ #
8
+ # For each catalogue, `url` must respond with HTTP 2xx, and each `_examples`
9
+ # number must produce a `url_pattern` link that responds with HTTP 2xx and
10
+ # whose body mentions the accession (except for JavaScript-rendered sites).
11
+ class LiveUrlsTest < Test::Unit::TestCase
12
+ include TestHelper
13
+ extend TestHelper
14
+
15
+ # Sites that render strain records with JavaScript: the accession is not in
16
+ # the raw HTML, so only the HTTP status is checked
17
+ JS_RENDERED = %w[CBS NCCB LMG CGMCC GDMCC ULC].freeze
18
+
19
+ USER_AGENT = 'Mozilla/5.0 (compatible; strain-code live tests)'.freeze
20
+ MAX_REDIRECTS = 5
21
+ TIMEOUT = 30
22
+
23
+ def self.live?
24
+ !ENV['STRAIN_CODE_LIVE'].to_s.empty?
25
+ end
26
+
27
+ def fetch(url, redirects = MAX_REDIRECTS)
28
+ uri = URI.parse(url)
29
+ res = Net::HTTP.start(
30
+ uri.host, uri.port,
31
+ use_ssl: uri.scheme == 'https',
32
+ open_timeout: TIMEOUT, read_timeout: TIMEOUT
33
+ ) do |http|
34
+ http.request(Net::HTTP::Get.new(uri, 'User-Agent' => USER_AGENT))
35
+ end
36
+ if res.is_a?(Net::HTTPRedirection) && redirects.positive?
37
+ return fetch(URI.join(url, res['location']).to_s, redirects - 1)
38
+ end
39
+
40
+ res
41
+ end
42
+
43
+ def assert_live(url, label, expect = nil)
44
+ res = fetch(url)
45
+ assert_kind_of(
46
+ Net::HTTPSuccess, res, "#{label}: HTTP #{res.code} for #{url}"
47
+ )
48
+ return unless expect
49
+
50
+ body = res.body.to_s.dup.force_encoding('UTF-8').scrub
51
+ assert_include(body, expect, "#{label}: '#{expect}' not found in #{url}")
52
+ rescue StandardError, Timeout::Error => e
53
+ raise if e.is_a?(Test::Unit::AssertionFailedError)
54
+
55
+ flunk("#{label}: #{e.class}: #{e.message} for #{url}")
56
+ end
57
+
58
+ if live?
59
+ catalogues.each do |c|
60
+ next unless c['url'] || c['url_pattern']
61
+
62
+ code = c['codes'].first
63
+ js_rendered = c['codes'].intersect?(JS_RENDERED)
64
+ define_method("test_live_#{code.downcase.gsub(/\W/, '_')}") do
65
+ assert_live(c['url'], "#{code} url") if c['url']
66
+ (c['_examples'] || []).each do |ex|
67
+ number = StrainCode.number(ex)
68
+ next unless number.url
69
+
70
+ expect = js_rendered ? nil : number.accession
71
+ assert_live(number.url, ex, expect)
72
+ end
73
+ end
74
+ end
75
+ else
76
+ def test_live_urls
77
+ omit('Set STRAIN_CODE_LIVE=1 (or run `rake test:live`) to check URLs')
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,88 @@
1
+ require 'test_helper'
2
+
3
+ class NumberTest < Test::Unit::TestCase
4
+ def test_code_separators
5
+ ['DSM 30083', 'DSM:30083', 'DSM_30083', 'DSM : 30083'].each do |str|
6
+ n = StrainCode.number(str)
7
+ assert_equal('DSM', n.code, str)
8
+ assert_equal('30083', n.accession, str)
9
+ assert_equal('DSM 30083', n.number, str)
10
+ end
11
+ end
12
+
13
+ def test_accession_may_contain_spaces
14
+ n = StrainCode.number('UTEX LB 3035')
15
+ assert_equal('UTEX', n.code)
16
+ assert_equal('LB 3035', n.accession)
17
+ end
18
+
19
+ def test_single_token_is_an_accession_without_code
20
+ n = StrainCode.number('12345')
21
+ assert_nil(n.code)
22
+ assert_equal('12345', n.accession)
23
+ assert_nil(n.catalogue)
24
+ assert_equal('12345', n.number)
25
+ assert_nil(n.url)
26
+ end
27
+
28
+ def test_unknown_code_keeps_original_number
29
+ n = StrainCode.number('Strain XYZ 1')
30
+ assert_nil(n.catalogue)
31
+ assert_equal('Strain XYZ 1', n.number)
32
+ assert_equal('Strain XYZ 1', n.to_s)
33
+ assert_nil(n.url)
34
+ end
35
+
36
+ def test_code_lookup_is_case_insensitive
37
+ assert_equal('JCM', StrainCode.number('jcm 1649').catalogue.codes.first)
38
+ end
39
+
40
+ def test_known_codes_are_upcased_in_number
41
+ assert_equal('JCM 1649', StrainCode.number('jcm 1649').number)
42
+ assert_equal('IFO 3301', StrainCode.number('Ifo:3301').number)
43
+ # The accession is kept as is
44
+ assert_equal('SAG 211-11b', StrainCode.number('sag 211-11b').number)
45
+ end
46
+
47
+ def test_url_from_pattern
48
+ assert_equal(
49
+ 'https://www.dsmz.de/collection/catalogue/details/culture/DSM-30083',
50
+ StrainCode.number('DSM 30083').url
51
+ )
52
+ end
53
+
54
+ def test_url_pattern_with_escaped_percent
55
+ assert_equal(
56
+ 'https://catalogue-crbip.pasteur.fr/fiche_catalogue.xhtml?crbip=CIP%2054.8',
57
+ StrainCode.number('CIP 54.8').url
58
+ )
59
+ end
60
+
61
+ def test_url_applies_number_tr
62
+ assert_equal(
63
+ 'https://www.ccap.ac.uk/catalogue/strain-1460-5',
64
+ StrainCode.number('CCAP 1460/5').url
65
+ )
66
+ assert_equal(
67
+ 'https://utex.org/products/utex-lb-3035',
68
+ StrainCode.number('UTEX LB 3035').url
69
+ )
70
+ end
71
+
72
+ def test_url_is_nil_without_pattern
73
+ n = StrainCode.number('KACC 19675')
74
+ assert_not_nil(n.catalogue)
75
+ assert_nil(n.url)
76
+ end
77
+
78
+ def test_url_is_nil_without_accession
79
+ assert_nil(StrainCode.number('DSM').url)
80
+ end
81
+
82
+ def test_synonym_codes_share_the_catalogue
83
+ assert_same(
84
+ StrainCode.number('IFO 3301').catalogue,
85
+ StrainCode.number('NBRC 3301').catalogue
86
+ )
87
+ end
88
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class ParserTest < Test::Unit::TestCase
4
+ README_EXAMPLE = <<~STR_NO
5
+ ATCC 33152 = CCUG 9568 = CIP 103854 ;
6
+ DSM 25069 = DSM_7513 = JCM:7571;
7
+ NBIMCC:8848 = NCTC 11192, Strain Philadelphia 1
8
+ STR_NO
9
+
10
+ def test_clean_collapses_whitespace_and_separators
11
+ assert_equal(
12
+ 'ATCC 1 = DSM 2 = JCM 3 = NCTC 4',
13
+ StrainCode::Parser.clean(" ATCC 1;\n DSM 2 ,JCM 3=NCTC 4 ")
14
+ )
15
+ end
16
+
17
+ def test_parse_readme_example
18
+ numbers = StrainCode.parse(README_EXAMPLE)
19
+ assert_equal(9, numbers.size)
20
+ assert(numbers.all? { |n| n.is_a?(StrainCode::Number) })
21
+ assert_equal(
22
+ ['ATCC 33152', 'CCUG 9568', 'CIP 103854', 'DSM 25069', 'DSM 7513',
23
+ 'JCM 7571', 'NBIMCC 8848', 'NCTC 11192', 'Strain Philadelphia 1'],
24
+ numbers.map(&:number)
25
+ )
26
+ end
27
+
28
+ def test_parse_readme_example_catalogues
29
+ numbers = StrainCode.parse(README_EXAMPLE)
30
+ assert_equal(
31
+ %w[US SE FR DE DE JP BG GB],
32
+ numbers[0..7].map { |n| n.catalogue.country_code }
33
+ )
34
+ assert_nil(numbers.last.catalogue)
35
+ assert_nil(numbers.last.url)
36
+ end
37
+
38
+ def test_parse_ignores_trailing_separator
39
+ assert_equal(['ATCC 1'], StrainCode.parse('ATCC 1;').map(&:number))
40
+ end
41
+
42
+ def test_parse_ignores_leading_and_repeated_separators
43
+ assert_equal(
44
+ ['ATCC 1', 'DSM 2'],
45
+ StrainCode.parse('; ATCC 1 ;; DSM 2 =').map(&:number)
46
+ )
47
+ assert_empty(StrainCode.parse(' ; , = '))
48
+ end
49
+
50
+ def test_parse_empty_input
51
+ assert_empty(StrainCode.parse(''))
52
+ assert_empty(StrainCode.parse(nil))
53
+ end
54
+ end
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'simplecov'
3
+ SimpleCov.start { add_filter '/test/' }
4
+ rescue LoadError
5
+ # Coverage is optional
6
+ end
7
+
8
+ require 'test/unit'
9
+ require 'date'
10
+ require 'strain-code'
11
+
12
+ module TestHelper
13
+ # Fields allowed in each catalogue entry, in their conventional order
14
+ FIELDS = %w[
15
+ codes country_code name name_en organization number_tr url url_pattern
16
+ _examples _tested _notes
17
+ ].freeze
18
+
19
+ def catalogues_path
20
+ StrainCode::Catalogue.catalogues_path
21
+ end
22
+
23
+ def source
24
+ @source ||= YAML.load_file(catalogues_path, permitted_classes: [Date])
25
+ end
26
+
27
+ def catalogues
28
+ source['catalogues']
29
+ end
30
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strain-code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luis M. Rodriguez-R
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-05-06 00:00:00.000000000 Z
11
+ date: 2026-11-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -56,6 +56,12 @@ files:
56
56
  - lib/strain-code/number.rb
57
57
  - lib/strain-code/parser.rb
58
58
  - lib/strain-code/version.rb
59
+ - test/catalogue_test.rb
60
+ - test/catalogues_data_test.rb
61
+ - test/live_urls_test.rb
62
+ - test/number_test.rb
63
+ - test/parser_test.rb
64
+ - test/test_helper.rb
59
65
  homepage: https://github.com/seq-code/strain-code
60
66
  licenses: []
61
67
  metadata: {}