dbd 0.0.5 → 0.0.6

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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/Guardfile +3 -2
  3. data/HISTORY.txt +5 -0
  4. data/dbd.gemspec +1 -1
  5. data/docs/stories/013_read_graph_from_CSV.txt +10 -0
  6. data/lib/dbd.rb +1 -1
  7. data/lib/dbd/fact.rb +23 -1
  8. data/lib/dbd/fact/id.rb +3 -3
  9. data/lib/dbd/fact/subject.rb +3 -3
  10. data/lib/dbd/graph.rb +25 -4
  11. data/lib/dbd/provenance_fact.rb +5 -8
  12. data/lib/dbd/provenance_resource.rb +21 -13
  13. data/lib/dbd/{rdf.rb → rdf_base.rb} +0 -0
  14. data/lib/dbd/resource.rb +25 -23
  15. data/lib/dbd/time_stamp.rb +13 -2
  16. data/lib/dbd/version.rb +1 -1
  17. data/spec/factories/fact.rb +23 -0
  18. data/spec/factories/fact/id.rb +14 -0
  19. data/spec/factories/fact/subject.rb +18 -0
  20. data/spec/factories/graph.rb +23 -0
  21. data/spec/factories/provenance_resource.rb +3 -3
  22. data/spec/factories/time_stamp.rb +12 -0
  23. data/spec/lib/dbd/fact/{collection_spec.rb → collection/collection_spec.rb} +0 -0
  24. data/spec/lib/dbd/fact/factory_spec.rb +82 -0
  25. data/spec/lib/dbd/fact/id/factory_spec.rb +20 -0
  26. data/spec/lib/dbd/fact/id/id_spec.rb +27 -0
  27. data/spec/lib/dbd/fact/methods_spec.rb +117 -0
  28. data/spec/lib/dbd/fact/new_spec.rb +91 -0
  29. data/spec/lib/dbd/fact/subject/factory_spec.rb +33 -0
  30. data/spec/lib/dbd/fact/subject/subject_spec.rb +27 -0
  31. data/spec/lib/dbd/graph/add_to_graph_spec.rb +139 -0
  32. data/spec/lib/dbd/graph/factory_spec.rb +33 -0
  33. data/spec/lib/dbd/graph/from_csv_spec.rb +29 -0
  34. data/spec/lib/dbd/{graph_spec.rb → graph/to_csv_spec.rb} +0 -120
  35. data/spec/lib/dbd/helpers/{ordered_set_collection_spec.rb → ordered_set_collection/ordered_set_collection_spec.rb} +0 -0
  36. data/spec/lib/dbd/helpers/{uuid_spec.rb → uuid/uuid_spec.rb} +0 -0
  37. data/spec/lib/dbd/performance_spec.rb +3 -1
  38. data/spec/lib/dbd/provenance_fact/factory_spec.rb +24 -0
  39. data/spec/lib/dbd/provenance_fact/methods_spec.rb +78 -0
  40. data/spec/lib/dbd/provenance_fact/new_spec.rb +51 -0
  41. data/spec/lib/dbd/{provenance_resource_spec.rb → provenance_resource/provenance_resource_spec.rb} +6 -10
  42. data/spec/lib/dbd/{rdf_base_spec.rb → rdf_base/rdf_base_spec.rb} +0 -0
  43. data/spec/lib/dbd/{resource_spec.rb → resource/collection_spec.rb} +8 -47
  44. data/spec/lib/dbd/resource/factory_spec.rb +14 -0
  45. data/spec/lib/dbd/resource/new_spec.rb +44 -0
  46. data/spec/lib/dbd/time_stamp/comparisons_spec.rb +64 -0
  47. data/spec/lib/dbd/time_stamp/factory_spec.rb +17 -0
  48. data/spec/lib/dbd/time_stamp/methods_spec.rb +37 -0
  49. data/spec/lib/dbd/time_stamp/new_spec.rb +38 -0
  50. metadata +66 -29
  51. data/spec/lib/dbd/fact/id_spec.rb +0 -19
  52. data/spec/lib/dbd/fact/subject_spec.rb +0 -19
  53. data/spec/lib/dbd/fact_spec.rb +0 -216
  54. data/spec/lib/dbd/provenance_fact_spec.rb +0 -120
  55. data/spec/lib/dbd/time_stamp_spec.rb +0 -115
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ class Fact
5
+ describe Subject do
6
+ describe ".new" do
7
+ it "creates a Subject" do
8
+ subject.should be_a(described_class)
9
+ end
10
+
11
+ it "takes an optional :uuid option argument" do
12
+ uuid = "fe75eae3-cb14-4495-b726-7ecba8798b6d"
13
+ subject = described_class.new(uuid: uuid)
14
+ subject.to_s.should == uuid
15
+ end
16
+ end
17
+
18
+ it "#to_s is a UUID string" do
19
+ subject.to_s.should match(Helpers::UUID.regexp)
20
+ end
21
+
22
+ it ".regexp has a regexp for the to_s" do
23
+ described_class.regexp.should == Helpers::UUID.regexp
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,139 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe Graph do
5
+
6
+ def new_subject
7
+ Fact.new_subject
8
+ end
9
+
10
+ let(:data_fact) { Factories::Fact.data_fact(new_subject, new_subject) }
11
+ let(:fact_no_subject) { Factories::Fact.data_fact(new_subject, nil) }
12
+ let(:fact_no_provenance) { Factories::Fact.data_fact(nil, new_subject) }
13
+
14
+ let(:provenance_facts) { Factories::Fact::Collection.provenance_facts(new_subject) }
15
+ let(:provenance_fact_1) { provenance_facts.first }
16
+
17
+ let(:subject_regexp) { Fact::Subject.regexp }
18
+
19
+ let(:provenance_resource) { Factories::ProvenanceResource.provenance_resource }
20
+ let(:resource) { Factories::Resource.facts_resource(provenance_resource.subject) }
21
+ let(:resource_array) { [provenance_resource, resource]}
22
+
23
+ describe "create a graph" do
24
+ it "does not fail" do
25
+ described_class.new # should_not raise_error
26
+ end
27
+ end
28
+
29
+ describe "<< " do
30
+ describe "a Fact" do
31
+ it "a data_fact does not fail" do
32
+ subject << data_fact
33
+ end
34
+
35
+ it "a provenance_fact does not fail" do
36
+ subject << provenance_fact_1
37
+ end
38
+
39
+ it "two facts does not fail" do
40
+ subject << provenance_fact_1
41
+ subject << data_fact
42
+ end
43
+
44
+ it "fact with missing subject raises FactError" do
45
+ lambda { subject << fact_no_subject } . should raise_error FactError
46
+ end
47
+
48
+ it "fact with missing provenance raises FactError" do
49
+ lambda { subject << fact_no_provenance } . should raise_error FactError
50
+ end
51
+ end
52
+
53
+ describe "sets the time_stamp and adds a random time (1..999 nanoseconds) if needed" do
54
+
55
+ # NOTE: reduced the far_future from 2500 to 2250 as work around for
56
+ # http://jira.codehaus.org/browse/JRUBY-7095
57
+ let(:far_future) { TimeStamp.new(time: Time.new(2250,1,1,12,0,0).utc) }
58
+
59
+ it "don't touch the time_stamp if already set" do
60
+ data_fact.time_stamp = far_future
61
+ subject << data_fact
62
+ subject.first.time_stamp.should == far_future
63
+ end
64
+
65
+ describe "sets the time_stamp if not yet set" do
66
+
67
+ let(:near_future) { Time.now.utc + 100}
68
+ let(:fake_time_stamp) { TimeStamp.new(time: near_future) }
69
+
70
+ before(:each) do
71
+ # get this before setting the stub
72
+ fake_time_stamp # get this before setting the stub
73
+ end
74
+
75
+ it "sets it (to TimeStamp.new)" do
76
+ TimeStamp.stub(:new).and_return(fake_time_stamp)
77
+ data_fact.time_stamp.should be_nil # assert pre-condition
78
+ subject << data_fact
79
+ subject.first.time_stamp.should == fake_time_stamp
80
+ end
81
+
82
+ it "sends a slightly higher time_stamp than newest_time_stamp if Time.now <= newest_time_stamp" do
83
+ subject.stub(:newest_time_stamp).and_return(fake_time_stamp)
84
+ subject << data_fact
85
+ subject.first.time_stamp.should > fake_time_stamp
86
+ (subject.first.time_stamp - fake_time_stamp).should < Rational('1/1000_000') # 1 us
87
+ end
88
+ end
89
+ end
90
+
91
+ describe "a ProvenanceResource and a Resource" do
92
+
93
+ it "does not fail" do
94
+ subject << provenance_resource
95
+ end
96
+
97
+ it "Adds the facts from the provenance_resource to the graph" do
98
+ subject << provenance_resource
99
+ subject.size.should == 2
100
+ end
101
+
102
+ it "Adds the facts from the provenance_resource and the resource to the graph" do
103
+ subject << provenance_resource
104
+ subject << resource
105
+ subject.size.should == 4
106
+ subject.first.should be_a(ProvenanceFact)
107
+ subject.last.class.should == Fact
108
+ end
109
+ end
110
+
111
+ describe "an array of Resources" do
112
+ it "does not fail" do
113
+ subject << resource_array
114
+ end
115
+
116
+ it "Adds the facts from the provenance_resource and the resource to the graph" do
117
+ subject << resource_array
118
+ subject.first.class.should == ProvenanceFact
119
+ subject.last.class.should == Fact
120
+ subject.size.should == 4
121
+ end
122
+
123
+ it "goes 3 levels over collection deep" do
124
+ subject << [resource_array]
125
+ subject.size.should == 4
126
+ end
127
+
128
+ it "works with different levels deep in 1 collection" do
129
+ subject << [provenance_resource, [[resource]]]
130
+ subject.size.should == 4
131
+ end
132
+ end
133
+
134
+ it "returns self" do
135
+ (subject << Factories::Fact::Collection.provenance_facts(new_subject)).should be_a(described_class)
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe Graph do
5
+ describe "factory" do
6
+ describe "with only provenance facts" do
7
+
8
+ let(:only_provenance) { Factories::Graph.only_provenance }
9
+
10
+ it "is a Graph" do
11
+ only_provenance.should be_a(Graph)
12
+ end
13
+
14
+ it "has some facts" do
15
+ only_provenance.size.should >= 2
16
+ end
17
+ end
18
+
19
+ describe "full" do
20
+
21
+ let(:full) { Factories::Graph.full }
22
+
23
+ it "is a Graph" do
24
+ full.should be_a(Graph)
25
+ end
26
+
27
+ it "full has many facts" do
28
+ full.size.should >= 4
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe Graph do
5
+
6
+ def new_subject
7
+ Fact.new_subject
8
+ end
9
+
10
+ def validate_round_trip(graph)
11
+ graph_from_csv = Graph.from_CSV(StringIO.new(graph.to_CSV))
12
+
13
+ # use to_csv as the "identity function" for Graph
14
+ graph.to_CSV.should == graph_from_csv.to_CSV
15
+ end
16
+
17
+ describe "#from_CSV reads back a csv exported graph correctly" do
18
+ it "is correct for a graph with only provenance_facts" do
19
+ graph = Factories::Graph.only_provenance
20
+ validate_round_trip(graph)
21
+ end
22
+
23
+ it "is correct for a graph with facts and provenance_facts" do
24
+ graph = Factories::Graph.only_provenance
25
+ validate_round_trip(graph)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -7,10 +7,6 @@ module Dbd
7
7
  Fact.new_subject
8
8
  end
9
9
 
10
- let(:data_fact) { Factories::Fact.data_fact(new_subject, new_subject) }
11
- let(:fact_no_subject) { Factories::Fact.data_fact(new_subject, nil) }
12
- let(:fact_no_provenance) { Factories::Fact.data_fact(nil, new_subject) }
13
-
14
10
  let(:provenance_facts) { Factories::Fact::Collection.provenance_facts(new_subject) }
15
11
  let(:provenance_fact_1) { provenance_facts.first }
16
12
  let(:fact_2_3) { Factories::Fact::Collection.fact_2_3(provenance_fact_1.subject) }
@@ -18,122 +14,6 @@ module Dbd
18
14
  let(:subject_regexp) { Fact::Subject.regexp }
19
15
  let(:id_regexp) { Fact::ID.regexp }
20
16
 
21
- let(:provenance_resource) { Factories::ProvenanceResource.provenance_resource }
22
- let(:resource) { Factories::Resource.facts_resource(provenance_resource.subject) }
23
- let(:resource_array) { [provenance_resource, resource]}
24
-
25
- describe "create a graph" do
26
- it "does not fail" do
27
- described_class.new # should_not raise_error
28
- end
29
- end
30
-
31
- describe "<< " do
32
- describe "a Fact" do
33
- it "a data_fact does not fail" do
34
- subject << data_fact
35
- end
36
-
37
- it "a provenance_fact does not fail" do
38
- subject << provenance_fact_1
39
- end
40
-
41
- it "two facts does not fail" do
42
- subject << provenance_fact_1
43
- subject << data_fact
44
- end
45
-
46
- it "fact with missing subject raises FactError" do
47
- lambda { subject << fact_no_subject } . should raise_error FactError
48
- end
49
-
50
- it "fact with missing provenance raises FactError" do
51
- lambda { subject << fact_no_provenance } . should raise_error FactError
52
- end
53
- end
54
-
55
- describe "sets the time_stamp and adds a random time (1..999 nanoseconds) if needed" do
56
-
57
- # NOTE: reduced the far_future from 2500 to 2250 as work around for
58
- # http://jira.codehaus.org/browse/JRUBY-7095
59
- let(:far_future) { TimeStamp.new(time: Time.new(2250,1,1,12,0,0).utc) }
60
-
61
- it "don't touch the time_stamp if already set" do
62
- data_fact.time_stamp = far_future
63
- subject << data_fact
64
- subject.first.time_stamp.should == far_future
65
- end
66
-
67
- describe "sets the time_stamp if not yet set" do
68
-
69
- let(:near_future) { Time.now.utc + 100}
70
- let(:fake_time_stamp) { TimeStamp.new(time: near_future) }
71
-
72
- before(:each) do
73
- # get this before setting the stub
74
- fake_time_stamp # get this before setting the stub
75
- end
76
-
77
- it "sets it (to TimeStamp.new)" do
78
- TimeStamp.stub(:new).and_return(fake_time_stamp)
79
- data_fact.time_stamp.should be_nil # assert pre-condition
80
- subject << data_fact
81
- subject.first.time_stamp.should == fake_time_stamp
82
- end
83
-
84
- it "sends a slightly higher time_stamp than newest_time_stamp if Time.now <= newest_time_stamp" do
85
- subject.stub(:newest_time_stamp).and_return(fake_time_stamp)
86
- subject << data_fact
87
- subject.first.time_stamp.should > fake_time_stamp
88
- (subject.first.time_stamp - fake_time_stamp).should < Rational('1/1000_000') # 1 us
89
- end
90
- end
91
- end
92
-
93
- describe "a ProvenanceResource and a Resource" do
94
-
95
- it "does not fail" do
96
- subject << provenance_resource
97
- end
98
-
99
- it "Adds the facts from the provenance_resource to the graph" do
100
- subject << provenance_resource
101
- subject.size.should == 2
102
- end
103
-
104
- it "Adds the facts from the provenance_resource and the resource to the graph" do
105
- subject << provenance_resource
106
- subject << resource
107
- subject.size.should == 4
108
- subject.first.should be_a(ProvenanceFact)
109
- subject.last.class.should == Fact
110
- end
111
- end
112
-
113
- describe "an array of Resources" do
114
- it "does not fail" do
115
- subject << resource_array
116
- end
117
-
118
- it "Adds the facts from the provenance_resource and the resource to the graph" do
119
- subject << resource_array
120
- subject.first.class.should == ProvenanceFact
121
- subject.last.class.should == Fact
122
- subject.size.should == 4
123
- end
124
-
125
- it "goes 3 levels over collection deep" do
126
- subject << [resource_array]
127
- subject.size.should == 4
128
- end
129
-
130
- it "works with different levels deep in 1 collection" do
131
- subject << [provenance_resource, [[resource]]]
132
- subject.size.should == 4
133
- end
134
- end
135
- end
136
-
137
17
  describe "#to_CSV with only provenance_facts" do
138
18
  before do
139
19
  provenance_facts.each_with_index do |provenance_fact, index|
@@ -6,6 +6,7 @@ module Dbd
6
6
 
7
7
  include Benchmark
8
8
 
9
+ # no let because we do not want memoized
9
10
  def new_subject
10
11
  Fact.new_subject
11
12
  end
@@ -33,7 +34,8 @@ module Dbd
33
34
  puts "\nDuration for inserting #{NUMBER_OF_FACTS} facts in the in-memory graph was #{duration*1000_000/NUMBER_OF_FACTS} us PER FACT"
34
35
  graph.size.should == 2 * NUMBER_OF_FACTS + 1
35
36
  duration.should < 0.000_15 * NUMBER_OF_FACTS
36
- # typ. 37 us on Mac Ruby 2.0.0
37
+ # typ. 37 us on Mac Ruby 2.0.0 (on 2013-05-15 over 15K iterations)
38
+ # typ. 45 us on Mac Ruby 2.0.0 (on 2013-06-05 over 10K iterations)
37
39
  # typ. 60 us on Mac jruby 1.7.3
38
40
  end
39
41
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe ProvenanceFact do
5
+
6
+ describe "Factories do not fail" do
7
+ it "Factories::ProvenanceFact.context is OK" do
8
+ Factories::ProvenanceFact.context.should_not be_nil
9
+ end
10
+
11
+ it "Factories::ProvenanceFact.created_by is OK" do
12
+ Factories::ProvenanceFact.created_by.should_not be_nil
13
+ end
14
+
15
+ it "Factories::ProvenanceFact.original_source is OK" do
16
+ Factories::ProvenanceFact.original_source.should_not be_nil
17
+ end
18
+
19
+ it "Factories::ProvenanceFact.new_subject is OK" do
20
+ Factories::ProvenanceFact.new_subject.should be_a(ProvenanceFact.new_subject.class)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ module Dbd
4
+ describe ProvenanceFact do
5
+
6
+ let(:subject) { described_class.new_subject }
7
+
8
+ let(:provenance_fact_1) do
9
+ Factories::ProvenanceFact.context(subject)
10
+ end
11
+
12
+ let(:provenance_fact_created) do
13
+ Factories::ProvenanceFact.created(subject)
14
+ end
15
+
16
+ describe "short" do
17
+ it "for a provenance fact shows [ prov ], subj, predicate, object" do
18
+ provenance_fact_1.short.should match(/^\[ prov \] : [0-9a-f]{8} : https:\/\/data\.vandenabeel : public$/)
19
+ end
20
+
21
+ it "for a provenance fact with non string object also works" do
22
+ provenance_fact_created.short.should match(/^\[ prov \] : [0-9a-f]{8} : dcterms:created : \d{4}/)
23
+ end
24
+ end
25
+
26
+ describe "errors" do
27
+ it "the factory has no errors" do
28
+ provenance_fact_1.errors.should be_empty
29
+ end
30
+
31
+ describe "with a provenance_subject" do
32
+
33
+ before(:each) do
34
+ provenance_fact_1.stub(:provenance_subject).and_return(subject)
35
+ end
36
+
37
+ it "errors returns an array with 1 error message" do
38
+ provenance_fact_1.errors.single.should match(/Provenance subject should not be present in Provenance Fact/)
39
+ end
40
+ end
41
+
42
+ describe "without subject" do
43
+
44
+ before(:each) do
45
+ provenance_fact_1.stub(:subject).and_return(nil)
46
+ end
47
+
48
+ it "errors returns an array with an error message" do
49
+ provenance_fact_1.errors.single.should match(/Subject is missing/)
50
+ end
51
+ end
52
+ end
53
+
54
+ describe "update_used_provenance_subjects" do
55
+ it "does nothing for a provenance_fact" do
56
+ h = {}
57
+ provenance_fact_1.update_used_provenance_subjects(h)
58
+ h.should be_empty
59
+ end
60
+ end
61
+
62
+ describe "attributes and values" do
63
+ it "there are 6 attributes" do
64
+ described_class.attributes.size.should == 6
65
+ end
66
+
67
+ it "there are 6 values" do
68
+ provenance_fact_1.values.size.should == 6
69
+ end
70
+ end
71
+
72
+ describe "provenance_fact?" do
73
+ it "is true for ProvenanceFact or derived from it" do
74
+ provenance_fact_1.provenance_fact?.should be_true
75
+ end
76
+ end
77
+ end
78
+ end