README.md in bq_factory-0.1.0 vs README.md in bq_factory-0.1.1
- old
+ new
@@ -26,81 +26,112 @@
### Configuration
Add configuration to initializer or spec_helper.
-```
+```ruby
# RSpec
# spec/spec_helper.rb
BqFactory.configure do |config|
config.project_id = 'google_project_id'
config.keyfile_path = '/path/to/keyfile.json' # from google developer console
end
```
-and, setup factories in fixture file.
+### specify schema
+This pattern is fetch schema from hash(not call api access).
+
+First, setup factories in fixture file:
+
+```ruby
+BqFactory.register :user, schema: [{ name: 'name', type: 'INTEGER' }, { name: 'age', type: 'INTEGER' }]
```
-BqFactory.define do
- # Reference target of dataset and tables in options.
- # Factory read schema from reference target!
- factory :user, dataset: 'my_dataset', table: 'my_table'
+
+And, In your test code:
+
+```
+describe 'test query' do
+ before(:all) do
+ BqFactory.create_dataset!(:test_dataset)
+ end
+
+ describe 'query is valid' do
+ before { BqFactory.create_view(:test_dataset, :test_view, users) }
+ subject { BqFactory.query(query) }
+
+ let(:users) { [{ name: 'alice', age: 20 }] }
+ let(:query) { 'SELECT * FROM [test_dataset.test_view]' }
+
+ it { expect(subject.first).to eq users.first }
+ end
end
```
-### Use Case
+### fetch schema from bigquery
-Example, if `user` table schema is this:
+This pattern is fetch schema from bigquery(call api access).
+First, setup factories in fixture file:
+
+```ruby
+# Reference target of dataset and tables in options.
+# Factory fetch schema from bigquery
+BqFactory.register :user, dataset: 'my_dataset' # => reference my_dataset.user table
+BqFactory.register :user, dataset: 'my_dataset', table: 'my_table' # => reference my_dataset.my_table
+```
+
+If register `:user, dataset: 'test_dataset'` and `user` table schema is this:
+
|column_name|type|
|:----|:----|
|name|STRING|
|age|INTEGER|
|create_at|TIMESTAMP|
|height|FLOAT|
|admin|BOOLEAN|
-In your test code:
+And, In your test code:
```ruby
# RSpec
-describe TestClass do
+describe 'query test' do
before(:all) do
BqFactory.create_dataset!('test_dataset') # => create test dataset
end
let(:alice) { { name: 'alice', age: 20, create_at: "2016-01-01 00:00:00", height: 150.1, admin: true } }
let(:bob) { { name: 'bob', age: 30, create_at: "2016-01-01 00:00:00", height: 170.1, admin: false } }
describe 'build query' do
- subject { BqFactory.build_query 'user', alice }
+ subject { BqFactory.build_query :user, alice }
let(:query) { 'SELECT * FROM (SELECT "alice" AS name, 20 AS age, TIMESTAMP("2016-01-01 00:00:00") AS create_at, 150.1 AS height, true AS admin )' }
it { is_expected.to eq query }
end
describe 'from Hash' do
- before { BqFactory.create_view 'test_dataset', 'test_view', alice }
+ before { BqFactory.create_view :test_dataset, :test_view1, alice }
# => Build query 'SELECT * FROM (SELECT "alice" AS name, 20 AS age, TIMESTAMP("2016-01-01 00:00:00") AS create_at, 150.1 AS height, true AS admin )'
# And create view "test_view" to "test_dataset"
let(:query) { "SELECT * FROM [test_dataset.test_view]" }
subject { BqFactory.query(query) }
- it { expect(subject.first.name).to eq alise["name"] }
+ it { expect(subject.first).to eq alice
end
describe 'from Array' do
- before { BqFactory.create_view 'test_dataset', 'test_view', [alice, bob] }
+ before { BqFactory.create_view :test_dataset, :test_view2, [alice, bob] }
# => Build query 'SELECT * FROM (SELECT "alice" AS name, 20 AS age, TIMESTAMP("2016-01-01 00:00:00") AS create_at, 150.1 AS height, true AS admin ), (SELECT "bob" AS name, 30 AS age, TIMESTAMP("2016-01-01 00:00:00") AS create_at, 170.1 AS height, false AS admafterin)'
# And create view "test_view" to "test_dataset"
let(:query) { "SELECT * FROM [test_dataset.test_view]" }
subject { BqFactory.query(query) }
- it { expect(subject.first.name).to eq alise["name"] }
- it { expect(subject.last.name).to eq bob["name"] }
+ it { expect(subject.first).to eq alise }
+ it { expect(subject.last).to eq bob }
end
after(:all) do
BqFactory.delete_dataset!('test_dataset') # => clean up!
end
@@ -110,16 +141,16 @@
## Testing
### Install dependencies
```shell
-bundle install
+$ bundle install
```
### Run rspec
-```
-bundle exec rspec
+```shell
+$ bundle exec rspec
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bq_factory. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.