Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
excelsieur
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
open-source
excelsieur
Commits
d04b051d
Verified
Commit
d04b051d
authored
6 years ago
by
Alexis Reigel
Browse files
Options
Downloads
Patches
Plain Diff
convert test to spec syntax
parent
4d59b234
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!6
Convert test to spec syntax
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
test/excelsior_test.rb
+83
-62
83 additions, 62 deletions
test/excelsior_test.rb
test/test_helper.rb
+6
-0
6 additions, 0 deletions
test/test_helper.rb
with
89 additions
and
62 deletions
test/excelsior_test.rb
+
83
−
62
View file @
d04b051d
...
@@ -13,90 +13,111 @@ class User < ActiveRecord::Base
...
@@ -13,90 +13,111 @@ class User < ActiveRecord::Base
validates
:first_name
,
presence:
true
validates
:first_name
,
presence:
true
end
end
class
ExcelsiorTest
<
Minitest
::
Test
describe
Excelsior
do
def
setup
before
do
User
.
delete_all
@import
=
UserImport
.
new
@import
=
UserImport
.
new
end
end
def
test_that_it_has_a_
version
_
number
it
'has a
version
number
'
do
refute_nil
::
Excelsior
::
VERSION
refute_nil
::
Excelsior
::
VERSION
end
end
def
test_class_level_source
describe
'source'
do
import
=
UserImport
.
new
it
'allows setting the source on the class'
do
assert_equal
"test/files/complete.xlsx"
,
import
.
source
import
=
UserImport
.
new
end
assert_equal
"test/files/complete.xlsx"
,
import
.
source
end
def
test_instance_level_source
it
'allows setting the source on an instance level'
do
import
=
UserImport
.
new
(
"test/files/missing-column.xlsx"
)
import
=
UserImport
.
new
(
"test/files/missing-column.xlsx"
)
assert_equal
"test/files/missing-column.xlsx"
,
import
.
source
assert_equal
"test/files/missing-column.xlsx"
,
import
.
source
end
end
end
def
test_import_rows
describe
'#rows'
do
assert_equal
2
,
@import
.
rows
.
length
it
'returns the correct number of rows'
do
assert_equal
2
,
@import
.
rows
.
length
end
end
end
def
test_import_columns
describe
'#columns'
do
assert_equal
3
,
@import
.
columns
.
length
it
'returns the columns as defined in the excel'
do
assert_equal
"E-Mail"
,
@import
.
columns
[
0
]
assert_equal
3
,
@import
.
columns
.
length
assert_equal
"Vorname"
,
@import
.
columns
[
1
]
assert_equal
"E-Mail"
,
@import
.
columns
[
0
]
assert_equal
"Nachname"
,
@import
.
columns
[
2
]
assert_equal
"Vorname"
,
@import
.
columns
[
1
]
assert_equal
"Nachname"
,
@import
.
columns
[
2
]
end
end
end
def
test_mapping
describe
'#fields'
do
assert_equal
@import
.
fields
,
[
it
'returns the mapped fields'
do
{
attribute: :first_name
,
header:
"Vorname"
},
assert_equal
@import
.
fields
,
[
{
attribute: :last_name
,
header:
"Nachname"
},
{
attribute: :first_name
,
header:
"Vorname"
},
{
attribute: :email
,
header:
"E-Mail"
}
{
attribute: :last_name
,
header:
"Nachname"
},
]
{
attribute: :email
,
header:
"E-Mail"
}
]
end
end
end
def
test_import_run
describe
'#run'
do
@import
.
run
describe
'without a block'
do
assert_equal
User
.
all
.
size
,
2
it
'inserts the records'
do
end
@import
.
run
assert_equal
User
.
all
.
size
,
2
end
end
def
test_import_run_with_block
describe
'with a block'
do
results
=
@import
.
run
{
|
v
|
v
}
it
'yields the records to the block'
do
assert_equal
results
[
0
],
{
results
=
@import
.
run
{
|
v
|
v
}
first_name:
"Hans"
,
assert_equal
results
[
0
],
{
last_name:
"Müller"
,
first_name:
"Hans"
,
email:
"hans@mueller.com"
last_name:
"Müller"
,
}
email:
"hans@mueller.com"
assert_equal
results
[
1
],
{
}
first_name:
"Jögi"
,
assert_equal
results
[
1
],
{
last_name:
"Brunz"
,
first_name:
"Jögi"
,
email:
"jb@runz.com"
last_name:
"Brunz"
,
}
email:
"jb@runz.com"
}
end
end
end
end
def
test_validations
describe
'#errors'
do
import
=
UserImport
.
new
(
"test/files/missing-column.xlsx"
)
it
'returns an error when a column is missing in the excel'
do
assert
import
.
errors
[
:missing_column
].
any?
import
=
UserImport
.
new
(
"test/files/missing-column.xlsx"
)
end
assert
import
.
errors
[
:missing_column
].
any?
end
def
test_model_validations
it
'returns the model validation errors'
do
import
=
UserImport
.
new
(
"test/files/missing-first-name.xlsx"
).
tap
(
&
:run
)
import
=
UserImport
.
new
(
"test/files/missing-first-name.xlsx"
).
tap
(
&
:run
)
assert
import
.
errors
[
:model
].
any?
assert
import
.
errors
[
:model
].
any?
assert_equal
import
.
errors
[
:model
],
[
Excelsior
::
Error
.
new
(
3
,
[
"First name can't be blank"
])]
assert_equal
import
.
errors
[
:model
],
[
Excelsior
::
Error
.
new
(
3
,
[
"First name can't be blank"
])]
end
end
end
def
test_report
describe
'#report'
do
import
=
UserImport
.
new
(
"test/files/missing-first-name.xlsx"
).
tap
(
&
:run
)
describe
'without a block'
do
assert_equal
2
,
import
.
report
.
inserted
it
'returns the inserted, failed and total number of rows'
do
assert_equal
1
,
import
.
report
.
failed
import
=
UserImport
.
new
(
"test/files/missing-first-name.xlsx"
).
tap
(
&
:run
)
assert_equal
3
,
import
.
report
.
total
assert_equal
2
,
import
.
report
.
inserted
end
assert_equal
1
,
import
.
report
.
failed
assert_equal
3
,
import
.
report
.
total
end
end
def
test_report_with_block
describe
'with a block'
do
import
=
UserImport
.
new
(
"test/files/missing-first-name.xlsx"
)
it
'returns the inserted, failed and total number of rows'
do
import
.
run
do
|
v
|
import
=
UserImport
.
new
(
"test/files/missing-first-name.xlsx"
)
raise
"failure!"
if
v
[
:first_name
].
nil?
import
.
run
do
|
v
|
v
raise
"failure!"
if
v
[
:first_name
].
nil?
v
end
assert_equal
2
,
import
.
report
.
inserted
assert_equal
1
,
import
.
report
.
failed
assert_equal
3
,
import
.
report
.
total
end
end
end
assert_equal
2
,
import
.
report
.
inserted
assert_equal
1
,
import
.
report
.
failed
assert_equal
3
,
import
.
report
.
total
end
end
end
end
This diff is collapsed.
Click to expand it.
test/test_helper.rb
+
6
−
0
View file @
d04b051d
...
@@ -3,6 +3,12 @@ require "excelsior"
...
@@ -3,6 +3,12 @@ require "excelsior"
require
'active_record'
require
'active_record'
require
"minitest/autorun"
require
"minitest/autorun"
class
MiniTest::Spec
before
do
User
.
delete_all
end
end
ActiveRecord
::
Base
.
establish_connection
(
ActiveRecord
::
Base
.
establish_connection
(
adapter:
'sqlite3'
,
adapter:
'sqlite3'
,
database:
File
.
dirname
(
__FILE__
)
+
'/test.sqlite3'
database:
File
.
dirname
(
__FILE__
)
+
'/test.sqlite3'
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment