Skip to content

Commit

Permalink
First cucumber tests for Database Load
Browse files Browse the repository at this point in the history
  • Loading branch information
Edouard Maffert committed Jun 27, 2017
1 parent 502247e commit e5136ea
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -7,6 +7,7 @@ group :test do
gem 'netrc'
gem 'http-cookie'
gem 'rest-client'
gem 'pg'
end

group :development do
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -37,6 +37,7 @@ GEM
multi_test (0.1.2)
multi_xml (0.5.5)
netrc (0.11.0)
pg (0.21.0)
rake (12.0.0)
rest-client (2.0.0)
http-cookie (>= 1.0.2, < 2.0)
Expand All @@ -63,6 +64,7 @@ DEPENDENCIES
license_finder
mime-types
netrc
pg
rake
rest-client
rspec-expectations
Expand Down
Expand Up @@ -12,7 +12,6 @@
<ns2:RequestorRef>NINOXE:default</ns2:RequestorRef>
<ns2:MessageIdentifier>StopMonitoring:Test:0</ns2:MessageIdentifier>
</ServiceRequestInfo>

<Request version="2.0:FR-IDF-2.4">
<ns2:LineRef>NINOXE:StopPoint:SP:27:LOC</ns2:LineRef>
<ns2:RequestTimestamp>2016-09-22T07:54:52.977Z</ns2:RequestTimestamp>
Expand Down
28 changes: 28 additions & 0 deletions features/database.feature
@@ -0,0 +1,28 @@
Feature: Test Database Load

@database
Scenario: Load Referentials and partners
Given the table "referentials" has the following data:
| referential_id | slug | settings | tokens |
| '6ba7b814-9dad-11d1-0000-00c04fd430c8' | 'first' | '{"key":"value"}' | '["testtoken"]' |
| '6ba7b814-9dad-11d1-0001-00c04fd430c8' | 'second' | '{}' | '["testtoken"]' |
And the table "partners" has the following data:
| id | referential_id | slug | settings | connector_types |
| '6ba7b814-9dad-11d1-0002-00c04fd430c8' | '6ba7b814-9dad-11d1-0000-00c04fd430c8' | 'first_partner' | '{"remote_url": "http://localhost", "remote_objectid_kind": "Reflex", "remote_credential": "edwig_cred"}' | '["siri-stop-monitoring-request-collector", "siri-check-status-client"]' |
| '6ba7b814-9dad-11d1-0003-00c04fd430c8' | '6ba7b814-9dad-11d1-0001-00c04fd430c8' | 'second_partner' | '{}' | '[]' |
When I start Edwig
Then one Referential has the following attributes:
| Id | 6ba7b814-9dad-11d1-0000-00c04fd430c8 |
| Slug | first |
| Settings | {"key":"value"} |
And one Referential has the following attributes:
| Id | 6ba7b814-9dad-11d1-0001-00c04fd430c8 |
| Slug | second |
And one Partner in Referential "first" has the following attributes:
| Id | 6ba7b814-9dad-11d1-0002-00c04fd430c8 |
| Slug | first_partner |
| Settings | {"remote_url": "http://localhost", "remote_objectid_kind": "Reflex", "remote_credential": "edwig_cred"} |
| ConnectorTypes | ["siri-check-status-client", "siri-stop-monitoring-request-collector"] |
And one Partner in Referential "second" has the following attributes:
| Id | 6ba7b814-9dad-11d1-0003-00c04fd430c8 |
| Slug | second_partner |
17 changes: 17 additions & 0 deletions features/step_definitions/database.rb
@@ -0,0 +1,17 @@
Given(/^the table "([^"]*)" has the following data:$/) do |table_name, datas|
data_array = datas.raw

request_string = "INSERT INTO #{table_name} (#{data_array.shift.join(',')}) VALUES"

data_array.each do |data|
request_string += "(#{data.join(',')}),"
end
request_string.gsub!(/,$/, ';')

conn = PG.connect dbname: $database
conn.exec(request_string)
end

When(/^I start Edwig$/) do
start_edwig()
end
12 changes: 12 additions & 0 deletions features/step_definitions/partners.rb
Expand Up @@ -12,3 +12,15 @@ def partners_path(attributes = {})
raise err
end
end

Then(/^one Partner(?: in Referential "([^"]+)")? has the following attributes:$/) do |referential, attributes|
response = RestClient.get partners_path(referential: referential), {content_type: :json, :Authorization => "Token token=#{$token}"}
response_array = api_attributes(response.body)

parsed_attributes = model_attributes(attributes)
found_value = response_array.find{|a| a["Id"] == parsed_attributes["Id"]}

expect(found_value).not_to be_nil

expect(found_value).to include(parsed_attributes)
end
12 changes: 12 additions & 0 deletions features/step_definitions/referentials.rb
Expand Up @@ -45,3 +45,15 @@ def referential_path(id)
expect(responseHash.find{|a| a["Slug"] == referential}).to be_nil
end
end

Then(/^one Referential has the following attributes:$/) do |attributes|
response = RestClient.get referentials_path, {content_type: :json, :Authorization => "Token token=#{$adminToken}"}
response_array = api_attributes(response.body)

parsed_attributes = model_attributes(attributes)
found_value = response_array.find{|a| a["Id"] == parsed_attributes["Id"]}

expect(found_value).not_to be_nil

expect(found_value).to include(parsed_attributes)
end
6 changes: 5 additions & 1 deletion features/support/attributes.rb
Expand Up @@ -78,6 +78,10 @@ def model_attributes(table)
attributes["ObjectIDs"] = JSON.parse("{ #{objectids} }")
end

if settings = attributes["Settings"]
attributes["Settings"] = JSON.parse(settings)
end

attributes
end

Expand Down Expand Up @@ -110,5 +114,5 @@ def has_attributes(response_array, attributes)
expect(found_value).not_to be_nil

parsed_attributes.delete("ObjectIDs")
expect(found_value).to include(parsed_attributes)
expect(found_value).to include(parsed_attributes)
end
19 changes: 19 additions & 0 deletions features/support/database_hook.rb
@@ -0,0 +1,19 @@
require 'pg'

$database = 'edwig_test'

After('@database') do
# Truncate all tables
conn = PG.connect dbname: $database
conn.exec(
"DO $$DECLARE statements CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_schema='public';
BEGIN
FOR stmt IN statements LOOP
IF stmt.table_name <> 'gorp_migrations' THEN
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.table_name) || ' CASCADE;';
END IF;
END LOOP;
END$$;")
end
6 changes: 5 additions & 1 deletion features/support/edwig.rb
Expand Up @@ -4,7 +4,7 @@
$adminToken = "6ceab96a-8d97-4f2a-8d69-32569a38fc64"
$token = "testtoken"

Before do
def start_edwig
unless File.directory?("tmp")
FileUtils.mkdir_p("tmp")
end
Expand All @@ -28,6 +28,10 @@
end
end

Before('~@database') do
start_edwig()
end

After do
pid = IO.read("tmp/pid")
Process.kill('KILL',pid.to_i)
Expand Down

0 comments on commit e5136ea

Please sign in to comment.