ruby-hl7 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message do
5
+ context 'batch parsing' do
6
+ it 'should have a class method HL7::Message.parse_batch' do
7
+ HL7::Message.should respond_to(:parse_batch)
8
+ end
9
+
10
+ it 'should raise an exception when parsing an empty batch' do
11
+ # :empty_batch message contains a valid batch envelope with no
12
+ # contents
13
+ lambda do
14
+ HL7::Message.parse_batch HL7MESSAGES[:empty_batch]
15
+ end.should raise_exception(HL7::ParseError, 'empty_batch_message')
16
+ end
17
+
18
+ it 'should raise an exception when parsing a single message as a batch' do
19
+ lambda do
20
+ HL7::Message.parse_batch HL7MESSAGES[:realm_minimal_message]
21
+ end.should raise_exception(HL7::ParseError, 'badly_formed_batch_message')
22
+ end
23
+
24
+ it 'should yield multiple messages from a valid batch' do
25
+ count = 0
26
+ HL7::Message.parse_batch(HL7MESSAGES[:realm_batch]) do |m|
27
+ count += 1
28
+ end
29
+ count.should == 2
30
+ end
31
+ end
32
+ end
33
+
34
+ describe 'String extension' do
35
+ before :all do
36
+ @batch_message = HL7MESSAGES[:realm_batch]
37
+ @plain_message = HL7MESSAGES[:realm_minimal_message]
38
+ end
39
+
40
+ it 'should respond_to :hl7_batch?' do
41
+ @batch_message.hl7_batch?.should be true
42
+ @plain_message.should respond_to(:hl7_batch?)
43
+ end
44
+
45
+ it 'should return true when passed a batch message' do
46
+ @batch_message.should be_an_hl7_batch
47
+ end
48
+
49
+ it 'should return false when passed a plain message' do
50
+ @plain_message.should_not be_an_hl7_batch
51
+ end
52
+ end
@@ -0,0 +1,66 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message do
5
+ context 'child segments' do
6
+ before :all do
7
+ @base = open( './test_data/obxobr.hl7' ).readlines
8
+ end
9
+
10
+ it 'allows access to child segments' do
11
+ msg = HL7::Message.new @base
12
+ msg.should_not be_nil
13
+ msg[:OBR].should_not be_nil
14
+ msg[:OBR].length.should == 3
15
+ msg[:OBR][0].children.should_not be_nil
16
+ msg[:OBR][0].children.length.should == 6
17
+ msg[:OBR][1].children.should_not be_nil
18
+ msg[:OBR][1].children.length.should == 3
19
+ msg[:OBR][2].children.should_not be_nil
20
+ msg[:OBR][2].children.length.should == 1
21
+ msg[:OBX][0].children.should_not be_nil
22
+ msg[:OBX][0].children.length.should == 1
23
+
24
+ msg[:OBR][0].children.each do |x|
25
+ x.should_not be_nil
26
+ end
27
+ msg[:OBR][1].children.each do |x|
28
+ x.should_not be_nil
29
+ end
30
+ msg[:OBR][2].children.each do |x|
31
+ x.should_not be_nil
32
+ end
33
+ end
34
+
35
+ it 'allows adding child segments' do
36
+ msg = HL7::Message.new @base
37
+ msg.should_not be_nil
38
+ msg[:OBR].should_not be_nil
39
+ ob = HL7::Message::Segment::OBR.new
40
+ ob.should_not be_nil
41
+
42
+ msg << ob
43
+ ob.children.should_not be_nil
44
+ ob.segment_parent.should_not be_nil
45
+ ob.segment_parent.should == msg
46
+ orig_cnt = msg.length
47
+
48
+ (1..4).each do |x|
49
+ m = HL7::Message::Segment::OBX.new
50
+ m.observation_value = "taco"
51
+ m.should_not be_nil
52
+ /taco/.match(m.to_s).should_not be_nil
53
+ ob.children << m
54
+ ob.children.length.should == x
55
+ m.segment_parent.should_not be_nil
56
+ m.segment_parent.should == ob
57
+ end
58
+
59
+ @base.should_not == msg.to_hl7
60
+ msg.length.should_not == orig_cnt
61
+ text_ver = msg.to_hl7
62
+ /taco/.match(text_ver).should_not be_nil
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Date do
4
+
5
+ subject{ Date.parse('2013-12-02').to_hl7 }
6
+
7
+ it "should respond to the HL7 timestamp" do
8
+ subject.should eq "20131202"
9
+ end
10
+ end
11
+
12
+ describe "to_hl7 for time related classes" do
13
+
14
+ let(:formated_time){ time_now.strftime('%Y%m%d%H%M%S') }
15
+ let(:fraction_3){ "." + sprintf('%06d', time_now.to_time.usec)[0, 3] }
16
+ let(:fraction_9){ "." + sprintf('%06d', time_now.to_time.usec) + ("0" * 3) }
17
+
18
+ shared_examples "a time to_hl7" do
19
+ context "without fraction" do
20
+ it { time_now.to_time.to_hl7.should eq formated_time }
21
+ end
22
+
23
+ context "with_fraction" do
24
+ it { time_now.to_time.to_hl7(3).should eq formated_time + fraction_3 }
25
+
26
+ it { time_now.to_time.to_hl7(9).should eq formated_time + fraction_9 }
27
+ end
28
+ end
29
+
30
+ describe Time do
31
+ let(:time_now){ Time.now }
32
+
33
+ it_should_behave_like "a time to_hl7"
34
+ end
35
+
36
+ describe DateTime do
37
+ let(:time_now){ DateTime.now }
38
+
39
+ it_should_behave_like "a time to_hl7"
40
+ end
41
+
42
+ end
43
+
@@ -0,0 +1,31 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::Default do
5
+ context 'general' do
6
+
7
+ before :all do
8
+ @base_msa = "MSA|AR|ZZ9380 ERR"
9
+ end
10
+
11
+ it 'stores an existing segment' do
12
+ seg = HL7::Message::Segment::Default.new( @base_msa )
13
+ seg.to_s.should == @base_msa
14
+ end
15
+
16
+ it 'converts to a string' do
17
+ seg = HL7::Message::Segment::Default.new( @base_msa )
18
+ seg.to_s.should == @base_msa
19
+ seg.to_hl7.should == seg.to_s
20
+ end
21
+
22
+ it 'creates a raw segment' do
23
+ seg = HL7::Message::Segment::Default.new
24
+ seg.e0 = "NK1"
25
+ seg.e1 = "INFO"
26
+ seg.e2 = "MORE INFO"
27
+ seg.e5 = "LAST INFO"
28
+ seg.to_s.should == "NK1|INFO|MORE INFO|||LAST INFO"
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,37 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe 'dynamic segment definition' do
5
+ context 'general' do
6
+ it 'accepts a block with a parameter' do
7
+ seg = HL7::Message::Segment.new do |s|
8
+ s.e0 = "MSK"
9
+ s.e1 = "1234"
10
+ s.e2 = "5678"
11
+ end
12
+
13
+ seg.to_s.should == "MSK|1234|5678"
14
+ end
15
+
16
+ it 'accepts a block without a parameter' do
17
+ seg = HL7::Message::Segment.new do
18
+ e0 "MSK"
19
+ e1 "1234"
20
+ e2 "5678"
21
+ end
22
+
23
+ seg.to_s.should == "MSK|1234|5678"
24
+ end
25
+
26
+ it "doesn't pollute the caller namespace" do
27
+ seg = HL7::Message::Segment.new do |s|
28
+ s.e0 = "MSK"
29
+ s.e1 = "1234"
30
+ s.e2 = "5678"
31
+ end
32
+
33
+ lambda { e3 "TEST" }.should raise_error(NoMethodError)
34
+ seg.to_s.should == "MSK|1234|5678"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::ERR do
5
+ context 'general' do
6
+ before :all do
7
+ @base_err = 'ERR||OBR^1|100^Segment sequence error^HL70357|E|||Missing required OBR segment|Email help desk for further information on this error||||^NET^Internet^helpdesk@hl7.org'
8
+ end
9
+
10
+ it 'creates an ERR segment' do
11
+ lambda do
12
+ err = HL7::Message::Segment::ERR.new( @base_err )
13
+ err.should_not be_nil
14
+ err.to_s.should == @base_err
15
+ end.should_not raise_error
16
+ end
17
+
18
+ it 'allows access to an ERR segment' do
19
+ lambda do
20
+ err = HL7::Message::Segment::ERR.new( @base_err )
21
+ err.severity.should == 'E'
22
+ err.error_location.should == 'OBR^1'
23
+ end.should_not raise_error
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::EVN do
5
+ context 'general' do
6
+ before :all do
7
+ @base = "EVN|A04|20060705000000"
8
+ end
9
+
10
+ it 'allows access to an EVN segment' do
11
+ evn = HL7::Message::Segment::EVN.new @base
12
+ evn.type_code.should == "A04"
13
+ end
14
+
15
+ it 'allows creation of an EVN segment' do
16
+ evn = HL7::Message::Segment::EVN.new
17
+ evn.event_facility="A Facility"
18
+ evn.event_facility.should == 'A Facility'
19
+ evn.recorded_date = Date.new 2001,2,3
20
+ evn.recorded_date.should == "20010203"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: UTF-8
2
+ $: << '../lib'
3
+ require 'ruby-hl7'
4
+
5
+ describe HL7::Message::Segment::MSA do
6
+ context 'general' do
7
+ before :all do
8
+ @base_msa = "MSA|AR|ZZ9380 ERR"
9
+ end
10
+
11
+ it 'creates an MSA segment' do
12
+ lambda do
13
+ msa = HL7::Message::Segment::MSA.new( @base_msa )
14
+ msa.should_not be_nil
15
+ msa.to_s.should == @base_msa
16
+ end.should_not raise_error
17
+ end
18
+
19
+ it 'allows access to an MSA segment' do
20
+ lambda do
21
+ msa = HL7::Message::Segment::MSA.new( @base_msa )
22
+ msa.ack_code.should == "AR"
23
+ msa.control_id.should == "ZZ9380 ERR"
24
+ end.should_not raise_error
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::MSH do
5
+ context 'general' do
6
+ before :all do
7
+ @base = "MSH|^~\\&||ABCHS||AUSDHSV|20070101112951||ADT^A04^ADT_A01|12334456778890|P|2.5|||NE|NE|AU|ASCII|ENGLISH|||AN ORG|||RECNET.ORG"
8
+ end
9
+
10
+ it 'allows access to an MSH segment' do
11
+ msh = HL7::Message::Segment::MSH.new @base
12
+ msh.enc_chars='^~\\&'
13
+ msh.version_id.should == '2.5'
14
+ msh.country_code.should=='AU'
15
+ msh.charset.should=='ASCII'
16
+ msh.sending_responsible_org.should=='AN ORG'
17
+ msh.receiving_network_address.should=='RECNET.ORG'
18
+ end
19
+
20
+ it 'allows creation of an MSH segment' do
21
+ msh = HL7::Message::Segment::MSH.new
22
+ msh.sending_facility="A Facility"
23
+ msh.sending_facility.should == 'A Facility'
24
+ msh.time = DateTime.iso8601('20010203T040506')
25
+ msh.time.should == '20010203040506'
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::NK1 do
5
+ context 'general' do
6
+ before :all do
7
+ @base_nk1 = 'NK1|1|Mum^Martha^M^^^^L|MTH^Mother^HL70063^^^^2.5.1| 444 Home Street^Apt B^Ann Arbor^MI^99999^USA^H|^PRN^PH^^1^555^5552006'
8
+ end
9
+
10
+ it 'creates an NK1 segment' do
11
+ lambda do
12
+ nk1 = HL7::Message::Segment::NK1.new( @base_nk1 )
13
+ nk1.should_not be_nil
14
+ nk1.to_s.should == @base_nk1
15
+ end.should_not raise_error
16
+ end
17
+
18
+ it 'allows access to an NK1 segment' do
19
+ lambda do
20
+ nk1 = HL7::Message::Segment::NK1.new( @base_nk1 )
21
+ nk1.name.should == 'Mum^Martha^M^^^^L'
22
+ nk1.phone_number.should == '^PRN^PH^^1^555^5552006'
23
+ end.should_not raise_error
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe HL7::Message::Segment::OBR do
5
+ context 'general' do
6
+ before :all do
7
+ @base = "OBR|2|^USSSA|0000000567^USSSA|37956^CT ABDOMEN^LN|||199405021550|||||||||||||0000763||||NMR|P||||||R/O TUMOR|202300&BAKER&MARK&E|||01&LOCHLEAR&JUDY|||||||||||||||123"
8
+ @obr = HL7::Message::Segment::OBR.new @base
9
+ end
10
+
11
+ it 'allows access to an OBR segment' do
12
+ @obr.to_s.should == @base
13
+ @obr.e1.should == "2"
14
+ @obr.set_id.should == "2"
15
+ @obr.placer_order_number.should == "^USSSA"
16
+ @obr.filler_order_number.should == "0000000567^USSSA"
17
+ @obr.universal_service_id.should == "37956^CT ABDOMEN^LN"
18
+ end
19
+
20
+ it 'allows modification of an OBR segment' do
21
+ @obr.set_id = 1
22
+ @obr.set_id.should == "1"
23
+ @obr.placer_order_number = "^DMCRES"
24
+ @obr.placer_order_number.should == "^DMCRES"
25
+ end
26
+
27
+ it 'supports the diagnostic_serv_sect_id method' do
28
+ @obr.should respond_to(:diagnostic_serv_sect_id)
29
+ @obr.diagnostic_serv_sect_id.should == "NMR"
30
+ end
31
+
32
+ it 'supports the result_status method' do
33
+ @obr.should respond_to(:result_status)
34
+ @obr.result_status.should == "P"
35
+ end
36
+
37
+ it 'supports the reason_for_study method' do
38
+ @obr.reason_for_study.should == "R/O TUMOR"
39
+ end
40
+
41
+ it 'supports the parent_universal_service_identifier method' do
42
+ @obr.parent_universal_service_identifier.should == "123"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+ describe HL7::Message::Segment::OBX do
4
+ context 'general' do
5
+ before :all do
6
+ @base = "OBX|1|NM|30341-2^Erythrocyte sedimentation rate^LN^815117^ESR^99USI^^^Erythrocyte sedimentation rate||10|mm/h^millimeter per hour^UCUM|0 to 17|N|0.1||F|||20110331140551-0800||Observer|||20110331150551-0800|^A Site|||Century Hospital^^^^^NIST-AA-1&2.16.840.1.113883.3.72.5.30.1&ISO^XX^^^987|2070 Test Park^^Los Angeles^CA^90067^USA^B^^06037|2343242^Knowsalot^Phil^J.^III^Dr.^^^NIST-AA-1&2.16.840.1.113883.3.72.5.30.1&ISO^L^^^DN"
7
+ end
8
+
9
+ it 'allows access to an OBX segment' do
10
+ obx = HL7::Message::Segment::OBX.new @base
11
+ obx.set_id.should == "1"
12
+ obx.value_type.should == "NM"
13
+ obx.observation_id.should == "30341-2^Erythrocyte sedimentation rate^LN^815117^ESR^99USI^^^Erythrocyte sedimentation rate"
14
+ obx.observation_sub_id.should == ""
15
+ obx.observation_value.should == "10"
16
+ obx.units.should == "mm/h^millimeter per hour^UCUM"
17
+ obx.references_range.should == "0 to 17"
18
+ obx.abnormal_flags.should == "N"
19
+ obx.probability.should == "0.1"
20
+ obx.nature_of_abnormal_test.should == ""
21
+ obx.observation_result_status.should == "F"
22
+ obx.effective_date_of_reference_range.should == ""
23
+ obx.user_defined_access_checks.should == ""
24
+ obx.observation_date.should == "20110331140551-0800"
25
+ obx.producer_id.should == ""
26
+ obx.responsible_observer.should == "Observer"
27
+ obx.observation_site.should == '^A Site'
28
+ obx.observation_method.should == ""
29
+ obx.equipment_instance_id.should == ""
30
+ obx.analysis_date.should == "20110331150551-0800"
31
+ obx.performing_organization_name.should == "Century Hospital^^^^^NIST-AA-1&2.16.840.1.113883.3.72.5.30.1&ISO^XX^^^987"
32
+ obx.performing_organization_address.should == "2070 Test Park^^Los Angeles^CA^90067^USA^B^^06037"
33
+ obx.performing_organization_medical_director.should == "2343242^Knowsalot^Phil^J.^III^Dr.^^^NIST-AA-1&2.16.840.1.113883.3.72.5.30.1&ISO^L^^^DN"
34
+ end
35
+
36
+ it 'allows creation of an OBX segment' do
37
+ lambda do
38
+ obx = HL7::Message::Segment::OBX.new
39
+ obx.value_type = "TESTIES"
40
+ obx.observation_id = "HR"
41
+ obx.observation_sub_id = "2"
42
+ obx.observation_value = "SOMETHING HAPPENned"
43
+ end.should_not raise_error
44
+ end
45
+ end
46
+ end