kibutsu 1.0.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.
- checksums.yaml +7 -0
- data/lib/kibutsu.rb +26 -0
- data/lib/kibutsu/database_connection.rb +56 -0
- data/lib/kibutsu/fixture.rb +60 -0
- data/lib/kibutsu/fixture_finder.rb +16 -0
- data/lib/kibutsu/fixture_loader.rb +52 -0
- data/lib/kibutsu/fixture_table.rb +33 -0
- data/lib/kibutsu/fixture_world.rb +63 -0
- data/lib/kibutsu/foreign_key_column.rb +12 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 626061649fb56e9beae8c3a6fe0ff0b7d88a75d7
|
4
|
+
data.tar.gz: 70fa236ab50e786f1f68699bc2409cb81ec678c0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1644bbd8069590b62aeb6c9f9374b5be4d9a10d2c9b7208896a39d70497ca530084864b29bd8b45b4a8fe579b13ed9a679879198f910e0526976eaf7779f6780
|
7
|
+
data.tar.gz: 708578e97cb24cefe8cda8cf289f1b5e89f51a1f9fccda216c103fc7f14406f087347a9069a0eca0687fd9d888a5e66fbff07202fc1a702785c518143335d597
|
data/lib/kibutsu.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'kibutsu/fixture_world'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Main module containing the public API of Kibutsu
|
5
|
+
#
|
6
|
+
module Kibutsu
|
7
|
+
def self.load_fixtures!(database_connection_url, fixtures_path)
|
8
|
+
fixture_world = FixtureWorld.instance
|
9
|
+
fixture_world.load(database_connection_url, fixtures_path)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.fixture_name_to_id(fixture_name)
|
13
|
+
fixture_name.to_s.hash
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.dont_care(type)
|
17
|
+
case type
|
18
|
+
when :string
|
19
|
+
"don't care about this string"
|
20
|
+
when :number
|
21
|
+
0
|
22
|
+
when :boolean
|
23
|
+
false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'sequel'
|
2
|
+
|
3
|
+
require_relative 'foreign_key_column'
|
4
|
+
|
5
|
+
module Kibutsu
|
6
|
+
# Only class that directly interacts with Sequel. Used for retrieving data
|
7
|
+
# from the database and inserting fixtures into it.
|
8
|
+
class DatabaseConnection
|
9
|
+
def initialize(connection_string)
|
10
|
+
@connection = Sequel.connect(connection_string)
|
11
|
+
end
|
12
|
+
|
13
|
+
def insert_fixture_tables(fixture_tables)
|
14
|
+
fixture_tables.each do |fixture_table|
|
15
|
+
insert_table(fixture_table)
|
16
|
+
insert_fixture_tables(fixture_table.foreign_key_source_tables)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def column_names(table_name)
|
21
|
+
connection.schema(table_name.to_s).map(&:first)
|
22
|
+
end
|
23
|
+
|
24
|
+
def foreign_key_columns(table_name)
|
25
|
+
connection.foreign_key_list(table_name.to_s).map do |foreign_key_info|
|
26
|
+
ForeignKeyColumn.new(
|
27
|
+
FixtureWorld.instance.find_table(table_name.to_s),
|
28
|
+
foreign_key_info[:columns].first.to_s,
|
29
|
+
FixtureWorld.instance.find_table(foreign_key_info[:table].to_s)
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def table_names
|
35
|
+
connection.tables
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
attr_reader :connection
|
41
|
+
|
42
|
+
def insert_table(fixture_table)
|
43
|
+
connection[fixture_table.name.to_sym].delete
|
44
|
+
|
45
|
+
fixture_table.fixtures.each do |fixture|
|
46
|
+
insert_fixture(fixture)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def insert_fixture(fixture)
|
51
|
+
fixture_table = fixture.table
|
52
|
+
attributes = fixture.enriched_attributes
|
53
|
+
connection[fixture_table.name.to_sym].insert(attributes)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
module Kibutsu
|
4
|
+
# Corresponds to one named fixture from a fixture file.
|
5
|
+
class Fixture
|
6
|
+
TIMESTAMP_COLUMN_NAMES = %w[created_at updated_at].freeze
|
7
|
+
|
8
|
+
def initialize(table, name, attributes)
|
9
|
+
@table = table
|
10
|
+
@name = name
|
11
|
+
@attributes = attributes
|
12
|
+
end
|
13
|
+
|
14
|
+
def enriched_attributes
|
15
|
+
enriched_attributes = enrich_with_id(attributes)
|
16
|
+
enriched_attributes = enrich_with_foreign_keys(enriched_attributes)
|
17
|
+
enrich_with_timestamps(enriched_attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :table, :name, :attributes
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def enrich_with_id(attr)
|
25
|
+
attr.merge('id' => Kibutsu.fixture_name_to_id(@name))
|
26
|
+
end
|
27
|
+
|
28
|
+
def enrich_with_foreign_keys(attr)
|
29
|
+
foreign_key_columns.each do |foreign_key_column|
|
30
|
+
foreign_fixture_name = foreign_table_reference(attr, foreign_key_column)
|
31
|
+
if foreign_fixture_name
|
32
|
+
attr[foreign_key_column.name] =
|
33
|
+
Kibutsu.fixture_name_to_id(foreign_fixture_name)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
attr
|
37
|
+
end
|
38
|
+
|
39
|
+
def foreign_key_columns
|
40
|
+
table.foreign_key_columns
|
41
|
+
end
|
42
|
+
|
43
|
+
def enrich_with_timestamps(attr)
|
44
|
+
date_time = DateTime.now
|
45
|
+
TIMESTAMP_COLUMN_NAMES.each do |timestamp_column_name|
|
46
|
+
next unless table.column_names.include? timestamp_column_name.to_sym
|
47
|
+
attr[timestamp_column_name] = date_time
|
48
|
+
end
|
49
|
+
attr
|
50
|
+
end
|
51
|
+
|
52
|
+
def foreign_table_reference(attr, foreign_key_column)
|
53
|
+
attr.delete(column_name_to_model_name(foreign_key_column.name))
|
54
|
+
end
|
55
|
+
|
56
|
+
def column_name_to_model_name(column_name)
|
57
|
+
column_name.to_s[0..-4]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Kibutsu
|
2
|
+
# Finds fixture files with the right file extensions to load.
|
3
|
+
class FixtureFinder
|
4
|
+
def initialize(fixtures_path)
|
5
|
+
@fixtures_path = fixtures_path
|
6
|
+
end
|
7
|
+
|
8
|
+
def fixture_file_paths
|
9
|
+
Dir[File.join(fixtures_path, '**', '*.{yml,yml.erb}')]
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
attr_reader :fixtures_path
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'erb'
|
3
|
+
|
4
|
+
require_relative 'fixture_table'
|
5
|
+
require_relative 'fixture'
|
6
|
+
|
7
|
+
module Kibutsu
|
8
|
+
# Loads fixtures from files, optionally with ERB pre-processing and creates
|
9
|
+
# Fixture objects from them.
|
10
|
+
class FixtureLoader
|
11
|
+
def initialize(fixture_file_path, database_connection)
|
12
|
+
@fixture_file_path = fixture_file_path
|
13
|
+
@database_connection = database_connection
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_fixture_tables
|
17
|
+
file_content = File.read(fixture_file_path)
|
18
|
+
yaml_content = if fixture_file_path.end_with? '.yml.erb'
|
19
|
+
run_erb(file_content)
|
20
|
+
else
|
21
|
+
file_content
|
22
|
+
end
|
23
|
+
fixtures_hash = load_fixture(yaml_content)
|
24
|
+
build_fixture_tables(fixtures_hash)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
attr_reader :fixture_file_path, :database_connection
|
30
|
+
|
31
|
+
def build_fixture_tables(fixtures_hash)
|
32
|
+
fixtures_hash.map do |table_name, fixtures|
|
33
|
+
table = Kibutsu::FixtureWorld.instance.find_table(table_name)
|
34
|
+
|
35
|
+
raise "Couldn't find table with name #{table_name}" if table.nil?
|
36
|
+
|
37
|
+
fixtures.each do |fixture_name, attributes|
|
38
|
+
table << Kibutsu::Fixture.new(table, fixture_name, attributes)
|
39
|
+
end
|
40
|
+
table
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def run_erb(file_content)
|
45
|
+
ERB.new(file_content).result(binding)
|
46
|
+
end
|
47
|
+
|
48
|
+
def load_fixture(yaml_content)
|
49
|
+
YAML.safe_load(yaml_content)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Kibutsu
|
2
|
+
# A database table in the fixture world.
|
3
|
+
class FixtureTable
|
4
|
+
def initialize(name)
|
5
|
+
@name = name
|
6
|
+
@column_names = nil
|
7
|
+
@foreign_key_columns = nil
|
8
|
+
@foreign_key_source_tables = []
|
9
|
+
@fixtures = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def <<(fixture)
|
13
|
+
@fixtures << fixture
|
14
|
+
end
|
15
|
+
|
16
|
+
def foreign_key_target_tables
|
17
|
+
@foreign_key_columns.map(&:target_table)
|
18
|
+
end
|
19
|
+
|
20
|
+
def foreign_key_columns=(foreign_key_columns)
|
21
|
+
@foreign_key_columns = foreign_key_columns
|
22
|
+
|
23
|
+
foreign_key_columns.each do |column|
|
24
|
+
column.target_table.foreign_key_source_tables << self
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader(
|
29
|
+
:name, :fixtures, :foreign_key_source_tables, :foreign_key_columns
|
30
|
+
)
|
31
|
+
attr_accessor :column_names
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
require_relative 'fixture_finder'
|
4
|
+
require_relative 'fixture_loader'
|
5
|
+
require_relative 'database_connection'
|
6
|
+
|
7
|
+
module Kibutsu
|
8
|
+
# Singleton class that holds all the information about fixtures and fixture
|
9
|
+
# tables. This class really ties whole the gem together.
|
10
|
+
class FixtureWorld
|
11
|
+
include Singleton
|
12
|
+
|
13
|
+
def load(database_connection_url, fixtures_path)
|
14
|
+
@database_connection = Kibutsu::DatabaseConnection.new(
|
15
|
+
database_connection_url
|
16
|
+
)
|
17
|
+
|
18
|
+
load_fixtures(fixtures_path)
|
19
|
+
database_connection.insert_fixture_tables(tables_without_dependencies)
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_fixtures(fixtures_path)
|
23
|
+
initialize_empty_fixture_tables
|
24
|
+
fill_fixture_table_information
|
25
|
+
|
26
|
+
fixture_file_paths = FixtureFinder.new(fixtures_path).fixture_file_paths
|
27
|
+
fixture_file_paths.each do |fixture_file_path|
|
28
|
+
FixtureLoader.new(
|
29
|
+
fixture_file_path, database_connection
|
30
|
+
).load_fixture_tables
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def find_table(table_name)
|
35
|
+
@fixture_tables.find { |table| table.name == table_name }
|
36
|
+
end
|
37
|
+
|
38
|
+
attr_reader :fixture_tables
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
attr_reader :fixtures_path, :database_connection
|
43
|
+
|
44
|
+
def initialize_empty_fixture_tables
|
45
|
+
@fixture_tables = database_connection.table_names.map do |table_name|
|
46
|
+
FixtureTable.new(table_name.to_s)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def fill_fixture_table_information
|
51
|
+
@fixture_tables.each do |fixture_table|
|
52
|
+
fixture_table.column_names =
|
53
|
+
database_connection.column_names(fixture_table.name)
|
54
|
+
fixture_table.foreign_key_columns =
|
55
|
+
database_connection.foreign_key_columns(fixture_table.name)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def tables_without_dependencies
|
60
|
+
@fixture_tables.select { |table| table.foreign_key_target_tables.none? }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Kibutsu
|
2
|
+
# Corresponds to a foreign key column in the database.
|
3
|
+
class ForeignKeyColumn
|
4
|
+
def initialize(source_table, name, target_table)
|
5
|
+
@source_table = source_table
|
6
|
+
@name = name
|
7
|
+
@target_table = target_table
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_reader :source_table, :name, :target_table
|
11
|
+
end
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kibutsu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ferdinand Niedermann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.49.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.49.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: |-
|
84
|
+
Kibutsu is an easy-to-use database fixture library.
|
85
|
+
|
86
|
+
The only dependency kibutsu has is the sequel gem. Thus, you may use it with any project that uses sequel, e.g. with a hanami app!
|
87
|
+
email: ferdinand.niedermann@gmail.com
|
88
|
+
executables: []
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- lib/kibutsu.rb
|
93
|
+
- lib/kibutsu/database_connection.rb
|
94
|
+
- lib/kibutsu/fixture.rb
|
95
|
+
- lib/kibutsu/fixture_finder.rb
|
96
|
+
- lib/kibutsu/fixture_loader.rb
|
97
|
+
- lib/kibutsu/fixture_table.rb
|
98
|
+
- lib/kibutsu/fixture_world.rb
|
99
|
+
- lib/kibutsu/foreign_key_column.rb
|
100
|
+
homepage: http://rubygems.org/gems/kibutsu
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata: {}
|
104
|
+
post_install_message:
|
105
|
+
rdoc_options: []
|
106
|
+
require_paths:
|
107
|
+
- lib
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 2.6.11
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: A fixture library for sequel
|
124
|
+
test_files: []
|