Skip to content

Commit

Permalink
Add change_log when updating fachinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
ngiger committed Nov 2, 2015
1 parent cd6af43 commit 66a6b61
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 43 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -12,6 +12,7 @@ gem 'archive-tarsimple', '1.1.1'
gem 'bigdecimal', '1.1.0'
gem 'builder', '3.2.2'
gem 'dbd-pg', '0.3.9'
gem 'diffy'
gem 'deprecated', '2.0.1'
gem 'facets', '1.8.54'
gem 'fastthread', '1.0.7'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -39,6 +39,7 @@ GEM
debugger-ruby_core_source (1.3.8)
deprecated (2.0.1)
diff-lcs (1.2.5)
diffy (3.0.7)
domain_name (0.5.24)
unf (>= 0.0.5, < 1.0.0)
facets (1.8.54)
Expand Down Expand Up @@ -209,6 +210,7 @@ DEPENDENCIES
dbd-pg (= 0.3.9)
dbi!
deprecated (= 2.0.1)
diffy
facets (= 1.8.54)
fastthread (= 1.0.7)
flexmock (~> 1.3.0)
Expand Down
45 changes: 28 additions & 17 deletions src/model/fachinfo.rb
Expand Up @@ -10,27 +10,28 @@
require 'util/language'
require 'util/searchterms'
require 'model/registration_observer'
require 'diffy'
require 'util/today'

module ODDB
class Fachinfo
class ChangeLogItem
attr_accessor :email, :time, :chapter, :language, :text
end
attr_accessor :links
class ChangeLogItem
attr_accessor :email, :time, :chapter, :language
end
attr_accessor :links
include Persistence
include Language
include RegistrationObserver
ODBA_SERIALIZABLE = ['@change_log']
def add_change_log_item(email, chapter, language, text = '')
item = ChangeLogItem.new
item.email = email
item.time = Time.now
item.chapter = chapter
ODBA_SERIALIZABLE = ['@change_log']
def add_change_log_item(email, chapter, language)
item = ChangeLogItem.new
item.email = email
item.time = Time.now
item.chapter = chapter
item.language = language
item.text = text
self.change_log.push(item)
self.odba_store
end
self.change_log.push(item)
self.odba_store
end
def article_codes(expired=true)
codes = []
@registrations.collect { |reg|
Expand Down Expand Up @@ -65,9 +66,6 @@ def atc_class
reg.atc_classes.first
end
end
def change_log
@change_log ||= []
end
def company
if(reg = @registrations.first)
reg.company
Expand Down Expand Up @@ -165,6 +163,19 @@ def photos(image_size="Thumbnail")
end
class FachinfoDocument
include Persistence
class ChangeLogItem
attr_accessor :time, :diff
end
def add_change_log_item(old_text, new_text)
item = ChangeLogItem.new
item.time = @@today
item.diff = Diffy::Diff.new(old_text, new_text)
self.change_log.push(item)
self.odba_store
end
def change_log
@change_log ||= []
end
attr_accessor :name, :galenic_form, :composition
attr_accessor :effects, :kinetic, :indications, :usage
attr_accessor :restrictions, :unwanted_effects
Expand Down
28 changes: 25 additions & 3 deletions src/plugin/text_info.rb
Expand Up @@ -132,16 +132,36 @@ def TextInfoPlugin::replace_textinfo(app, new_ti, container, type) # description
end
app.update(old_ti.pointer, {:descriptions => old_ti.descriptions})
else
require 'pry';
binding.pry if not container or not container.respond_to?(:pointer)
binding.pry if not new_ti or not new_ti.respond_to?(:pointer)
app.update(container.pointer, {type => new_ti.pointer})
end
end
def TextInfoPlugin::add_change_log_item(text_item, old_text, new_text, lang)
$stdout.puts("add_change_log_item: update #{text_item.class} lang #{lang} #{text_item.class} #{old_text[-30..-1]} -> #{new_text[-30..-1]}")
text_item.add_change_log_item(old_text, new_text)
text_item.odba_isolated_store
end
def TextInfoPlugin::store_fachinfo(app, reg, fis)
existing = reg.fachinfo
$stdout.puts("store_fachinfo: #{reg.iksnr} existing #{existing.class}")
ptr = Persistence::Pointer.new(:fachinfo).creator
if existing
old_text_de = existing.de.text
old_text_fr = existing.fr.text
ptr = existing.pointer
end
app.update ptr, fis
updated_fi = app.update ptr, fis
if existing
if old_text_de && old_text_de != (new_text = updated_fi.de.text)
TextInfoPlugin::add_change_log_item(updated_fi.de, old_text_de, new_text, 'de')
end
if old_text_fr && old_text_fr != (new_text = updated_fi.fr.text)
TextInfoPlugin::add_change_log_item(updated_fi.fr, old_text_fr, new_text, 'fr')
end
end
updated_fi
end
def store_orphaned iksnr, info, point=:orphaned_fachinfo
if info
Expand Down Expand Up @@ -241,6 +261,7 @@ def update_fachinfo name, iksnrs_from_xml, fis, fi_flags
## identification of Pseudo-Fachinfos happens at download-time.
# but because we still want to extract the iksnrs, we just mark them
# and defer inaction until here:
require 'pry'; binding.pry if fis.is_a?(ArgumentError)
unless fi_flags[:pseudo] || fis.empty?
LogFile.debug "update_fachinfo #{name} iksnr #{iksnr} store_fachinfo #{fi_flags} #{fis.keys} ATC #{fis.values.first.atc_code}"
fachinfo ||= TextInfoPlugin::store_fachinfo(@app, reg, fis)
Expand Down Expand Up @@ -1175,6 +1196,7 @@ def parse_and_update(names, type)
unless infos.empty?
_infos = {}
[:de, :fr].map do |lang|
LogFile.debug "_infos #{lang} #{infos[lang]} #{infos[lang].class}"
unless strange?(infos[lang])
_infos[lang] = infos[lang]
end
Expand Down Expand Up @@ -1414,8 +1436,8 @@ def import_swissmedicinfo_by_iksnrs(iksnrs, target)
LogFile.debug "import_swissmedicinfo_by_iksnrs #{msg}"
next
end
LogFile.debug "import_swissmedicinfo_by_iksnrs iksnr #{iksnr.inspect} #{names.inspect}"
TextInfoPlugin::create_registration(@app, @@iksnrs_meta_info[iksnr])
LogFile.debug "import_swissmedicinfo_by_iksnrs iksnr #{iksnr.inspect} #{names.inspect} #{@@iksnrs_meta_info[iksnr].inspect} found? #{@app.registration(iksnr).inspect}"
TextInfoPlugin::create_registration(@app, @@iksnrs_meta_info[iksnr]) unless @app.registration(iksnr)
[:de, :fr].each do |lang|
keys.each_pair do |typ, type|
names[lang][typ] = [extract_matched_name(iksnr, type, lang)]
Expand Down
2 changes: 1 addition & 1 deletion src/util/oddbapp.rb
Expand Up @@ -2066,7 +2066,7 @@ def _migrate_child_to_utf8 child, queue, table, iconv, opts={}
end
when Float, Fixnum, TrueClass, FalseClass, NilClass,
ODDB::Persistence::Pointer, Symbol, Time, Date, ODDB::Dose, Quanty,
ODDB::Util::Money, ODDB::Fachinfo::ChangeLogItem, ODDB::AtcNode,
ODDB::Util::Money, ODDB::FachinfoDocument::ChangeLogItem, ODDB::Fachinfo::ChangeLogItem, ODDB::AtcNode,
DateTime, ODDB::NotificationLogger::LogEntry, ODDB::Text::Format,
ODDB::YusStub, ODDB::Text::ImageLink
# do nothing
Expand Down
70 changes: 49 additions & 21 deletions test/test_model/fachinfo.rb
Expand Up @@ -35,27 +35,6 @@ def test_add_registration
@fachinfo.add_registration(reg)
assert_equal([reg], @fachinfo.registrations)
end
def test_add_change_log_item
item, = @fachinfo.add_change_log_item 'test@email.ch', :indications, :de
item = @fachinfo.change_log[0]
assert_instance_of ODDB::Fachinfo::ChangeLogItem, item
assert_equal 'test@email.ch', item.email
assert_equal :indications, item.chapter
assert_equal :de, item.language
assert_equal [item], @fachinfo.change_log
end
def test_add_indication
item = @fachinfo.add_change_log_item 'test@email.ch', :indications, :de
item = @fachinfo.change_log[0]
assert_instance_of ODDB::Fachinfo::ChangeLogItem, item
assert_equal 'test@email.ch', item.email
assert_equal :indications, item.chapter
assert_equal :de, item.language
assert_equal [item], @fachinfo.change_log
skip "Must FIX!!! @indications"
assert @indications, 'add_change_log_item should add @indications'
assert_equal 1, @indications.size
end
def test_atc_class
reg1 = flexmock :atc_classes => ['first atc', 'second atc']
reg2 = flexmock :atc_classes => ['third atc']
Expand Down Expand Up @@ -200,6 +179,13 @@ class TestFachinfoDocument <Minitest::Test
include FlexMock::TestCase
def setup
@doc = FachinfoDocument.new
@old_text = 'old text'
@new_text = 'new text'
@expected = "-old text
\\ Kein Zeilenumbruch am Dateiende.
+new text
\\ Kein Zeilenumbruch am Dateiende.
"
end
def test_first_chapter
ue = flexmock 'unwanted_effects'
Expand All @@ -215,5 +201,47 @@ def test_first_chapter
@doc.composition = flexmock 'composition'
assert_equal gf, @doc.first_chapter
end
def test_add_change_log_item
item = @doc.add_change_log_item 'old text', 'new text'
item = @doc.change_log[0]
assert_instance_of ODDB::FachinfoDocument::ChangeLogItem, item
assert_equal [item], @doc.change_log
assert_equal @@today, item.time
assert_instance_of Diffy::Diff, item.diff
expected = "-old text
\\ Kein Zeilenumbruch am Dateiende.
+new text
\\ Kein Zeilenumbruch am Dateiende.
"
assert_equal expected, item.diff.to_s
end
def test_fachinfo_change_log_text
old_fi = ODDB::FachinfoDocument2001.new
old_fi.composition = ODDB::Text::Chapter.new
old_fi.composition.heading = 'Zusammensetzung'
old_paragraph = old_fi.composition.next_section.next_paragraph
old_paragraph << 'line 1
line 2
line 3
'
@doc = ODDB::FachinfoDocument2001.new
@doc.composition = ODDB::Text::Chapter.new
@doc.composition.heading = 'Zusammensetzung'
changed_paragraph = @doc.composition.next_section.next_paragraph
changed_paragraph << 'line 1
Changed line 2
line 3
'
expected = " Zusammensetzung
line 1
-line 2
+Changed line 2
line 3
\\ Kein Zeilenumbruch am Dateiende.
"
@doc.add_change_log_item(old_fi.text, @doc.text)
assert_equal(expected, @doc.change_log[0].diff.to_s)
assert_equal(@@today, @doc.change_log[0].time)
end
end
end
2 changes: 1 addition & 1 deletion test/test_plugin/text_info.rb
Expand Up @@ -876,6 +876,6 @@ def update_notify_simple(klass, subj, update_method=:update, args=[])
assert_equal(1, @app.registration(iksnr).packages.size)
assert_equal('000', @app.registration(iksnr).packages.first.ikscd)
assert_equal('drug_name', @app.registration(iksnr).name_base)
end if false
end
end
end
20 changes: 20 additions & 0 deletions test/test_plugin/text_info_swissmedicinfo.rb
Expand Up @@ -34,6 +34,26 @@ def read_packages
end
end

class TestTextInfoChangeLogin <MiniTest::Test
include FlexMock::TestCase
def setup
end # Fuer Problem mit fachinfo italic

def teardown
end
def test_odba_store
old_text = "Some text\nLine 2\nLine 3"
new_text = "Some text\nLine 2 was changed\nLine 3"
txt_diff = Diffy::Diff.new(old_text, new_text)
result = Marshal.dump(txt_diff)
expected = "Line 2
Line 3\x06;\bT:\r@string2I\"(Some text
Line 2 was changed
Line 3\x06;\bT"
assert(result.index(expected) > 0)
end
end

class TestTextInfoPluginAipsMetaData <MiniTest::Test
include FlexMock::TestCase
unless defined?(@@datadir)
Expand Down

0 comments on commit 66a6b61

Please sign in to comment.