lazylead 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/ .dockerignore +12 -0
  3. data/.0pdd.yml +5 -0
  4. data/.circleci/config.yml +50 -0
  5. data/.circleci/release_image.sh +13 -0
  6. data/.circleci/validate.sh +10 -0
  7. data/.docker/Dockerfile +31 -0
  8. data/.docker/build.sh +6 -0
  9. data/.docker/docker-compose.yml +23 -0
  10. data/.docker/readme.md +21 -0
  11. data/.gitattributes +9 -0
  12. data/.github/CODE_OF_CONDUCT.md +76 -0
  13. data/.github/CONTRIBUTING.md +9 -0
  14. data/.github/ISSUE_TEMPLATE.md +14 -0
  15. data/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  16. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  17. data/.github/PULL_REQUEST_TEMPLATE.md +11 -0
  18. data/.github/tasks.yml +24 -0
  19. data/.github/trello.md +18 -0
  20. data/.gitignore +12 -0
  21. data/.pdd +5 -0
  22. data/.rubocop.yml +87 -0
  23. data/.ruby-version +1 -0
  24. data/.rultor.yml +31 -0
  25. data/.simplecov +16 -0
  26. data/.travis.yml +16 -0
  27. data/Gemfile +27 -0
  28. data/Guardfile +33 -0
  29. data/Rakefile +93 -0
  30. data/appveyor.yml +50 -0
  31. data/bin/.ruby-version +1 -0
  32. data/bin/lazylead +99 -0
  33. data/build.sh +6 -0
  34. data/deploy.sh +16 -0
  35. data/lazylead.gemspec +106 -0
  36. data/lib/lazylead.rb +52 -0
  37. data/lib/lazylead/allocated.rb +56 -0
  38. data/lib/lazylead/cli/app.rb +87 -0
  39. data/lib/lazylead/confluence.rb +157 -0
  40. data/lib/lazylead/email.rb +74 -0
  41. data/lib/lazylead/exchange.rb +83 -0
  42. data/lib/lazylead/log.rb +71 -0
  43. data/lib/lazylead/model.rb +140 -0
  44. data/lib/lazylead/postman.rb +78 -0
  45. data/lib/lazylead/salt.rb +91 -0
  46. data/lib/lazylead/schedule.rb +88 -0
  47. data/lib/lazylead/smtp.rb +82 -0
  48. data/lib/lazylead/system/empty.rb +36 -0
  49. data/lib/lazylead/system/fake.rb +41 -0
  50. data/lib/lazylead/system/jira.rb +249 -0
  51. data/lib/lazylead/system/synced.rb +41 -0
  52. data/lib/lazylead/task/alert.rb +105 -0
  53. data/lib/lazylead/task/confluence_ref.rb +59 -0
  54. data/lib/lazylead/task/echo.rb +38 -0
  55. data/lib/lazylead/task/fix_version.rb +79 -0
  56. data/lib/lazylead/task/missing_comment.rb +53 -0
  57. data/lib/lazylead/version.rb +27 -0
  58. data/lib/messages/due_date_expired.erb +96 -0
  59. data/lib/messages/illegal_fixversion_change.erb +120 -0
  60. data/lib/messages/missing_comment.erb +128 -0
  61. data/license.txt +21 -0
  62. data/readme.md +160 -0
  63. data/test/lazylead/allocated_test.rb +51 -0
  64. data/test/lazylead/cli/app_test.rb +74 -0
  65. data/test/lazylead/confluence_test.rb +55 -0
  66. data/test/lazylead/exchange_test.rb +68 -0
  67. data/test/lazylead/model_test.rb +65 -0
  68. data/test/lazylead/salt_test.rb +42 -0
  69. data/test/lazylead/smoke_test.rb +38 -0
  70. data/test/lazylead/smtp_test.rb +65 -0
  71. data/test/lazylead/system/jira_test.rb +110 -0
  72. data/test/lazylead/task/confluence_ref_test.rb +66 -0
  73. data/test/lazylead/task/duedate_test.rb +126 -0
  74. data/test/lazylead/task/echo_test.rb +34 -0
  75. data/test/lazylead/task/fix_version_test.rb +52 -0
  76. data/test/lazylead/task/missing_comment_test.rb +56 -0
  77. data/test/lazylead/version_test.rb +36 -0
  78. data/test/sqlite_test.rb +80 -0
  79. data/test/test.rb +103 -0
  80. data/todo.yml +16 -0
  81. data/upgrades/sqlite/001-install-main-lazylead-tables.sql +63 -0
  82. data/upgrades/sqlite/999.testdata.sql +39 -0
  83. metadata +815 -0
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require "mail"
26
+
27
+ require_relative "../../test"
28
+ require_relative "../../../lib/lazylead/task/confluence_ref"
29
+
30
+ module Lazylead
31
+ # @todo #/DEV Test took ~5s+ and should be optimized.
32
+ # Potentially, a confluence HTTP client is a first candidate.
33
+ class ConfluenceRefTest < Lazylead::Test
34
+ # @todo #/DEV Test fully depends on external system.
35
+ # Before test the external system should be cleaned up.
36
+ # Moreover, the test itself just creates a reference,
37
+ # but didn't check that the link is exist.
38
+ # Developer have to open the ticket and check.
39
+ test "link issue and confluence page from comments" do
40
+ skip "No Confluence details provided" unless env? "CONFLUENCE_URL",
41
+ "CONFLUENCE_APP_ID",
42
+ "CONFLUENCE_NAME",
43
+ "CONFLUENCE_USER",
44
+ "CONFLUENCE_PASS",
45
+ "CONFLUENCE_JQL"
46
+ skip "No JIRA credentials provided" unless env? "JIRA_URL", "JIRA_USER",
47
+ "JIRA_PASS"
48
+ Task::ConfluenceRef.new.run(
49
+ Jira.new(
50
+ username: ENV["JIRA_USER"],
51
+ password: ENV["JIRA_PASS"],
52
+ site: ENV["JIRA_URL"],
53
+ context_path: ""
54
+ ), "",
55
+ "jql" => ENV["CONFLUENCE_JQL"],
56
+ "confluences" => [
57
+ {
58
+ "url" => ENV["CONFLUENCE_URL"], "app" => ENV["CONFLUENCE_APP_ID"],
59
+ "name" => ENV["CONFLUENCE_NAME"], "type" => "com.atlassian.confluence",
60
+ "user" => ENV["CONFLUENCE_USER"], "pass" => ENV["CONFLUENCE_PASS"]
61
+ }
62
+ ].to_json
63
+ )
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require_relative "../../test"
26
+ require_relative "../../../lib/lazylead/log"
27
+ require_relative "../../../lib/lazylead/smtp"
28
+ require_relative "../../../lib/lazylead/postman"
29
+ require_relative "../../../lib/lazylead/schedule"
30
+ require_relative "../../../lib/lazylead/model"
31
+ require_relative "../../../lib/lazylead/cli/app"
32
+ require_relative "../../../lib/lazylead/system/jira"
33
+ require_relative "../../../lib/lazylead/task/alert"
34
+
35
+ module Lazylead
36
+ class DuedateTest < Lazylead::Test
37
+ # @todo #/DEV Print to logger the details about emails which were sent
38
+ # Mail::TestMailer.deliveries.each { |m| p m.html_part.body.raw_source }
39
+ test "issues were fetched" do
40
+ Smtp.new.enable
41
+ Task::AssigneeAlert.new.run(
42
+ NoAuthJira.new("https://jira.spring.io"),
43
+ Postman.new,
44
+ "from" => "fake@email.com",
45
+ "sql" => "filter=16743",
46
+ "subject" => "[DD] PDTN!",
47
+ "template" => "lib/messages/due_date_expired.erb"
48
+ )
49
+ assert_equal 2,
50
+ Mail::TestMailer.deliveries
51
+ .filter { |m| m.subject.eql? "[DD] PDTN!" }
52
+ .length
53
+ end
54
+
55
+ test "configuration properties merged successfully" do
56
+ CLI::App.new(Log::NOTHING, NoSchedule.new).run(
57
+ home: ".",
58
+ sqlite: "test/resources/#{no_ext(__FILE__)}.#{__method__}.db",
59
+ vcs4sql: "upgrades/sqlite",
60
+ testdata: true
61
+ )
62
+ assert_entries(
63
+ {
64
+ "sql" => "filter=100500",
65
+ "subject" => "[DD] PDTN!",
66
+ "from" => "basquad@fake.com",
67
+ "template" => "lib/messages/due_date_expired.erb"
68
+ },
69
+ ORM::Task.find(2).props
70
+ )
71
+ end
72
+
73
+ test "html msg has ticket details" do
74
+ Smtp.new.enable
75
+ Task::AssigneeAlert.new.run(
76
+ NoAuthJira.new("https://jira.spring.io"),
77
+ Postman.new,
78
+ "from" => "fake@email.com",
79
+ "sql" => "key in ('STS-3599')",
80
+ "subject" => "[DD] HMCHT!",
81
+ "template" => "lib/messages/due_date_expired.erb"
82
+ )
83
+ assert_words %w[STS-3599 2013-11-08 Major Miles\ Parker Use JavaFX WebView],
84
+ Mail::TestMailer.deliveries
85
+ .filter { |m| m.subject.eql? "[DD] HMCHT!" }
86
+ .first.body.parts.first.body
87
+ end
88
+
89
+ test "send notification about bunch of tickets" do
90
+ Smtp.new.enable
91
+ Task::Alert.new.run(
92
+ NoAuthJira.new("https://jira.spring.io"),
93
+ Postman.new,
94
+ "from" => "fake@email.com",
95
+ "sql" => "key in ('STS-3599')",
96
+ "subject" => "ALRT: Frozen",
97
+ "template" => "lib/messages/due_date_expired.erb",
98
+ "to" => "big.boss@example.com",
99
+ "addressee" => "Boss"
100
+ )
101
+ assert_words %w[Hi Boss STS-3599 2013-11-08 Major Miles\ Parker Use JavaFX WebView],
102
+ Mail::TestMailer.deliveries
103
+ .filter { |m| m.subject.eql? "ALRT: Frozen" }
104
+ .first.body.parts.first.body
105
+ end
106
+
107
+ test "cc got notification" do
108
+ Smtp.new.enable
109
+ Task::Alert.new.run(
110
+ NoAuthJira.new("https://jira.spring.io"),
111
+ Postman.new,
112
+ "from" => "fake@email.com",
113
+ "sql" => "key in ('STS-3599')",
114
+ "subject" => "CC: Watching",
115
+ "template" => "lib/messages/due_date_expired.erb",
116
+ "to" => "big.boss@example.com",
117
+ "addressee" => "Boss",
118
+ "cc" => "another.boss@example.com,mom@home.com"
119
+ )
120
+ assert_equal %w[another.boss@example.com mom@home.com],
121
+ Mail::TestMailer.deliveries
122
+ .filter { |m| m.subject.eql? "CC: Watching" }
123
+ .first.cc
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require_relative "../../test"
26
+ require_relative "../../../lib/lazylead/task/echo"
27
+
28
+ module Lazylead
29
+ class EchoTest < Lazylead::Test
30
+ test "default task" do
31
+ assert_equal "Lazylead::Task::Echo", Task::Echo.new.run("", "", {})
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require "mail"
26
+
27
+ require_relative "../../test"
28
+ require_relative "../../../lib/lazylead/smtp"
29
+ require_relative "../../../lib/lazylead/postman"
30
+ require_relative "../../../lib/lazylead/task/fix_version"
31
+
32
+ module Lazylead
33
+ class FixVersionTest < Lazylead::Test
34
+ test "alert in case fixvesion changed by not authorized person" do
35
+ Lazylead::Smtp.new.enable
36
+ Task::FixVersion.new.run(
37
+ NoAuthJira.new("https://jira.spring.io"),
38
+ Postman.new,
39
+ "to" => "lead@company.com",
40
+ "from" => "ll@company.com",
41
+ "jql" => "key in ('DATAJDBC-480') and fixVersion is not empty",
42
+ "allowed" => "tom,mike,bob",
43
+ "subject" => "FixVersion: How dare you?",
44
+ "template" => "lib/messages/illegal_fixversion_change.erb"
45
+ )
46
+ assert_words %w[DATAJDBC-480 01-Apr-2020 Minor Mark\ Paluch tom,mike,bob EntityInstantiators],
47
+ Mail::TestMailer.deliveries
48
+ .filter { |m| m.subject.eql? "FixVersion: How dare you?" }
49
+ .first.body.parts.first.body
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require "mail"
26
+
27
+ require_relative "../../test"
28
+ require_relative "../../../lib/lazylead/smtp"
29
+ require_relative "../../../lib/lazylead/postman"
30
+ require_relative "../../../lib/lazylead/system/jira"
31
+ require_relative "../../../lib/lazylead/task/missing_comment"
32
+
33
+ module Lazylead
34
+ class MissingCommentTest < Lazylead::Test
35
+ test "alert in case missing comment" do
36
+ Lazylead::Smtp.new.enable
37
+ Task::MissingComment.new.run(
38
+ NoAuthJira.new("https://jira.spring.io"),
39
+ Postman.new,
40
+ "to" => "lead@company.com",
41
+ "addressee" => "Tom",
42
+ "from" => "ll@company.com",
43
+ "jql" => "key=DATAJDBC-523",
44
+ "text" => "ftp.com/demo.avi",
45
+ "details" => "reference to <code>ftp.com/demo.avi</code>",
46
+ "subject" => "Expected ftp link is missing",
47
+ "template" => "lib/messages/missing_comment.erb",
48
+ "template_details" => "reference to <a href='file://ftp/path'>ftp</a>"
49
+ )
50
+ assert_words %w[DATAJDBC-523 Major Mark\ Paluch https://github.com/spring-projects/spring-data-jdbc/commit/aadbb667ed1d61139d5ac51a06eb3dd1b39316db#diff-510a5041bb8a0575e97fedf105606b83R130],
51
+ Mail::TestMailer.deliveries
52
+ .filter { |m| m.subject.eql? "Expected ftp link is missing" }
53
+ .first.body.parts.first.body
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require_relative "../test"
26
+ require_relative "../../lib/lazylead/version"
27
+
28
+ # @todo #/DEV Rename xxx_test.rb to test_xx.rb
29
+ # as this naming is more clear.
30
+ module Lazylead
31
+ class VersionTest < Lazylead::Test
32
+ test "gem has version" do
33
+ refute_nil Lazylead::VERSION
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The MIT License
4
+ #
5
+ # Copyright (c) 2019-2020 Yurii Dubinka
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"),
9
+ # to deal in the Software without restriction, including without limitation
10
+ # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
+ # and/or sell copies of the Software, and to permit persons to whom
12
+ # the Software is furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included
15
+ # in all copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22
+ # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23
+ # OR OTHER DEALINGS IN THE SOFTWARE.
24
+
25
+ require "sqlite3"
26
+ require_relative "test"
27
+
28
+ module Lazylead
29
+ class SqliteTest < Lazylead::Test
30
+ # Assert that table(s) has required column(s)
31
+ def assert_tables(file, tables)
32
+ raise "Table names is a null" if tables.nil?
33
+ raise "Table names is empty" if tables.empty?
34
+ schema = conn(file).query(
35
+ "select t.name 'table', group_concat(i.name) 'columns'
36
+ from sqlite_master t join pragma_table_info(t.name) i
37
+ where
38
+ t.type = 'table'
39
+ and t.name in (#{tables.keys.map { |t| "'#{t}'" }.join(',')})
40
+ group by t.name
41
+ order by t.name"
42
+ ).to_h
43
+ refute_empty schema, "No tables found in #{file} for #{tables}"
44
+ tables.each_with_index do |t, i|
45
+ raise "Name not found for table with id #{i} in #{tables}" if t.empty?
46
+ tbl = t.first.to_s
47
+ cols = t.values_at(1).sort.join(",")
48
+ raise "No columns found for table #{tbl} in #{tables}" if cols.empty?
49
+ refute schema[tbl].nil?, "Table '#{tbl}' not found in #{schema}"
50
+ assert_equal cols, schema[tbl], "Columns mismatch for '#{tbl}'"
51
+ end
52
+ end
53
+
54
+ # Assert that table(s) has required foreign key(s)
55
+ # which refers to the existing table.
56
+ def assert_fk(file, *fks)
57
+ raise "No foreign keys specified" if fks.nil? || fks.empty?
58
+ schema = conn(file).query(
59
+ "select t.name, fks.'from', fks.'table', fks.'to'
60
+ from
61
+ sqlite_master t join pragma_foreign_key_list(t.name) fks,
62
+ sqlite_master e
63
+ where e.type = 'table' and e.name = fks.'table'
64
+ order by t.name, fks.'from'"
65
+ ).map { |f| Array.new(f) }
66
+ fks.each do |f|
67
+ assert schema.any? { |fk| fk.sort == f.sort },
68
+ "No foreign key found from #{f[0]}.#{f[1]} to #{f[2]}.#{f[3]}"
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ # Establish the connection to the local sqlite database
75
+ def conn(file)
76
+ raise "Path to sqlite file is a null" if file.nil?
77
+ SQLite3::Database.new file
78
+ end
79
+ end
80
+ end