tax_generator 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,78 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <taxonomies>
3
+ <taxonomy>
4
+ <taxonomy_name>World</taxonomy_name>
5
+ <node atlas_node_id = "355064" ethyl_content_object_id="82534" geo_id = "355064">
6
+ <node_name>Africa</node_name>
7
+ <node atlas_node_id = "355611" ethyl_content_object_id="3210" geo_id = "355611">
8
+ <node_name>South Africa</node_name>
9
+ <node atlas_node_id = "355612" ethyl_content_object_id="35474" geo_id = "355612">
10
+ <node_name>Cape Town</node_name>
11
+ <node atlas_node_id = "355613" ethyl_content_object_id="" geo_id = "355613">
12
+ <node_name>Table Mountain National Park</node_name>
13
+ </node>
14
+ </node>
15
+ <node atlas_node_id = "355614" ethyl_content_object_id="" geo_id = "355614">
16
+ <node_name>Free State</node_name>
17
+ <node atlas_node_id = "355615" ethyl_content_object_id="1000550692" geo_id = "355615">
18
+ <node_name>Bloemfontein</node_name>
19
+ </node>
20
+ </node>
21
+ <node atlas_node_id = "355616" ethyl_content_object_id="" geo_id = "355616">
22
+ <node_name>Gauteng</node_name>
23
+ <node atlas_node_id = "355617" ethyl_content_object_id="37710" geo_id = "355617">
24
+ <node_name>Johannesburg</node_name>
25
+ </node>
26
+ <node atlas_node_id = "355618" ethyl_content_object_id="1000548256" geo_id = "355618">
27
+ <node_name>Pretoria</node_name>
28
+ </node>
29
+ </node>
30
+ <node atlas_node_id = "355619" ethyl_content_object_id="" geo_id = "355619">
31
+ <node_name>KwaZulu-Natal</node_name>
32
+ <node atlas_node_id = "355620" ethyl_content_object_id="43725" geo_id = "355620">
33
+ <node_name>Durban</node_name>
34
+ </node>
35
+ <node atlas_node_id = "355621" ethyl_content_object_id="1000576780" geo_id = "355621">
36
+ <node_name>Pietermaritzburg</node_name>
37
+ </node>
38
+ </node>
39
+ <node atlas_node_id = "355622" ethyl_content_object_id="" geo_id = "355622">
40
+ <node_name>Mpumalanga</node_name>
41
+ <node atlas_node_id = "355623" ethyl_content_object_id="67561" geo_id = "355623">
42
+ <node_name>Kruger National Park</node_name>
43
+ </node>
44
+ </node>
45
+ <node atlas_node_id = "355624" ethyl_content_object_id="" geo_id = "355624">
46
+ <node_name>The Drakensberg</node_name>
47
+ <node atlas_node_id = "355625" ethyl_content_object_id="" geo_id = "355625">
48
+ <node_name>Royal Natal National Park</node_name>
49
+ </node>
50
+ </node>
51
+ <node atlas_node_id = "355626" ethyl_content_object_id="" geo_id = "355626">
52
+ <node_name>The Garden Route</node_name>
53
+ <node atlas_node_id = "355627" ethyl_content_object_id="" geo_id = "355627">
54
+ <node_name>Oudtshoorn</node_name>
55
+ </node>
56
+ <node atlas_node_id = "355628" ethyl_content_object_id="" geo_id = "355628">
57
+ <node_name>Tsitsikamma Coastal National Park</node_name>
58
+ </node>
59
+ </node>
60
+ </node>
61
+ <node atlas_node_id = "355629" ethyl_content_object_id="3263" geo_id = "355629">
62
+ <node_name>Sudan</node_name>
63
+ <node atlas_node_id = "355630" ethyl_content_object_id="" geo_id = "355630">
64
+ <node_name>Eastern Sudan</node_name>
65
+ <node atlas_node_id = "355631" ethyl_content_object_id="" geo_id = "355631">
66
+ <node_name>Port Sudan</node_name>
67
+ </node>
68
+ </node>
69
+ <node atlas_node_id = "355632" ethyl_content_object_id="" geo_id = "355632">
70
+ <node_name>Khartoum</node_name>
71
+ </node>
72
+ </node>
73
+ <node atlas_node_id = "355633" ethyl_content_object_id="3272" geo_id = "355633">
74
+ <node_name>Swaziland</node_name>
75
+ </node>
76
+ </node>
77
+ </taxonomy>
78
+ </taxonomies>
@@ -63,6 +63,15 @@ module TaxGenerator
63
63
  @output_folder = job['output_folder']
64
64
  end
65
65
 
66
+ # finds all the nodes in the tree with the given name
67
+ #
68
+ # @return [Array<Tree::TreeNode>]
69
+ #
70
+ # @api public
71
+ def atlas_node
72
+ @taxonomy.find_by_name(@job_id).first
73
+ end
74
+
66
75
  # renders the template and creates new file with the template html
67
76
  #
68
77
  # @return [void]
@@ -91,7 +100,6 @@ module TaxGenerator
91
100
  #
92
101
  # @api public
93
102
  def fetch_atlas_details
94
- atlas_node = @taxonomy[@job_id]
95
103
  content = @destination.present? ? TaxGenerator::Destination.new(@destination).to_hash : {}
96
104
  content.merge(details: atlas_node)
97
105
  end
@@ -23,12 +23,30 @@ module TaxGenerator
23
23
  def initialize(file_path)
24
24
  @document = nokogiri_xml(file_path)
25
25
  taxonomy_root = @document.at_xpath('//taxonomy_name')
26
- @root_node = Tree::TreeNode.new(taxonomy_root.content, nil)
27
- @document.xpath('//node').pmap do |taxonomy_node|
26
+ @root_node = Tree::TreeNode.new('0', taxonomy_root.content)
27
+ @document.xpath('.//taxonomy/node').pmap do |taxonomy_node|
28
28
  add_node(taxonomy_node, @root_node)
29
29
  end
30
30
  end
31
31
 
32
+ # finds a node by the name in the tree list
33
+ #
34
+ # @param [String] node_id the name of the node that needs to be found
35
+ # @param [Tree::TreeNode] node the node that will be used to search in
36
+ # @param [Array] list the list that holds the nodes
37
+ #
38
+ # @return [void]
39
+ #
40
+ # @api public
41
+ def find_by_name(node_id, node = @root_node, list = [])
42
+ if node.name.to_s == node_id.to_s
43
+ list << node
44
+ else
45
+ node.children.each { |child| find_by_name(node_id, child, list) }
46
+ end
47
+ list.compact
48
+ end
49
+
32
50
  # gets the atlas_id from the nokogiri element and then searches first child whose name is 'node_name'
33
51
  # and uses this to insert the node
34
52
  # @see #insert_node
@@ -77,7 +95,7 @@ module TaxGenerator
77
95
  return unless taxonomy_node.children.any?
78
96
  tax_node = add_taxonomy_node(taxonomy_node, node)
79
97
  taxonomy_node.xpath('./node').pmap do |child_node|
80
- add_taxonomy_node(child_node, tax_node) if tax_node.present?
98
+ add_node(child_node, tax_node) if tax_node.present?
81
99
  end
82
100
  end
83
101
 
@@ -15,7 +15,7 @@ module TaxGenerator
15
15
  # major release version
16
16
  MAJOR = 0
17
17
  # minor release version
18
- MINOR = 1
18
+ MINOR = 2
19
19
  # tiny release version
20
20
  TINY = 0
21
21
  # prelease version ( set this only if it is a prelease)
@@ -8,12 +8,12 @@ describe TaxGenerator::FileCreator do
8
8
  let(:subject) { TaxGenerator::FileCreator.new }
9
9
  let(:first_destination) { "some destination" }
10
10
 
11
- let(:fake_node) { FakeNode.new('something') }
11
+ let(:fake_node) { mock }
12
12
 
13
13
  let(:atlas_id_value) { 'some atlas id value' }
14
- let(:atlas_id) { AtlasID.new(atlas_id_value) }
15
- let(:fake_tilt) {FakeTilt.new}
16
- let(:fake_condition) {FakeCelluloidCondition.new}
14
+ let(:atlas_id) { mock }
15
+ let(:fake_tilt) {mock}
16
+
17
17
 
18
18
  let(:job) { { atlas_id: atlas_id_value, taxonomy: fake_node, destination: first_destination, output_folder: output_folder } }
19
19
  before(:each) do
@@ -26,9 +26,7 @@ describe TaxGenerator::FileCreator do
26
26
  Tilt.stubs(:new).returns(fake_tilt)
27
27
  fake_tilt.stubs(:render).returns(true)
28
28
  subject.stubs(:mark_job_completed).returns(true)
29
- default_processor.stubs(:condition).returns(fake_condition)
30
29
  default_processor.stubs(:all_workers_finished).returns(false)
31
- fake_condition.stubs(:signal).returns(true)
32
30
  end
33
31
 
34
32
  context 'checks the job keys' do
@@ -59,6 +57,7 @@ describe TaxGenerator::FileCreator do
59
57
  context "job related " do
60
58
 
61
59
  before(:each) do
60
+
62
61
  subject.work(job, default_processor)
63
62
  default_processor.stubs(:register_worker_for_job).returns(true)
64
63
  end
@@ -72,16 +71,9 @@ describe TaxGenerator::FileCreator do
72
71
  end
73
72
 
74
73
  it 'mark_job_completed' do
75
- default_processor.jobs.expects(:[]).with(subject.job_id).returns(job)
76
- job.expects(:[]=).with('status', 'finished')
77
- default_processor.expects(:all_workers_finished).returns(false)
78
- subject.mark_job_completed
79
- end
80
-
81
- it 'mark_job_completed and signals complete' do
82
- default_processor.expects(:all_workers_finished).returns(true)
83
- fake_condition.stubs(:signal).with('completed')
74
+ default_processor.register_jobs(job)
84
75
  subject.mark_job_completed
76
+ expect(subject.processor.jobs[subject.job_id]).to eq(job.stringify_keys)
85
77
  end
86
78
 
87
79
  it 'fetch_atlas_details' do
@@ -89,7 +81,7 @@ describe TaxGenerator::FileCreator do
89
81
  TaxGenerator::Destination.expects(:new).with(first_destination).returns(first_destination)
90
82
  first_destination.stubs(:to_hash).returns({})
91
83
  actual = subject.fetch_atlas_details
92
- expect(actual).to eq({details: fake_node, root: root})
84
+ expect(actual).to eq({details: fake_node})
93
85
  end
94
86
  end
95
87
 
@@ -1,30 +1,83 @@
1
1
  # encoding:utf-8
2
2
  require 'spec_helper'
3
3
  describe TaxGenerator::Processor do
4
- let(:subject) {TaxGenerator::Processor.new}
5
-
6
- # it 'parses the xml' do
7
- # subject.expects(:nokogiri_xml).with(destinations_file_path).returns(destination_xml)
8
- # actual = subject.destinations
9
- # expect(actual).to eq destination_xml
10
- # end
11
- #
12
- # it 'generates the files with 0' do
13
- # subject.expects(:create_file).with(0, taxonomy, nil).returns(true)
14
- # subject.generate_files(taxonomy)
15
- # end
16
- #
17
- # it 'searches the destinations' do
18
- # subject.destinations.expects(:xpath).with('//destination').returns([])
19
- # subject.generate_files(taxonomy)
20
- # end
21
- #
22
- # it 'tries to create all the files for all destinations' do
23
- # destination_xml.xpath('//destination').each do |destination|
24
- # destination.expects(:attributes).returns('atlas_id' => atlas_id)
25
- # subject.expects(:create_file).with(atlas_id.value, taxonomy, destination).returns(true)
26
- # end
27
- # subject.generate_files(taxonomy)
28
- # end
4
+
5
+ let(:actor_pool) {mock}
6
+ let(:workers) { TaxGenerator::FileCreator.new }
7
+
8
+ before(:each) do
9
+ Celluloid::SupervisionGroup.stubs(:run!).returns(actor_pool)
10
+ actor_pool.stubs(:pool).returns(workers)
11
+ Actor.current.stubs(:link).returns(true)
12
+ end
13
+
14
+ context "intialize" do
15
+
16
+ it 'boots the celluloid' do
17
+ Celluloid.expects(:boot).returns(true)
18
+ TaxGenerator::Processor.new
19
+ end
20
+
21
+ it 'runs the supervision group' do
22
+ Celluloid::SupervisionGroup.expects(:run!).returns(actor_pool)
23
+ TaxGenerator::Processor.new
24
+ end
25
+
26
+ it 'creates the pool of workers' do
27
+ actor_pool.expects(:pool).with(TaxGenerator::FileCreator, as: :workers, size: 50).returns(workers)
28
+ TaxGenerator::Processor.new
29
+ end
30
+
31
+ it "links the actor to the current actor" do
32
+ Actor.current.stubs(:link).returns(true)
33
+ TaxGenerator::Processor.new
34
+ end
35
+
36
+
37
+ end
38
+
39
+ context "intialize" do
40
+
41
+ specify { expect(subject.jobs).to eq({}) }
42
+ specify { expect(subject.job_to_worker).to eq({}) }
43
+ specify { expect(subject.worker_to_job).to eq({}) }
44
+ end
45
+
46
+
47
+
48
+ it 'returns the input folder' do
49
+ subject.options.expects(:fetch).with(:input_dir, "#{root}/data/input")
50
+ subject.input_folder
51
+ end
52
+
53
+ it 'returns the output_folder' do
54
+ subject.options.expects(:fetch).with(:output_dir, "#{root}/data/output")
55
+ subject.output_folder
56
+ end
57
+
58
+ it 'returns the taxonomy_filename' do
59
+ subject.options.expects(:fetch).with(:taxonomy_filename, 'taxonomy.xml')
60
+ subject.taxonomy_file_name
61
+ end
62
+
63
+ it 'returns the destinations_file_name' do
64
+ subject.options.expects(:fetch).with(:destinations_filename, 'destinations.xml')
65
+ subject.destinations_file_name
66
+ end
67
+
68
+ it 'returns the taxonomy_file_path' do
69
+ File.expects(:join).with(subject.input_folder, subject.taxonomy_file_name).returns(true)
70
+ subject.taxonomy_file_path
71
+ end
72
+ it 'returns the destinations_file_path' do
73
+ File.expects(:join).with(subject.input_folder, subject.destinations_file_name).returns(true)
74
+ subject.destinations_file_path
75
+ end
76
+
77
+ it 'returns the static_output_dir' do
78
+ File.expects(:join).with(subject.output_folder, 'static').returns(true)
79
+ subject.static_output_dir
80
+ end
81
+
29
82
 
30
83
  end
@@ -12,31 +12,17 @@ SimpleCov.start 'rails' do
12
12
  at_exit {}
13
13
  end
14
14
 
15
- # fake class to use for elements that need atlas id as attribute
16
- AtlasID = Struct.new(:value)
17
15
 
18
- # fake class to use for elements that need atlas id as attribute
19
- FakeNode = Struct.new(:value)
20
16
 
21
- #fake class for tilt
22
- class FakeTilt
23
-
24
- def render(*args) end
25
- end
26
17
 
27
18
  require 'bundler/setup'
28
19
  require 'tax_generator'
29
20
  require_relative '../lib/tax_generator/helpers/application_helper'
30
21
  require 'rspec'
31
- # fake class for actors
22
+ #fake actor class
32
23
  class Actor
33
24
  def self.current
34
- end
35
- end
36
-
37
- class FakeCelluloidCondition
38
-
39
- def signal
25
+ Struct.new(:link)
40
26
  end
41
27
  end
42
28
 
@@ -47,7 +33,6 @@ RSpec.configure do |config|
47
33
 
48
34
  config.mock_with :mocha
49
35
 
50
-
51
36
  config.after(:suite) do
52
37
  if SimpleCov.running
53
38
  SimpleCov::Formatter::HTMLFormatter.new.format(SimpleCov.result)
@@ -58,3 +43,9 @@ RSpec.configure do |config|
58
43
  end
59
44
 
60
45
  Celluloid.logger = nil
46
+
47
+ at_exit do
48
+ Celluloid::Actor.all.each do |actor|
49
+ Celluloid::Actor.kill(actor)
50
+ end
51
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tax_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - bogdanRada