diff --git a/.gitignore b/.gitignore index 4e964262..001a24cb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ jrp jrp.exe .DS_Store *.log + +# NOT IGNORE FOLDER +!jrp/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml index ee87a6c3..71e6b5b9 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -17,7 +17,7 @@ builds: - darwin ldflags: - -s -w - - -X github.com/yanosea/jrp/cmd.version=v{{.Version}} + - -X github.com/yanosea/jrp/cmd.ver=v{{.Version}} archives: - format: tar.gz diff --git a/CREDITS b/CREDITS index 3fd60e60..3bcc349a 100644 --- a/CREDITS +++ b/CREDITS @@ -211,6 +211,61 @@ https://github.com/briandowns/spinner ================================================================ +github.com/chzyer/readline +https://github.com/chzyer/readline +---------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2015 Chzyer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + + +================================================================ + +github.com/chzyer/test +https://github.com/chzyer/test +---------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2016 chzyer + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +================================================================ + github.com/dustin/go-humanize https://github.com/dustin/go-humanize ---------------------------------------------------------------- @@ -1082,6 +1137,41 @@ https://github.com/inconshreveable/mousetrap ================================================================ +github.com/manifoldco/promptui +https://github.com/manifoldco/promptui +---------------------------------------------------------------- +BSD 3-Clause License + +Copyright (c) 2017, Arigato Machine Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +================================================================ + github.com/mattn/go-colorable https://github.com/mattn/go-colorable ---------------------------------------------------------------- @@ -1124,6 +1214,33 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI ================================================================ +github.com/mattn/go-runewidth +https://github.com/mattn/go-runewidth +---------------------------------------------------------------- +The MIT License (MIT) + +Copyright (c) 2016 Yasuhiro Matsumoto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +================================================================ + github.com/ncruces/go-strftime https://github.com/ncruces/go-strftime ---------------------------------------------------------------- @@ -1151,6 +1268,31 @@ SOFTWARE. ================================================================ +github.com/olekukonko/tablewriter +https://github.com/olekukonko/tablewriter +---------------------------------------------------------------- +Copyright (C) 2014 by Oleku Konko + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +================================================================ + github.com/pmezard/go-difflib https://github.com/pmezard/go-difflib ---------------------------------------------------------------- @@ -1217,6 +1359,33 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ================================================================ +github.com/rivo/uniseg +https://github.com/rivo/uniseg +---------------------------------------------------------------- +MIT License + +Copyright (c) 2019 Oliver Kuederle + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +================================================================ + github.com/spf13/cobra https://github.com/spf13/cobra ---------------------------------------------------------------- diff --git a/README.md b/README.md index bfbeb4a5..64f2389f 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,18 @@ [![Latest Release](https://img.shields.io/github/v/release/yanosea/jrp?style=flat-square)](https://github.com/yanosea/jrp/releases/latest)
[Coverage Report](https://yanosea.github.io/jrp/coverage.html) +
+![demo](docs/demo.gif "demo") -![demo](docs/demo.gif "demo") - ## ℹ️ About `jrp` is the CLI tool to generate Japanese random phrase(s). (It's jokeey tool!) -This tool uses [WordNet Japan](https://bond-lab.github.io/wnja/jpn/downloads.html) sqlite database file. +You can save the generated phrase(s) to the history and manage them. +Also, you can save the generated phrase(s) to the favorite and manage them. + +This tool uses [WordNet Japan sqlite database file](https://bond-lab.github.io/wnja/jpn/downloads.html). ## πŸ’» Usage @@ -32,34 +35,56 @@ Usage: jrp [command] Available Subcommands: - download, dl, d πŸ“¦ Download Japanese Wordnet sqlite3 database file from the official site. - generate, gen, g ✨ Generate Japanese random phrase(s). You can abbreviate "generate" sub command. ("jrp" and "jrp generate" are the same.) - help 🀝 Help of jrp. - completion πŸ”§ Generate the autocompletion script for the specified shell. - version πŸ”– Show the version of jrp. + download, dl, d πŸ“¦ Download WordNet Japan sqlite database file from the official web site. + generate, gen, g ✨ Generate Japanese random phrase(s). You can abbreviate "generate" sub command. ("jrp" and "jrp generate" are the same.) + history, hist, h πŸ“œ Manage the history of the "generate" command. + favorite, fav, f ⭐ Manage the favorited phrase(s) of the history of "generate" command. + help 🀝 Help of jrp. + completion πŸ”§ Generate the autocompletion script for the specified shell. + version πŸ”– Show the version of jrp. Flags: - -n, --number πŸ”’ number of phrases to generate (default 1, e.g: 10). - -p --prefix πŸ’¬ prefix of phrase(s) to generate. - -s --suffix πŸ’¬ suffix of phrase(s) to generate. + -n, --number πŸ”’ number of phrases to generate (default 1, e.g: 10) + -p --prefix πŸ’¬ prefix of phrase(s) to generate + -s --suffix πŸ’¬ suffix of phrase(s) to generate + -d --dry-run πŸ§ͺ generate phrase(s) without saving to the history + -P, --plain πŸ“ plain text output instead of table output -h, --help 🀝 help for jrp -v, --version πŸ”– version for jrp Arguments: - number πŸ”’ number of phrases to generate (e.g: 10). + number πŸ”’ number of phrases to generate (e.g: 10) +``` + +## 🌍 Environments + +### πŸ“ Directory to store WordNet Japan sqlite database file + +Default : `$XDG_DATA_HOME/jrp` or `$HOME/.local/share/jrp` + +```sh +export JRP_WNJPN_DB_FILE_DIR=/path/to/your/directory +``` + +### πŸ“ Directory to store jrp sqlite database file + +Default : `$XDG_DATA_HOME/jrp` or `$HOME/.local/share/jrp` + +```sh +export JRP_DB_FILE_DIR=/path/to/your/directory ``` ## πŸ”§ Installation ### 🐭 Using go -``` +```sh go install github.com/yanosea/jrp@latest ``` ### 🍺 Using homebrew -``` +```sh brew tap yanosea/tap brew install yanosea/tap/jrp ``` @@ -74,13 +99,13 @@ Go to the [Releases](https://github.com/yanosea/jrp/releases) and download the l reinstall `jrp`! -``` +```sh go install github.com/yanosea/jrp@latest ``` ### 🍺 Using homebrew -``` +```sh brew update brew upgrade jrp ``` @@ -89,6 +114,45 @@ brew upgrade jrp Download the latest binary from the [Releases](https://github.com/yanosea/jrp/releases) page and replace the old binary in your `$PATH`. +## 🧹 Uninstallation + +### πŸ”§ Uninstall jrp + +#### 🐭 Using go + +```sh +rm $GOPATH/bin/jrp +sudo rm -fr $GOPATH/pkg/mod/github.com/yanosea/jrp@* +``` + +#### 🍺 Using homebrew + +```sh +brew uninstall jrp +brew untap yanosea/tap/jrp +``` + +#### πŸ“¦ Download from release + +Remove the binary you downloaded and placed in your `$PATH`. + +### πŸ—‘οΈ Remove data files + +If you've set jrp envs, please replace `$HOME/.local/share/jrp` with envs you've set. +These below commands are in the case of default. Ofcourse you can remove whole the directory. + +#### πŸ’Ύ Remove WordNet Japan sqlite database file + +```sh +rm $HOME/.local/share/jrp/wnjpn.db +``` + +#### πŸ’Ύ Remove jrp sqlite database file + +```sh +rm $HOME/.local/share/jrp/jrp.db +``` + ## πŸ“ƒ License [πŸ”“MIT](./LICENSE) diff --git a/app/database/jrp/model/godoc.go b/app/database/jrp/model/godoc.go new file mode 100644 index 00000000..4b8e552d --- /dev/null +++ b/app/database/jrp/model/godoc.go @@ -0,0 +1,2 @@ +// Package model is a package for providing jrp model. +package model diff --git a/app/database/jrp/model/jrp.go b/app/database/jrp/model/jrp.go new file mode 100644 index 00000000..5be52387 --- /dev/null +++ b/app/database/jrp/model/jrp.go @@ -0,0 +1,17 @@ +package model + +import ( + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/time" +) + +// Jrp is a struct that represents jrp. +type Jrp struct { + ID int + Phrase string + Prefix *sqlproxy.NullStringInstance + Suffix *sqlproxy.NullStringInstance + IsFavorited int + CreatedAt *timeproxy.TimeInstance + UpdatedAt *timeproxy.TimeInstance +} diff --git a/app/database/jrp/repository/constant.go b/app/database/jrp/repository/constant.go new file mode 100644 index 00000000..cb8cb99e --- /dev/null +++ b/app/database/jrp/repository/constant.go @@ -0,0 +1,48 @@ +package repository + +// SaveStatus is a type for save status +type SaveStatus int + +const ( + // SavedSuccessfully is a status for saved successfully + SavedSuccessfully SaveStatus = iota + // SavedFailed is a status for saved failed + SavedFailed + // SavedNone is a status for saved none + SavedNone + // SavedNotAll is a status for saved not all + SavedNotAll +) + +// RemoveStatus is a type for remove status +type RemoveStatus int + +const ( + // RemovedSuccessfully is a status for removed successfully + RemovedSuccessfully RemoveStatus = iota + // RemovedFailed is a status for removed failed + RemovedFailed + // RemovedNone is a status for removed none + RemovedNone + // RemovedNotAll is a status for removed not all + RemovedNotAll +) + +// AddStatus is a type for add status +type AddStatus int + +const ( + // AddedSuccessfully is a status for added successfully + AddedSuccessfully AddStatus = iota + // AddedFailed is a status for added failed + AddedFailed + // AddedNone is a status for added none + AddedNone + // AddedNotAll is a status for added not all + AddedNotAll +) + +const ( + // jrp sqlite database file name + JRP_DB_FILE_NAME = "jrp.db" +) diff --git a/app/database/jrp/repository/godoc.go b/app/database/jrp/repository/godoc.go new file mode 100644 index 00000000..58279c96 --- /dev/null +++ b/app/database/jrp/repository/godoc.go @@ -0,0 +1,2 @@ +// Package repository is a package for providing jrp repository. +package repository diff --git a/app/database/jrp/repository/query/add_favorite_by_ids.sql b/app/database/jrp/repository/query/add_favorite_by_ids.sql new file mode 100644 index 00000000..481cfd4c --- /dev/null +++ b/app/database/jrp/repository/query/add_favorite_by_ids.sql @@ -0,0 +1,9 @@ +UPDATE + jrp +SET + IsFavorite = 1 + -- add 9 hours to the current time to get JST + , UpdatedAt = datetime(CURRENT_TIMESTAMP, '+9 hours') +WHERE + jrp.IsFavorite = 0 + AND jrp.ID IN (%s); diff --git a/app/database/jrp/repository/query/count_jrp.sql b/app/database/jrp/repository/query/count_jrp.sql new file mode 100644 index 00000000..9bdc6a4b --- /dev/null +++ b/app/database/jrp/repository/query/count_jrp.sql @@ -0,0 +1,4 @@ +SELECT + COUNT(*) +FROM + jrp; diff --git a/app/database/jrp/repository/query/create_table_jrp.sql b/app/database/jrp/repository/query/create_table_jrp.sql new file mode 100644 index 00000000..8fcf2a79 --- /dev/null +++ b/app/database/jrp/repository/query/create_table_jrp.sql @@ -0,0 +1,10 @@ +CREATE TABLE IF NOT EXISTS + jrp ( + ID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT + , Phrase TEXT NOT NULL + , Prefix TEXT + , Suffix TEXT + , IsFavorite INTEGER DEFAULT 0 + , CreatedAt TIMESTAMP + , UpdatedAt TIMESTAMP + ); diff --git a/app/database/jrp/repository/query/get_all_favorite.sql b/app/database/jrp/repository/query/get_all_favorite.sql new file mode 100644 index 00000000..af805f53 --- /dev/null +++ b/app/database/jrp/repository/query/get_all_favorite.sql @@ -0,0 +1,14 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +WHERE + jrp.IsFavorite = 1 +ORDER BY + jrp.ID ASC; diff --git a/app/database/jrp/repository/query/get_all_jrp.sql b/app/database/jrp/repository/query/get_all_jrp.sql new file mode 100644 index 00000000..c51a4f02 --- /dev/null +++ b/app/database/jrp/repository/query/get_all_jrp.sql @@ -0,0 +1,12 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +ORDER BY + jrp.ID ASC; diff --git a/app/database/jrp/repository/query/get_favorite_by_number.sql b/app/database/jrp/repository/query/get_favorite_by_number.sql new file mode 100644 index 00000000..8c793ad4 --- /dev/null +++ b/app/database/jrp/repository/query/get_favorite_by_number.sql @@ -0,0 +1,15 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +WHERE + jrp.IsFavorite = 1 +ORDER BY + ID DESC +LIMIT ?; diff --git a/app/database/jrp/repository/query/get_jrp_by_number.sql b/app/database/jrp/repository/query/get_jrp_by_number.sql new file mode 100644 index 00000000..a058bcff --- /dev/null +++ b/app/database/jrp/repository/query/get_jrp_by_number.sql @@ -0,0 +1,13 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +ORDER BY + jrp.ID DESC +LIMIT ?; diff --git a/app/database/jrp/repository/query/godoc.go b/app/database/jrp/repository/query/godoc.go new file mode 100644 index 00000000..96b7d74a --- /dev/null +++ b/app/database/jrp/repository/query/godoc.go @@ -0,0 +1,2 @@ +// Package query is a package for providing queries to manage jrp database. +package query diff --git a/app/database/jrp/repository/query/insert_jrp.sql b/app/database/jrp/repository/query/insert_jrp.sql new file mode 100644 index 00000000..462d3bf0 --- /dev/null +++ b/app/database/jrp/repository/query/insert_jrp.sql @@ -0,0 +1,14 @@ +INSERT INTO + jrp ( + Phrase + , Prefix + , Suffix + , CreatedAt + , UpdatedAt + ) VALUES ( + ? + , ? + , ? + , ? + , ? + ); diff --git a/app/database/jrp/repository/query/query.go b/app/database/jrp/repository/query/query.go new file mode 100644 index 00000000..5947eee3 --- /dev/null +++ b/app/database/jrp/repository/query/query.go @@ -0,0 +1,100 @@ +package query + +import ( + _ "embed" +) + +// CreateTableJrp is a query to create table jrp. +// +//go:embed create_table_jrp.sql +var CreateTableJrp string + +// InsertJrp is a query to insert jrp. +// +//go:embed insert_jrp.sql +var InsertJrp string + +// GetAllJrp is a query to get all jrp. +// +//go:embed get_all_jrp.sql +var GetAllJrp string + +// GetJrpByNumber is a query to get jrp by number. +// +//go:embed get_jrp_by_number.sql +var GetJrpByNumber string + +// RemoveJrpByIDs is a query to remove jrp by ids. +// +//go:embed remove_jrp_by_ids.sql +var RemoveJrpByIDs string + +// RemoveJrpByIDsExceptFavorite is a query to remove jrp by ids except favorite. +// +//go:embed remove_jrp_by_ids_except_favorite.sql +var RemoveJrpByIDsExceptFavorite string + +// RemoveAllJrp is a query to remove all jrp. +// +//go:embed remove_all_jrp.sql +var RemoveAllJrp string + +// RemoveAllJrpExceptFavorite is a query to remove all jrp except favorite. +// +//go:embed remove_all_jrp_except_favorite.sql +var RemoveAllJrpExceptFavorite string + +// CountJrp is a query to count jrp. +// +//go:embed count_jrp.sql +var CountJrp string + +// RemoveJrpSeq is a query to remove jrp seq. +// +//go:embed remove_jrp_seq.sql +var RemoveJrpSeq string + +// SearchAllJrp is a query to search all jrp. +// +//go:embed search_all_jrp.sql +var SearchAllJrp string + +// SearchJrpByNumber is a query to search jrp by number. +// +//go:embed search_jrp_by_number.sql +var SearchJrpByNumber string + +// GetAllFavorite is a query to get all favorite. +// +//go:embed get_all_favorite.sql +var GetAllFavorite string + +// GetFavoriteByNumber is a query to get favorite by number. +// +//go:embed get_favorite_by_number.sql +var GetFavoriteByNumber string + +// AddFavoriteByIDs is a query to add favorite by ids. +// +//go:embed add_favorite_by_ids.sql +var AddFavoriteByIDs string + +// RemoveFavoriteByIDs is a query to remove favorite by ids. +// +//go:embed remove_favorite_by_ids.sql +var RemoveFavoriteByIDs string + +// RemoveAllFavorite is a query to remove all favorite. +// +//go:embed remove_all_favorite.sql +var RemoveAllFavorite string + +// SearchAllFavorite is a query to search all favorite. +// +//go:embed search_all_favorite.sql +var SearchAllFavorite string + +// SearchFavoriteByNumber is a query to search favorite by number. +// +//go:embed search_favorite_by_number.sql +var SearchFavoriteByNumber string diff --git a/app/database/jrp/repository/query/remove_all_favorite.sql b/app/database/jrp/repository/query/remove_all_favorite.sql new file mode 100644 index 00000000..89e6333b --- /dev/null +++ b/app/database/jrp/repository/query/remove_all_favorite.sql @@ -0,0 +1,7 @@ +UPDATE + jrp +SET + IsFavorite = 0 + , UpdatedAt = datetime(CURRENT_TIMESTAMP, '+9 hours') +WHERE + jrp.IsFavorite = 1; diff --git a/app/database/jrp/repository/query/remove_all_jrp.sql b/app/database/jrp/repository/query/remove_all_jrp.sql new file mode 100644 index 00000000..660f5705 --- /dev/null +++ b/app/database/jrp/repository/query/remove_all_jrp.sql @@ -0,0 +1,3 @@ +DELETE +FROM + jrp; diff --git a/app/database/jrp/repository/query/remove_all_jrp_except_favorite.sql b/app/database/jrp/repository/query/remove_all_jrp_except_favorite.sql new file mode 100644 index 00000000..b501bbc4 --- /dev/null +++ b/app/database/jrp/repository/query/remove_all_jrp_except_favorite.sql @@ -0,0 +1,5 @@ +DELETE +FROM + jrp +WHERE + jrp.IsFavorite = 0; diff --git a/app/database/jrp/repository/query/remove_favorite_by_ids.sql b/app/database/jrp/repository/query/remove_favorite_by_ids.sql new file mode 100644 index 00000000..e9e33ff7 --- /dev/null +++ b/app/database/jrp/repository/query/remove_favorite_by_ids.sql @@ -0,0 +1,8 @@ +UPDATE + jrp +SET + IsFavorite = 0 + , UpdatedAt = datetime(CURRENT_TIMESTAMP, '+9 hours') +WHERE + jrp.IsFavorite = 1 + AND jrp.ID IN (%s); diff --git a/app/database/jrp/repository/query/remove_jrp_by_ids.sql b/app/database/jrp/repository/query/remove_jrp_by_ids.sql new file mode 100644 index 00000000..de12c080 --- /dev/null +++ b/app/database/jrp/repository/query/remove_jrp_by_ids.sql @@ -0,0 +1,5 @@ +DELETE +FROM + jrp +WHERE + jrp.ID IN (%s); diff --git a/app/database/jrp/repository/query/remove_jrp_by_ids_except_favorite.sql b/app/database/jrp/repository/query/remove_jrp_by_ids_except_favorite.sql new file mode 100644 index 00000000..faa61ecf --- /dev/null +++ b/app/database/jrp/repository/query/remove_jrp_by_ids_except_favorite.sql @@ -0,0 +1,6 @@ +DELETE +FROM + jrp +WHERE + jrp.IsFavorite = 0 + AND jrp.ID IN (%s); diff --git a/app/database/jrp/repository/query/remove_jrp_seq.sql b/app/database/jrp/repository/query/remove_jrp_seq.sql new file mode 100644 index 00000000..85e3e921 --- /dev/null +++ b/app/database/jrp/repository/query/remove_jrp_seq.sql @@ -0,0 +1,5 @@ +DELETE +FROM + sqlite_sequence +WHERE + sqlite_sequence.name = 'jrp'; diff --git a/app/database/jrp/repository/query/search_all_favorite.sql b/app/database/jrp/repository/query/search_all_favorite.sql new file mode 100644 index 00000000..32d8f141 --- /dev/null +++ b/app/database/jrp/repository/query/search_all_favorite.sql @@ -0,0 +1,15 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +WHERE + jrp.IsFavorite = 1 + AND (%s) +ORDER BY + jrp.ID ASC; diff --git a/app/database/jrp/repository/query/search_all_jrp.sql b/app/database/jrp/repository/query/search_all_jrp.sql new file mode 100644 index 00000000..4e39411d --- /dev/null +++ b/app/database/jrp/repository/query/search_all_jrp.sql @@ -0,0 +1,14 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +WHERE + (%s) +ORDER BY + jrp.ID ASC; diff --git a/app/database/jrp/repository/query/search_favorite_by_number.sql b/app/database/jrp/repository/query/search_favorite_by_number.sql new file mode 100644 index 00000000..654530cd --- /dev/null +++ b/app/database/jrp/repository/query/search_favorite_by_number.sql @@ -0,0 +1,16 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +WHERE + jrp.IsFavorite = 1 + AND (%s) +ORDER BY + jrp.ID DESC +LIMIT ?; diff --git a/app/database/jrp/repository/query/search_jrp_by_number.sql b/app/database/jrp/repository/query/search_jrp_by_number.sql new file mode 100644 index 00000000..7d9abfed --- /dev/null +++ b/app/database/jrp/repository/query/search_jrp_by_number.sql @@ -0,0 +1,15 @@ +SELECT + jrp.ID + , jrp.Phrase + , jrp.Prefix + , jrp.Suffix + , jrp.IsFavorite + , jrp.CreatedAt + , jrp.UpdatedAt +FROM + jrp +WHERE + (%s) + ORDER BY + jrp.ID DESC +LIMIT ?; diff --git a/app/database/jrp/repository/repository.go b/app/database/jrp/repository/repository.go new file mode 100644 index 00000000..7a212669 --- /dev/null +++ b/app/database/jrp/repository/repository.go @@ -0,0 +1,1003 @@ +package repository + +import ( + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository/query" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strings" +) + +// JrpRepositoryInterface is an interface for JrpRepository. +type JrpRepositoryInterface interface { + SaveHistory(jrpDBFilePath string, jrps []model.Jrp) (SaveStatus, error) + GetAllHistory(jrpDBFilePath string) ([]model.Jrp, error) + GetHistoryWithNumber(jrpDBFilePath string, number int) ([]model.Jrp, error) + SearchHistoryWithNumber(jrpDBFilePath string, number int, keywords []string, and bool) ([]model.Jrp, error) + SearchAllHistory(jrpDBFilePath string, keywords []string, and bool) ([]model.Jrp, error) + RemoveHistoryByIDs(jrpDBFilePath string, ids []int, force bool) (RemoveStatus, error) + RemoveHistoryAll(jrpDBFilePath string, force bool) (RemoveStatus, error) + GetAllFavorite(jrpDBFilePath string) ([]model.Jrp, error) + GetFavoriteWithNumber(jrpDBFilePath string, number int) ([]model.Jrp, error) + SearchAllFavorite(jrpDBFilePath string, keywords []string, and bool) ([]model.Jrp, error) + SearchFavoriteWithNumber(jrpDBFilePath string, number int, keywords []string, and bool) ([]model.Jrp, error) + AddFavoriteByIDs(jrpDBFilePath string, ids []int) (AddStatus, error) + RemoveFavoriteByIDs(jrpDBFilePath string, ids []int) (RemoveStatus, error) + RemoveFavoriteAll(jrpDBFilePath string) (RemoveStatus, error) +} + +// JrpRepository is a struct that implements JrpRepositoryInterface. +type JrpRepository struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings +} + +// New is a constructor for JrpRepository. +func New( + fmtProxy fmtproxy.Fmt, + sortProxy sortproxy.Sort, + sqlProxy sqlproxy.Sql, + stringsProxy stringsproxy.Strings, +) *JrpRepository { + return &JrpRepository{ + FmtProxy: fmtProxy, + SortProxy: sortProxy, + SqlProxy: sqlProxy, + StringsProxy: stringsProxy, + } +} + +// SaveHistory saves jrps as history. +func (j JrpRepository) SaveHistory(jrpDBFilePath string, jrps []model.Jrp) (SaveStatus, error) { + var deferErr error + // if jrps is nil or empty, return nil + if jrps == nil || len(jrps) <= 0 { + return SavedNone, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return SavedFailed, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return SavedFailed, err + } + + // start transaction + tx, err := db.Begin() + if err != nil { + return SavedFailed, err + } + defer func() { + deferErr = tx.Rollback() + }() + + // prepare insert statement + stmt, err := db.Prepare(query.InsertJrp) + if err != nil { + return SavedFailed, err + } + defer func() { + deferErr = stmt.Close() + }() + + // insert jrp and count affected rows + count := int64(0) + for _, jrp := range jrps { + res, err := stmt.Exec( + jrp.Phrase, + jrp.Prefix, + jrp.Suffix, + jrp.CreatedAt, + jrp.UpdatedAt, + ) + if err != nil { + return SavedFailed, err + } + c, err := res.RowsAffected() + if err != nil { + // failed to get rows affected + return SavedFailed, err + } + count += c + } + + // commit transaction + if err := tx.Commit(); err != nil { + return SavedFailed, err + } + + if count != int64(len(jrps)) { + // not all rows affected + return SavedNotAll, nil + } + + return SavedSuccessfully, deferErr +} + +// GetAllHistory gets all jrps as history. +func (j JrpRepository) GetAllHistory(jrpDBFilePath string) ([]model.Jrp, error) { + var deferErr error + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // get all history from jrp + rows, err := db.Query(query.GetAllJrp) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var allHistory []model.Jrp + for rows.Next() { + var history model.Jrp + if err := rows.Scan( + &history.ID, + &history.Phrase, + &history.Prefix, + &history.Suffix, + &history.IsFavorited, + &history.CreatedAt, + &history.UpdatedAt, + ); err != nil { + return nil, err + } + history.Prefix = j.SqlProxy.IfNullToNullString(history.Prefix) + history.Suffix = j.SqlProxy.IfNullToNullString(history.Suffix) + + allHistory = append(allHistory, history) + } + + return allHistory, deferErr +} + +// GetHistoryWithNumber gets history with number. +func (j JrpRepository) GetHistoryWithNumber(jrpDBFilePath string, number int) ([]model.Jrp, error) { + var deferErr error + if number <= 0 { + // if number is less than or equal to 0, return nil + return nil, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // prepare the query + stmt, err := db.Prepare(query.GetJrpByNumber) + if err != nil { + return nil, err + } + defer func() { + deferErr = stmt.Close() + }() + + // get history from jrp by number + rows, err := stmt.Query(number) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var allHistory []model.Jrp + for rows.Next() { + var history model.Jrp + if err := rows.Scan( + &history.ID, + &history.Phrase, + &history.Prefix, + &history.Suffix, + &history.IsFavorited, + &history.CreatedAt, + &history.UpdatedAt, + ); err != nil { + return nil, err + } + history.Prefix = j.SqlProxy.IfNullToNullString(history.Prefix) + history.Suffix = j.SqlProxy.IfNullToNullString(history.Suffix) + + allHistory = append(allHistory, history) + } + + // sort by ID asc + j.SortProxy.Slice(allHistory, func(i, j int) bool { + return allHistory[i].ID < allHistory[j].ID + }) + + return allHistory, deferErr +} + +// SearchAllHistory searches all jrps as history with keywords. +func (j JrpRepository) SearchAllHistory(jrpDBFilePath string, keywords []string, and bool) ([]model.Jrp, error) { + var deferErr error + if keywords == nil || len(keywords) <= 0 { + // if keywords is nil or empty, return nil + return nil, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // build query + args := []interface{}{} + conditions := []string{} + + // build conditions + for _, keyword := range keywords { + conditions = append(conditions, "jrp.Phrase LIKE ?") + args = append(args, "%"+keyword+"%") + } + + // build where clause + var whereClause string + if len(conditions) > 0 { + separator := " OR " + if and { + separator = " AND " + } + whereClause = j.StringsProxy.Join(conditions, separator) + } + + query := j.FmtProxy.Sprintf(query.SearchAllJrp, whereClause) + + // execute query + rows, err := db.Query(query, args...) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var searchedAllHistory []model.Jrp + for rows.Next() { + var history model.Jrp + if err := rows.Scan( + &history.ID, + &history.Phrase, + &history.Prefix, + &history.Suffix, + &history.IsFavorited, + &history.CreatedAt, + &history.UpdatedAt, + ); err != nil { + return nil, err + } + history.Prefix = j.SqlProxy.IfNullToNullString(history.Prefix) + history.Suffix = j.SqlProxy.IfNullToNullString(history.Suffix) + + searchedAllHistory = append(searchedAllHistory, history) + } + + return searchedAllHistory, deferErr +} + +// SearchHistoryWithNumber searches jrps as history with number and keywords. +func (j JrpRepository) SearchHistoryWithNumber( + jrpDBFilePath string, + number int, + keywords []string, + and bool, +) ([]model.Jrp, error) { + var deferErr error + if number <= 0 || keywords == nil || len(keywords) <= 0 { + // if number is less than or equal to 0 or keywords is nil or empty + return nil, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // build query + args := []interface{}{} + conditions := []string{} + + // build conditions + for _, keyword := range keywords { + conditions = append(conditions, "jrp.Phrase LIKE ?") + args = append(args, "%"+keyword+"%") + } + + // build where clause + var whereClause string + if len(conditions) > 0 { + separator := " OR " + if and { + separator = " AND " + } + whereClause = j.StringsProxy.Join(conditions, separator) + } + + query := j.FmtProxy.Sprintf(query.SearchJrpByNumber, whereClause) + args = append(args, number) + + // execute query + rows, err := db.Query(query, args...) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var searchedHistory []model.Jrp + for rows.Next() { + var history model.Jrp + if err := rows.Scan( + &history.ID, + &history.Phrase, + &history.Prefix, + &history.Suffix, + &history.IsFavorited, + &history.CreatedAt, + &history.UpdatedAt, + ); err != nil { + return nil, err + } + history.Prefix = j.SqlProxy.IfNullToNullString(history.Prefix) + history.Suffix = j.SqlProxy.IfNullToNullString(history.Suffix) + + searchedHistory = append(searchedHistory, history) + } + + // sort by ID asc + j.SortProxy.Slice(searchedHistory, func(i, j int) bool { + return searchedHistory[i].ID < searchedHistory[j].ID + }) + + return searchedHistory, deferErr +} + +// RemoveHistoryByIDs removes jrps by IDs. +func (j JrpRepository) RemoveHistoryByIDs(jrpDBFilePath string, ids []int, force bool) (RemoveStatus, error) { + var deferErr error + if ids == nil || len(ids) <= 0 { + // if ids is nil or empty, return nil + return RemovedNone, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return RemovedFailed, err + } + + // create the correct number of placeholders for the IDs + placeholders := make([]string, len(ids)) + for i := range ids { + placeholders[i] = "?" + } + placeholdersStr := j.StringsProxy.Join(placeholders, ",") + + // prepare the delete q with the correct number of placeholders + var q string + if force { + q = j.FmtProxy.Sprintf(query.RemoveJrpByIDs, placeholdersStr) + } else { + q = j.FmtProxy.Sprintf(query.RemoveJrpByIDsExceptFavorite, placeholdersStr) + } + stmt, err := db.Prepare(q) + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = stmt.Close() + }() + + // convert ids to interface slice for Exec + args := make([]interface{}, len(ids)) + for i, id := range ids { + args[i] = id + } + + // execute the delete query + res, err := stmt.Exec(args...) + if err != nil { + return RemovedFailed, err + } + + if count, err := res.RowsAffected(); err != nil { + // failed to get rows affected + return RemovedFailed, err + } else if count <= 0 { + // no rows affected + return RemovedNone, nil + } else if count != int64(len(ids)) { + // not all rows affected + return RemovedNotAll, nil + } + + return RemovedSuccessfully, deferErr +} + +// RemoveHistoryAll removes all jrps. +func (j JrpRepository) RemoveHistoryAll(jrpDBFilePath string, force bool) (RemoveStatus, error) { + var deferErr error + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return RemovedFailed, err + } + + // start transaction + tx, err := db.Begin() + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = tx.Rollback() + }() + + // set q + var q string + if force { + q = query.RemoveAllJrp + } else { + q = query.RemoveAllJrpExceptFavorite + } + + // remove all jrp + var res sqlproxy.ResultInstanceInterface + if res, err = tx.Exec(q); err != nil { + return RemovedFailed, err + } + + // check if the count of rows is zero after execution + checkCount := query.CountJrp + var count int + if err := tx.QueryRow(checkCount).Scan(&count); err != nil { + return RemovedFailed, err + } + + if count == 0 { + // remove jrp sequence + if _, err := tx.Exec(query.RemoveJrpSeq); err != nil { + return RemovedFailed, err + } + } + + // check rows affected by the remove jrp query + if affected, err := res.RowsAffected(); err != nil { + return RemovedFailed, err + } else if affected == 0 { + return RemovedNone, nil + } + + // commit transaction + if err := tx.Commit(); err != nil { + return RemovedFailed, err + } + + return RemovedSuccessfully, deferErr +} + +// GetAllFavorite gets all jrps that are favorited. +func (j JrpRepository) GetAllFavorite(jrpDBFilePath string) ([]model.Jrp, error) { + var deferErr error + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // get all favorite from jrp + rows, err := db.Query(query.GetAllFavorite) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var allFavorite []model.Jrp + for rows.Next() { + var favorite model.Jrp + if err := rows.Scan( + &favorite.ID, + &favorite.Phrase, + &favorite.Prefix, + &favorite.Suffix, + &favorite.IsFavorited, + &favorite.CreatedAt, + &favorite.UpdatedAt, + ); err != nil { + return nil, err + } + favorite.Prefix = j.SqlProxy.IfNullToNullString(favorite.Prefix) + favorite.Suffix = j.SqlProxy.IfNullToNullString(favorite.Suffix) + + allFavorite = append(allFavorite, favorite) + } + + return allFavorite, deferErr +} + +// GetFavoriteWithNumber gets jrps that are favorited with number. +func (j JrpRepository) GetFavoriteWithNumber(jrpDBFilePath string, number int) ([]model.Jrp, error) { + var deferErr error + if number <= 0 { + // if number is less than or equal to 0, return nil + return nil, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // prepare the query + stmt, err := db.Prepare(query.GetFavoriteByNumber) + if err != nil { + return nil, err + } + defer func() { + deferErr = stmt.Close() + }() + + // get favorite from jrp by number + rows, err := stmt.Query(number) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var allFavorite []model.Jrp + for rows.Next() { + var favorite model.Jrp + if err := rows.Scan( + &favorite.ID, + &favorite.Phrase, + &favorite.Prefix, + &favorite.Suffix, + &favorite.IsFavorited, + &favorite.CreatedAt, + &favorite.UpdatedAt, + ); err != nil { + return nil, err + } + favorite.Prefix = j.SqlProxy.IfNullToNullString(favorite.Prefix) + favorite.Suffix = j.SqlProxy.IfNullToNullString(favorite.Suffix) + + allFavorite = append(allFavorite, favorite) + } + + // sort by ID asc + j.SortProxy.Slice(allFavorite, func(i, j int) bool { + return allFavorite[i].ID < allFavorite[j].ID + }) + + return allFavorite, deferErr +} + +// SearchAllFavorite searches all jrps that are favorited with keywords. +func (j JrpRepository) SearchAllFavorite(jrpDBFilePath string, keywords []string, and bool) ([]model.Jrp, error) { + var deferErr error + if keywords == nil || len(keywords) <= 0 { + // if keywords is nil or empty, return nil + return nil, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // build query + args := []interface{}{} + conditions := []string{} + + // build conditions + for _, keyword := range keywords { + conditions = append(conditions, "jrp.Phrase LIKE ?") + args = append(args, "%"+keyword+"%") + } + + // build where clause + var whereClause string + if len(conditions) > 0 { + separator := " OR " + if and { + separator = " AND " + } + whereClause = j.StringsProxy.Join(conditions, separator) + } + + query := j.FmtProxy.Sprintf(query.SearchAllFavorite, whereClause) + + // execute query + rows, err := db.Query(query, args...) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var searchedAllFavorite []model.Jrp + for rows.Next() { + var favorite model.Jrp + if err := rows.Scan(&favorite.ID, + &favorite.Phrase, + &favorite.Prefix, + &favorite.Suffix, + &favorite.IsFavorited, + &favorite.CreatedAt, + &favorite.UpdatedAt, + ); err != nil { + return nil, err + } + favorite.Prefix = j.SqlProxy.IfNullToNullString(favorite.Prefix) + favorite.Suffix = j.SqlProxy.IfNullToNullString(favorite.Suffix) + + searchedAllFavorite = append(searchedAllFavorite, favorite) + } + + return searchedAllFavorite, deferErr +} + +// SearchFavoriteWithNumber searches jrps that are favorited with number and keywords. +func (j JrpRepository) SearchFavoriteWithNumber( + jrpDBFilePath string, + number int, + keywords []string, + and bool, +) ([]model.Jrp, error) { + var deferErr error + if number <= 0 || keywords == nil || len(keywords) <= 0 { + // if number is less than or equal to 0 or keywords is nil or empty + return nil, nil + } + + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return nil, err + } + + // build query + args := []interface{}{} + conditions := []string{} + + // build conditions + for _, keyword := range keywords { + conditions = append(conditions, "jrp.Phrase LIKE ?") + args = append(args, "%"+keyword+"%") + } + + // build where clause + var whereClause string + if len(conditions) > 0 { + separator := " OR " + if and { + separator = " AND " + } + whereClause = j.StringsProxy.Join(conditions, separator) + } + + query := j.FmtProxy.Sprintf(query.SearchFavoriteByNumber, whereClause) + args = append(args, number) + + // execute query + rows, err := db.Query(query, args...) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + var searchedFavorite []model.Jrp + for rows.Next() { + var favorite model.Jrp + if err := rows.Scan( + &favorite.ID, + &favorite.Phrase, + &favorite.Prefix, + &favorite.Suffix, + &favorite.IsFavorited, + &favorite.CreatedAt, + &favorite.UpdatedAt, + ); err != nil { + return nil, err + } + favorite.Prefix = j.SqlProxy.IfNullToNullString(favorite.Prefix) + favorite.Suffix = j.SqlProxy.IfNullToNullString(favorite.Suffix) + + searchedFavorite = append(searchedFavorite, favorite) + } + + // sort by ID asc + j.SortProxy.Slice(searchedFavorite, func(i, j int) bool { + return searchedFavorite[i].ID < searchedFavorite[j].ID + }) + + return searchedFavorite, deferErr +} + +// AddFavoriteByIDs adds jrps to favorite by IDs. +func (j JrpRepository) AddFavoriteByIDs(jrpDBFilePath string, ids []int) (AddStatus, error) { + var deferErr error + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return AddedFailed, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return AddedFailed, err + } + + // create the correct number of placeholders for the IDs + placeholders := make([]string, len(ids)) + for i := range ids { + placeholders[i] = "?" + } + placeholdersStr := j.StringsProxy.Join(placeholders, ",") + + // prepare the delete query with the correct number of placeholders + query := j.FmtProxy.Sprintf(query.AddFavoriteByIDs, placeholdersStr) + stmt, err := db.Prepare(query) + if err != nil { + return AddedFailed, err + } + + // convert ids to interface slice for Exec + args := make([]interface{}, len(ids)) + for i, id := range ids { + args[i] = id + } + + // execute the delete query + res, err := stmt.Exec(args...) + if err != nil { + return AddedFailed, err + } + + if count, err := res.RowsAffected(); err != nil { + // failed to get rows affected + return AddedFailed, err + } else if count <= 0 { + // no rows affected + return AddedNone, nil + } else if count != int64(len(ids)) { + // not all rows affected + return AddedNotAll, nil + } + + return AddedSuccessfully, deferErr +} + +// RemoveFavoriteByIDs removes jrps from favorite by IDs. +func (j JrpRepository) RemoveFavoriteByIDs(jrpDBFilePath string, ids []int) (RemoveStatus, error) { + var deferErr error + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return RemovedFailed, err + } + + // create the correct number of placeholders for the IDs + placeholders := make([]string, len(ids)) + for i := range ids { + placeholders[i] = "?" + } + placeholdersStr := j.StringsProxy.Join(placeholders, ",") + + // prepare the delete query with the correct number of placeholders + query := j.FmtProxy.Sprintf(query.RemoveFavoriteByIDs, placeholdersStr) + stmt, err := db.Prepare(query) + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = stmt.Close() + }() + + // convert ids to interface slice for Exec + args := make([]interface{}, len(ids)) + for i, id := range ids { + args[i] = id + } + + // execute the delete query + res, err := stmt.Exec(args...) + if err != nil { + return RemovedFailed, err + } + + // check rows affected + if count, err := res.RowsAffected(); err != nil { + // failed to get rows affected + return RemovedFailed, err + } else if count <= 0 { + // no rows affected + return RemovedNone, nil + } else if count != int64(len(ids)) { + // not all rows affected + return RemovedNotAll, nil + } + + return RemovedSuccessfully, deferErr +} + +// RemoveFavoriteAll removes all jrps from favorite. +func (j JrpRepository) RemoveFavoriteAll(jrpDBFilePath string) (RemoveStatus, error) { + var deferErr error + // connect to db + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = db.Close() + }() + + // create table 'jrp' + if _, err := j.createTableJrp(db); err != nil { + return RemovedFailed, err + } + + // start transaction + tx, err := db.Begin() + if err != nil { + return RemovedFailed, err + } + defer func() { + deferErr = tx.Rollback() + }() + + // remove all favorite + var res sqlproxy.ResultInstanceInterface + if res, err = tx.Exec(query.RemoveAllFavorite); err != nil { + return RemovedFailed, err + } + + // check rows affected + if count, err := res.RowsAffected(); err != nil { + // failed to get rows affected + return RemovedFailed, err + } else if count <= 0 { + // no rows affected + return RemovedNone, err + } + + // commit transaction + if err := tx.Commit(); err != nil { + return RemovedFailed, err + } + + return RemovedSuccessfully, deferErr +} + +// createTableJrp creates table 'jrp'. +func (j JrpRepository) createTableJrp(db sqlproxy.DBInstanceInterface) (sqlproxy.ResultInstanceInterface, error) { + return db.Exec(query.CreateTableJrp) +} diff --git a/app/database/jrp/repository/repository_test.go b/app/database/jrp/repository/repository_test.go new file mode 100644 index 00000000..48ec623e --- /dev/null +++ b/app/database/jrp/repository/repository_test.go @@ -0,0 +1,14218 @@ +package repository + +import ( + "errors" + "reflect" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + + "github.com/yanosea/jrp/mock/app/proxy/sort" + "github.com/yanosea/jrp/mock/app/proxy/sql" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNew(t *testing.T) { + fmtProxy := fmtproxy.New() + sortProxy := sortproxy.New() + sqlProxy := sqlproxy.New() + stringProxy := stringsproxy.New() + + type args struct { + fmtProxy fmtproxy.Fmt + sortProxy sortproxy.Sort + sqlProxy sqlproxy.Sql + stringsProxy stringsproxy.Strings + } + tests := []struct { + name string + args args + want *JrpRepository + }{ + { + name: "positive testing", + args: args{ + fmtProxy: fmtProxy, + sortProxy: sortProxy, + sqlProxy: sqlProxy, + stringsProxy: stringProxy, + }, + want: &JrpRepository{ + FmtProxy: fmtProxy, + SortProxy: sortProxy, + SqlProxy: sqlProxy, + StringsProxy: stringProxy, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.fmtProxy, tt.args.sortProxy, tt.args.sqlProxy, tt.args.stringsProxy); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want=\n%v", got, tt.want) + } + }) + } +} + +func TestJrpRepository_SaveHistory(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + jrps []model.Jrp + } + tests := []struct { + name string + fields fields + args args + wantStatus SaveStatus + wantJrps []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (jrps are nil)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: nil, + }, + wantStatus: SavedNone, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (jrps are empty)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{}, + }, + wantStatus: SavedNone, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (jrps are one, there is no jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedSuccessfully, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (jrps are two, there is no jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedSuccessfully, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (jrps are one, there is jrps in the database alresdy)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedSuccessfully, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (jrps are two, there is jrps in the databasea already)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedSuccessfully, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 3, + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedFailed, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedFailed, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Begin() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedFailed, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(nil, errors.New("DBInstance.Begin() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Prepare() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedFailed, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(nil, errors.New("DBInstance.Prepare() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (stmt.Exec() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedFailed, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("StmtInstance.Exec() failed")) + mockStmtInstance.EXPECT().Close().Return(nil) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (res.RowsAffected() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedFailed, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(0), errors.New("ResultInstance.RowsAffected() failed")) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockStmtInstance.EXPECT().Close().Return(nil) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (tx.Commit() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedFailed, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(0), nil) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockStmtInstance.EXPECT().Close().Return(nil) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Commit().Return(errors.New("TxInstance.Commit() failed")) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (res.RowsAffected() != len(jrps))", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantStatus: SavedNotAll, + wantJrps: nil, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(10), nil) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockStmtInstance.EXPECT().Close().Return(nil) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Commit().Return(nil) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.SaveHistory(tt.args.jrpDBFilePath, tt.args.jrps) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if got != tt.wantStatus { + t.Errorf("JrpRepository.SaveHistory() : got =\n%v, want =\n%v", got, tt.wantStatus) + } + savedJrps, err := jrpRepository.GetAllHistory(tt.args.jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(savedJrps, tt.wantJrps) { + t.Errorf("JrpRepository.SaveHistory() : savedJrps =\n%v, want =\n%v", savedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_GetAllHistory(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + JrpDBFilePath string + } + type args struct { + jrpDBFilePath string + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (there are no jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is two jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any()).Return(nil, errors.New("DBInstance.Query() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any()).Return(mockRowsInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.GetAllHistory(tt.args.jrpDBFilePath) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.GetAllHistory() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_GetHistoryWithNumber(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + number int + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (there are no jrps in the database, number is -1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: -1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are no jrps in the database, number is 0)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are no jrps in the database, number is 1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are no jrps in the database, number is 2)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is -1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: -1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 0)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 2)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is -1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: -1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 0)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 2)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Prepare() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(nil, errors.New("DBInstance.Prepare() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (stmt.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Query(gomock.Any()).Return(nil, errors.New("StmtInstance.Query() failed")) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Query(gomock.Any()).Return(mockRowsInstance, nil) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SortProxy.Slice() returns false)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSortProxy := mocksortproxy.NewMockSort(mockCtrl) + mockSortProxy.EXPECT().Slice(gomock.Any(), gomock.Any()) + tt.SortProxy = mockSortProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.GetHistoryWithNumber(tt.args.jrpDBFilePath, tt.args.number) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.GetHistoryWithNumber() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.GetHistoryWithNumber() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_SearchAllHistory(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + keywords []string + and bool + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (keywords are nil, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: nil, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (keywords are nil, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: nil, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (keywords are empty, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (keywords are empty, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, do not match any jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, do not match any jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match one jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match one jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match one jrp, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match one jrp, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match two jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match two jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(nil, errors.New("DBInstance.Query() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(mockRowsInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.SearchAllHistory(tt.args.jrpDBFilePath, tt.args.keywords, tt.args.and) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.SearchAllHistory() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.SearchAllHistory() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_SearchHistoryWithNumber(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + number int + keywords []string + and bool + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (number is 0, keywords are nil, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: nil, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, keywords are nil, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: nil, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0 keywords are nil, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: nil, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, keywords are empty, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, keywords are empty, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, single keyword, do not match any jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, single keyword, do not match any jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match one jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match one jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match one jrp, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match one jrp, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(nil, errors.New("DBInstance.Query() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(mockRowsInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.SearchHistoryWithNumber(tt.args.jrpDBFilePath, tt.args.number, tt.args.keywords, tt.args.and) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.SearchHistoryWithNumber() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.SearchHistoryWithNumber() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_RemoveHistoryByIDs(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + ids []int + force bool + } + tests := []struct { + name string + fields fields + args args + want RemoveStatus + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (ids are nil, not force)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: nil, + force: false, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are empty, not force)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{}, + force: false, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, not force, database does not have the id)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{2}, + force: false, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, not force, database has the id and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: false, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, not force, database has the id and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: false, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, force, database has the id and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: true, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, force, database has the id and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: true, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, not force, database does not have the id)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{2, 3}, + force: false, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, not force, database has the id and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + force: false, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, not force, database has the id and both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + force: false, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, not force, database has the id and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + force: false, + }, + want: RemovedNotAll, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, force, database has the id and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + force: true, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, force, database has the id and both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + force: true, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, force, database has the id and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + force: true, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: false, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: false, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Prepare() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: false, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(nil, errors.New("DBInstance.Prepare() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (stmt.Exec() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: false, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("StmtInstance.Exec() failed")) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (res.RowsAffected() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + force: false, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(0), errors.New("ResultInstance.RowsAffected() failed")) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.RemoveHistoryByIDs(tt.args.jrpDBFilePath, tt.args.ids, tt.args.force) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.RemoveHistoryByIDs() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if got != tt.want { + t.Errorf("JrpRepository.RemoveHistoryByIDs() : got =\n%v, want =\n%v", got, tt.want) + } + if err == nil && tt.args.ids != nil && len(tt.args.ids) > 0 { + for _, id := range tt.args.ids { + isExist, err := jrpChecker.IsExist(jrpDBFilePath, id) + if err != nil { + t.Errorf("JrpChecker.IsExist() : error =\n%v", err) + } + if isExist { + isFavorited, err := jrpChecker.IsFavorited(jrpDBFilePath, id) + if err != nil { + t.Errorf("JrpChecker.IsFavorited() : error =\n%v", err) + } + if !isFavorited || tt.args.force { + t.Errorf("JrpRepository.RemoveHistoryByIDs() : did not removed \n[%v]", id) + } + } + } + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_RemoveHistoryAll(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + force bool + } + tests := []struct { + name string + fields fields + args args + want RemoveStatus + wantLeftJrpCount int + wantSeq int + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (not force, there is no jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedNone, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not force, there is one jrp in the database and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not force, there is one jrp in the database and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedNone, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not force, there is two jrps in the database and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not force, there is two jrps in the database and both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedNone, + wantLeftJrpCount: 2, + wantSeq: 2, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not force, there is two jrps in the database and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 1, + wantSeq: 2, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (force, there is no jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: true, + }, + want: RemovedNone, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (force, there is one jrp in the database and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: true, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (force, there is one jrp in the database and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: true, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (force, there is two jrps in the database and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: true, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (force, there is two jrps in the database and both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: true, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (force, there is two jrps in the database and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: true, + }, + want: RemovedSuccessfully, + wantLeftJrpCount: 0, + wantSeq: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Begin() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(nil, errors.New("DBInstance.Begin() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (tx.Exec(q) failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("TxInstance.Exec() failed")) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (tx.QueryRow().Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowInstance := mocksqlproxy.NewMockRowInstanceInterface(mockCtrl) + mockRowInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowInstance.Scan() failed")) + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockTxInstance.EXPECT().QueryRow(gomock.Any()).Return(mockRowInstance) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (tx.Exec(query.RemoveJrpSeq) failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowInstance := mocksqlproxy.NewMockRowInstanceInterface(mockCtrl) + mockRowInstance.EXPECT().Scan(gomock.Any()).Return(nil) + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockTxInstance.EXPECT().QueryRow(gomock.Any()).Return(mockRowInstance) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, errors.New("TxInstance.Exec() failed")) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (res.RowsAffected() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowInstance := mocksqlproxy.NewMockRowInstanceInterface(mockCtrl) + mockRowInstance.EXPECT().Scan(gomock.Any()).Return(nil) + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(0), errors.New("ResultInstance.RowsAffected() failed")) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockTxInstance.EXPECT().QueryRow(gomock.Any()).Return(mockRowInstance) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (tx.Commit() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + force: false, + }, + want: RemovedFailed, + wantLeftJrpCount: 1, + wantSeq: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowInstance := mocksqlproxy.NewMockRowInstanceInterface(mockCtrl) + mockRowInstance.EXPECT().Scan(gomock.Any()).Return(nil) + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(1), nil) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockTxInstance.EXPECT().QueryRow(gomock.Any()).Return(mockRowInstance) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockTxInstance.EXPECT().Commit().Return(errors.New("TxInstance.Commit() failed")) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.RemoveHistoryAll(tt.args.jrpDBFilePath, tt.args.force) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.RemoveHistoryAll() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if got != tt.want { + t.Errorf("JrpRepository.RemoveHistoryAll() : got =\n%v, want =\n%v", got, tt.want) + } + leftJrps, err := jrpRepository.GetAllHistory(tt.args.jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if len(leftJrps) != tt.wantLeftJrpCount { + t.Errorf("JrpRepository.RemoveHistoryAll() : len(leftJrps) =\n%v, want =\n%v", leftJrps, tt.wantLeftJrpCount) + } + seq, err := jrpChecker.GetJrpSeq(jrpDBFilePath) + if err != nil { + t.Errorf("JrpChecker.GetJrpSeq() : error =\n%v", err) + } + if seq != tt.wantSeq { + t.Errorf("JrpRepository.RemoveHistoryAll() : seq =\n%v, want =\n%v", seq, tt.wantSeq) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_GetAllFavorite(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (there are no jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrp in the database and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrp in the database and both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrp in the database and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(nil, errors.New("DBInstance.Query() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(mockRowsInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.GetAllFavorite(tt.args.jrpDBFilePath) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.GetAllFavorite() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_GetFavoriteWithNumber(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + number int + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (there are no jrps in the database, number is -1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: -1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are no jrps in the database, number is 0)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are no jrps in the database, number is 1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are no jrps in the database, number is 2)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is -1)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: -1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 0)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 1, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 1, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 2, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database, number is 2, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 0)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 1, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 1, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 2, both not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 2, both favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there are two jrps in the database, number is 2, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Prepare() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(nil, errors.New("DBInstance.Prepare() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (stmt.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Query(gomock.Any()).Return(nil, errors.New("StmtInstance.Query() failed")) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Query(gomock.Any()).Return(mockRowsInstance, nil) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SortProxy.Slice() returns false)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockSortProxy := mocksortproxy.NewMockSort(mockCtrl) + mockSortProxy.EXPECT().Slice(gomock.Any(), gomock.Any()) + tt.SortProxy = mockSortProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.GetFavoriteWithNumber(tt.args.jrpDBFilePath, tt.args.number) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.GetFavoriteWithNumber() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.GetFavoriteWithNumber() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_SearchAllFavorite(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + keywords []string + and bool + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (keywords are nil, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: nil, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (keywords are nil, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: nil, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (keywords are empty, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (keywords are empty, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, do not match any jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, do not match any jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match one jrps, OR condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match one jrps, OR condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match one jrps, AND condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match one jrps, AND condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, OR condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, OR condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, OR condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, AND condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, AND condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (single keyword, match two jrps, AND condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match one jrp, OR condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match one jrp, OR condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match one jrp, OR condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match one jrp, AND condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match one jrp, AND condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match two jrps, OR condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match two jrps, OR condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match two jrps, AND condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match two jrps, AND condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two keywords, match two jrps, AND condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(nil, errors.New("DBInstance.Query() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(mockRowsInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.SearchAllFavorite(tt.args.jrpDBFilePath, tt.args.keywords, tt.args.and) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.SearchAllFavorite() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.SearchAllFavorite() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_SearchFavoriteWithNumber(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + number int + keywords []string + and bool + } + tests := []struct { + name string + fields fields + args args + want []model.Jrp + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (number is 0, keywords are nil, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: nil, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, keywords are nil, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: nil, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0 keywords are nil, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: nil, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, keywords are empty, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, keywords are empty, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, single keyword, do not match any jrps, OR condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 0, single keyword, do not match any jrps, AND condition)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 0, + keywords: []string{"γƒ†γ‚Ήγƒˆ"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match one jrps, OR condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match one jrps, OR condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match one jrps, AND condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match one jrps, AND condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, OR condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, OR condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, OR condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, OR condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, OR condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, OR condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, AND condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, AND condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, single keyword, match two jrps, AND condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, AND condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: true, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, AND condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, single keyword, match two jrps, AND condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: true, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match one jrp, OR condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match one jrp, OR condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match one jrp, AND condition, it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match one jrp, AND condition, it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, OR condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, OR condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, OR condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, OR condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, OR condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, OR condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "時雨", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, AND condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, AND condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 2, two keywords, match two jrps, AND condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 2, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, AND condition, both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, AND condition, both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 2, + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (number is 1, two keywords, match two jrps, AND condition, the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"凛", "時雨"}, + and: false, + }, + want: []model.Jrp{ + { + ID: 1, + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "凛として時雨1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "凛として時雨2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Query() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(nil, errors.New("DBInstance.Query() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + number: 1, + keywords: []string{"test"}, + and: false, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("RowsInstance.Scan() failed")) + mockRowsInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Query(gomock.Any(), gomock.Any()).Return(mockRowsInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.SearchFavoriteWithNumber(tt.args.jrpDBFilePath, tt.args.number, tt.args.keywords, tt.args.and) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.SearchFavoriteWithNumber() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if !jrpChecker.IsSameJrps(got, tt.want) { + t.Errorf("JrpRepository.SearchFavoriteWithNumber() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_AddFavoriteByIDs(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + ids []int + } + tests := []struct { + name string + fields fields + args args + want AddStatus + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (ids are nil)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: nil, + }, + want: AddedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are empty)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{}, + }, + want: AddedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, database does not have the id)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{2}, + }, + want: AddedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, database has the id and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: AddedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, database has the id and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: AddedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, database does not have the id)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{2, 3}, + }, + want: AddedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, database has the id and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + }, + want: AddedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, database has the id and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + }, + want: AddedNotAll, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: AddedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: AddedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Prepare() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: AddedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(nil, errors.New("DBInstance.Prepare() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (stmt.Exec() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: AddedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("StmtInstance.Exec() failed")) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (res.RowsAffected() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: AddedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(0), errors.New("ResultInstance.RowsAffected() failed")) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.AddFavoriteByIDs(tt.args.jrpDBFilePath, tt.args.ids) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if got != tt.want { + t.Errorf("JrpRepository.AddFavoriteByIDs() : got =\n%v, want =\n%v", got, tt.want) + } + if err == nil && tt.args.ids != nil && len(tt.args.ids) > 0 { + for _, id := range tt.args.ids { + isExist, err := jrpChecker.IsExist(jrpDBFilePath, id) + if err != nil { + t.Errorf("JrpChecker.IsExist() : error =\n%v", err) + } + if isExist { + isFavorited, err := jrpChecker.IsFavorited(jrpDBFilePath, id) + if err != nil { + t.Errorf("JrpChecker.IsFavorited() : error =\n%v", err) + } + if !isFavorited { + t.Errorf("JrpRepository.AddFavoriteByIDs() : did not favorited \n[%v]", id) + } + } + } + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_RemoveFavoriteByIDs(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + jrpDBFilePath string + ids []int + } + tests := []struct { + name string + fields fields + args args + want RemoveStatus + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (ids are nil)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: nil, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are empty)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{}, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, database does not have the id)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{2}, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, database has the id and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids is one, database has the id and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, database does not have the id)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{2, 3}, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, database has the id and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + }, + want: RemovedNone, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, not force, database has the id and both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + }, + want: RemovedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (ids are two, database has the id and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1, 2}, + }, + want: RemovedNotAll, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Prepare() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(nil, errors.New("DBInstance.Prepare() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (stmt.Exec() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("StmtInstance.Exec() failed")) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (res.RowsAffected() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + ids: []int{1}, + }, + want: RemovedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(0), errors.New("ResultInstance.RowsAffected() failed")) + mockStmtInstance := mocksqlproxy.NewMockStmtInstanceInterface(mockCtrl) + mockStmtInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockStmtInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Prepare(gomock.Any()).Return(mockStmtInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.RemoveFavoriteByIDs(tt.args.jrpDBFilePath, tt.args.ids) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.RemoveFavoriteByIDs() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if got != tt.want { + t.Errorf("JrpRepository.RemoveFavoriteByIDs() : got =\n%v, want =\n%v", got, tt.want) + } + if err == nil && tt.args.ids != nil && len(tt.args.ids) > 0 { + for _, id := range tt.args.ids { + isExist, err := jrpChecker.IsExist(jrpDBFilePath, id) + if err != nil { + t.Errorf("JrpChecker.IsExist() : error =\n%v", err) + } + if isExist { + isFavorited, err := jrpChecker.IsFavorited(jrpDBFilePath, id) + if err != nil { + t.Errorf("JrpChecker.IsFavorited() : error =\n%v", err) + } + if isFavorited { + t.Errorf("JrpRepository.RemoveFavoriteByIDs() : is still favorited \n[%v]", id) + } + } + } + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_RemoveFavoriteAll(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + sqlProxy := sqlproxy.New() + jrpRepository := New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + timeProxy := timeproxy.New() + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + JrpDBFilePath string + } + type args struct { + jrpDBFilePath string + } + tests := []struct { + name string + fields fields + args args + want RemoveStatus + wantFavoritedJrpCount int + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (there is no jrps in the database)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedNone, + wantFavoritedJrpCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database and it is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedNone, + wantFavoritedJrpCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is one jrp in the database and it is favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedSuccessfully, + wantFavoritedJrpCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is two jrps in the database and both are not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedNone, + wantFavoritedJrpCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is two jrps in the database and both are favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedSuccessfully, + wantFavoritedJrpCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (there is two jrps in the database and the one is favorited and the other is not favorited)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedSuccessfully, + wantFavoritedJrpCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedFailed, + wantFavoritedJrpCount: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.createTableJrp() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedFailed, + wantFavoritedJrpCount: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Begin() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedFailed, + wantFavoritedJrpCount: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory(jrpDBFilePath, []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(nil, errors.New("DBInstance.Begin() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (tx.Exec(query.RemoveAllFavorite) failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedFailed, + wantFavoritedJrpCount: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("TxInstance.Exec() failed")) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (res.RowsAffected() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedFailed, + wantFavoritedJrpCount: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(0), errors.New("ResultInstance.RowsAffected() failed")) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (tx.Commit() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + JrpDBFilePath: jrpDBFilePath, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + want: RemovedFailed, + wantFavoritedJrpCount: 1, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + mockResultInstance := mocksqlproxy.NewMockResultInstanceInterface(mockCtrl) + mockResultInstance.EXPECT().RowsAffected().Return(int64(1), nil) + mockTxInstance := mocksqlproxy.NewMockTxInstanceInterface(mockCtrl) + mockTxInstance.EXPECT().Exec(gomock.Any()).Return(mockResultInstance, nil) + mockTxInstance.EXPECT().Commit().Return(errors.New("TxInstance.Commit() failed")) + mockTxInstance.EXPECT().Rollback().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(&sqlproxy.ResultInstance{}, nil) + mockDBInstance.EXPECT().Begin().Return(mockTxInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + got, err := j.RemoveFavoriteAll(tt.args.jrpDBFilePath) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.RemoveFavoriteAll() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if got != tt.want { + t.Errorf("JrpRepository.RemoveFavoriteAll() : got =\n%v, want =\n%v", got, tt.want) + } + favoritedJrps, err := jrpRepository.GetAllFavorite(tt.args.jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v", err) + } + if len(favoritedJrps) != tt.wantFavoritedJrpCount { + t.Errorf("JrpRepository.RemoveFavoriteAll() : len(favoritedJrps) =\n%v, wantFavoritedJrpCount =\n%v", favoritedJrps, tt.wantFavoritedJrpCount) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestJrpRepository_createTableJrp(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, JRP_DB_FILE_NAME) + + type fields struct { + FmtProxy fmtproxy.Fmt + SortProxy sortproxy.Sort + SqlProxy sqlproxy.Sql + StringsProxy stringsproxy.Strings + } + type args struct { + db sqlproxy.DBInstanceInterface + } + tests := []struct { + name string + fields fields + args args + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: sqlproxy.New(), + StringsProxy: stringsproxy.New(), + }, + args: args{ + db: nil, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Exec() failed)", + fields: fields{ + FmtProxy: fmtproxy.New(), + SortProxy: sortproxy.New(), + SqlProxy: nil, + StringsProxy: stringsproxy.New(), + }, + args: args{ + db: nil, + }, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Exec(gomock.Any()).Return(nil, errors.New("DBInstance.Exec() failed")) + mocksqlproxy := mocksqlproxy.NewMockSql(mockCtrl) + mocksqlproxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mocksqlproxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("Os.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + j := New( + tt.fields.FmtProxy, + tt.fields.SortProxy, + tt.fields.SqlProxy, + tt.fields.StringsProxy, + ) + db, err := j.SqlProxy.Open(sqlproxy.Sqlite, jrpDBFilePath) + if err != nil { + t.Errorf("SqlProxy.Open() : error =\n%v", err) + } + tt.args.db = db + _, err = j.createTableJrp(tt.args.db) + if (err != nil) != tt.wantErr { + t.Errorf("JrpRepository.createTableJrp() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} diff --git a/app/database/wnjpn/model/godoc.go b/app/database/wnjpn/model/godoc.go new file mode 100644 index 00000000..80926a25 --- /dev/null +++ b/app/database/wnjpn/model/godoc.go @@ -0,0 +1,2 @@ +// Package model is a package for providing wnjpn model. +package model diff --git a/app/database/wnjpn/model/word.go b/app/database/wnjpn/model/word.go new file mode 100644 index 00000000..10660039 --- /dev/null +++ b/app/database/wnjpn/model/word.go @@ -0,0 +1,14 @@ +package model + +import ( + "github.com/yanosea/jrp/app/proxy/sql" +) + +// Word is a struct that represents word in wnjpn db file. +type Word struct { + WordID int + Lang *sqlproxy.NullStringInstance + Lemma *sqlproxy.NullStringInstance + Pron *sqlproxy.NullStringInstance + Pos *sqlproxy.NullStringInstance +} diff --git a/app/database/wnjpn/repository/constant.go b/app/database/wnjpn/repository/constant.go new file mode 100644 index 00000000..c842980e --- /dev/null +++ b/app/database/wnjpn/repository/constant.go @@ -0,0 +1,6 @@ +package repository + +const ( + // WordNet Japan sqlite database file name + WNJPN_DB_FILE_NAME = "wnjpn.db" +) diff --git a/app/database/wnjpn/repository/godoc.go b/app/database/wnjpn/repository/godoc.go new file mode 100644 index 00000000..0b9c1149 --- /dev/null +++ b/app/database/wnjpn/repository/godoc.go @@ -0,0 +1,2 @@ +// Package repository is a package for providing wnjpn repository. +package repository diff --git a/app/database/wnjpn/repository/query/get_all_av_words.sql b/app/database/wnjpn/repository/query/get_all_av_words.sql new file mode 100644 index 00000000..77dc1e9a --- /dev/null +++ b/app/database/wnjpn/repository/query/get_all_av_words.sql @@ -0,0 +1,8 @@ +SELECT + word.Lemma + , word.Pos +FROM + word +WHERE + word.Lang = 'jpn' + AND word.Pos in ('a', 'v'); diff --git a/app/database/wnjpn/repository/query/get_all_avn_words.sql b/app/database/wnjpn/repository/query/get_all_avn_words.sql new file mode 100644 index 00000000..8c3d3273 --- /dev/null +++ b/app/database/wnjpn/repository/query/get_all_avn_words.sql @@ -0,0 +1,8 @@ +SELECT + word.Lemma + , word.Pos +FROM + word +WHERE + word.Lang = 'jpn' + AND word.Pos in ('a', 'v', 'n'); diff --git a/app/database/wnjpn/repository/query/get_all_n_words.sql b/app/database/wnjpn/repository/query/get_all_n_words.sql new file mode 100644 index 00000000..7254bae6 --- /dev/null +++ b/app/database/wnjpn/repository/query/get_all_n_words.sql @@ -0,0 +1,8 @@ +SELECT + word.Lemma + , word.Pos +FROM + word +WHERE + word.Lang = 'jpn' + AND word.Pos = 'n'; diff --git a/app/database/wnjpn/repository/query/godoc.go b/app/database/wnjpn/repository/query/godoc.go new file mode 100644 index 00000000..eaaab6f4 --- /dev/null +++ b/app/database/wnjpn/repository/query/godoc.go @@ -0,0 +1,2 @@ +// Package query is a package for providing queries to get word from wnjpn database. +package query diff --git a/app/database/wnjpn/repository/query/query.go b/app/database/wnjpn/repository/query/query.go new file mode 100644 index 00000000..c2bbe86c --- /dev/null +++ b/app/database/wnjpn/repository/query/query.go @@ -0,0 +1,20 @@ +package query + +import ( + _ "embed" +) + +// GetAllJapaneseAVNWords is a query to get all japanese avn words. +// +//go:embed get_all_avn_words.sql +var GetAllJapaneseAVNWords string + +// GetAllJapaneseNWords is a query to get all japanese n words. +// +//go:embed get_all_n_words.sql +var GetAllJapaneseNWords string + +// GetAllJapaneseAVWords is a query to get all japanese av words. +// +//go:embed get_all_av_words.sql +var GetAllJapaneseAVWords string diff --git a/app/database/wnjpn/repository/repository.go b/app/database/wnjpn/repository/repository.go new file mode 100644 index 00000000..b9ee3af3 --- /dev/null +++ b/app/database/wnjpn/repository/repository.go @@ -0,0 +1,78 @@ +package repository + +import ( + "github.com/yanosea/jrp/app/database/wnjpn/model" + "github.com/yanosea/jrp/app/database/wnjpn/repository/query" + "github.com/yanosea/jrp/app/proxy/sql" +) + +// WNJpnRepositoryInterface is an interface for WNJpnRepository. +type WNJpnRepositoryInterface interface { + GetAllAVNWords(wnJpnDBFilePath string) ([]model.Word, error) + GetAllNWords(wnJpnDBFilePath string) ([]model.Word, error) + GetAllAVWords(wnJpnDBFilePath string) ([]model.Word, error) +} + +// WNJpnRepository is a struct that implements WNJpnRepositoryInterface. +type WNJpnRepository struct { + SqlProxy sqlproxy.Sql +} + +// New is a constructor for WNJpnRepository. +func New( + sqlProxy sqlproxy.Sql, +) *WNJpnRepository { + return &WNJpnRepository{ + SqlProxy: sqlProxy, + } +} + +// GetAllAVNWords gets all AVN words. +func (w *WNJpnRepository) GetAllAVNWords(wnJpnDBFilePath string) ([]model.Word, error) { + return w.getWords(wnJpnDBFilePath, query.GetAllJapaneseAVNWords) +} + +// GetAllNWords gets all N words. +func (w *WNJpnRepository) GetAllNWords(wnJpnDBFilePath string) ([]model.Word, error) { + return w.getWords(wnJpnDBFilePath, query.GetAllJapaneseNWords) +} + +// GetAllAVWords gets all AV words. +func (w *WNJpnRepository) GetAllAVWords(wnJpnDBFilePath string) ([]model.Word, error) { + return w.getWords(wnJpnDBFilePath, query.GetAllJapaneseAVWords) +} + +// getWords gets words. +func (w *WNJpnRepository) getWords(wnJpnDBFilePath string, query string) ([]model.Word, error) { + var deferErr error + // connect to db + db, err := w.SqlProxy.Open(sqlproxy.Sqlite, wnJpnDBFilePath) + if err != nil { + return nil, err + } + defer func() { + deferErr = db.Close() + }() + + // execute query + rows, err := db.Query(query) + if err != nil { + return nil, err + } + defer func() { + deferErr = rows.Close() + }() + + // scan rows + allWords := make([]model.Word, 0) + for rows.Next() { + var word model.Word + if err := rows.Scan(&word.Lemma, &word.Pos); err != nil { + return nil, err + } + + allWords = append(allWords, word) + } + + return allWords, deferErr +} diff --git a/app/database/wnjpn/repository/repository_test.go b/app/database/wnjpn/repository/repository_test.go new file mode 100644 index 00000000..e143eaf2 --- /dev/null +++ b/app/database/wnjpn/repository/repository_test.go @@ -0,0 +1,514 @@ +package repository + +import ( + "errors" + "reflect" + "testing" + + "github.com/yanosea/jrp/app/database/wnjpn/model" + "github.com/yanosea/jrp/app/database/wnjpn/repository/query" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/downloader" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/user" + + "github.com/yanosea/jrp/mock/app/proxy/sql" + "go.uber.org/mock/gomock" +) + +func TestNew(t *testing.T) { + sqlProxy := sqlproxy.New() + + type args struct { + sqlProxy sqlproxy.Sql + } + tests := []struct { + name string + args args + want *WNJpnRepository + }{ + { + name: "positive testing", + args: args{ + sqlProxy: sqlProxy, + }, + want: &WNJpnRepository{ + SqlProxy: sqlProxy, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.sqlProxy); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want=\n%v", got, tt.want) + } + }) + } +} + +func TestWNJpnRepository_GetAllAVNWords(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + wnJpnRepository := New( + sqlproxy.New(), + ) + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + allAVNWords, err := wnJpnRepository.GetAllAVNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVNWords() : error =\n%v", err) + } + + type fields struct { + SqlProxy sqlproxy.Sql + WNJpnDBFilePath string + } + tests := []struct { + name string + fields fields + want []model.Word + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + SqlProxy: sqlproxy.New(), + }, + want: allAVNWords, + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.Remove(wnJpnDBFilePath); err != nil { + t.Errorf("Os.Remove() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + w := New( + tt.fields.SqlProxy, + ) + got, err := w.GetAllAVNWords(wnJpnDBFilePath) + if (err != nil) != tt.wantErr { + t.Errorf("WNJpnRepository.GetAllAVNWords() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("WNJpnRepository.GetAllAVNWords() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestWNJpnRepository_GetAllNWords(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + wnJpnRepository := New( + sqlproxy.New(), + ) + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + allNWords, err := wnJpnRepository.GetAllNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllNWords() : error =\n%v", err) + } + + type fields struct { + SqlProxy sqlproxy.Sql + } + tests := []struct { + name string + fields fields + want []model.Word + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + SqlProxy: sqlproxy.New(), + }, + want: allNWords, + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.Remove(wnJpnDBFilePath); err != nil { + t.Errorf("Os.Remove() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + w := New( + tt.fields.SqlProxy, + ) + got, err := w.GetAllNWords(wnJpnDBFilePath) + if (err != nil) != tt.wantErr { + t.Errorf("WNJpnRepository.GetAllNWords() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("WNJpnRepository.GetAllNWords() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestWNJpnRepository_GetAllAVWords(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + wnJpnRepository := New( + sqlproxy.New(), + ) + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + allAVWords, err := wnJpnRepository.GetAllAVWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVWords() : error =\n%v", err) + } + + type fields struct { + SqlProxy sqlproxy.Sql + WNJpnDBFilePath string + } + tests := []struct { + name string + fields fields + want []model.Word + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + SqlProxy: sqlproxy.New(), + WNJpnDBFilePath: wnJpnDBFilePath, + }, + want: allAVWords, + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.Remove(wnJpnDBFilePath); err != nil { + t.Errorf("Os.Remove() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + w := New( + tt.fields.SqlProxy, + ) + got, err := w.GetAllAVWords(wnJpnDBFilePath) + if (err != nil) != tt.wantErr { + t.Errorf("WNJpnRepository.GetAllAVWords() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("WNJpnRepository.GetAllAVWords() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestWNJpnRepository_getWords(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + wnJpnRepository := New( + sqlproxy.New(), + ) + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + allAVNWords, err := wnJpnRepository.GetAllAVNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVNWords() : error =\n%v", err) + } + + type fields struct { + SqlProxy sqlproxy.Sql + WNJpnDBFilePath string + } + type args struct { + query string + } + tests := []struct { + name string + fields fields + args args + want []model.Word + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + SqlProxy: sqlproxy.New(), + WNJpnDBFilePath: wnJpnDBFilePath, + }, + args: args{ + query: query.GetAllJapaneseAVNWords, + }, + want: allAVNWords, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.Remove(wnJpnDBFilePath); err != nil { + t.Errorf("Os.Remove() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (SqlProxy.Open() failed)", + fields: fields{ + SqlProxy: nil, + WNJpnDBFilePath: wnJpnDBFilePath, + }, + args: args{ + query: query.GetAllJapaneseAVNWords, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(nil, errors.New("SqlProxy.Open() failed")) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.Remove(wnJpnDBFilePath); err != nil { + t.Errorf("Os.Remove() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (db.Query() failed)", + fields: fields{ + SqlProxy: nil, + WNJpnDBFilePath: wnJpnDBFilePath, + }, + args: args{ + query: query.GetAllJapaneseAVNWords, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Query(gomock.Any()).Return(nil, errors.New("DBInstance.Query() failed")) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.Remove(wnJpnDBFilePath); err != nil { + t.Errorf("Os.Remove() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rows.Scan() failed)", + fields: fields{ + SqlProxy: nil, + WNJpnDBFilePath: wnJpnDBFilePath, + }, + args: args{ + query: query.GetAllJapaneseAVNWords, + }, + want: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + mockRowsInstance := mocksqlproxy.NewMockRowsInstanceInterface(mockCtrl) + mockRowsInstance.EXPECT().Scan(gomock.Any()).Return(errors.New("Rows.Scan() failed")) + mockRowsInstance.EXPECT().Next().Return(true) + mockRowsInstance.EXPECT().Close().Return(nil) + mockDBInstance := mocksqlproxy.NewMockDBInstanceInterface(mockCtrl) + mockDBInstance.EXPECT().Query(gomock.Any()).Return(mockRowsInstance, nil) + mockDBInstance.EXPECT().Close().Return(nil) + mockSqlProxy := mocksqlproxy.NewMockSql(mockCtrl) + mockSqlProxy.EXPECT().Open(gomock.Any(), gomock.Any()).Return(mockDBInstance, nil) + tt.SqlProxy = mockSqlProxy + }, + cleanup: func() { + if err := osProxy.Remove(wnJpnDBFilePath); err != nil { + t.Errorf("Os.Remove() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + w := New( + tt.fields.SqlProxy, + ) + got, err := w.getWords(wnJpnDBFilePath, tt.args.query) + if (err != nil) != tt.wantErr { + t.Errorf("WNJpnRepository.getWords() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("WNJpnRepository.getWords() = %v, want %v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} diff --git a/app/library/dbfiledirpathprovider/constant.go b/app/library/dbfiledirpathprovider/constant.go new file mode 100644 index 00000000..24d29d41 --- /dev/null +++ b/app/library/dbfiledirpathprovider/constant.go @@ -0,0 +1,8 @@ +package dbfiledirpathprovider + +const ( + // jrp environment variable saving the directory of wnjpn.db + JRP_ENV_WNJPN_DB_FILE_DIR = "JRP_WNJPN_DB_FILE_DIR" + // jrp environment variable saving the directory of jrp.db + JRP_ENV_JRP_DB_FILE_DIR = "JRP_DB_FILE_DIR" +) diff --git a/app/library/dbfiledirpathprovider/dbfiledirpathprovider.go b/app/library/dbfiledirpathprovider/dbfiledirpathprovider.go new file mode 100644 index 00000000..1d94756c --- /dev/null +++ b/app/library/dbfiledirpathprovider/dbfiledirpathprovider.go @@ -0,0 +1,72 @@ +package dbfiledirpathprovider + +import ( + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/user" +) + +// DBFileDirPathProvidable is an interface for DBFileDirPathProvider. +type DBFileDirPathProvidable interface { + GetJrpDBFileDirPath() (string, error) + GetWNJpnDBFileDirPath() (string, error) +} + +// DBFileDirPathProvider is a struct that implements DBFileDirPathProvidable. +type DBFileDirPathProvider struct { + FilepathProxy filepathproxy.FilePath + OsProxy osproxy.Os + UserProxy userproxy.User +} + +// New is a constructor for DBFileDirPathProvider. +func New( + filepath filepathproxy.FilePath, + os osproxy.Os, + user userproxy.User, +) *DBFileDirPathProvider { + return &DBFileDirPathProvider{ + FilepathProxy: filepath, + OsProxy: os, + UserProxy: user, + } +} + +// GetJrpDBFileDirPath provides db file directory path for jrp db file. +func (d *DBFileDirPathProvider) GetJrpDBFileDirPath() (string, error) { + return d.getDBFileDirPath(JRP_ENV_JRP_DB_FILE_DIR) +} + +// GetWNJpnDBFileDirPath provides db file directory path for wnjpn db file. +func (d *DBFileDirPathProvider) GetWNJpnDBFileDirPath() (string, error) { + return d.getDBFileDirPath(JRP_ENV_WNJPN_DB_FILE_DIR) +} + +// getDBFileDirPath gets db file directory path from env var or default. +func (d *DBFileDirPathProvider) getDBFileDirPath(envVar string) (string, error) { + // get env var + envDir := d.OsProxy.Getenv(envVar) + if envDir != "" { + // if env var is set, use it + return envDir, nil + } + + // get current user + currentUser, err := d.UserProxy.Current() + if err != nil { + return "", err + } + + // get default db file dir path + var defaultDBFileDirPath string + xdgDataHome := d.OsProxy.Getenv("XDG_DATA_HOME") + if xdgDataHome != "" { + // if XDG_DATA_HOME is set, use it + defaultDBFileDirPath = d.FilepathProxy.Join(xdgDataHome, "jrp") + } else { + // if XDG_DATA_HOME is not set, use default + defaultDBFileDirPath = d.FilepathProxy.Join(currentUser.FieldUser.HomeDir, ".local", "share", "jrp") + } + + return defaultDBFileDirPath, nil +} diff --git a/app/library/dbfiledirpathprovider/dbfiledirpathprovider_test.go b/app/library/dbfiledirpathprovider/dbfiledirpathprovider_test.go new file mode 100644 index 00000000..cd2b18cb --- /dev/null +++ b/app/library/dbfiledirpathprovider/dbfiledirpathprovider_test.go @@ -0,0 +1,451 @@ +package dbfiledirpathprovider + +import ( + "errors" + "os" + "reflect" + "testing" + + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/user" + + "github.com/yanosea/jrp/mock/app/proxy/user" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNew(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + userProxy := userproxy.New() + + type args struct { + filepath filepathproxy.FilePath + os osproxy.Os + user userproxy.User + } + tests := []struct { + name string + args args + want *DBFileDirPathProvider + }{ + { + name: "positive testing", + args: args{ + filepath: filepathProxy, + os: osProxy, + user: userProxy, + }, + want: &DBFileDirPathProvider{ + FilepathProxy: filepathProxy, + OsProxy: osProxy, + UserProxy: userProxy, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.filepath, tt.args.os, tt.args.user); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want=\n%v", got, tt.want) + } + }) + } +} + +func TestDBFileDirPathProvider_GetJrpDBFileDirPath(t *testing.T) { + userProxy := userproxy.New() + currentUser, err := userProxy.Current() + if err != nil { + t.Errorf("UserProxy.Current() : error =\n%v", err) + } + + type fields struct { + FilepathProxy filepathproxy.FilePath + OsProxy osproxy.Os + UserProxy userproxy.User + } + tests := []struct { + name string + fields fields + want string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (with no xdg data home, with no env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/.local/share/jrp"), + wantErr: false, + setup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_DB_FILE_DIR") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_DB_FILE_DIR") + }, + }, + { + name: "positive testing (with xdg data home, with no env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/.local/share/jrp"), + wantErr: false, + setup: func() { + os.Setenv("XDG_DATA_HOME", currentUser.FieldUser.HomeDir+"/.local/share") + os.Unsetenv("JRP_DB_FILE_DIR") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_DB_FILE_DIR") + }, + }, + { + name: "positive testing (with no xdg data home, with env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/jrp"), + wantErr: false, + setup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Setenv("JRP_DB_FILE_DIR", currentUser.FieldUser.HomeDir+"/jrp") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_DB_FILE_DIR") + }, + }, + { + name: "positive testing (with xdg data home, with env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/jrp"), + wantErr: false, + setup: func() { + os.Setenv("XDG_DATA_HOME", currentUser.FieldUser.HomeDir+"/.local/share") + os.Setenv("JRP_DB_FILE_DIR", currentUser.FieldUser.HomeDir+"/jrp") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_DB_FILE_DIR") + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + d := New( + tt.fields.FilepathProxy, + tt.fields.OsProxy, + tt.fields.UserProxy, + ) + got, err := d.GetJrpDBFileDirPath() + gotDir := testutility.ReplaceDoubleSlashToSingleSlash(got) + if (err != nil) != tt.wantErr { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if gotDir != tt.want { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestDBFileDirPathProvider_GetWNJpnDBFileDirPath(t *testing.T) { + userProxy := userproxy.New() + currentUser, err := userProxy.Current() + if err != nil { + t.Errorf("UserProxy.Current() : error =\n%v", err) + } + + type fields struct { + FilepathProxy filepathproxy.FilePath + OsProxy osproxy.Os + UserProxy userproxy.User + } + tests := []struct { + name string + fields fields + want string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (with no xdg data home, with no env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/.local/share/jrp"), + wantErr: false, + setup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_WNJPN_DB_FILE_DIR") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_WNJPN_DB_FILE_DIR") + }, + }, + { + name: "positive testing (with xdg data home, with no env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/.local/share/jrp"), + wantErr: false, + setup: func() { + os.Setenv("XDG_DATA_HOME", currentUser.FieldUser.HomeDir+"/.local/share") + os.Unsetenv("JRP_WNJPN_DB_FILE_DIR") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_WNJPN_DB_FILE_DIR") + }, + }, + { + name: "positive testing (with no xdg data home, with env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/jrp"), + wantErr: false, + setup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Setenv("JRP_WNJPN_DB_FILE_DIR", currentUser.FieldUser.HomeDir+"/jrp") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_WNJPN_DB_FILE_DIR") + }, + }, + { + name: "positive testing (with xdg data home, with env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/jrp"), + wantErr: false, + setup: func() { + os.Setenv("XDG_DATA_HOME", currentUser.FieldUser.HomeDir+"/.local/share") + os.Setenv("JRP_WNJPN_DB_FILE_DIR", currentUser.FieldUser.HomeDir+"/jrp") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("JRP_WNJPN_DB_FILE_DIR") + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + d := New( + tt.fields.FilepathProxy, + tt.fields.OsProxy, + tt.fields.UserProxy, + ) + got, err := d.GetWNJpnDBFileDirPath() + gotDir := testutility.ReplaceDoubleSlashToSingleSlash(got) + if (err != nil) != tt.wantErr { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if gotDir != tt.want { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestDBFileDirPathProvider_getDBFileDirPath(t *testing.T) { + userProxy := userproxy.New() + currentUser, err := userProxy.Current() + if err != nil { + t.Errorf("UserProxy.Current() : error =\n%v", err) + } + + type fields struct { + FilepathProxy filepathproxy.FilePath + OsProxy osproxy.Os + UserProxy userproxy.User + } + type args struct { + envVar string + } + tests := []struct { + name string + fields fields + args args + want string + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (with no xdg data home, with no env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + args: args{ + envVar: "TEST_ENV", + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/.local/share/jrp"), + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("TEST_ENV") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("TEST_ENV") + }, + }, + { + name: "positive testing (with xdg data home, with no env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + args: args{ + envVar: "TEST_ENV", + }, + want: testutility.ReplaceDoubleSlashToSingleSlash(currentUser.FieldUser.HomeDir + "/.local/share/jrp"), + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + os.Setenv("XDG_DATA_HOME", currentUser.FieldUser.HomeDir+"/.local/share") + os.Unsetenv("TEST_ENV") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("TEST_ENV") + }, + }, + { + name: "positive testing (with no xdg data home, with env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + args: args{ + envVar: "TEST_ENV", + }, + want: "TEST_DIR", + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + os.Unsetenv("XDG_DATA_HOME") + os.Setenv("TEST_ENV", "TEST_DIR") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("TEST_ENV") + }, + }, + { + name: "positive testing (with xdg data home, with env)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: userproxy.New(), + }, + args: args{ + envVar: "TEST_ENV", + }, + want: "TEST_DIR", + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + os.Setenv("XDG_DATA_HOME", currentUser.FieldUser.HomeDir+"/.local/share") + os.Setenv("TEST_ENV", "TEST_DIR") + }, + cleanup: func() { + os.Unsetenv("XDG_DATA_HOME") + os.Unsetenv("TEST_ENV") + }, + }, + { + name: "negative testing (UserProxy.Current() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + OsProxy: osproxy.New(), + UserProxy: nil, + }, + args: args{ + envVar: "TEST_ENV", + }, + want: "", + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + os.Unsetenv("TEST_ENV") + mockUserProxy := mockuserproxy.NewMockUser(mockCtrl) + mockUserProxy.EXPECT().Current().Return(nil, errors.New("UserProxy.Current() failed")) + tt.UserProxy = mockUserProxy + }, + cleanup: func() { + os.Unsetenv("TEST_ENV") + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + d := New( + tt.fields.FilepathProxy, + tt.fields.OsProxy, + tt.fields.UserProxy, + ) + got, err := d.getDBFileDirPath(tt.args.envVar) + gotDir := testutility.ReplaceDoubleSlashToSingleSlash(got) + if (err != nil) != tt.wantErr { + t.Errorf("DBFileDirPathProvider.getDBFileDirPath() error = %v, wantErr %v", err, tt.wantErr) + return + } + if gotDir != tt.want { + t.Errorf("DBFileDirPathProvider.getDBFileDirPath() = %v, want %v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} diff --git a/app/library/dbfiledirpathprovider/godoc.go b/app/library/dbfiledirpathprovider/godoc.go new file mode 100644 index 00000000..6b582d58 --- /dev/null +++ b/app/library/dbfiledirpathprovider/godoc.go @@ -0,0 +1,2 @@ +// Package dbfiledirpathprovider is a package for providing db file directory path. +package dbfiledirpathprovider diff --git a/app/library/downloader/constant.go b/app/library/downloader/constant.go new file mode 100644 index 00000000..35cf43fc --- /dev/null +++ b/app/library/downloader/constant.go @@ -0,0 +1,22 @@ +package downloader + +// DownloadStatus is a type for download status +type DownloadStatus int + +const ( + // DownloadedSuccessfully is a status for downloaded successfully + DownloadedSuccessfully DownloadStatus = iota + // DownloadedFailed is a status for downloaded failed + DownloadedFailed + // DownloadedAlready is a status for downloaded already + DownloadedAlready +) + +const ( + // WordNet Japan database archive file URL + WNJPN_DB_ARCHIVE_FILE_URL = "https://github.com/bond-lab/wnja/releases/download/v1.1/wnjpn.db.gz" + // WordNet Japan database archive file name + WNJPN_DB_ARCHIVE_FILE_NAME = "wnjpn.db.gz" + // WordNet Japan sqlite database file name + WNJPN_DB_FILE_NAME = "wnjpn.db" +) diff --git a/app/library/downloader/downloader.go b/app/library/downloader/downloader.go new file mode 100644 index 00000000..9b39a401 --- /dev/null +++ b/app/library/downloader/downloader.go @@ -0,0 +1,160 @@ +package downloader + +import ( + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" +) + +// Downloadable is an interface that provides a function to download wnjpn db file. +type Downloadable interface { + DownloadWNJpnDBFile(wnJpnDBFileDirPath string) (DownloadStatus, error) +} + +// Downloader is a struct that implements Downloadable interface. +type Downloader struct { + FilepathProxy filepathproxy.FilePath + GzipProxy gzipproxy.Gzip + HttpProxy httpproxy.Http + IoProxy ioproxy.Io + OsProxy osproxy.Os + Utility utility.UtilityInterface +} + +// New is a constructor of Downloader. +func New( + filepathProxy filepathproxy.FilePath, + gzipProxy gzipproxy.Gzip, + httpProxy httpproxy.Http, + ioProxy ioproxy.Io, + osProxy osproxy.Os, + utility utility.UtilityInterface, +) *Downloader { + return &Downloader{ + FilepathProxy: filepathProxy, + GzipProxy: gzipProxy, + HttpProxy: httpProxy, + IoProxy: ioProxy, + OsProxy: osProxy, + Utility: utility, + } +} + +// DownloadWNJpnDBFile downloads wnjpn db file. +func (d *Downloader) DownloadWNJpnDBFile(wnJpnDBFileDirPath string) (DownloadStatus, error) { + // create dir if not exist + if err := d.Utility.CreateDirIfNotExist(wnJpnDBFileDirPath); err != nil { + // if failed to create dir, return failure + return DownloadedFailed, err + } + + // check if db file is already downloaded + dbFilePath := d.FilepathProxy.Join(wnJpnDBFileDirPath, WNJPN_DB_FILE_NAME) + if _, err := d.OsProxy.Stat(dbFilePath); d.OsProxy.IsNotExist(err) { + // if not downloaded, download and extract db file + return d.downloadAndExtractDBFile(dbFilePath) + } + + // if already downloaded, return + return DownloadedAlready, nil +} + +// downloadAndExtractDBFile downloads and extracts wnjapn db file. +func (d *Downloader) downloadAndExtractDBFile(dbFilePath string) (DownloadStatus, error) { + var deferErr error + // download gzip file + resp, err := d.downloadGzipFile() + if err != nil { + return DownloadedFailed, err + } + defer func() { + deferErr = resp.FieldResponse.Body.Close() + }() + + // save to temp file + tempFilePath, err := d.saveToTempFile(resp.FieldResponse.Body) + if err != nil { + return DownloadedFailed, err + } + defer func() { + deferErr = d.OsProxy.Remove(tempFilePath) + }() + + // extract gzip file + if err := d.extractGzipFile(tempFilePath, dbFilePath); err != nil { + return DownloadedFailed, err + } + + return DownloadedSuccessfully, deferErr +} + +// downloadGzipFile downloads gzip file. +func (d *Downloader) downloadGzipFile() (*httpproxy.ResponseInstance, error) { + // download gzip file + return d.HttpProxy.Get(WNJPN_DB_ARCHIVE_FILE_URL) +} + +// saveToTempFile saves body to temp file. +func (d *Downloader) saveToTempFile(body ioproxy.ReaderInstanceInterface) (string, error) { + var deferErr error + // create temp file + tempFilePath := d.FilepathProxy.Join(d.OsProxy.TempDir(), WNJPN_DB_ARCHIVE_FILE_NAME) + out, err := d.OsProxy.Create(tempFilePath) + if err != nil { + return "", err + } + defer func() { + deferErr = out.Close() + }() + + // copy downloaded file to temp file + if _, err := d.IoProxy.Copy(out, body); err != nil { + return "", err + } + + // seek to start + if _, err := out.Seek(0, ioproxy.SeekStart); err != nil { + return "", err + } + + return tempFilePath, deferErr +} + +// extractGzipFile extracts gzip file. +func (d *Downloader) extractGzipFile(srcPath, destPath string) error { + var deferErr error + // open gzip file + file, err := d.OsProxy.Open(srcPath) + if err != nil { + return err + } + defer func() { + deferErr = file.Close() + }() + gz, err := d.GzipProxy.NewReader(file) + if err != nil { + return err + } + defer func() { + deferErr = gz.Close() + }() + + // create file to save + out, err := d.OsProxy.Create(destPath) + if err != nil { + return err + } + defer func() { + deferErr = out.Close() + }() + + // copy gzip file to dest file + if _, err := d.IoProxy.Copy(out, gz); err != nil { + return err + } + + return deferErr +} diff --git a/app/library/downloader/downloader_test.go b/app/library/downloader/downloader_test.go new file mode 100644 index 00000000..479a9cef --- /dev/null +++ b/app/library/downloader/downloader_test.go @@ -0,0 +1,973 @@ +package downloader + +import ( + "errors" + "reflect" + "testing" + + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/user" + + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/mock/app/proxy/gzip" + "github.com/yanosea/jrp/mock/app/proxy/http" + "github.com/yanosea/jrp/mock/app/proxy/io" + "github.com/yanosea/jrp/mock/app/proxy/os" + "go.uber.org/mock/gomock" +) + +func TestNew(t *testing.T) { + filepathProxy := filepathproxy.New() + gzipProxy := gzipproxy.New() + httpProxy := httpproxy.New() + ioProxy := ioproxy.New() + osProxy := osproxy.New() + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + + type args struct { + filepathProxy filepathproxy.FilePath + gzipProxy gzipproxy.Gzip + httpProxy httpproxy.Http + ioProxy ioproxy.Io + osProxy osproxy.Os + utility utility.UtilityInterface + wnJpnDBFileDirPath string + } + tests := []struct { + name string + args args + want *Downloader + }{ + { + name: "positive testing", + args: args{ + filepathProxy: filepathProxy, + gzipProxy: gzipProxy, + httpProxy: httpProxy, + ioProxy: ioProxy, + osProxy: osProxy, + utility: util, + wnJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + want: &Downloader{ + FilepathProxy: filepathProxy, + GzipProxy: gzipProxy, + HttpProxy: httpProxy, + IoProxy: ioProxy, + OsProxy: osProxy, + Utility: util, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.filepathProxy, tt.args.gzipProxy, tt.args.httpProxy, tt.args.ioProxy, tt.args.osProxy, tt.args.utility); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want=\n%v", got, tt.want) + } + }) + } +} + +func TestDownloader_DownloadWNJpnDBFile(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, WNJPN_DB_FILE_NAME) + + type fields struct { + FilepathProxy filepathproxy.FilePath + GzipProxy gzipproxy.Gzip + HttpProxy httpproxy.Http + IoProxy ioproxy.Io + OsProxy osproxy.Os + Utility utility.UtilityInterface + WNJpnDBFileDirPath string + } + tests := []struct { + name string + fields fields + want DownloadStatus + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (DB file exists)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + want: DownloadedAlready, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (DB file does not exists)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + want: DownloadedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExits() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: nil, + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + want: DownloadedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockUtil := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtil.EXPECT().CreateDirIfNotExist(gomock.Any()).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + tt.Utility = mockUtil + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + d := New( + tt.fields.FilepathProxy, + tt.fields.GzipProxy, + tt.fields.HttpProxy, + tt.fields.IoProxy, + tt.fields.OsProxy, + tt.fields.Utility, + ) + got, err := d.DownloadWNJpnDBFile(wnJpnDBFileDirPath) + if (err != nil) != tt.wantErr { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Downloader.DownloadWNJpnDBFile() : got = %v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestDownloader_downloadAndExtractDBFile(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, WNJPN_DB_FILE_NAME) + + type fields struct { + FilepathProxy filepathproxy.FilePath + GzipProxy gzipproxy.Gzip + HttpProxy httpproxy.Http + IoProxy ioproxy.Io + OsProxy osproxy.Os + Utility utility.UtilityInterface + WNJpnDBFileDirPath string + } + type args struct { + dbFilePath string + } + tests := []struct { + name string + fields fields + args args + want DownloadStatus + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + dbFilePath: wnJpnDBFilePath, + }, + want: DownloadedSuccessfully, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Downloader.downloadGzipFile() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: nil, + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + dbFilePath: wnJpnDBFilePath, + }, + want: DownloadedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockHttpProxy := mockhttpproxy.NewMockHttp(mockCtrl) + mockHttpProxy.EXPECT().Get(gomock.Any()).Return(nil, errors.New("HttpProxy.Get() failed")) + tt.HttpProxy = mockHttpProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Downloader.saveToTempFile() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: nil, + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + dbFilePath: wnJpnDBFilePath, + }, + want: DownloadedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockIoProxy := mockioproxy.NewMockIo(mockCtrl) + mockIoProxy.EXPECT().Copy(gomock.Any(), gomock.Any()).Return(int64(0), errors.New("IoProxy.Copy() failed")) + tt.IoProxy = mockIoProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Downloader.extractGzipFile() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: nil, + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + dbFilePath: wnJpnDBFilePath, + }, + want: DownloadedFailed, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockGzipProxy := mockgzipproxy.NewMockGzip(mockCtrl) + mockGzipProxy.EXPECT().NewReader(gomock.Any()).Return(nil, errors.New("GzipProxy.NewReader() failed")) + tt.GzipProxy = mockGzipProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + d := New( + tt.fields.FilepathProxy, + tt.fields.GzipProxy, + tt.fields.HttpProxy, + tt.fields.IoProxy, + tt.fields.OsProxy, + tt.fields.Utility, + ) + got, err := d.downloadAndExtractDBFile(tt.args.dbFilePath) + if (err != nil) != tt.wantErr { + t.Errorf("Downloader.downloadAndExtractDBFile() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Downloader.downloadAndExtractDBFile() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestDownloader_downloadGzipFile(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + + type fields struct { + FilepathProxy filepathproxy.FilePath + GzipProxy gzipproxy.Gzip + HttpProxy httpproxy.Http + IoProxy ioproxy.Io + OsProxy osproxy.Os + Utility utility.UtilityInterface + WNJpnDBFileDirPath string + } + tests := []struct { + name string + fields fields + wantStatusCode int + wantErr bool + }{ + { + name: "positive testing", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + wantStatusCode: 200, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + d := New( + tt.fields.FilepathProxy, + tt.fields.GzipProxy, + tt.fields.HttpProxy, + tt.fields.IoProxy, + tt.fields.OsProxy, + tt.fields.Utility, + ) + got, err := d.downloadGzipFile() + if (err != nil) != tt.wantErr { + t.Errorf("Downloader.downloadGzipFile() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if got.FieldResponse.StatusCode != tt.wantStatusCode { + t.Errorf("Downloader.downloadGzipFile() : got =\n%v, want =\n%v", got.FieldResponse.StatusCode, tt.wantStatusCode) + } + }) + } +} + +func TestDownloader_saveToTempFile(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + tempFilePath := filepathProxy.Join(osProxy.TempDir(), WNJPN_DB_ARCHIVE_FILE_NAME) + resp, err := dl.downloadGzipFile() + if err != nil { + t.Errorf("Downloader.downloadGzipFile() : error =\n%v", err) + } + + type fields struct { + FilepathProxy filepathproxy.FilePath + GzipProxy gzipproxy.Gzip + HttpProxy httpproxy.Http + IoProxy ioproxy.Io + OsProxy osproxy.Os + Utility utility.UtilityInterface + WNJpnDBFileDirPath string + } + type args struct { + body ioproxy.ReaderInstanceInterface + } + tests := []struct { + name string + fields fields + args args + want string + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + }{ + { + name: "positive testing", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + body: resp.FieldResponse.Body, + }, + want: tempFilePath, + wantErr: false, + setup: nil, + }, + { + name: "negative testing (OsProxy.Create() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: nil, + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + body: resp.FieldResponse.Body, + }, + want: "", + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockOsProxy := mockosproxy.NewMockOs(mockCtrl) + mockOsProxy.EXPECT().TempDir().Return(osProxy.TempDir()) + mockOsProxy.EXPECT().Create(gomock.Any()).Return(nil, errors.New("OsProxy.Create() failed")) + tt.OsProxy = mockOsProxy + }, + }, + { + name: "negative testing (IoProxy.Copy() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: nil, + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + body: resp.FieldResponse.Body, + }, + want: "", + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockIoProxy := mockioproxy.NewMockIo(mockCtrl) + mockIoProxy.EXPECT().Copy(gomock.Any(), gomock.Any()).Return(int64(0), errors.New("IoProxy.Copy() failed")) + tt.IoProxy = mockIoProxy + }, + }, + { + name: "negative testing (out.Seek() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: nil, + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + body: resp.FieldResponse.Body, + }, + want: "", + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockOsProxy := mockosproxy.NewMockOs(mockCtrl) + mockOsProxy.EXPECT().TempDir().Return(osProxy.TempDir()) + mockFileInstance := mockosproxy.NewMockFileInstanceInterface(mockCtrl) + mockFileInstance.EXPECT().Seek(int64(0), ioproxy.SeekStart).Return(int64(0), errors.New("FileInstance.Seek() failed")) + mockFileInstance.EXPECT().Close().Return(nil) + mockOsProxy.EXPECT().Create(gomock.Any()).Return(mockFileInstance, nil) + tt.OsProxy = mockOsProxy + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + d := New( + tt.fields.FilepathProxy, + tt.fields.GzipProxy, + tt.fields.HttpProxy, + tt.fields.IoProxy, + tt.fields.OsProxy, + tt.fields.Utility, + ) + got, err := d.saveToTempFile(tt.args.body) + if (err != nil) != tt.wantErr { + t.Errorf("Downloader.saveToTempFile() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("Downloader.saveToTempFile() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} + +func TestDownloader_extractGzipFile(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, WNJPN_DB_FILE_NAME) + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + srcPath := filepathProxy.Join(osProxy.TempDir(), WNJPN_DB_ARCHIVE_FILE_NAME) + destPath := filepathProxy.Join(wnJpnDBFileDirPath, WNJPN_DB_FILE_NAME) + + type fields struct { + FilepathProxy filepathproxy.FilePath + GzipProxy gzipproxy.Gzip + HttpProxy httpproxy.Http + IoProxy ioproxy.Io + OsProxy osproxy.Os + Utility utility.UtilityInterface + WNJpnDBFileDirPath string + } + type args struct { + srcPath string + destPath string + } + tests := []struct { + name string + fields fields + args args + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + srcPath: srcPath, + destPath: destPath, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + resp, err := dl.downloadGzipFile() + if err != nil { + t.Errorf("Downloader.downloadGzipFile() : error =\n%v", err) + } + _, err = dl.saveToTempFile(resp.FieldResponse.Body) + if err != nil { + t.Errorf("Downloader.saveToTempFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (OsProxy.Open() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: nil, + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + srcPath: srcPath, + destPath: destPath, + }, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + resp, err := dl.downloadGzipFile() + if err != nil { + t.Errorf("Downloader.downloadGzipFile() : error =\n%v", err) + } + _, err = dl.saveToTempFile(resp.FieldResponse.Body) + if err != nil { + t.Errorf("Downloader.saveToTempFile() : error =\n%v", err) + } + mockOsProxy := mockosproxy.NewMockOs(mockCtrl) + mockOsProxy.EXPECT().Open(gomock.Any()).Return(nil, errors.New("OsProxy.Open() failed")) + tt.OsProxy = mockOsProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (GzipProxy.NewReader() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: nil, + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + srcPath: srcPath, + destPath: destPath, + }, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + resp, err := dl.downloadGzipFile() + if err != nil { + t.Errorf("Downloader.downloadGzipFile() : error =\n%v", err) + } + _, err = dl.saveToTempFile(resp.FieldResponse.Body) + if err != nil { + t.Errorf("Downloader.saveToTempFile() : error =\n%v", err) + } + mockGzipProxy := mockgzipproxy.NewMockGzip(mockCtrl) + mockGzipProxy.EXPECT().NewReader(gomock.Any()).Return(nil, errors.New("GzipProxy.NewReader() failed")) + tt.GzipProxy = mockGzipProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (OsProxy.Create() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: ioproxy.New(), + OsProxy: nil, + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + srcPath: srcPath, + destPath: destPath, + }, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + resp, err := dl.downloadGzipFile() + if err != nil { + t.Errorf("Downloader.downloadGzipFile() : error =\n%v", err) + } + _, err = dl.saveToTempFile(resp.FieldResponse.Body) + if err != nil { + t.Errorf("Downloader.saveToTempFile() : error =\n%v", err) + } + mockOsProxy := mockosproxy.NewMockOs(mockCtrl) + fileInstance, err := osProxy.Open(srcPath) + if err != nil { + t.Errorf("OsProxy.Open() : error =\n%v", err) + } + mockOsProxy.EXPECT().Open(gomock.Any()).Return(fileInstance, nil) + mockOsProxy.EXPECT().Create(gomock.Any()).Return(nil, errors.New("OsProxy.Create() failed")) + tt.OsProxy = mockOsProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (IoProxy.Copy() failed)", + fields: fields{ + FilepathProxy: filepathproxy.New(), + GzipProxy: gzipproxy.New(), + HttpProxy: httpproxy.New(), + IoProxy: nil, + OsProxy: osproxy.New(), + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + WNJpnDBFileDirPath: wnJpnDBFileDirPath, + }, + args: args{ + srcPath: srcPath, + destPath: destPath, + }, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + resp, err := dl.downloadGzipFile() + if err != nil { + t.Errorf("Downloader.downloadGzipFile() : error =\n%v", err) + } + _, err = dl.saveToTempFile(resp.FieldResponse.Body) + if err != nil { + t.Errorf("Downloader.saveToTempFile() : error =\n%v", err) + } + mockIoProxy := mockioproxy.NewMockIo(mockCtrl) + mockIoProxy.EXPECT().Copy(gomock.Any(), gomock.Any()).Return(int64(0), errors.New("IoProxy.Copy() failed")) + tt.IoProxy = mockIoProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + d := New( + tt.fields.FilepathProxy, + tt.fields.GzipProxy, + tt.fields.HttpProxy, + tt.fields.IoProxy, + tt.fields.OsProxy, + tt.fields.Utility, + ) + if err := d.extractGzipFile(tt.args.srcPath, tt.args.destPath); (err != nil) != tt.wantErr { + t.Errorf("Downloader.extractGzipFile() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} diff --git a/app/library/downloader/godoc.go b/app/library/downloader/godoc.go new file mode 100644 index 00000000..62f2ce9c --- /dev/null +++ b/app/library/downloader/godoc.go @@ -0,0 +1,2 @@ +// Package downloader is a package for downloading wnjpn db file. +package downloader diff --git a/app/library/generator/constant.go b/app/library/generator/constant.go new file mode 100644 index 00000000..5ce1127b --- /dev/null +++ b/app/library/generator/constant.go @@ -0,0 +1,25 @@ +package generator + +// GenerateMode is a type for generate mode. +type GenerateMode int + +const ( + // WithNoPrefixOrSuffix is a mode for generate without prefix or suffix. + WithNoPrefixOrSuffix GenerateMode = iota + // WithPrefix is a mode for generate with prefix. + WithPrefix + // WithSuffix is a mode for generate with suffix. + WithSuffix +) + +// GenerateResult is a type for generate result. +type GenerateResult int + +const ( + // GeneratedSuccessfully is a result for generated successfully. + GeneratedSuccessfully GenerateResult = iota + // GeneratedFailed is a result for generated failed. + GeneratedFailed + // DBFileNotFound is a result for generated failed because db file is not found. + DBFileNotFound +) diff --git a/app/library/generator/generator.go b/app/library/generator/generator.go new file mode 100644 index 00000000..181e2ac7 --- /dev/null +++ b/app/library/generator/generator.go @@ -0,0 +1,184 @@ +package generator + +import ( + jrp "github.com/yanosea/jrp/app/database/jrp/model" + wnjpn "github.com/yanosea/jrp/app/database/wnjpn/model" + "github.com/yanosea/jrp/app/database/wnjpn/repository" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/rand" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/time" +) + +// Generatable is an interface for Generator. +type Generatable interface { + GenerateJrp(wnJpnDBFilePath string, num int, word string, mode GenerateMode) (GenerateResult, []jrp.Jrp, error) +} + +// Generatoor is a struct that implements Generatable interface. +type Generator struct { + OsProxy osproxy.Os + RandProxy randproxy.Rand + SqlProxy sqlproxy.Sql + TimeProxy timeproxy.Time + WNJpnRepository repository.WNJpnRepositoryInterface +} + +// New is a constructor of Generator. +func New( + osProxy osproxy.Os, + randProxy randproxy.Rand, + sqlProxy sqlproxy.Sql, + timeProxy timeproxy.Time, + wnJpnRepository repository.WNJpnRepositoryInterface, +) *Generator { + return &Generator{ + OsProxy: osProxy, + RandProxy: randProxy, + SqlProxy: sqlProxy, + TimeProxy: timeProxy, + WNJpnRepository: wnJpnRepository, + } +} + +// GenerateJrp generates jrps. +func (g *Generator) GenerateJrp(wnJpnDBFilePath string, num int, word string, mode GenerateMode) (GenerateResult, []jrp.Jrp, error) { + if _, err := g.OsProxy.Stat(wnJpnDBFilePath); g.OsProxy.IsNotExist(err) { + // if WordNet Japan sqlite database file does not exist, return warning + return DBFileNotFound, nil, nil + } + + // define prefix, suffix and query + prefix, suffix := g.getPrefixAndSuffix(word, mode) + + // execute query and get all words + allWords, err := g.getAllWords(wnJpnDBFilePath, mode) + if err != nil { + return GeneratedFailed, nil, err + } + + // separate all words into AV and N words + allAVWords, allNWords := g.separateWords(allWords) + + // get jrps + jrps := g.getJrps(num, allAVWords, allNWords, prefix, suffix, mode) + + return GeneratedSuccessfully, jrps, nil +} + +// getAllWords gets all words based on mode. +func (g *Generator) getAllWords(wnJpnDBFilePath string, mode GenerateMode) ([]wnjpn.Word, error) { + var allWords []wnjpn.Word + var err error + switch mode { + case WithNoPrefixOrSuffix: + allWords, err = g.WNJpnRepository.GetAllAVNWords(wnJpnDBFilePath) + case WithPrefix: + allWords, err = g.WNJpnRepository.GetAllNWords(wnJpnDBFilePath) + case WithSuffix: + allWords, err = g.WNJpnRepository.GetAllAVWords(wnJpnDBFilePath) + } + return allWords, err +} + +// getJrps gets jrps based on mode. +func (g *Generator) getJrps(num int, + allAVWords []wnjpn.Word, + allNWords []wnjpn.Word, + argPrefix string, + argSuffix string, + mode GenerateMode, +) []jrp.Jrp { + jrps := make([]jrp.Jrp, 0) + createdAt := g.TimeProxy.Now() + + for i := 0; i < num; i++ { + var prefixWord string + var suffixWord string + switch mode { + case WithNoPrefixOrSuffix: + // get random number for prefix + randomIndexForPrefix := g.RandProxy.Intn(len(allAVWords)) + // get random prefix word + randomPrefix := allAVWords[randomIndexForPrefix] + // get random number for suffix + randomIndexForSuffix := g.RandProxy.Intn(len(allNWords)) + // get random suffix word + randomSuffix := allNWords[randomIndexForSuffix] + // set prefix word and suffix word + prefixWord = randomPrefix.Lemma.FieldNullString.String + suffixWord = randomSuffix.Lemma.FieldNullString.String + // set argPrefix and argSuffix to empty string + argPrefix = "" + argSuffix = "" + case WithPrefix: + // get random number for suffix + randomIndexSuffix := g.RandProxy.Intn(len(allAVWords)) + // get random prefix word + randomSuffix := allAVWords[randomIndexSuffix] + // set prefix word and suffix word + prefixWord = argPrefix + suffixWord = randomSuffix.Lemma.FieldNullString.String + // set argSuffix to empty string + argSuffix = "" + case WithSuffix: + // get random number for argPrefix + randomIndexPrefix := g.RandProxy.Intn(len(allNWords)) + // get random prefix word + randomPrefix := allNWords[randomIndexPrefix] + // set prefix word and suffix word + prefixWord = randomPrefix.Lemma.FieldNullString.String + suffixWord = argSuffix + // set argPrefix to empty string + argPrefix = "" + } + + jrp := jrp.Jrp{ + Phrase: prefixWord + suffixWord, + Prefix: g.SqlProxy.StringToNullString(argPrefix), + Suffix: g.SqlProxy.StringToNullString(argSuffix), + CreatedAt: createdAt, + UpdatedAt: createdAt, + } + + jrps = append(jrps, jrp) + } + + return jrps +} + +// getPrefixAndSuffix gets prefix word and suffix word based on mode. +func (g *Generator) getPrefixAndSuffix(word string, mode GenerateMode) (string, string) { + var prefix, suffix string + // define prefix and suffix + switch mode { + case WithNoPrefixOrSuffix: + prefix = "" + suffix = "" + case WithPrefix: + prefix = word + suffix = "" + case WithSuffix: + prefix = "" + suffix = word + } + + return prefix, suffix +} + +// separateWords separates all words into AV and N words. +func (g *Generator) separateWords(allWords []wnjpn.Word) ([]wnjpn.Word, []wnjpn.Word) { + allAVWords := []wnjpn.Word{} + allNWords := []wnjpn.Word{} + for _, word := range allWords { + if word.Pos.FieldNullString.Valid && word.Pos.FieldNullString.String == "n" { + // if word is noun + allNWords = append(allNWords, word) + } else { + // if word is adjective or verb + allAVWords = append(allAVWords, word) + } + } + + return allAVWords, allNWords +} diff --git a/app/library/generator/generator_test.go b/app/library/generator/generator_test.go new file mode 100644 index 00000000..6bf29a54 --- /dev/null +++ b/app/library/generator/generator_test.go @@ -0,0 +1,1244 @@ +package generator + +import ( + "errors" + "reflect" + "testing" + + "github.com/yanosea/jrp/app/database/wnjpn/model" + "github.com/yanosea/jrp/app/database/wnjpn/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/downloader" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/rand" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + + "github.com/yanosea/jrp/mock/app/database/wnjpn/repository" + "go.uber.org/mock/gomock" +) + +func TestNew(t *testing.T) { + osProxy := osproxy.New() + randProxy := randproxy.New() + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + wnJpnRepository := repository.New( + sqlProxy, + ) + + type args struct { + osProxy osproxy.Os + randProxy randproxy.Rand + sqlProxy sqlproxy.Sql + timeProxy timeproxy.Time + wnJpnRepository repository.WNJpnRepositoryInterface + } + tests := []struct { + name string + args args + want *Generator + }{ + { + name: "positive testing", + args: args{ + osProxy: osProxy, + randProxy: randProxy, + sqlProxy: sqlProxy, + timeProxy: timeProxy, + wnJpnRepository: wnJpnRepository, + }, + want: &Generator{ + OsProxy: osProxy, + RandProxy: randProxy, + SqlProxy: sqlProxy, + TimeProxy: timeProxy, + WNJpnRepository: wnJpnRepository, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.osProxy, tt.args.randProxy, tt.args.sqlProxy, tt.args.timeProxy, tt.args.wnJpnRepository); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want=\n%v", got, tt.want) + } + }) + } +} + +func TestGenerator_GenerateJrp(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + + type fields struct { + OsProxy osproxy.Os + RandProxy randproxy.Rand + SqlProxy sqlproxy.Sql + TimeProxy timeproxy.Time + WNJpnRepository repository.WNJpnRepositoryInterface + } + type args struct { + wnJpnDBFilePath string + num int + word string + mode GenerateMode + } + tests := []struct { + name string + fields fields + args args + wantResult GenerateResult + wantCount int + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + num: 1, + word: "", + mode: WithNoPrefixOrSuffix, + }, + wantResult: GeneratedSuccessfully, + wantCount: 1, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (DB file not exists)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + num: 1, + word: "", + mode: WithNoPrefixOrSuffix, + }, + wantResult: DBFileNotFound, + wantCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: nil, + }, + { + name: "negative testing (Generator.getAllWords() failed)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: nil, + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + num: 1, + word: "", + mode: WithNoPrefixOrSuffix, + }, + wantResult: GeneratedFailed, + wantCount: 0, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + mockWNJpnRepository := mockrepository.NewMockWNJpnRepositoryInterface(mockCtrl) + mockWNJpnRepository.EXPECT().GetAllAVNWords(gomock.Any()).Return(nil, errors.New("WNJpnRepository.GetAllAVNWords() failed")) + tt.WNJpnRepository = mockWNJpnRepository + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + g := New( + tt.fields.OsProxy, + tt.fields.RandProxy, + tt.fields.SqlProxy, + tt.fields.TimeProxy, + tt.fields.WNJpnRepository, + ) + gotResult, jrps, err := g.GenerateJrp(tt.args.wnJpnDBFilePath, tt.args.num, tt.args.word, tt.args.mode) + if (err != nil) != tt.wantErr { + t.Errorf("Generator.GenerateJrp() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if gotResult != tt.wantResult { + t.Errorf("Generator.GenerateJrp() : got =\n%v, want =\n%v", gotResult, tt.wantResult) + } + if len(jrps) != tt.wantCount { + t.Errorf("Generator.GenerateJrp() : got(count) =\n%v, want =\n%v", len(jrps), tt.wantCount) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func TestGenerator_getAllWords(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + wnJpnRepository := repository.New( + sqlproxy.New(), + ) + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + allAVNWords, err := wnJpnRepository.GetAllAVNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVNWords() : error =\n%v", err) + } + allAVWords, err := wnJpnRepository.GetAllAVWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVWords() : error =\n%v", err) + } + allNWords, err := wnJpnRepository.GetAllNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllNWords() : error =\n%v", err) + } + + type fields struct { + OsProxy osproxy.Os + RandProxy randproxy.Rand + SqlProxy sqlproxy.Sql + TimeProxy timeproxy.Time + WNJpnRepository repository.WNJpnRepositoryInterface + } + type args struct { + wnJpnDBFilePath string + mode GenerateMode + } + tests := []struct { + name string + fields fields + args args + want []model.Word + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (mode is NoPrefixOrSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + mode: WithNoPrefixOrSuffix, + }, + want: allAVNWords, + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (mode is WithPrefix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + mode: WithPrefix, + }, + want: allNWords, + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (mode is WithSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + mode: WithSuffix, + }, + want: allAVWords, + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + g := New( + tt.fields.OsProxy, + tt.fields.RandProxy, + tt.fields.SqlProxy, + tt.fields.TimeProxy, + tt.fields.WNJpnRepository, + ) + got, err := g.getAllWords(tt.args.wnJpnDBFilePath, tt.args.mode) + if (err != nil) != tt.wantErr { + t.Errorf("Generator.getAllWords() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Generator.getAllWords() : got =\n%v, want =\n%v", got, tt.want) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } +} + +func TestGenerator_getJrps(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + wnJpnRepository := repository.New( + sqlproxy.New(), + ) + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + allAVWords, err := wnJpnRepository.GetAllAVWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVWords() : error =\n%v", err) + } + allNWords, err := wnJpnRepository.GetAllNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllNWords() : error =\n%v", err) + } + + type fields struct { + OsProxy osproxy.Os + RandProxy randproxy.Rand + SqlProxy sqlproxy.Sql + TimeProxy timeproxy.Time + WNJpnRepository repository.WNJpnRepositoryInterface + } + type args struct { + num int + allAVWords []model.Word + allNWords []model.Word + prefix string + suffix string + mode GenerateMode + } + tests := []struct { + name string + fields fields + args args + wantCount int + wantPrefix string + wantSuffix string + setup func() + cleanup func() + }{ + { + name: "positive testing (num is -1, mode is NoPrefixOrSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 0, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithNoPrefixOrSuffix, + }, + wantCount: 0, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is -1, mode is WithPrefix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: -1, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithPrefix, + }, + wantCount: 0, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is -1, mode is WithSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: -1, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithSuffix, + }, + wantCount: 0, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 0, mode is NoPrefixOrSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 0, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithNoPrefixOrSuffix, + }, + wantCount: 0, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 0, mode is WithPrefix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 0, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithPrefix, + }, + wantCount: 0, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 0, mode is WithSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 0, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithSuffix, + }, + wantCount: 0, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 1, mode is NoPrefixOrSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 1, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithNoPrefixOrSuffix, + }, + wantCount: 1, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 1, mode is WithPrefix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 1, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "testPrefix", + suffix: "dummySuffix", + mode: WithPrefix, + }, + wantCount: 1, + wantPrefix: "testPrefix", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 1, mode is WithSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 1, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "testSuffix", + mode: WithSuffix, + }, + wantCount: 1, + wantPrefix: "", + wantSuffix: "testSuffix", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 2, mode is NoPrefixOrSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 2, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "dummySuffix", + mode: WithNoPrefixOrSuffix, + }, + wantCount: 2, + wantPrefix: "", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 2, mode is WithPrefix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 2, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "testPrefix", + suffix: "dummySuffix", + mode: WithPrefix, + }, + wantCount: 2, + wantPrefix: "testPrefix", + wantSuffix: "", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (num is 2, mode is WithSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + num: 2, + allAVWords: allAVWords, + allNWords: allNWords, + prefix: "dummyPrefix", + suffix: "testSuffix", + mode: WithSuffix, + }, + wantCount: 2, + wantPrefix: "", + wantSuffix: "testSuffix", + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + g := New( + tt.fields.OsProxy, + tt.fields.RandProxy, + tt.fields.SqlProxy, + tt.fields.TimeProxy, + tt.fields.WNJpnRepository, + ) + gots := g.getJrps(tt.args.num, tt.args.allAVWords, tt.args.allNWords, tt.args.prefix, tt.args.suffix, tt.args.mode) + if len(gots) != tt.wantCount { + t.Errorf("Generator.getJrps() : got(count) =\n%v, want =\n%v", len(gots), tt.wantCount) + } + for _, got := range gots { + if got.Prefix.FieldNullString.String != tt.wantPrefix { + t.Errorf("Generator.getJrps() : got(prefix) =\n%v, want =\n%v", gots[0].Prefix.FieldNullString.String, tt.wantPrefix) + } + if got.Suffix.FieldNullString.String != tt.wantSuffix { + t.Errorf("Generator.getJrps() : got(suffix) =\n%v, want =\n%v", gots[0].Suffix.FieldNullString.String, tt.wantSuffix) + } + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + } +} + +func TestGenerator_getPrefixAndSuffix(t *testing.T) { + type fields struct { + OsProxy osproxy.Os + RandProxy randproxy.Rand + SqlProxy sqlproxy.Sql + TimeProxy timeproxy.Time + WNJpnRepository repository.WNJpnRepositoryInterface + } + type args struct { + word string + mode GenerateMode + } + tests := []struct { + name string + fields fields + args args + wantPrefix string + wantSuffix string + }{ + { + name: "positive testing (mode is NoPrefixOrSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + word: "dummy", + mode: WithNoPrefixOrSuffix, + }, + wantPrefix: "", + wantSuffix: "", + }, + { + name: "positive testing (mode is WithPrefix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + word: "prefix", + mode: WithPrefix, + }, + wantPrefix: "prefix", + wantSuffix: "", + }, + { + name: "positive testing (mode is WithSuffix)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + word: "suffix", + mode: WithSuffix, + }, + wantPrefix: "", + wantSuffix: "suffix", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := New( + tt.fields.OsProxy, + tt.fields.RandProxy, + tt.fields.SqlProxy, + tt.fields.TimeProxy, + tt.fields.WNJpnRepository, + ) + gotPrefix, gotSuffix := g.getPrefixAndSuffix(tt.args.word, tt.args.mode) + if gotPrefix != tt.wantPrefix { + t.Errorf("Generator.getPrefixAndSuffix() : gotPrefix =\n%v, want =\n%v", gotPrefix, tt.wantPrefix) + } + if gotSuffix != tt.wantSuffix { + t.Errorf("Generator.getPrefixAndSuffix() : gotSuffix =\n%v, want =\n%v", gotSuffix, tt.wantSuffix) + } + }) + } +} + +func TestGenerator_separateWords(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + util := utility.New( + fmtproxy.New(), + osProxy, + strconvproxy.New(), + ) + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + wnJpnRepository := repository.New( + sqlproxy.New(), + ) + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + allAVNWords, err := wnJpnRepository.GetAllAVNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVNWords() : error =\n%v", err) + } + allAVWords, err := wnJpnRepository.GetAllAVWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllAVWords() : error =\n%v", err) + } + allNWords, err := wnJpnRepository.GetAllNWords(wnJpnDBFilePath) + if err != nil { + t.Errorf("WNJpnRepository.GetAllNWords() : error =\n%v", err) + } + type fields struct { + OsProxy osproxy.Os + RandProxy randproxy.Rand + SqlProxy sqlproxy.Sql + TimeProxy timeproxy.Time + WNJpnRepository repository.WNJpnRepositoryInterface + } + type args struct { + allWords []model.Word + } + tests := []struct { + name string + fields fields + args args + wantAVWords []model.Word + wantNWords []model.Word + setup func() + cleanup func() + }{ + { + name: "positive testing (allWords is nil)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + allWords: nil, + }, + wantAVWords: []model.Word{}, + wantNWords: []model.Word{}, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (allWords is empty)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + allWords: []model.Word{}, + }, + wantAVWords: []model.Word{}, + wantNWords: []model.Word{}, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (allWords is allAVNWords)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + allWords: allAVNWords, + }, + wantAVWords: allAVWords, + wantNWords: allNWords, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (allWords is allAVWords)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + allWords: allAVWords, + }, + wantAVWords: allAVWords, + wantNWords: []model.Word{}, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (allWords is allNWords)", + fields: fields{ + OsProxy: osproxy.New(), + RandProxy: randproxy.New(), + SqlProxy: sqlproxy.New(), + TimeProxy: timeproxy.New(), + WNJpnRepository: repository.New( + sqlproxy.New(), + ), + }, + args: args{ + allWords: allNWords, + }, + wantAVWords: []model.Word{}, + wantNWords: allNWords, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + g := New( + tt.fields.OsProxy, + tt.fields.RandProxy, + tt.fields.SqlProxy, + tt.fields.TimeProxy, + tt.fields.WNJpnRepository, + ) + avWords, nWords := g.separateWords(tt.args.allWords) + if !reflect.DeepEqual(avWords, tt.wantAVWords) { + t.Errorf("Generator.separateWords() : got =\n%v, want =\n%v", avWords, tt.wantAVWords) + } + if !reflect.DeepEqual(nWords, tt.wantNWords) { + t.Errorf("Generator.separateWords() : got =\n%v, want =\n%v", nWords, tt.wantNWords) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} diff --git a/app/library/generator/godoc.go b/app/library/generator/godoc.go new file mode 100644 index 00000000..0ae5925d --- /dev/null +++ b/app/library/generator/godoc.go @@ -0,0 +1,2 @@ +// Package generator is a package for generating jrp. +package generator diff --git a/app/library/jrpwriter/godoc.go b/app/library/jrpwriter/godoc.go new file mode 100644 index 00000000..010de5ea --- /dev/null +++ b/app/library/jrpwriter/godoc.go @@ -0,0 +1,2 @@ +// Package jrpwriter is a package for writing jrp as table. +package jrpwriter diff --git a/app/library/jrpwriter/jrpwriter.go b/app/library/jrpwriter/jrpwriter.go new file mode 100644 index 00000000..ce1dc207 --- /dev/null +++ b/app/library/jrpwriter/jrpwriter.go @@ -0,0 +1,129 @@ +package jrpwriter + +import ( + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/tablewriter" +) + +// JrpWritable is an interface for JrpWriter. +type JrpWritable interface { + WriteGenerateResultAsTable(writer ioproxy.WriterInstanceInterface, jrps []model.Jrp) + WriteAsTable(writer ioproxy.WriterInstanceInterface, jrps []model.Jrp) +} + +// JrpWriter is a struct that implements JrpWritable. +type JrpWriter struct { + StrconvProxy strconvproxy.Strconv + TableWriterProxy tablewriterproxy.TableWriter +} + +// New is a constructor for JrpWriter. +func New( + strconvProxy strconvproxy.Strconv, + tableWriterProxy tablewriterproxy.TableWriter, +) *JrpWriter { + return &JrpWriter{ + StrconvProxy: strconvProxy, + TableWriterProxy: tableWriterProxy, + } +} + +// WriteGenerateResultAsTable writes the generate result as table. +func (j *JrpWriter) WriteGenerateResultAsTable(writer ioproxy.WriterInstanceInterface, jrps []model.Jrp) { + if jrps == nil || len(jrps) <= 0 { + return + } + + headers := []string{"phrase", "prefix", "suffix", "created_at"} + rowFunc := func(jrp model.Jrp) []string { + prefix := "" + if jrp.Prefix.FieldNullString.Valid { + prefix = jrp.Prefix.FieldNullString.String + } + suffix := "" + if jrp.Suffix.FieldNullString.Valid { + suffix = jrp.Suffix.FieldNullString.String + } + return []string{ + jrp.Phrase, + prefix, + suffix, + jrp.CreatedAt.Format("2006-01-02 15:04:05"), + } + } + + j.writeTable(writer, jrps, headers, rowFunc) +} + +// WriteAsTable writes the jrps as table. +func (j *JrpWriter) WriteAsTable(writer ioproxy.WriterInstanceInterface, jrps []model.Jrp) { + if jrps == nil || len(jrps) <= 0 { + return + } + + headers := []string{"id", "phrase", "prefix", "suffix", "is_favorited", "created_at", "updated_at"} + rowFunc := func(jrp model.Jrp) []string { + prefix := "" + if jrp.Prefix.FieldNullString.Valid { + prefix = jrp.Prefix.FieldNullString.String + } + suffix := "" + if jrp.Suffix.FieldNullString.Valid { + suffix = jrp.Suffix.FieldNullString.String + } + isFavorited := "" + if jrp.IsFavorited == 1 { + isFavorited = "β—‹" + } + return []string{ + j.StrconvProxy.Itoa(jrp.ID), + jrp.Phrase, + prefix, + suffix, + isFavorited, + jrp.CreatedAt.Format("2006-01-02 15:04:05"), + jrp.UpdatedAt.Format("2006-01-02 15:04:05"), + } + } + + j.writeTable(writer, jrps, headers, rowFunc) +} + +// writeTable writes the table. +func (j *JrpWriter) writeTable(writer ioproxy.WriterInstanceInterface, jrps []model.Jrp, headers []string, rowFunc func(model.Jrp) []string) { + if jrps == nil || len(jrps) <= 0 { + return + } + + var rows [][]string + for _, jrp := range jrps { + rows = append(rows, rowFunc(jrp)) + } + total := j.StrconvProxy.Itoa(len(rows)) + rows = append(rows, make([]string, len(headers))) + rows = append(rows, append([]string{"TOTAL : " + total}, make([]string, len(headers)-1)...)) + + table := j.getDefaultTableWriter(writer) + table.SetHeader(headers) + table.AppendBulk(rows) + table.Render() +} + +// getDefaultTableWriter gets the default table instance. +func (j *JrpWriter) getDefaultTableWriter(o ioproxy.WriterInstanceInterface) tablewriterproxy.TableInstanceInterface { + table := j.TableWriterProxy.NewTable(o) + table.SetAutoWrapText(false) + table.SetAutoFormatHeaders(true) + table.SetHeaderAlignment(tablewriterproxy.ALIGN_LEFT) + table.SetAlignment(tablewriterproxy.ALIGN_LEFT) + table.SetCenterSeparator("") + table.SetColumnSeparator("") + table.SetRowSeparator("") + table.SetHeaderLine(false) + table.SetBorder(false) + table.SetTablePadding("\t") + table.SetNoWhiteSpace(true) + return table +} diff --git a/app/library/jrpwriter/jrpwriter_test.go b/app/library/jrpwriter/jrpwriter_test.go new file mode 100644 index 00000000..d5dd6355 --- /dev/null +++ b/app/library/jrpwriter/jrpwriter_test.go @@ -0,0 +1,471 @@ +package jrpwriter + +import ( + "reflect" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + + "github.com/yanosea/jrp/test/testutility" +) + +func TestNew(t *testing.T) { + strconvProxy := strconvproxy.New() + tableWriterProxy := tablewriterproxy.New() + + type args struct { + strconvProxy strconvproxy.Strconv + tableWriterProxy tablewriterproxy.TableWriter + } + tests := []struct { + name string + args args + want *JrpWriter + }{ + { + name: "positive testing", + args: args{ + strconvProxy: strconvProxy, + tableWriterProxy: tableWriterProxy, + }, + want: &JrpWriter{ + StrconvProxy: strconvProxy, + TableWriterProxy: tableWriterProxy, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.strconvProxy, tt.args.tableWriterProxy); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} + +func TestJrpWriter_WriteGenerateResultAsTable(t *testing.T) { + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osproxy.New(), + ) + jrpWriter := New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil)", + fields: fields{ + t: t, + fnc: func() { + jrpWriter.WriteGenerateResultAsTable(osproxy.Stdout, nil) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty)", + fields: fields{ + t: t, + fnc: func() { + jrpWriter.WriteGenerateResultAsTable(osproxy.Stdout, []model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are one)", + fields: fields{ + t: t, + fnc: func() { + jrps := []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + } + jrpWriter.WriteGenerateResultAsTable(osproxy.Stdout, jrps) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\tPREFIX\tSUFFIX\tCREATED AT\ntest\tprefix\tsuffix\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 1\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are two)", + fields: fields{ + t: t, + fnc: func() { + jrps := []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + } + jrpWriter.WriteGenerateResultAsTable(osproxy.Stdout, jrps) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\tPREFIX\tSUFFIX\tCREATED AT\ntest1\tprefix1\tsuffix1\t9999-12-31 00:00:00\ntest2\tprefix2\tsuffix2\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 2\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("JrpWriter.WriteGenerateResultAsTable() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("JrpWriter.WriteGenerateResultAsTable() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} + +func TestJrpWriter_WriteAsTable(t *testing.T) { + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osproxy.New(), + ) + jrpWriter := New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + sqlProxy := sqlproxy.New() + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil)", + fields: fields{ + t: t, + fnc: func() { + jrpWriter.WriteAsTable(osproxy.Stdout, nil) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty)", + fields: fields{ + t: t, + fnc: func() { + jrpWriter.WriteAsTable(osproxy.Stdout, []model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are one)", + fields: fields{ + t: t, + fnc: func() { + jrps := []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + } + jrpWriter.WriteAsTable(osproxy.Stdout, jrps) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\tprefix\tsuffix\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + }, + { + name: "positive testing (jrps are two)", + fields: fields{ + t: t, + fnc: func() { + jrps := []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 1, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + } + jrpWriter.WriteAsTable(osproxy.Stdout, jrps) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\tprefix1\tsuffix1\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n1\ttest2\tprefix2\tsuffix2\tβ—‹\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 2\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("JrpWriter.WriteJrpAsTable() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("JrpWriter.WriteJrpAsTable() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} + +func TestJrpWriter_writeTable(t *testing.T) { + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osproxy.New(), + ) + jrpWriter := New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + headers := []string{"phrase"} + rowFunc := func(jrp model.Jrp) []string { + return []string{ + jrp.Phrase, + } + } + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil)", + fields: fields{ + t: t, + fnc: func() { + jrpWriter.writeTable(osproxy.Stdout, nil, headers, rowFunc) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, { + name: "positive testing (jrps are empty)", + fields: fields{ + t: t, + fnc: func() { + jrpWriter.writeTable(osproxy.Stdout, []model.Jrp{}, headers, rowFunc) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, { + name: "positive testing (jrps are one)", + fields: fields{ + t: t, + fnc: func() { + jrps := []model.Jrp{ + { + Phrase: "test", + }, + } + jrpWriter.writeTable(osproxy.Stdout, jrps, headers, rowFunc) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\ntest\n\t\nTOTAL : 1\n", + wantStdErr: "", + }, { + name: "positive testing (jrps are two)", + fields: fields{ + t: t, + fnc: func() { + jrps := []model.Jrp{ + { + Phrase: "test1", + }, { + Phrase: "test2", + }, + } + jrpWriter.writeTable(osproxy.Stdout, jrps, headers, rowFunc) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\ntest1\ntest2\n\t\nTOTAL : 2\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("JrpWriter.writeTable() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("JrpWriter.writeTable() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} + +func TestJrpWriter_getDefaultTableWriter(t *testing.T) { + tablewriterProxy := tablewriterproxy.New() + table := tablewriterProxy.NewTable(osproxy.Stdout) + table.SetAutoWrapText(false) + table.SetAutoFormatHeaders(true) + table.SetHeaderAlignment(tablewriterproxy.ALIGN_LEFT) + table.SetAlignment(tablewriterproxy.ALIGN_LEFT) + table.SetCenterSeparator("") + table.SetColumnSeparator("") + table.SetRowSeparator("") + table.SetHeaderLine(false) + table.SetBorder(false) + table.SetTablePadding("\t") + table.SetNoWhiteSpace(true) + + type fields struct { + StrconvProxy strconvproxy.Strconv + TableWriterProxy tablewriterproxy.TableWriter + } + type args struct { + o ioproxy.WriterInstanceInterface + } + tests := []struct { + name string + fields fields + args args + want tablewriterproxy.TableInstanceInterface + }{ + { + name: "positive testing", + fields: fields{ + StrconvProxy: strconvproxy.New(), + TableWriterProxy: tablewriterproxy.New(), + }, + args: args{ + o: osproxy.Stdout, + }, + want: table, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + j := New( + tt.fields.StrconvProxy, + tt.fields.TableWriterProxy, + ) + if got := j.getDefaultTableWriter(tt.args.o); !reflect.DeepEqual(got, tt.want) { + t.Errorf("JrpWriter.getDefaultTableWriter() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} diff --git a/app/library/utility/godoc.go b/app/library/utility/godoc.go new file mode 100644 index 00000000..2319be10 --- /dev/null +++ b/app/library/utility/godoc.go @@ -0,0 +1,2 @@ +// Package utility is a package of collection of utility functions. +package utility diff --git a/app/library/utility/utility.go b/app/library/utility/utility.go new file mode 100644 index 00000000..20e22256 --- /dev/null +++ b/app/library/utility/utility.go @@ -0,0 +1,101 @@ +package utility + +import ( + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" +) + +// UtilityInterface is an interface for Utility. +type UtilityInterface interface { + PrintlnWithWriter(writer ioproxy.WriterInstanceInterface, a ...any) + GetMaxConvertibleString(args []string, def string) string + GetLargerNumber(num int, argNum string) int + CreateDirIfNotExist(dirPath string) error +} + +// Utility is a struct that implements UtilityInterface. +type Utility struct { + FmtProxy fmtproxy.Fmt + OsProxy osproxy.Os + StrconvProxy strconvproxy.Strconv +} + +// New is a constructor for Utility. +func New( + fmtProxy fmtproxy.Fmt, + osProxy osproxy.Os, + strconvProxy strconvproxy.Strconv, +) *Utility { + return &Utility{ + FmtProxy: fmtProxy, + OsProxy: osProxy, + StrconvProxy: strconvProxy, + } +} + +// PrintlnWithWriter prints any with a writer. +func (u *Utility) PrintlnWithWriter(writer ioproxy.WriterInstanceInterface, a ...any) { + u.FmtProxy.Fprintf(writer, u.FmtProxy.Sprintf("%s", a[0])+"\n") +} + +// GetMaxConvertibleString gets the maximum number from args and converts it to a string. +func (u *Utility) GetMaxConvertibleString(args []string, def string) string { + var maxArg string + var maxValue int + initialized := false + + for _, arg := range args { + if convertedArg, err := u.StrconvProxy.Atoi(arg); err == nil { + if !initialized || convertedArg > maxValue { + // if the value is the first one or the value is larger than the max value + maxValue = convertedArg + maxArg = arg + initialized = true + } + } + } + + if initialized { + // if there is less than 1 convertible arg, return the max arg + return maxArg + } + + // if there is no convertible args, return default value + return def +} + +// GetLargerNumber gets the larger number between num and argNum. +func (u *Utility) GetLargerNumber(num int, argNum string) int { + if num <= 0 { + // if num is less than 1, set num to 1 + num = 1 + } + + convertedArgNum, err := u.StrconvProxy.Atoi(argNum) + if err != nil { + // if argNum is not convertible, set argNum to 1 + convertedArgNum = 1 + } + if convertedArgNum <= 0 { + // if argNum is less than 1, set argNum to 1 + convertedArgNum = 1 + } + + // return the larger number + if convertedArgNum > num { + return convertedArgNum + } else { + return num + } +} + +// CreateDirIfNotExist creates a directory if it does not exist. +func (u *Utility) CreateDirIfNotExist(dirPath string) error { + if _, err := u.OsProxy.Stat(dirPath); u.OsProxy.IsNotExist(err) { + // if not exist, create dir + return u.OsProxy.MkdirAll(dirPath, u.OsProxy.FileMode(0755)) + } + return nil +} diff --git a/app/library/utility/utility_test.go b/app/library/utility/utility_test.go new file mode 100644 index 00000000..6feec887 --- /dev/null +++ b/app/library/utility/utility_test.go @@ -0,0 +1,474 @@ +package utility + +import ( + "reflect" + "testing" + + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" + + "github.com/yanosea/jrp/test/testutility" +) + +func TestNew(t *testing.T) { + fmtProxy := fmtproxy.New() + osProxy := osproxy.New() + strconvProxy := strconvproxy.New() + + type args struct { + fmtProxy fmtproxy.Fmt + osProxy osproxy.Os + strconvProxy strconvproxy.Strconv + } + tests := []struct { + name string + args args + want *Utility + }{ + { + name: "positive testing", + args: args{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + want: &Utility{ + FmtProxy: fmtProxy, + OsProxy: osProxy, + StrconvProxy: strconvProxy, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.fmtProxy, tt.args.osProxy, tt.args.strconvProxy); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} + +func TestUtility_PrintlnWithWriter(t *testing.T) { + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osproxy.New(), + ) + util := New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (stdout)", + fields: fields{ + t: t, + fnc: func() { + util.PrintlnWithWriter(osproxy.Stdout, "stdout") + }, + capturer: capturer, + }, + wantStdOut: "stdout\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (stderr)", + fields: fields{ + t: t, + fnc: func() { + util.PrintlnWithWriter(osproxy.Stderr, "stderr") + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "stderr\n", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotStdOut, gotStdErr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if gotStdOut != tt.wantStdOut { + t.Errorf("Utility.PrintlnWithWriter() : gotStdOut =\n%v, want =\n%v", gotStdOut, tt.wantStdOut) + } + if gotStdErr != tt.wantStdErr { + t.Errorf("Utility.PrintlnWithWriter() : gotStdErr =\n%v, want =\n%v", gotStdErr, tt.wantStdErr) + } + }) + } +} + +func TestUtility_GetMaxConvertibleString(t *testing.T) { + fmtProxy := fmtproxy.New() + osProxy := osproxy.New() + strconvProxy := strconvproxy.New() + + type fields struct { + fmtProxy fmtproxy.Fmt + osProxy osproxy.Os + strconvProxy strconvproxy.Strconv + } + type args struct { + args []string + def string + } + tests := []struct { + name string + fields fields + args args + want string + }{ + { + name: "positive testing (args have convertible string, it is first)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + args: []string{"2", "test", "3"}, + def: "1", + }, + want: "3", + }, + { + name: "positive testing (args have convertible string, it is second)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + args: []string{"test", "4", "5"}, + def: "1", + }, + want: "5", + }, + { + name: "positive testing (args have convertible string, it is last)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + args: []string{"test", "test", "6"}, + def: "1", + }, + want: "6", + }, + { + name: "positive testing (args have no convertible string)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + args: []string{"test", "test", "test"}, + def: "1", + }, + want: "1", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + u := New( + tt.fields.fmtProxy, + tt.fields.osProxy, + tt.fields.strconvProxy, + ) + if got := u.GetMaxConvertibleString(tt.args.args, tt.args.def); got != tt.want { + t.Errorf("Utility.GetMaxConvertibleString() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} + +func TestUtility_GetLargerNumber(t *testing.T) { + fmtProxy := fmtproxy.New() + osProxy := osproxy.New() + strconvProxy := strconvproxy.New() + + type fields struct { + fmtProxy fmtproxy.Fmt + osProxy osproxy.Os + strconvProxy strconvproxy.Strconv + } + type args struct { + num int + argNum string + } + tests := []struct { + name string + fields fields + args args + want int + }{ + { + name: "positive testing (num is -1, argNum is empty)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: -1, + argNum: "", + }, + want: 1, + }, + { + name: "positive testing (num is 0, argNum is empty)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 0, + argNum: "", + }, + want: 1, + }, + { + name: "positive testing (num is 1, argNum is empty)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 1, + argNum: "", + }, + want: 1, + }, + { + name: "positive testing (num is 2, argsNum is empty)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 2, + argNum: "", + }, + want: 2, + }, + { + name: "positive testing (num is 0, argNum is -1)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 0, + argNum: "-1", + }, + want: 1, + }, + { + name: "positive testing (num is 0, argNum is 0)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 0, + argNum: "0", + }, + want: 1, + }, + { + name: "positive testing (num is 0, argNum can't be converted to int)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 0, + argNum: "test", + }, + want: 1, + }, + { + name: "positive testing (num is 0, argNum is 1)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 0, + argNum: "1", + }, + want: 1, + }, + { + name: "positive testing (num is 0, argNum is 2)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 0, + argNum: "2", + }, + want: 2, + }, + { + name: "positive testing (num is 3, argNum is 2)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 3, + argNum: "2", + }, + want: 3, + }, + { + name: "positive testing (num is 2, argNum is 3)", + fields: fields{ + fmtProxy: fmtProxy, + osProxy: osProxy, + strconvProxy: strconvProxy, + }, + args: args{ + num: 2, + argNum: "3", + }, + want: 3, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + u := New( + tt.fields.fmtProxy, + tt.fields.osProxy, + tt.fields.strconvProxy, + ) + if got := u.GetLargerNumber(tt.args.num, tt.args.argNum); got != tt.want { + t.Errorf("Utility.GetLargerNumber() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} + +func TestUtility_CreateDirIfNotExist(t *testing.T) { + filepathProxy := filepathproxy.New() + osProxy := osproxy.New() + testDirPath := filepathProxy.Join(osProxy.TempDir(), "jrp_test") + + type fields struct { + fmtProxy fmtproxy.Fmt + osProxy osproxy.Os + strconvProxy strconvproxy.Strconv + } + type args struct { + dirPath string + } + tests := []struct { + name string + fields fields + args args + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (dirPath does not exists)", + fields: fields{ + fmtProxy: fmtproxy.New(), + osProxy: osproxy.New(), + strconvProxy: strconvproxy.New(), + }, + args: args{ + dirPath: testDirPath, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(testDirPath); err != nil { + t.Errorf("OsProxy.RemoveAll(%v) : error =\n%v", testDirPath, err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(testDirPath); err != nil { + t.Errorf("OsProxy.RemoveAll(%v) : error =\n%v", testDirPath, err) + } + }, + }, + { + name: "positive testing (dirPath exists)", + fields: fields{ + fmtProxy: fmtproxy.New(), + osProxy: osproxy.New(), + strconvProxy: strconvproxy.New(), + }, + args: args{ + dirPath: testDirPath, + }, + wantErr: false, + setup: func() { + if err := osProxy.MkdirAll(testDirPath, osProxy.FileMode(0755)); err != nil { + t.Errorf("OsProxy.MkdirAll(%v) : error =\n%v", testDirPath, err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(testDirPath); err != nil { + t.Errorf("OsProxy.RemoveAll(%v) : error =\n%v", testDirPath, err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + u := New( + tt.fields.fmtProxy, + tt.fields.osProxy, + tt.fields.strconvProxy, + ) + if tt.setup != nil { + tt.setup() + } + if err := u.CreateDirIfNotExist(tt.args.dirPath); (err != nil) != tt.wantErr { + t.Errorf("Utility.CreateDirIfNotExist() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} diff --git a/app/library/versionprovider/godoc.go b/app/library/versionprovider/godoc.go new file mode 100644 index 00000000..7de4cf3f --- /dev/null +++ b/app/library/versionprovider/godoc.go @@ -0,0 +1,2 @@ +// Package versionprovider is a package for providing version from build info. +package versionprovider diff --git a/app/library/versionprovider/versionprovider.go b/app/library/versionprovider/versionprovider.go new file mode 100644 index 00000000..b74a927f --- /dev/null +++ b/app/library/versionprovider/versionprovider.go @@ -0,0 +1,43 @@ +package versionprovider + +import ( + "github.com/yanosea/jrp/app/proxy/debug" +) + +// VersionProvidable is an interface for VersionProvider. +type VersionProvidable interface { + GetVersion(emmbeddedVersion string) string +} + +// VersionProvider is a struct that implements VersionProvidable. +type VersionProvider struct { + DebugProxy debugproxy.Debug +} + +// New is a constructor of VersionProvider. +func New(debugProxy debugproxy.Debug) *VersionProvider { + return &VersionProvider{ + DebugProxy: debugProxy, + } +} + +// GetVersion gets the version from build info if version is not embedded. +func (v *VersionProvider) GetVersion(embeddedVersion string) string { + // if version is embedded, return it + if embeddedVersion != "" { + return embeddedVersion + } + + i, ok := v.DebugProxy.ReadBuildInfo() + if !ok { + // if reading build info fails, return unknown + return "unknown" + } + if i.FieldBuildInfo.Main.Version == "" || i.FieldBuildInfo.Main.Version == "(devel)" { + // if version from build info is empty, return dev + return "devel" + } + + // return version from build info + return i.FieldBuildInfo.Main.Version +} diff --git a/app/library/versionprovider/versionprovider_test.go b/app/library/versionprovider/versionprovider_test.go new file mode 100644 index 00000000..0feaa13a --- /dev/null +++ b/app/library/versionprovider/versionprovider_test.go @@ -0,0 +1,166 @@ +package versionprovider + +import ( + "reflect" + "runtime/debug" + "testing" + + "github.com/yanosea/jrp/app/proxy/debug" + + "github.com/yanosea/jrp/mock/app/proxy/debug" + "go.uber.org/mock/gomock" +) + +func TestNew(t *testing.T) { + debugProxy := debugproxy.New() + + type args struct { + debugProxy debugproxy.Debug + } + tests := []struct { + name string + args args + want *VersionProvider + }{ + { + name: "positive testing", + args: args{ + debugProxy: debugProxy, + }, + want: &VersionProvider{ + DebugProxy: debugProxy, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.debugProxy); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} + +func TestVersionProvider_GetVersion(t *testing.T) { + debugProxy := debugproxy.New() + + type fields struct { + versionProvider *VersionProvider + } + type args struct { + embeddedVersion string + } + tests := []struct { + name string + fields fields + args args + want string + setup func(mockCtrl *gomock.Controller, tt *fields) + }{ + { + name: "positive testing (version is embedded)", + fields: fields{versionProvider: nil}, + args: args{ + embeddedVersion: "vx.x.x", + }, + want: "vx.x.x", + setup: func(_ *gomock.Controller, tt *fields) { + versionProvider := New(debugProxy) + tt.versionProvider = versionProvider + }, + }, + { + name: "positive testing (version is not embedded and DebugProxy.ReadBuildInfo() returns not ok)", + fields: fields{versionProvider: nil}, + args: args{ + embeddedVersion: "", + }, + want: "unknown", + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockDebugProxy := mockdebugproxy.NewMockDebug(mockCtrl) + buildInfo, ok := debugProxy.ReadBuildInfo() + if !ok { + t.Errorf("DebugProxy.ReadBuildInfo() failed") + } + mockDebugProxy.EXPECT().ReadBuildInfo().Return(buildInfo, false) + versionProvider := New(mockDebugProxy) + tt.versionProvider = versionProvider + }, + }, + { + name: "positive testing (version is not embedded and DebugProxy.ReadBuildInfo() returns ok, but version is empty)", + fields: fields{versionProvider: nil}, + args: args{ + embeddedVersion: "", + }, + want: "devel", + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockDebugProxy := mockdebugproxy.NewMockDebug(mockCtrl) + mockBuildInfo := &debugproxy.BuildInfoInstance{ + FieldBuildInfo: &debug.BuildInfo{ + Main: debug.Module{ + Version: "", + }, + }, + } + mockDebugProxy.EXPECT().ReadBuildInfo().Return(mockBuildInfo, true) + versionProvider := New(mockDebugProxy) + tt.versionProvider = versionProvider + }, + }, + { + name: "positive testing (version is not embedded and DebugProxy.ReadBuildInfo() returns ok, but version is (devel))", + fields: fields{versionProvider: nil}, + args: args{ + embeddedVersion: "", + }, + want: "devel", + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockDebugProxy := mockdebugproxy.NewMockDebug(mockCtrl) + mockBuildInfo := &debugproxy.BuildInfoInstance{ + FieldBuildInfo: &debug.BuildInfo{ + Main: debug.Module{ + Version: "(devel)", + }, + }, + } + mockDebugProxy.EXPECT().ReadBuildInfo().Return(mockBuildInfo, true) + versionProvider := New(mockDebugProxy) + tt.versionProvider = versionProvider + }, + }, + { + name: "positive testing (version is not embedded and DebugProxy.ReadBuildInfo() returns ok, version is not empty, and version is not (devel))", + fields: fields{versionProvider: nil}, + args: args{ + embeddedVersion: "", + }, + want: "vy.y.y", + setup: func(mockCtrl *gomock.Controller, tt *fields) { + mockDebugProxy := mockdebugproxy.NewMockDebug(mockCtrl) + mockBuildInfo := &debugproxy.BuildInfoInstance{ + FieldBuildInfo: &debug.BuildInfo{ + Main: debug.Module{ + Version: "vy.y.y", + }, + }, + } + mockDebugProxy.EXPECT().ReadBuildInfo().Return(mockBuildInfo, true) + versionProvider := New(mockDebugProxy) + tt.versionProvider = versionProvider + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + if got := tt.fields.versionProvider.GetVersion(tt.args.embeddedVersion); got != tt.want { + t.Errorf("VersionProvider.GetVersion() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} diff --git a/app/proxy/buffer/bufferproxy.go b/app/proxy/buffer/bufferproxy.go new file mode 100644 index 00000000..770f361c --- /dev/null +++ b/app/proxy/buffer/bufferproxy.go @@ -0,0 +1,45 @@ +package bufferproxy + +import ( + "bytes" + + "github.com/yanosea/jrp/app/proxy/io" +) + +// Buffer is an interface for buffer. +type Buffer interface { + ReadFrom(r ioproxy.ReaderInstanceInterface) (int64, error) + Reset() + String() string + Write(p []byte) (n int, err error) +} + +// BufferProxy is a struct that implements Buffer. +type BufferProxy struct { + bytes.Buffer +} + +// New is a constructor for BufferProxy. +func New() Buffer { + return &BufferProxy{} +} + +// ReadFrom is a proxy for buffer.ReadFrom. +func (b *BufferProxy) ReadFrom(r ioproxy.ReaderInstanceInterface) (int64, error) { + return b.Buffer.ReadFrom(r) +} + +// Reset is a proxy for buffer.Reset. +func (b *BufferProxy) Reset() { + b.Buffer.Reset() +} + +// String is a proxy for buffer.String. +func (b *BufferProxy) String() string { + return b.Buffer.String() +} + +// Write is a proxy for buffer.Write. +func (b *BufferProxy) Write(p []byte) (n int, err error) { + return b.Buffer.Write(p) +} diff --git a/app/proxy/buffer/godoc.go b/app/proxy/buffer/godoc.go new file mode 100644 index 00000000..f5fe67fb --- /dev/null +++ b/app/proxy/buffer/godoc.go @@ -0,0 +1,2 @@ +// Package bufferproxy is a package that provides a buffer proxy for the app. +package bufferproxy diff --git a/app/proxy/cobra/cobraproxy.go b/app/proxy/cobra/cobraproxy.go new file mode 100644 index 00000000..59024470 --- /dev/null +++ b/app/proxy/cobra/cobraproxy.go @@ -0,0 +1,29 @@ +package cobraproxy + +import ( + "github.com/spf13/cobra" +) + +// Cobra is an interface for cobra. +type Cobra interface { + MaximumNArgs(int) *PositionalArgsInstance + NewCommand() *CommandInstance +} + +// CobraProxy is a struct that implements Cobra. +type CobraProxy struct{} + +// New is a constructor for CommandProxy. +func New() Cobra { + return &CobraProxy{} +} + +// MaximumNArgs is a proxy for cobra.MaximumNArgs. +func (*CobraProxy) MaximumNArgs(n int) *PositionalArgsInstance { + return &PositionalArgsInstance{FieldPositionalArgs: cobra.MaximumNArgs(n)} +} + +// NewCommand is a proxy for getting cobra.Command struct. +func (*CobraProxy) NewCommand() *CommandInstance { + return &CommandInstance{FieldCommand: &cobra.Command{}} +} diff --git a/app/proxy/cobra/commandinstance.go b/app/proxy/cobra/commandinstance.go new file mode 100644 index 00000000..795ef920 --- /dev/null +++ b/app/proxy/cobra/commandinstance.go @@ -0,0 +1,68 @@ +package cobraproxy + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/pflag" +) + +// CommandInstanceInterface is an interface for cobra.Command. +type CommandInstanceInterface interface { + AddCommand(cmds ...*CommandInstance) + Execute() error + GetCommand() *cobra.Command + PersistentFlags() *pflagproxy.FlagSetInstance + SetArgs(args []string) + SetErr(io ioproxy.WriterInstanceInterface) + SetHelpTemplate(s string) + SetOut(io ioproxy.WriterInstanceInterface) +} + +// CommandInstance is a struct that implements CommandInstanceInterface. +type CommandInstance struct { + FieldCommand *cobra.Command +} + +// AddCommand is a proxy for cobra.Command.AddCommand. +func (c *CommandInstance) AddCommand(cmds ...*CommandInstance) { + for _, cmd := range cmds { + c.FieldCommand.AddCommand(cmd.FieldCommand) + } +} + +// Execute is a proxy for cobra.Command.Execute. +func (c *CommandInstance) Execute() error { + return c.FieldCommand.Execute() +} + +// GetCommand returns the cobra.Command. +func (c *CommandInstance) GetCommand() *cobra.Command { + return c.FieldCommand +} + +// PersistentFlags is a proxy for cobra.Command.PersistentFlags. +func (c *CommandInstance) PersistentFlags() *pflagproxy.FlagSetInstance { + return &pflagproxy.FlagSetInstance{FieldFlagSet: c.FieldCommand.PersistentFlags()} +} + +// SetArgs is a proxy for cobra.Command.SetArgs. +func (c *CommandInstance) SetArgs(args []string) { + c.FieldCommand.SetArgs(args) +} + +// SetErr is a proxy for cobra.Command.SetErr. +func (c *CommandInstance) SetErr(io ioproxy.WriterInstanceInterface) { + c.FieldCommand.SetErr(io) +} + +// SetHelpTemplate is a proxy for cobra.Command.SetHelpTemplate. +func (c *CommandInstance) SetHelpTemplate(s string) { + c.FieldCommand.SetHelpTemplate(s) +} + +// SetOut is a proxy for cobra.Command.SetOut. + +func (c *CommandInstance) SetOut(io ioproxy.WriterInstanceInterface) { + c.FieldCommand.SetOut(io) +} diff --git a/app/proxy/cobra/godoc.go b/app/proxy/cobra/godoc.go new file mode 100644 index 00000000..729a0595 --- /dev/null +++ b/app/proxy/cobra/godoc.go @@ -0,0 +1,2 @@ +// Package cobraproxy is a package that provides a cobra proxy for the app. +package cobraproxy diff --git a/app/proxy/cobra/positionalargsinstance.go b/app/proxy/cobra/positionalargsinstance.go new file mode 100644 index 00000000..3f07e013 --- /dev/null +++ b/app/proxy/cobra/positionalargsinstance.go @@ -0,0 +1,14 @@ +package cobraproxy + +import ( + "github.com/spf13/cobra" +) + +// PositionalArgsInstanceInterface is an interface for cobra.PositionalArgs. +type PositionalArgsInstanceInterface interface { +} + +// PositionalArgsInstance is a struct that implements PositionalArgsInstanceInterface. +type PositionalArgsInstance struct { + FieldPositionalArgs cobra.PositionalArgs +} diff --git a/app/proxy/color/colorproxy.go b/app/proxy/color/colorproxy.go new file mode 100644 index 00000000..018da330 --- /dev/null +++ b/app/proxy/color/colorproxy.go @@ -0,0 +1,35 @@ +package colorproxy + +import ( + "github.com/fatih/color" +) + +// Color is an interface for color. +type Color interface { + GreenString(format string, a ...interface{}) string + RedString(format string, a ...interface{}) string + YellowString(format string, a ...interface{}) string +} + +// ColorProxy is a struct that implements Color. +type ColorProxy struct{} + +// New is a constructor for BufferProxy. +func New() Color { + return &ColorProxy{} +} + +// GreenString is a proxy for color.GreenString. +func (*ColorProxy) GreenString(format string, a ...interface{}) string { + return color.GreenString(format, a...) +} + +// RedString is a proxy for color.RedString. +func (*ColorProxy) RedString(format string, a ...interface{}) string { + return color.RedString(format, a...) +} + +// YellowString is a proxy for color.YellowString. +func (*ColorProxy) YellowString(format string, a ...interface{}) string { + return color.YellowString(format, a...) +} diff --git a/app/proxy/color/godoc.go b/app/proxy/color/godoc.go new file mode 100644 index 00000000..93fadaf7 --- /dev/null +++ b/app/proxy/color/godoc.go @@ -0,0 +1,2 @@ +// Package colorproxy is a package that provides a color proxy for the app. +package colorproxy diff --git a/app/proxy/debug/buildinfoinstance.go b/app/proxy/debug/buildinfoinstance.go new file mode 100644 index 00000000..2fe50a09 --- /dev/null +++ b/app/proxy/debug/buildinfoinstance.go @@ -0,0 +1,13 @@ +package debugproxy + +import ( + "runtime/debug" +) + +// BuildInfoInstanceInterface is an interface for debug.BuildInfo. +type BuildInfoInstanceInterface interface{} + +// BuildInfoInstance is a struct that implements BuildInfoInstanceInterface. +type BuildInfoInstance struct { + FieldBuildInfo *debug.BuildInfo +} diff --git a/app/proxy/debug/debugproxy.go b/app/proxy/debug/debugproxy.go new file mode 100644 index 00000000..7d42279a --- /dev/null +++ b/app/proxy/debug/debugproxy.go @@ -0,0 +1,24 @@ +package debugproxy + +import ( + "runtime/debug" +) + +// Debug is an interface for debug. +type Debug interface { + ReadBuildInfo() (*BuildInfoInstance, bool) +} + +// DebugProxy is a struct that implements Debug. +type DebugProxy struct{} + +// New is a constructor for DebugProxy. +func New() Debug { + return &DebugProxy{} +} + +// ReadBuildInfo is a proxy for debug.ReadBuildInfo. +func (*DebugProxy) ReadBuildInfo() (*BuildInfoInstance, bool) { + buildInfo, ok := debug.ReadBuildInfo() + return &BuildInfoInstance{FieldBuildInfo: buildInfo}, ok +} diff --git a/app/proxy/debug/godoc.go b/app/proxy/debug/godoc.go new file mode 100644 index 00000000..193da42e --- /dev/null +++ b/app/proxy/debug/godoc.go @@ -0,0 +1,2 @@ +// Package debugproxy is a package that provides a debug proxy for the app. +package debugproxy diff --git a/app/proxy/filepath/filepathproxy.go b/app/proxy/filepath/filepathproxy.go new file mode 100644 index 00000000..96fad8dd --- /dev/null +++ b/app/proxy/filepath/filepathproxy.go @@ -0,0 +1,23 @@ +package filepathproxy + +import ( + "path/filepath" +) + +// FilePath is an interface for filepath. +type FilePath interface { + Join(elem ...string) string +} + +// FilePathProxy is a struct that implements FilePath. +type FilePathProxy struct{} + +// New is a constructor for FilepathProxy +func New() FilePath { + return &FilePathProxy{} +} + +// Join is a proxy for filepath.Join. +func (*FilePathProxy) Join(elem ...string) string { + return filepath.Join(elem...) +} diff --git a/app/proxy/filepath/godoc.go b/app/proxy/filepath/godoc.go new file mode 100644 index 00000000..95272675 --- /dev/null +++ b/app/proxy/filepath/godoc.go @@ -0,0 +1,2 @@ +// Package filepathproxy is a package that provides a filepath proxy for the app. +package filepathproxy diff --git a/app/proxy/fmt/fmtproxy.go b/app/proxy/fmt/fmtproxy.go new file mode 100644 index 00000000..36d4908b --- /dev/null +++ b/app/proxy/fmt/fmtproxy.go @@ -0,0 +1,31 @@ +package fmtproxy + +import ( + "fmt" + + "github.com/yanosea/jrp/app/proxy/io" +) + +// Fmt is an interface for fmt. +type Fmt interface { + Fprintf(w ioproxy.WriterInstanceInterface, format string, a ...any) + Sprintf(format string, a ...any) string +} + +// FmtProxy is a struct that implements Fmt. +type FmtProxy struct{} + +// New is a constructor for FmtProxy. +func New() Fmt { + return &FmtProxy{} +} + +// Fprintf is a proxy for fmt.Fprintf. +func (*FmtProxy) Fprintf(w ioproxy.WriterInstanceInterface, format string, a ...any) { + fmt.Fprintf(w, format, a...) +} + +// Sprintf is a proxy for fmt.Sprintf. +func (*FmtProxy) Sprintf(format string, a ...any) string { + return fmt.Sprintf(format, a...) +} diff --git a/app/proxy/fmt/godoc.go b/app/proxy/fmt/godoc.go new file mode 100644 index 00000000..04106997 --- /dev/null +++ b/app/proxy/fmt/godoc.go @@ -0,0 +1,2 @@ +// Package fmtproxy is a package that provides a fmt proxy for the app. +package fmtproxy diff --git a/app/proxy/fs/fileinfoinstance.go b/app/proxy/fs/fileinfoinstance.go new file mode 100644 index 00000000..443f3d98 --- /dev/null +++ b/app/proxy/fs/fileinfoinstance.go @@ -0,0 +1,16 @@ +package fsproxy + +import ( + "io/fs" +) + +// FileInfoInstanceInterface is an interface for fs.FileInfo. +type FileInfoInstanceInterface interface{} + +// FileInfoInstance is a struct that implements FileInfoInstanceInterface. +type FileInfoInstance struct { + FieldFileInfo fs.FileInfo +} + +// FileMode is for fs.FileMode. +type FileMode fs.FileMode diff --git a/app/proxy/fs/fsproxy.go b/app/proxy/fs/fsproxy.go new file mode 100644 index 00000000..3c68e9d8 --- /dev/null +++ b/app/proxy/fs/fsproxy.go @@ -0,0 +1,7 @@ +package fsproxy + +// Fs is an interface for fs. +type Fs interface{} + +// FsProxy is a struct that implements Fs. +type FsProxy struct{} diff --git a/app/proxy/fs/godoc.go b/app/proxy/fs/godoc.go new file mode 100644 index 00000000..a5677edd --- /dev/null +++ b/app/proxy/fs/godoc.go @@ -0,0 +1,2 @@ +// Package fsproxy is a package that provides a fs proxy for the app. +package fsproxy diff --git a/app/proxy/gzip/godoc.go b/app/proxy/gzip/godoc.go new file mode 100644 index 00000000..3a822d7b --- /dev/null +++ b/app/proxy/gzip/godoc.go @@ -0,0 +1,2 @@ +// Package gzipproxy is a package that provides a gzip proxy for the app. +package gzipproxy diff --git a/app/proxy/gzip/gzipproxy.go b/app/proxy/gzip/gzipproxy.go new file mode 100644 index 00000000..b5733dde --- /dev/null +++ b/app/proxy/gzip/gzipproxy.go @@ -0,0 +1,25 @@ +package gzipproxy + +import ( + "compress/gzip" + + "github.com/yanosea/jrp/app/proxy/io" +) + +// Gzip is an interface for gzip. +type Gzip interface { + NewReader(r ioproxy.ReaderInstanceInterface) (ReaderInstanceInterface, error) +} + +// GzipProxy is a struct that implements Gzip. +type GzipProxy struct{} + +// New is a constructor for GzipProxy. +func New() Gzip { + return &GzipProxy{} +} + +// NewReader is a proxy for gzip.NewReader. +func (*GzipProxy) NewReader(r ioproxy.ReaderInstanceInterface) (ReaderInstanceInterface, error) { + return gzip.NewReader(r) +} diff --git a/app/proxy/gzip/readerinstance.go b/app/proxy/gzip/readerinstance.go new file mode 100644 index 00000000..4d568b58 --- /dev/null +++ b/app/proxy/gzip/readerinstance.go @@ -0,0 +1,17 @@ +package gzipproxy + +import ( + "compress/gzip" + "github.com/yanosea/jrp/app/proxy/io" +) + +// ReaderInstanceInterface is an interface for gzip.Reader. +type ReaderInstanceInterface interface { + ioproxy.ReaderInstanceInterface + Close() error +} + +// ReaderInstance is a struct that implements ReaderInstanceInterface. +type ReaderInstance struct { + FieldReader gzip.Reader +} diff --git a/app/proxy/http/godoc.go b/app/proxy/http/godoc.go new file mode 100644 index 00000000..3b1196b4 --- /dev/null +++ b/app/proxy/http/godoc.go @@ -0,0 +1,2 @@ +// Package httpproxy is a package that provides a http proxy for the app. +package httpproxy diff --git a/app/proxy/http/httpproxy.go b/app/proxy/http/httpproxy.go new file mode 100644 index 00000000..16f35d86 --- /dev/null +++ b/app/proxy/http/httpproxy.go @@ -0,0 +1,24 @@ +package httpproxy + +import ( + "net/http" +) + +// Http is an interface for http. +type Http interface { + Get(url string) (*ResponseInstance, error) +} + +// HttpProxy is a struct that implements Http. +type HttpProxy struct{} + +// New is a constructor for HttpProxy. +func New() Http { + return &HttpProxy{} +} + +// Get is a proxy for http.Get. +func (*HttpProxy) Get(url string) (*ResponseInstance, error) { + resp, err := http.Get(url) + return &ResponseInstance{FieldResponse: resp}, err +} diff --git a/app/proxy/http/responseinstance.go b/app/proxy/http/responseinstance.go new file mode 100644 index 00000000..6f6a5e93 --- /dev/null +++ b/app/proxy/http/responseinstance.go @@ -0,0 +1,13 @@ +package httpproxy + +import ( + "net/http" +) + +// ResponseInstanceInterface is an interface for http.Response. +type ResponseInstanceInterface interface{} + +// ResponseInstance is a struct that implements ResponseInstanceInterface. +type ResponseInstance struct { + FieldResponse *http.Response +} diff --git a/app/proxy/io/constant.go b/app/proxy/io/constant.go new file mode 100644 index 00000000..bc7668a4 --- /dev/null +++ b/app/proxy/io/constant.go @@ -0,0 +1,10 @@ +package ioproxy + +import ( + "io" +) + +const ( + // SeekStart is a const for io.SeekStart. + SeekStart = io.SeekStart +) diff --git a/app/proxy/io/godoc.go b/app/proxy/io/godoc.go new file mode 100644 index 00000000..02de4adb --- /dev/null +++ b/app/proxy/io/godoc.go @@ -0,0 +1,2 @@ +// Package ioproxy is a package that provides an io proxy for the app. +package ioproxy diff --git a/app/proxy/io/ioproxy.go b/app/proxy/io/ioproxy.go new file mode 100644 index 00000000..7d8f9944 --- /dev/null +++ b/app/proxy/io/ioproxy.go @@ -0,0 +1,23 @@ +package ioproxy + +import ( + "io" +) + +// Io is an interface for io. +type Io interface { + Copy(dst WriterInstanceInterface, src ReaderInstanceInterface) (int64, error) +} + +// IoProxy is a struct that implements Io. +type IoProxy struct{} + +// New is a constructor for IoProxy. +func New() Io { + return &IoProxy{} +} + +// Copy is a proxy for io.Copy. +func (*IoProxy) Copy(dst WriterInstanceInterface, src ReaderInstanceInterface) (int64, error) { + return io.Copy(dst, src) +} diff --git a/app/proxy/io/readerinstance.go b/app/proxy/io/readerinstance.go new file mode 100644 index 00000000..4182494a --- /dev/null +++ b/app/proxy/io/readerinstance.go @@ -0,0 +1,13 @@ +package ioproxy + +import ( + "io" +) + +// ReaderInstanceInterface is an interface for io.Reader. +type ReaderInstanceInterface interface { + io.Reader +} + +// ReaderInstance is a struct that implements ReaderInstanceInterface. +type ReaderInstance struct{} diff --git a/app/proxy/io/writerinstance.go b/app/proxy/io/writerinstance.go new file mode 100644 index 00000000..8ed1a4fc --- /dev/null +++ b/app/proxy/io/writerinstance.go @@ -0,0 +1,13 @@ +package ioproxy + +import ( + "io" +) + +// WriterInstanceInterface is an interface for io.Writer. +type WriterInstanceInterface interface { + io.Writer +} + +// WriterInstance is a struct that implements WriterInterface. +type WriterInstance struct{} diff --git a/app/proxy/os/constant.go b/app/proxy/os/constant.go new file mode 100644 index 00000000..f314d5b4 --- /dev/null +++ b/app/proxy/os/constant.go @@ -0,0 +1,14 @@ +package osproxy + +import ( + "os" +) + +// Args is a variable for os.Args. +var Args = os.Args + +// Stderr is a variable for os.Stderr. +var Stderr = os.Stderr + +// Stdout is a variable for os.Stdout. +var Stdout = os.Stdout diff --git a/app/proxy/os/fileinstance.go b/app/proxy/os/fileinstance.go new file mode 100644 index 00000000..cdcce482 --- /dev/null +++ b/app/proxy/os/fileinstance.go @@ -0,0 +1,38 @@ +package osproxy + +import ( + "os" +) + +// FileInstanceInterface is an interface for os.File. +type FileInstanceInterface interface { + Close() error + Read(p []byte) (n int, err error) + Seek(offset int64, whence int) (int64, error) + Write(b []byte) (n int, err error) +} + +// FileInstance is a struct that implements FileInstanceInterface. +type FileInstance struct { + FieldFile *os.File +} + +// Close is a proxy for os.File.Close(). +func (f *FileInstance) Close() error { + return f.FieldFile.Close() +} + +// Read is a proxy for os.File.Read(). +func (f *FileInstance) Read(p []byte) (n int, err error) { + return f.FieldFile.Read(p) +} + +// Seek is a proxy for os.File.Seek(). +func (f *FileInstance) Seek(offset int64, whence int) (int64, error) { + return f.FieldFile.Seek(offset, whence) +} + +// Write is a proxy for os.File.Write(). +func (f *FileInstance) Write(b []byte) (n int, err error) { + return f.FieldFile.Write(b) +} diff --git a/app/proxy/os/godoc.go b/app/proxy/os/godoc.go new file mode 100644 index 00000000..fbf52d11 --- /dev/null +++ b/app/proxy/os/godoc.go @@ -0,0 +1,2 @@ +// Package osproxy is a package that provides an os proxy for the app. +package osproxy diff --git a/app/proxy/os/osproxy.go b/app/proxy/os/osproxy.go new file mode 100644 index 00000000..a5d3e591 --- /dev/null +++ b/app/proxy/os/osproxy.go @@ -0,0 +1,89 @@ +package osproxy + +import ( + "os" + + "github.com/yanosea/jrp/app/proxy/fs" +) + +// Os is an interface for os. +type Os interface { + Create(name string) (FileInstanceInterface, error) + FileMode(perm fsproxy.FileMode) fsproxy.FileMode + Getenv(key string) string + IsNotExist(err error) bool + MkdirAll(path string, perm fsproxy.FileMode) error + Open(name string) (*FileInstance, error) + Pipe() (*FileInstance, *FileInstance, error) + Remove(name string) error + RemoveAll(path string) error + Stat(name string) (*fsproxy.FileInfoInstance, error) + TempDir() string +} + +// OsProxy is a struct that implements Os. +type OsProxy struct{} + +// New is a constructor for OsProxy. +func New() Os { + return &OsProxy{} +} + +// Create is a proxy for os.Create. +func (*OsProxy) Create(name string) (FileInstanceInterface, error) { + file, _ := os.Create(name) + return &FileInstance{FieldFile: file}, nil +} + +// Filemode is a proxy for os.FileMode. +func (*OsProxy) FileMode(perm fsproxy.FileMode) fsproxy.FileMode { + return perm +} + +// Getenv is a proxy for os.Getenv. +func (*OsProxy) Getenv(key string) string { + return os.Getenv(key) +} + +// IsNotExist is a proxy for os.IsNotExist. +func (*OsProxy) IsNotExist(err error) bool { + return os.IsNotExist(err) +} + +// MkdirAll is a proxy for os.MkdirAll. +func (*OsProxy) MkdirAll(path string, perm fsproxy.FileMode) error { + return os.MkdirAll(path, os.FileMode(perm)) +} + +// Open is a proxy for os.Open. +func (*OsProxy) Open(name string) (*FileInstance, error) { + file, _ := os.Open(name) + return &FileInstance{FieldFile: file}, nil +} + +// Pipe is a proxy for os.Pipe. +func (*OsProxy) Pipe() (*FileInstance, *FileInstance, error) { + r, w, err := os.Pipe() + return &FileInstance{FieldFile: r}, &FileInstance{FieldFile: w}, err +} + +// Remove is a proxy for os.Remove. +func (*OsProxy) Remove(name string) error { + return os.Remove(name) +} + +// RemoveAll is a proxy for os.RemoveAll. +func (*OsProxy) RemoveAll(path string) error { + return os.RemoveAll(path) +} + +// Stat is a proxy for os.Stat. +func (*OsProxy) Stat(name string) (*fsproxy.FileInfoInstance, error) { + fileInfo, err := os.Stat(name) + return &fsproxy.FileInfoInstance{FieldFileInfo: fileInfo}, err +} + +// TempDir is a proxy for os.TempDir. +func (*OsProxy) TempDir() string { + return os.TempDir() +} diff --git a/app/proxy/pflag/flagsetinstance.go b/app/proxy/pflag/flagsetinstance.go new file mode 100644 index 00000000..490e63d5 --- /dev/null +++ b/app/proxy/pflag/flagsetinstance.go @@ -0,0 +1,32 @@ +package pflagproxy + +import ( + "github.com/spf13/pflag" +) + +// FlagSetInstanceInterface is an interface for pflag.FlagSet. +type FlagSetInstanceInterface interface { + BoolVarP(p *bool, name string, shorthand string, value bool, usage string) + IntVarP(p *int, name string, shorthand string, value int, usage string) + StringVarP(p *string, name string, shorthand string, value string, usage string) +} + +// FlagSetInstance is a struct that implements FlagSetInstanceInterface. +type FlagSetInstance struct { + FieldFlagSet *pflag.FlagSet +} + +// BoolVarP is a proxy for pflag.FlagSet.BoolVarP. +func (f *FlagSetInstance) BoolVarP(p *bool, name string, shorthand string, value bool, usage string) { + f.FieldFlagSet.BoolVarP(p, name, shorthand, value, usage) +} + +// IntVarP is a proxy for pflag.FlagSet.IntVarP. +func (f *FlagSetInstance) IntVarP(p *int, name string, shorthand string, value int, usage string) { + f.FieldFlagSet.IntVarP(p, name, shorthand, value, usage) +} + +// StringVarP is a proxy for pflag.FlagSet.StringVarP. +func (f *FlagSetInstance) StringVarP(p *string, name string, shorthand string, value string, usage string) { + f.FieldFlagSet.StringVarP(p, name, shorthand, value, usage) +} diff --git a/app/proxy/pflag/godoc.go b/app/proxy/pflag/godoc.go new file mode 100644 index 00000000..5c8691cb --- /dev/null +++ b/app/proxy/pflag/godoc.go @@ -0,0 +1,2 @@ +// Package pflagproxy is a package that provides a pflag proxy for the app. +package pflagproxy diff --git a/app/proxy/pflag/pflagproxy.go b/app/proxy/pflag/pflagproxy.go new file mode 100644 index 00000000..63de73ad --- /dev/null +++ b/app/proxy/pflag/pflagproxy.go @@ -0,0 +1,9 @@ +package pflagproxy + +import () + +// Pflag is an interface for pflag. +type Pflag interface{} + +// PflagProxy is a struct that implements Pflag. +type PflagProxy struct{} diff --git a/app/proxy/promptui/godoc.go b/app/proxy/promptui/godoc.go new file mode 100644 index 00000000..bee3b607 --- /dev/null +++ b/app/proxy/promptui/godoc.go @@ -0,0 +1,2 @@ +// Package promptuiproxy is a package that provides a promptui proxy for the app. +package promptuiproxy diff --git a/app/proxy/promptui/promptinstance.go b/app/proxy/promptui/promptinstance.go new file mode 100644 index 00000000..cebac54e --- /dev/null +++ b/app/proxy/promptui/promptinstance.go @@ -0,0 +1,26 @@ +package promptuiproxy + +import ( + "github.com/manifoldco/promptui" +) + +// PromptInstanceInterface is an interface for promptui.Prompt. +type PromptInstanceInterface interface { + Run() (string, error) + SetLabel(label string) +} + +// PromptInstance is a struct that implements PromptInstanceInterface. +type PromptInstance struct { + FieldPrompt *promptui.Prompt +} + +// Run is a proxy for promptui.Prompt.Run. +func (p *PromptInstance) Run() (string, error) { + return p.FieldPrompt.Run() +} + +// SetLabel is a proxy for promptui.Prompt.Label. +func (p *PromptInstance) SetLabel(label string) { + p.FieldPrompt.Label = label +} diff --git a/app/proxy/promptui/promptinstance_test.go b/app/proxy/promptui/promptinstance_test.go new file mode 100644 index 00000000..20302ce4 --- /dev/null +++ b/app/proxy/promptui/promptinstance_test.go @@ -0,0 +1,75 @@ +package promptuiproxy + +import ( + "testing" + + "github.com/manifoldco/promptui" +) + +func TestPromptInstance_Run(t *testing.T) { + type fields struct { + FieldPrompt *promptui.Prompt + } + tests := []struct { + name string + fields fields + want string + wantErr bool + }{ + { + name: "positive testing", + fields: fields{ + FieldPrompt: &promptui.Prompt{}, + }, + want: "", + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PromptInstance{ + FieldPrompt: tt.fields.FieldPrompt, + } + got, err := p.Run() + if (err != nil) != tt.wantErr { + t.Errorf("PromptInstance.Run() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if got != tt.want { + t.Errorf("PromptInstance.Run() : got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} + +func TestPromptInstance_SetLabel(t *testing.T) { + type fields struct { + FieldPrompt *promptui.Prompt + } + type args struct { + label string + } + tests := []struct { + name string + fields fields + args args + }{ + { + name: "positive testing", + fields: fields{ + FieldPrompt: &promptui.Prompt{}, + }, + args: args{ + label: "test", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PromptInstance{ + FieldPrompt: tt.fields.FieldPrompt, + } + p.SetLabel(tt.args.label) + }) + } +} diff --git a/app/proxy/promptui/promptuiproxy.go b/app/proxy/promptui/promptuiproxy.go new file mode 100644 index 00000000..b56f744b --- /dev/null +++ b/app/proxy/promptui/promptuiproxy.go @@ -0,0 +1,23 @@ +package promptuiproxy + +import ( + "github.com/manifoldco/promptui" +) + +// Promptui is an interface for promptui. +type Promptui interface { + NewPrompt() PromptInstanceInterface +} + +// PromptuiProxy is a struct that implements Promptui. +type PromptuiProxy struct{} + +// New is a constructor for PromptuiProxy. +func New() Promptui { + return &PromptuiProxy{} +} + +// NewPrompt is a proxy for getting promptui.Prompt struct. +func (*PromptuiProxy) NewPrompt() PromptInstanceInterface { + return &PromptInstance{FieldPrompt: &promptui.Prompt{}} +} diff --git a/app/proxy/promptui/promptuiproxy_test.go b/app/proxy/promptui/promptuiproxy_test.go new file mode 100644 index 00000000..9a1a1afc --- /dev/null +++ b/app/proxy/promptui/promptuiproxy_test.go @@ -0,0 +1,36 @@ +package promptuiproxy + +import ( + "testing" +) + +func TestNew(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "positive testing", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + New() + }) + } +} + +func TestPromptuiProxy_NewPrompt(t *testing.T) { + tests := []struct { + name string + }{ + { + name: "positive testing", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := New() + p.NewPrompt() + }) + } +} diff --git a/app/proxy/rand/godoc.go b/app/proxy/rand/godoc.go new file mode 100644 index 00000000..e327a8e9 --- /dev/null +++ b/app/proxy/rand/godoc.go @@ -0,0 +1,2 @@ +// Package randproxy is a package that provides a rand proxy for the app. +package randproxy diff --git a/app/proxy/rand/randproxy.go b/app/proxy/rand/randproxy.go new file mode 100644 index 00000000..c1cc2ceb --- /dev/null +++ b/app/proxy/rand/randproxy.go @@ -0,0 +1,23 @@ +package randproxy + +import ( + "math/rand" +) + +// Rand is an interface for rand. +type Rand interface { + Intn(n int) int +} + +// RandProxy is a struct that implements Rand. +type RandProxy struct{} + +// New is a constructor for RandProxy. +func New() Rand { + return &RandProxy{} +} + +// Intn is a proxy for rand.Intn. +func (*RandProxy) Intn(n int) int { + return rand.Intn(n) +} diff --git a/app/proxy/sort/godoc.go b/app/proxy/sort/godoc.go new file mode 100644 index 00000000..63b8adcf --- /dev/null +++ b/app/proxy/sort/godoc.go @@ -0,0 +1,2 @@ +// Package sortproxy is a package that provides a sort proxy for the app. +package sortproxy diff --git a/app/proxy/sort/sortproxy.go b/app/proxy/sort/sortproxy.go new file mode 100644 index 00000000..36f11827 --- /dev/null +++ b/app/proxy/sort/sortproxy.go @@ -0,0 +1,23 @@ +package sortproxy + +import ( + "sort" +) + +// Sort is an interface for sort. +type Sort interface { + Slice(x any, less func(i, j int) bool) +} + +// SortProxy is a struct that implements Sort. +type SortProxy struct{} + +// New is a constructor of SortProxy. +func New() Sort { + return &SortProxy{} +} + +// Slice is a proxy for sort.Slice. +func (*SortProxy) Slice(x any, less func(i, j int) bool) { + sort.Slice(x, less) +} diff --git a/app/proxy/spinner/godoc.go b/app/proxy/spinner/godoc.go new file mode 100644 index 00000000..6c0ea832 --- /dev/null +++ b/app/proxy/spinner/godoc.go @@ -0,0 +1,2 @@ +// Package spinner proxy is a package that provides a spinner proxy for the app. +package spinnerproxy diff --git a/app/proxy/spinner/spinnerinstance.go b/app/proxy/spinner/spinnerinstance.go new file mode 100644 index 00000000..548c4f42 --- /dev/null +++ b/app/proxy/spinner/spinnerinstance.go @@ -0,0 +1,44 @@ +package spinnerproxy + +import ( + "github.com/briandowns/spinner" +) + +// SpinnerInstanceInterface is an interface for spinner.Spinner +type SpinnerInstanceInterface interface { + Reverse() + SetColor(colors ...string) error + SetSuffix(suffix string) + Start() + Stop() +} + +// SpinnerInstance is a struct that implements SpinnerInstanceInterface. +type SpinnerInstance struct { + FieldSpinner *spinner.Spinner +} + +// Reverse is a proxy for spinner.Spinner.Reverse. +func (s *SpinnerInstance) Reverse() { + s.FieldSpinner.Reverse() +} + +// SetColor is a proxy for spinner.Spinner.Color. +func (s *SpinnerInstance) SetColor(colors ...string) error { + return s.FieldSpinner.Color(colors...) +} + +// SetSuffix is a proxy for spinner.Spinner.Suffix. +func (s *SpinnerInstance) SetSuffix(suffix string) { + s.FieldSpinner.Suffix = suffix +} + +// Start is a proxy for spinner.Spinner.Start. +func (s *SpinnerInstance) Start() { + s.FieldSpinner.Start() +} + +// Stop is a proxy for spinner.Spinner.Stop. +func (s *SpinnerInstance) Stop() { + s.FieldSpinner.Stop() +} diff --git a/app/proxy/spinner/spinnerproxy.go b/app/proxy/spinner/spinnerproxy.go new file mode 100644 index 00000000..b4d40906 --- /dev/null +++ b/app/proxy/spinner/spinnerproxy.go @@ -0,0 +1,25 @@ +package spinnerproxy + +import ( + "github.com/briandowns/spinner" + + "github.com/yanosea/jrp/app/proxy/time" +) + +// Spinner is an interface for spinner. +type Spinner interface { + NewSpinner() SpinnerInstanceInterface +} + +// SpinnerProxy is a struct that implements Spinner. +type SpinnerProxy struct{} + +// New is a constructor for SpinnerProxy. +func New() Spinner { + return &SpinnerProxy{} +} + +// New is a proxy for spinner.New. +func (*SpinnerProxy) NewSpinner() SpinnerInstanceInterface { + return &SpinnerInstance{FieldSpinner: spinner.New(spinner.CharSets[11], 100*timeproxy.Millisecond)} +} diff --git a/app/proxy/sql/constant.go b/app/proxy/sql/constant.go new file mode 100644 index 00000000..2a5212a3 --- /dev/null +++ b/app/proxy/sql/constant.go @@ -0,0 +1,6 @@ +package sqlproxy + +const ( + // Sqlite is a constant for sqlite driver. + Sqlite = "sqlite" +) diff --git a/app/proxy/sql/dbinstance.go b/app/proxy/sql/dbinstance.go new file mode 100644 index 00000000..407a3c8b --- /dev/null +++ b/app/proxy/sql/dbinstance.go @@ -0,0 +1,48 @@ +package sqlproxy + +import ( + "database/sql" +) + +// DBInstanceInterface is an interface for sql.DB. +type DBInstanceInterface interface { + Begin() (TxInstanceInterface, error) + Close() error + Exec(query string, args ...interface{}) (ResultInstanceInterface, error) + Prepare(query string) (StmtInstanceInterface, error) + Query(query string, args ...interface{}) (RowsInstanceInterface, error) +} + +// DBInstance is a struct that implements DBInstanceInterface. +type DBInstance struct { + FieldDB *sql.DB +} + +// Begin is a proxy for sql.DB.Begin. +func (d *DBInstance) Begin() (TxInstanceInterface, error) { + tx, err := d.FieldDB.Begin() + return &TxInstance{FieldTx: tx}, err +} + +// Close is a proxy for sql.DB.Close. +func (d *DBInstance) Close() error { + return d.FieldDB.Close() +} + +// Exec is a proxy for sql.DB.Exec. +func (d *DBInstance) Exec(query string, args ...interface{}) (ResultInstanceInterface, error) { + res, err := d.FieldDB.Exec(query, args...) + return &ResultInstance{FieldResult: res}, err +} + +// Prepare is a proxy for sql.DB.Prepare. +func (d *DBInstance) Prepare(query string) (StmtInstanceInterface, error) { + stmt, err := d.FieldDB.Prepare(query) + return &StmtInstance{FieldStmt: stmt}, err +} + +// Query is a proxy for sql.DB.Query. +func (d *DBInstance) Query(query string, args ...interface{}) (RowsInstanceInterface, error) { + rows, _ := d.FieldDB.Query(query, args...) + return &RowsInstance{rows}, nil +} diff --git a/app/proxy/sql/godoc.go b/app/proxy/sql/godoc.go new file mode 100644 index 00000000..320dda65 --- /dev/null +++ b/app/proxy/sql/godoc.go @@ -0,0 +1,2 @@ +// Package sqlproxy is a package that provides a sql proxy for the app. +package sqlproxy diff --git a/app/proxy/sql/nullstringinstance.go b/app/proxy/sql/nullstringinstance.go new file mode 100644 index 00000000..c1f5aa6f --- /dev/null +++ b/app/proxy/sql/nullstringinstance.go @@ -0,0 +1,39 @@ +package sqlproxy + +import ( + "database/sql" + "database/sql/driver" +) + +// NullStringInstanceInterface is an interface for sql.NullString. +type NullStringInstanceInterface interface { + Scan(value interface{}) error + Value() (driver.Value, error) +} + +// NullStringInstance is a struct that implements NullStringInstanceInterface. +type NullStringInstance struct { + FieldNullString *sql.NullString +} + +// Scan implements the sql.Scanner interface. +func (n *NullStringInstance) Scan(value interface{}) error { + if n.FieldNullString == nil { + n.FieldNullString = &sql.NullString{ + String: "", + Valid: false, + } + } + str, _ := value.(string) + n.FieldNullString.String, n.FieldNullString.Valid = str, true + return nil +} + +// Value implements the driver Valuer interface. +func (n *NullStringInstance) Value() (driver.Value, error) { + var v driver.Value + if n.FieldNullString == nil || !n.FieldNullString.Valid { + v = nil + } + return v, nil +} diff --git a/app/proxy/sql/resultinstance.go b/app/proxy/sql/resultinstance.go new file mode 100644 index 00000000..28d8d4da --- /dev/null +++ b/app/proxy/sql/resultinstance.go @@ -0,0 +1,20 @@ +package sqlproxy + +import ( + "database/sql" +) + +// ResultInstanceInterface is an interface for sql.Result. +type ResultInstanceInterface interface { + RowsAffected() (int64, error) +} + +// ResultInstance is a struct that implements ResultInstanceInterface. +type ResultInstance struct { + FieldResult sql.Result +} + +// RowsAffected is a proxy for sql.Result.RowsAffected. +func (r ResultInstance) RowsAffected() (int64, error) { + return r.FieldResult.RowsAffected() +} diff --git a/app/proxy/sql/rowinstance.go b/app/proxy/sql/rowinstance.go new file mode 100644 index 00000000..99cf5450 --- /dev/null +++ b/app/proxy/sql/rowinstance.go @@ -0,0 +1,20 @@ +package sqlproxy + +import ( + "database/sql" +) + +// RowInstanceInterface is an interface for sql.Row. +type RowInstanceInterface interface { + Scan(dest ...interface{}) error +} + +// RowInstance is a struct that implements RowInstanceInterface. +type RowInstance struct { + FieldRow *sql.Row +} + +// Scan is a proxy for sql.Row.Scan. +func (r *RowInstance) Scan(dest ...interface{}) error { + return r.FieldRow.Scan(dest...) +} diff --git a/app/proxy/sql/rowsinstance.go b/app/proxy/sql/rowsinstance.go new file mode 100644 index 00000000..30f14944 --- /dev/null +++ b/app/proxy/sql/rowsinstance.go @@ -0,0 +1,32 @@ +package sqlproxy + +import ( + "database/sql" +) + +// RowsInstanceInterface is an interface for sql.Rows. +type RowsInstanceInterface interface { + Close() error + Next() bool + Scan(dest ...any) error +} + +// RowsInstance is a struct that implements RowsInstanceInterface. +type RowsInstance struct { + FieldRows *sql.Rows +} + +// Close is a proxy for sql.Rows.Close. +func (r *RowsInstance) Close() error { + return r.FieldRows.Close() +} + +// Next is a proxy for sql.Rows.Next. +func (r *RowsInstance) Next() bool { + return r.FieldRows.Next() +} + +// Scan is a proxy for sql.Rows.Scan. +func (r *RowsInstance) Scan(dest ...any) error { + return r.FieldRows.Scan(dest...) +} diff --git a/app/proxy/sql/sqlproxy.go b/app/proxy/sql/sqlproxy.go new file mode 100644 index 00000000..53c803e9 --- /dev/null +++ b/app/proxy/sql/sqlproxy.go @@ -0,0 +1,51 @@ +package sqlproxy + +import ( + "database/sql" + + _ "modernc.org/sqlite" +) + +// Sql is an interface for sql. +type Sql interface { + IfNullToNullString(nullStringInstance *NullStringInstance) *NullStringInstance + Open(driverName string, dataSourceName string) (DBInstanceInterface, error) + StringToNullString(s string) *NullStringInstance +} + +// SqlProxy is a struct that implements Sql. +type SqlProxy struct{} + +// New is a constructor for SqlProxy. +func New() Sql { + return &SqlProxy{} +} + +// IfNullToNullString returns a NullStringInstance if the argument is nil. +func (*SqlProxy) IfNullToNullString(nullStringInstance *NullStringInstance) *NullStringInstance { + if nullStringInstance == nil { + nullStringInstance = &NullStringInstance{ + FieldNullString: &sql.NullString{ + String: "", + Valid: false, + }, + } + } + return nullStringInstance +} + +// Open is a proxy for sql.Open. +func (*SqlProxy) Open(driverName string, dataSourceName string) (DBInstanceInterface, error) { + db, err := sql.Open(driverName, dataSourceName) + return &DBInstance{FieldDB: db}, err +} + +// StringToNullString returns a NullStringInstance with the argument as the String field. +func (*SqlProxy) StringToNullString(s string) *NullStringInstance { + return &NullStringInstance{ + FieldNullString: &sql.NullString{ + String: s, + Valid: s != "", + }, + } +} diff --git a/app/proxy/sql/stmtinstance.go b/app/proxy/sql/stmtinstance.go new file mode 100644 index 00000000..61e89cdf --- /dev/null +++ b/app/proxy/sql/stmtinstance.go @@ -0,0 +1,32 @@ +package sqlproxy + +import ( + "database/sql" +) + +// StmtInstanceInterface is an interface for sql.Stmt. +type StmtInstanceInterface interface { + Close() error + Exec(args ...interface{}) (ResultInstanceInterface, error) + Query(args ...interface{}) (RowsInstanceInterface, error) +} + +// StmtInstance is a struct that implements StmtInstanceInterface. +type StmtInstance struct { + FieldStmt *sql.Stmt +} + +// Close is a proxy for sql.Stmt.Close. +func (s *StmtInstance) Close() error { + return s.FieldStmt.Close() +} + +func (s *StmtInstance) Exec(args ...interface{}) (ResultInstanceInterface, error) { + res, err := s.FieldStmt.Exec(args...) + return &ResultInstance{FieldResult: res}, err +} + +func (s *StmtInstance) Query(args ...interface{}) (RowsInstanceInterface, error) { + rows, err := s.FieldStmt.Query(args...) + return &RowsInstance{FieldRows: rows}, err +} diff --git a/app/proxy/sql/txinstance.go b/app/proxy/sql/txinstance.go new file mode 100644 index 00000000..b2ba980c --- /dev/null +++ b/app/proxy/sql/txinstance.go @@ -0,0 +1,39 @@ +package sqlproxy + +import ( + "database/sql" +) + +// TxInstanceInterface is an interface for sql.Tx. +type TxInstanceInterface interface { + Commit() error + Exec(query string, args ...interface{}) (ResultInstanceInterface, error) + QueryRow(query string, args ...interface{}) RowInstanceInterface + Rollback() error +} + +// TxInstance is a struct that implements TxInstanceInterface. +type TxInstance struct { + FieldTx *sql.Tx +} + +// Commit is a proxy for sql.Tx.Commit. +func (t *TxInstance) Commit() error { + return t.FieldTx.Commit() +} + +// Exec is a proxy for sql.Tx.Exec. +func (t *TxInstance) Exec(query string, args ...interface{}) (ResultInstanceInterface, error) { + res, err := t.FieldTx.Exec(query, args...) + return &ResultInstance{FieldResult: res}, err +} + +// QueryRow is a proxy for sql.Tx.QueryRow. +func (t *TxInstance) QueryRow(query string, args ...interface{}) RowInstanceInterface { + return &RowInstance{FieldRow: t.FieldTx.QueryRow(query, args...)} +} + +// Rollback is a proxy for sql.Tx.Rollback. +func (t *TxInstance) Rollback() error { + return t.FieldTx.Rollback() +} diff --git a/app/proxy/strconv/godoc.go b/app/proxy/strconv/godoc.go new file mode 100644 index 00000000..f26eacf3 --- /dev/null +++ b/app/proxy/strconv/godoc.go @@ -0,0 +1,2 @@ +// Package strconvproxy is a package that provides a strconv proxy for the app. +package strconvproxy diff --git a/app/proxy/strconv/strconvproxy.go b/app/proxy/strconv/strconvproxy.go new file mode 100644 index 00000000..87653a59 --- /dev/null +++ b/app/proxy/strconv/strconvproxy.go @@ -0,0 +1,29 @@ +package strconvproxy + +import ( + "strconv" +) + +// Strconv is an interface for strconv. +type Strconv interface { + Atoi(s string) (int, error) + Itoa(i int) string +} + +// StrconvProxy is a struct that implements Strconv. +type StrconvProxy struct{} + +// New is a constructor for StrconvProxy. +func New() Strconv { + return &StrconvProxy{} +} + +// Atoi is a proxy for strconv.Atoi. +func (*StrconvProxy) Atoi(s string) (int, error) { + return strconv.Atoi(s) +} + +// Itoa is a proxy for strconv.Itoa. +func (*StrconvProxy) Itoa(i int) string { + return strconv.Itoa(i) +} diff --git a/app/proxy/strings/godoc.go b/app/proxy/strings/godoc.go new file mode 100644 index 00000000..8d94d3b6 --- /dev/null +++ b/app/proxy/strings/godoc.go @@ -0,0 +1,2 @@ +// Package stringsproxy is a package that provides a strings proxy for the app. +package stringsproxy diff --git a/app/proxy/strings/stringsproxy.go b/app/proxy/strings/stringsproxy.go new file mode 100644 index 00000000..841cd9fd --- /dev/null +++ b/app/proxy/strings/stringsproxy.go @@ -0,0 +1,23 @@ +package stringsproxy + +import ( + "strings" +) + +// Strings is an interface for strings. +type Strings interface { + Join(elems []string, sep string) string +} + +// StringsProxy is a struct that implements Strings. +type StringsProxy struct{} + +// New is a constructor for StringsProxy. +func New() Strings { + return &StringsProxy{} +} + +// Join is a proxy for strings.Join. +func (*StringsProxy) Join(elems []string, sep string) string { + return strings.Join(elems, sep) +} diff --git a/app/proxy/tablewriter/constant.go b/app/proxy/tablewriter/constant.go new file mode 100644 index 00000000..f2fa5f55 --- /dev/null +++ b/app/proxy/tablewriter/constant.go @@ -0,0 +1,10 @@ +package tablewriterproxy + +import ( + "github.com/olekukonko/tablewriter" +) + +const ( + // ALIGN_CENTER is a proxy for tablewriter.ALIGN_CENTER. + ALIGN_LEFT = tablewriter.ALIGN_LEFT +) diff --git a/app/proxy/tablewriter/godoc.go b/app/proxy/tablewriter/godoc.go new file mode 100644 index 00000000..7ff229f5 --- /dev/null +++ b/app/proxy/tablewriter/godoc.go @@ -0,0 +1,2 @@ +// Package tablewriterproxy is a package that provides a tablewriter proxy for the app. +package tablewriterproxy diff --git a/app/proxy/tablewriter/tableinstance.go b/app/proxy/tablewriter/tableinstance.go new file mode 100644 index 00000000..f00366f7 --- /dev/null +++ b/app/proxy/tablewriter/tableinstance.go @@ -0,0 +1,98 @@ +package tablewriterproxy + +import ( + "github.com/olekukonko/tablewriter" +) + +// TableInstanceInterface is an interface for tablewriter.Table. +type TableInstanceInterface interface { + AppendBulk(rows [][]string) + Render() + SetAlignment(align int) + SetAutoFormatHeaders(auto bool) + SetAutoWrapText(auto bool) + SetBorder(border bool) + SetCenterSeparator(sep string) + SetColumnSeparator(sep string) + SetHeader(keys []string) + SetHeaderAlignment(hAlign int) + SetHeaderLine(line bool) + SetNoWhiteSpace(allow bool) + SetRowSeparator(sep string) + SetTablePadding(padding string) +} + +// TableInstance is a struct that implements TableInstanceInterface. +type TableInstance struct { + FieldTable *tablewriter.Table +} + +// AppendBulk is a proxy for tablewriter.Table.AppendBulk. +func (t *TableInstance) AppendBulk(rows [][]string) { + t.FieldTable.AppendBulk(rows) +} + +// Render is a proxy for tablewriter.Table.Render. +func (t *TableInstance) Render() { + t.FieldTable.Render() +} + +// SetAlignment is a proxy for tablewriter.Table.SetAlignment. +func (t *TableInstance) SetAlignment(align int) { + t.FieldTable.SetAlignment(align) +} + +// SetAutoFormatHeaders is a proxy for tablewriter.Table.SetAutoFormatHeaders. +func (t *TableInstance) SetAutoFormatHeaders(auto bool) { + t.FieldTable.SetAutoFormatHeaders(auto) +} + +// SetAutoWrapText is a proxy for tablewriter.Table.SetAutoWrapText. +func (t *TableInstance) SetAutoWrapText(auto bool) { + t.FieldTable.SetAutoWrapText(auto) +} + +// SetBorder is a proxy for tablewriter.Table.SetBorder. +func (t *TableInstance) SetBorder(border bool) { + t.FieldTable.SetBorder(border) +} + +// SetCenterSeparator is a proxy for tablewriter.Table.SetCenterSeparator. +func (t *TableInstance) SetCenterSeparator(sep string) { + t.FieldTable.SetCenterSeparator(sep) +} + +// SetColumnSeparator is a proxy for tablewriter.Table.SetColumnSeparator. +func (t *TableInstance) SetColumnSeparator(sep string) { + t.FieldTable.SetColumnSeparator(sep) +} + +// SetHeader is a proxy for tablewriter.Table.SetHeader. +func (t *TableInstance) SetHeader(keys []string) { + t.FieldTable.SetHeader(keys) +} + +// SetHeaderAlignment is a proxy for tablewriter.Table.SetHeaderAlignment. +func (t *TableInstance) SetHeaderAlignment(hAlign int) { + t.FieldTable.SetHeaderAlignment(hAlign) +} + +// SetHeaderLine is a proxy for tablewriter.Table.SetHeaderLine. +func (t TableInstance) SetHeaderLine(line bool) { + t.FieldTable.SetHeaderLine(line) +} + +// SetNoWhiteSpace is a proxy for tablewriter.Table.SetNoWhiteSpace. +func (t *TableInstance) SetNoWhiteSpace(allow bool) { + t.FieldTable.SetNoWhiteSpace(allow) +} + +// SetRowSeparator is a proxy for tablewriter.Table.SetRowSeparator. +func (t *TableInstance) SetRowSeparator(sep string) { + t.FieldTable.SetRowSeparator(sep) +} + +// SetTablePadding is a proxy for tablewriter.Table.SetTablePadding. +func (t *TableInstance) SetTablePadding(padding string) { + t.FieldTable.SetTablePadding(padding) +} diff --git a/app/proxy/tablewriter/tablewriterproxy.go b/app/proxy/tablewriter/tablewriterproxy.go new file mode 100644 index 00000000..3c5e8b93 --- /dev/null +++ b/app/proxy/tablewriter/tablewriterproxy.go @@ -0,0 +1,25 @@ +package tablewriterproxy + +import ( + "github.com/olekukonko/tablewriter" + + "github.com/yanosea/jrp/app/proxy/io" +) + +// TableWriter is an interface for tablewriter. +type TableWriter interface { + NewTable(writer ioproxy.WriterInstanceInterface) *TableInstance +} + +// TableWriterProxy is a struct that implements TableWriter. +type TableWriterProxy struct{} + +// New is a constructor for TableWriterProxy. +func New() TableWriter { + return &TableWriterProxy{} +} + +// NewTable is a proxy for tablewriter.NewTable. +func (*TableWriterProxy) NewTable(writer ioproxy.WriterInstanceInterface) *TableInstance { + return &TableInstance{FieldTable: tablewriter.NewWriter(writer)} +} diff --git a/app/proxy/time/constant.go b/app/proxy/time/constant.go new file mode 100644 index 00000000..ab033ac1 --- /dev/null +++ b/app/proxy/time/constant.go @@ -0,0 +1,13 @@ +package timeproxy + +import ( + "time" +) + +const ( + // Millisecond is a proxy for time.Millisecond. + Millisecond = time.Millisecond +) + +// UTC is a variable for time.UTC. +var UTC = *time.UTC diff --git a/app/proxy/time/godoc.go b/app/proxy/time/godoc.go new file mode 100644 index 00000000..7dc32fee --- /dev/null +++ b/app/proxy/time/godoc.go @@ -0,0 +1,2 @@ +// Package time proxy is a package that provides a time proxy for the app. +package timeproxy diff --git a/app/proxy/time/timeinstance.go b/app/proxy/time/timeinstance.go new file mode 100644 index 00000000..b19c61ae --- /dev/null +++ b/app/proxy/time/timeinstance.go @@ -0,0 +1,34 @@ +package timeproxy + +import ( + "database/sql/driver" + "time" +) + +// TimeInstanceInterface is an interface for time.Time. +type TimeInstanceInterface interface { + Format(layout string) string + Scan(value interface{}) error + Value() (driver.Value, error) +} + +// TimeInstance is a struct that implements TimeInstanceInterface. +type TimeInstance struct { + FieldTime time.Time +} + +// Format is a proxy for time.Time.Format. +func (t *TimeInstance) Format(layout string) string { + return t.FieldTime.Format(layout) +} + +// Scan implements the sql.Scanner interface. +func (t *TimeInstance) Scan(value interface{}) error { + t.FieldTime = value.(time.Time) + return nil +} + +// Value implements the driver.Valuer interface. +func (t *TimeInstance) Value() (driver.Value, error) { + return t.FieldTime, nil +} diff --git a/app/proxy/time/timeproxy.go b/app/proxy/time/timeproxy.go new file mode 100644 index 00000000..2eee8646 --- /dev/null +++ b/app/proxy/time/timeproxy.go @@ -0,0 +1,29 @@ +package timeproxy + +import ( + "time" +) + +// Time is an interface for time. +type Time interface { + Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) *TimeInstance + Now() *TimeInstance +} + +// TimeProxy is a struct that implements Time. +type TimeProxy struct{} + +// New is a constructor for TimeProxy. +func New() Time { + return &TimeProxy{} +} + +// Date is a proxy for time.Date. +func (t *TimeProxy) Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) *TimeInstance { + return &TimeInstance{FieldTime: time.Date(year, month, day, hour, min, sec, nsec, loc)} +} + +// Now is a proxy for time.Now. +func (*TimeProxy) Now() *TimeInstance { + return &TimeInstance{FieldTime: time.Now()} +} diff --git a/app/proxy/user/godoc.go b/app/proxy/user/godoc.go new file mode 100644 index 00000000..678488f3 --- /dev/null +++ b/app/proxy/user/godoc.go @@ -0,0 +1,2 @@ +// Package userproxy is a package that provides a user proxy for the app. +package userproxy diff --git a/app/proxy/user/userinstance.go b/app/proxy/user/userinstance.go new file mode 100644 index 00000000..d8eafe6b --- /dev/null +++ b/app/proxy/user/userinstance.go @@ -0,0 +1,13 @@ +package userproxy + +import ( + "os/user" +) + +// UserInstanceInterface is a interface for user.User. +type UserInstanceInterface interface{} + +// UserInstance is a struct that implements UserInstanceInterface. +type UserInstance struct { + FieldUser *user.User +} diff --git a/app/proxy/user/userproxy.go b/app/proxy/user/userproxy.go new file mode 100644 index 00000000..d3baac98 --- /dev/null +++ b/app/proxy/user/userproxy.go @@ -0,0 +1,24 @@ +package userproxy + +import ( + "os/user" +) + +// User is an interface for user. +type User interface { + Current() (*UserInstance, error) +} + +// UserProxy is a struct that implements User. +type UserProxy struct{} + +// New is a constructor for UserProxy. +func New() User { + return &UserProxy{} +} + +// Current is a proxy for user.Current. +func (*UserProxy) Current() (*UserInstance, error) { + currentUser, err := user.Current() + return &UserInstance{FieldUser: currentUser}, err +} diff --git a/cmd/completion.go b/cmd/completion.go index a7963d7e..b36fa9e1 100644 --- a/cmd/completion.go +++ b/cmd/completion.go @@ -3,33 +3,43 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" - "github.com/yanosea/jrp/util" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/cmd/constant" ) -func newCompletionCommand(globalOption *GlobalOption) *cobra.Command { - cmd := &cobra.Command{ - Use: constant.COMPLETION_USE, - Short: constant.COMPLETION_SHORT, - Long: constant.COMPLETION_LONG, - RunE: func(cmd *cobra.Command, args []string) error { - // if no sub command is specified, print the message and return nil. - util.PrintlnWithWriter(globalOption.Out, constant.COMPLETION_MESSAGE_NO_SUB_COMMAND) - - return nil - }, - } - - cmd.SetOut(globalOption.Out) - cmd.SetErr(globalOption.ErrOut) +// NewCompletionCommand creates a new completion command. +func NewCompletionCommand(g *GlobalOption) *cobraproxy.CommandInstance { + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.COMPLETION_USE + cmd.FieldCommand.RunE = g.completionRunE + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) cmd.SetHelpTemplate(constant.COMPLETION_HELP_TEMPLATE) cmd.AddCommand( - newCompletionBashCommand(globalOption), - newCompletionFishCommand(globalOption), - newCompletionPowerShellCommand(globalOption), - newCompletionZshCommand(globalOption), + NewCompletionBashCommand(g), + NewCompletionFishCommand(g), + NewCompletionPowerShellCommand(g), + NewCompletionZshCommand(g), ) return cmd } + +// completionRunE is the function that is called when the completion command is executed. +func (g *GlobalOption) completionRunE(_ *cobra.Command, _ []string) error { + return g.completion() +} + +// completion just prints the message. +func (g *GlobalOption) completion() error { + // if no sub command is specified, print the message and return nil. + colorProxy := colorproxy.New() + g.Utility.PrintlnWithWriter(g.Out, colorProxy.YellowString(constant.COMPLETION_MESSAGE_NO_SUB_COMMAND)) + + return nil +} diff --git a/cmd/completion_bash.go b/cmd/completion_bash.go index 956c7f42..31694daa 100644 --- a/cmd/completion_bash.go +++ b/cmd/completion_bash.go @@ -3,22 +3,31 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/cmd/constant" ) -func newCompletionBashCommand(globalOption *GlobalOption) *cobra.Command { - cmd := &cobra.Command{ - Use: constant.COMPLETION_BASH_USE, - Short: constant.COMPLETION_BASH_SHORT, - Long: constant.COMPLETION_BASH_LONG, - RunE: func(cmd *cobra.Command, args []string) error { - return cmd.GenBashCompletion(globalOption.Out) - }, - } - - cmd.SetOut(globalOption.Out) - cmd.SetErr(globalOption.ErrOut) +// NewCompletionBashCommand creates a new completion bash command. +func NewCompletionBashCommand(g *GlobalOption) *cobraproxy.CommandInstance { + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.COMPLETION_BASH_USE + cmd.FieldCommand.RunE = g.completionBashRunE + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) cmd.SetHelpTemplate(constant.COMPLETION_BASH_HELP_TEMPLATE) return cmd } + +// completionBashRunE is a function that is called when the completion bash command is executed. +func (g *GlobalOption) completionBashRunE(c *cobra.Command, _ []string) error { + return g.completionBash(c) +} + +// completionBash generates the bash completion script. +func (g *GlobalOption) completionBash(c *cobra.Command) error { + return c.GenBashCompletion(g.Out) +} diff --git a/cmd/completion_bash_test.go b/cmd/completion_bash_test.go index 60e655bc..f74911db 100644 --- a/cmd/completion_bash_test.go +++ b/cmd/completion_bash_test.go @@ -1,42 +1,145 @@ package cmd import ( - "os" "testing" "github.com/spf13/cobra" - - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" ) func TestNewCompletionBashCommand(t *testing.T) { type args struct { - globalOption *GlobalOption + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewCompletionBashCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewCompletionBashCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func TestGlobalOption_completionBashRunE(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + type args struct { + c *cobra.Command + in0 []string } tests := []struct { name string + fields fields args args - want *cobra.Command wantErr bool }{ { name: "positive testing", - args: args{globalOption: &GlobalOption{Out: os.Stdout, ErrOut: os.Stderr}}, - want: &cobra.Command{ - Use: constant.COMPLETION_BASH_USE, - Short: constant.COMPLETION_BASH_SHORT, - Long: constant.COMPLETION_BASH_LONG, + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), + in0: nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, + } + if err := g.completionBashRunE(tt.args.c, tt.args.in0); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionBashRunE() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + }) + } +} + +func TestGlobalOption_completionBash(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + type args struct { + c *cobra.Command + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "positive testing", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := newCompletionBashCommand(tt.args.globalOption) - if got.Use != tt.want.Use || got.Short != tt.want.Short || got.Long != tt.want.Long { - t.Errorf("newCompletionBashCommand() : got = %v, want = %v", got, tt.want) + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, } - if err := got.Execute(); (err != nil) != tt.wantErr { - t.Errorf("newCompletionBashCommand().Execute() : error = %v, wantErr %v", err, tt.wantErr) + if err := g.completionBash(tt.args.c); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionBash() : error =\n%v, wantErr =\n%v", err, tt.wantErr) } }) } diff --git a/cmd/completion_fish.go b/cmd/completion_fish.go index f805bd6b..08bdc142 100644 --- a/cmd/completion_fish.go +++ b/cmd/completion_fish.go @@ -3,22 +3,31 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/cmd/constant" ) -func newCompletionFishCommand(globalOption *GlobalOption) *cobra.Command { - cmd := &cobra.Command{ - Use: constant.COMPLETION_FISH_USE, - Short: constant.COMPLETION_FISH_SHORT, - Long: constant.COMPLETION_FISH_LONG, - RunE: func(cmd *cobra.Command, args []string) error { - return cmd.GenFishCompletion(globalOption.Out, false) - }, - } - - cmd.SetOut(globalOption.Out) - cmd.SetErr(globalOption.ErrOut) +// NewCompletionFishCommand creates a new command for fish completion. +func NewCompletionFishCommand(g *GlobalOption) *cobraproxy.CommandInstance { + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.COMPLETION_FISH_USE + cmd.FieldCommand.RunE = g.completionFishRunE + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) cmd.SetHelpTemplate(constant.COMPLETION_FISH_HELP_TEMPLATE) return cmd } + +// completionFishRunE is the function that is called when the completion fish command is executed. +func (g *GlobalOption) completionFishRunE(c *cobra.Command, _ []string) error { + return g.completionFish(c) +} + +// completionFish generates the fish completion script. +func (g *GlobalOption) completionFish(c *cobra.Command) error { + return c.GenFishCompletion(g.Out, false) +} diff --git a/cmd/completion_fish_test.go b/cmd/completion_fish_test.go index d13f7cb7..f5376624 100644 --- a/cmd/completion_fish_test.go +++ b/cmd/completion_fish_test.go @@ -1,42 +1,145 @@ package cmd import ( - "os" "testing" "github.com/spf13/cobra" - - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" ) func TestNewCompletionFishCommand(t *testing.T) { type args struct { - globalOption *GlobalOption + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewCompletionFishCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewCompletionFishCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func TestGlobalOption_completionFishRunE(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + type args struct { + c *cobra.Command + in0 []string } tests := []struct { name string + fields fields args args - want *cobra.Command wantErr bool }{ { name: "positive testing", - args: args{globalOption: &GlobalOption{Out: os.Stdout, ErrOut: os.Stderr}}, - want: &cobra.Command{ - Use: constant.COMPLETION_FISH_USE, - Short: constant.COMPLETION_FISH_SHORT, - Long: constant.COMPLETION_FISH_LONG, + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), + in0: nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, + } + if err := g.completionFishRunE(tt.args.c, nil); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionFishRunE() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + }) + } +} + +func TestGlobalOption_completionFish(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + type args struct { + c *cobra.Command + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "positive testing", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := newCompletionFishCommand(tt.args.globalOption) - if got.Use != tt.want.Use || got.Short != tt.want.Short || got.Long != tt.want.Long { - t.Errorf("newCompletionFishCommand() : got = %v, want = %v", got, tt.want) + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, } - if err := got.Execute(); (err != nil) != tt.wantErr { - t.Errorf("newCompletionFishCommand().Execute() : error = %v, wantErr %v", err, tt.wantErr) + if err := g.completionFish(tt.args.c); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionFish() : error =\n%v, wantErr =\n%v", err, tt.wantErr) } }) } diff --git a/cmd/completion_powershell.go b/cmd/completion_powershell.go index 3de8772a..91b6e9bc 100644 --- a/cmd/completion_powershell.go +++ b/cmd/completion_powershell.go @@ -3,22 +3,31 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/cmd/constant" ) -func newCompletionPowerShellCommand(globalOption *GlobalOption) *cobra.Command { - cmd := &cobra.Command{ - Use: constant.COMPLETION_POWERSHELL_USE, - Short: constant.COMPLETION_POWERSHELL_SHORT, - Long: constant.COMPLETION_POWERSHELL_LONG, - RunE: func(cmd *cobra.Command, args []string) error { - return cmd.GenPowerShellCompletion(globalOption.Out) - }, - } - - cmd.SetOut(globalOption.Out) - cmd.SetErr(globalOption.ErrOut) +// NewCompletionPowerShellCommand creates a new completion powershell command. +func NewCompletionPowerShellCommand(g *GlobalOption) *cobraproxy.CommandInstance { + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.COMPLETION_POWERSHELL_USE + cmd.FieldCommand.RunE = g.completionPowerShellRunE + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) cmd.SetHelpTemplate(constant.COMPLETION_POWERSHELL_HELP_TEMPLATE) return cmd } + +// completionPowerShellRunE is a function that is called when the completion powershell command is executed. +func (g *GlobalOption) completionPowerShellRunE(c *cobra.Command, _ []string) error { + return g.completionPowerShell(c) +} + +// completionPowerShell generates the powershell completion script. +func (g *GlobalOption) completionPowerShell(c *cobra.Command) error { + return c.GenPowerShellCompletion(g.Out) +} diff --git a/cmd/completion_powershell_test.go b/cmd/completion_powershell_test.go index f677b366..adbe0076 100644 --- a/cmd/completion_powershell_test.go +++ b/cmd/completion_powershell_test.go @@ -1,42 +1,145 @@ package cmd import ( - "os" "testing" "github.com/spf13/cobra" - - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" ) -func TestNewCompletioncPowerShellCommand(t *testing.T) { +func TestNewCompletionPowerShellCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewCompletionPowerShellCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewCompletionPowerShellCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func TestGlobalOption_completionPowerShellRunE(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := globalOption.NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } type args struct { - globalOption *GlobalOption + c *cobra.Command + in0 []string } tests := []struct { name string + fields fields args args - want *cobra.Command wantErr bool }{ { name: "positive testing", - args: args{globalOption: &GlobalOption{Out: os.Stdout, ErrOut: os.Stderr}}, - want: &cobra.Command{ - Use: constant.COMPLETION_POWERSHELL_USE, - Short: constant.COMPLETION_POWERSHELL_SHORT, - Long: constant.COMPLETION_POWERSHELL_LONG, + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), + in0: nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, + } + if err := g.completionPowerShellRunE(tt.args.c, tt.args.in0); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionPowerShellRunE() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + }) + } +} + +func TestGlobalOption_completionPowerShell(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := globalOption.NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + type args struct { + c *cobra.Command + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "positive testing", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := newCompletionPowerShellCommand(tt.args.globalOption) - if got.Use != tt.want.Use || got.Short != tt.want.Short || got.Long != tt.want.Long { - t.Errorf("newCompletioncPowerShellCommand() : got = %v, want = %v", got, tt.want) + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, } - if err := got.Execute(); (err != nil) != tt.wantErr { - t.Errorf("newCompletioncPowerShellCommand().Execute() : error = %v, wantErr %v", err, tt.wantErr) + if err := g.completionPowerShell(tt.args.c); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionPowerShell() : error =\n%v, wantErr =\n%v", err, tt.wantErr) } }) } diff --git a/cmd/completion_test.go b/cmd/completion_test.go index 2c581338..590b6d1d 100644 --- a/cmd/completion_test.go +++ b/cmd/completion_test.go @@ -1,42 +1,159 @@ package cmd import ( - "os" "testing" "github.com/spf13/cobra" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/cmd/constant" - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/test/testutility" ) func TestNewCompletionCommand(t *testing.T) { + + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewCompletionCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewCompletionCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func TestGlobalOption_completionRunE(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } type args struct { - globalOption *GlobalOption + in0 *cobra.Command + in1 []string } tests := []struct { name string + fields fields args args - want *cobra.Command wantErr bool }{ { name: "positive testing", - args: args{globalOption: &GlobalOption{Out: os.Stdout, ErrOut: os.Stderr}}, - want: &cobra.Command{ - Use: constant.COMPLETION_USE, - Short: constant.COMPLETION_SHORT, - Long: constant.COMPLETION_LONG, + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + in0: nil, + in1: nil, }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := newCompletionCommand(tt.args.globalOption) - if got.Use != tt.want.Use || got.Short != tt.want.Short || got.Long != tt.want.Long { - t.Errorf("newCompletionCommand() : got = %v, want = %v", got, tt.want) + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, + } + if err := g.completionRunE(tt.args.in0, tt.args.in1); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionRunE() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + }) + } +} + +func TestGlobalOption_completion(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + globalOption := NewGlobalOption(fmtproxy.New(), osProxy, strconvproxy.New()) + globalOption.Out = capturer.OutBuffer + globalOption.ErrOut = capturer.ErrBuffer + colorProxy := colorproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing", + fields: fields{ + t: t, + fnc: func() { + if err := globalOption.completion(); err != nil { + t.Errorf("GlobalOption.completion() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.COMPLETION_MESSAGE_NO_SUB_COMMAND) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("GlobalOption.completion() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) } - if err := got.Execute(); (err != nil) != tt.wantErr { - t.Errorf("newCompletionCommand().Execute() : error = %v, wantErr %v", err, tt.wantErr) + if stderr != tt.wantStdErr { + t.Errorf("GlobalOption.completion() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } }) } diff --git a/cmd/completion_zsh.go b/cmd/completion_zsh.go index e4c70fd2..665c4b37 100644 --- a/cmd/completion_zsh.go +++ b/cmd/completion_zsh.go @@ -3,22 +3,31 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/cmd/constant" ) -func newCompletionZshCommand(globalOption *GlobalOption) *cobra.Command { - cmd := &cobra.Command{ - Use: constant.COMPLETION_ZSH_USE, - Short: constant.COMPLETION_ZSH_SHORT, - Long: constant.COMPLETION_ZSH_LONG, - RunE: func(cmd *cobra.Command, args []string) error { - return cmd.GenZshCompletion(globalOption.Out) - }, - } - - cmd.SetOut(globalOption.Out) - cmd.SetErr(globalOption.ErrOut) +// NewCompletionZshCommand creates a new completion zsh command. +func NewCompletionZshCommand(g *GlobalOption) *cobraproxy.CommandInstance { + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.COMPLETION_ZSH_USE + cmd.FieldCommand.RunE = g.completionZshRunE + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) cmd.SetHelpTemplate(constant.COMPLETION_ZSH_HELP_TEMPLATE) return cmd } + +// completionZshRunE is a function that is called when the completion zsh command is executed. +func (g *GlobalOption) completionZshRunE(c *cobra.Command, _ []string) error { + return g.completionZsh(c) +} + +// completionZsh generates the zsh completion script. +func (g *GlobalOption) completionZsh(c *cobra.Command) error { + return c.GenZshCompletion(g.Out) +} diff --git a/cmd/completion_zsh_test.go b/cmd/completion_zsh_test.go index b2eb9a4b..48a3b747 100644 --- a/cmd/completion_zsh_test.go +++ b/cmd/completion_zsh_test.go @@ -1,42 +1,145 @@ package cmd import ( - "os" "testing" "github.com/spf13/cobra" - - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" ) -func TestNewCompletioncZshCommand(t *testing.T) { +func TestNewCompletionZshCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewCompletionZshCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewCompletionZshCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func TestGlobalOption_completionZshRunE(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := globalOption.NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } type args struct { - globalOption *GlobalOption + c *cobra.Command + in0 []string } tests := []struct { name string + fields fields args args - want *cobra.Command wantErr bool }{ { name: "positive testing", - args: args{globalOption: &GlobalOption{Out: os.Stdout, ErrOut: os.Stderr}}, - want: &cobra.Command{ - Use: constant.COMPLETION_ZSH_USE, - Short: constant.COMPLETION_ZSH_SHORT, - Long: constant.COMPLETION_ZSH_LONG, + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), + in0: nil, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, + } + if err := g.completionZshRunE(tt.args.c, tt.args.in0); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionZshRunE() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + }) + } +} + +func TestGlobalOption_completionZsh(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + cmd := globalOption.NewRootCommand(globalOption.Out, globalOption.ErrOut, globalOption.Args) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + type args struct { + c *cobra.Command + } + tests := []struct { + name string + fields fields + args args + wantErr bool + }{ + { + name: "positive testing", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + c: cmd.GetCommand(), }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := newCompletionZshCommand(tt.args.globalOption) - if got.Use != tt.want.Use || got.Short != tt.want.Short || got.Long != tt.want.Long { - t.Errorf("newCompletioncZshCommand() : got = %v, want = %v", got, tt.want) + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, } - if err := got.Execute(); (err != nil) != tt.wantErr { - t.Errorf("newCompletioncZshCommand().Execute() : error = %v, wantErr %v", err, tt.wantErr) + if err := g.completionZsh(tt.args.c); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.completionZsh() : error =\n%v, wantErr =\n%v", err, tt.wantErr) } }) } diff --git a/constant/completion.go b/cmd/constant/completion.go similarity index 65% rename from constant/completion.go rename to cmd/constant/completion.go index 14e0f103..a7563be9 100644 --- a/constant/completion.go +++ b/cmd/constant/completion.go @@ -1,6 +1,7 @@ package constant const ( + COMPLETION_USE = "completion" COMPLETION_HELP_TEMPLATE = `πŸ”§ Generate the autocompletion script for the specified shell. Usage: @@ -18,21 +19,11 @@ Flags: Use "jrp completion [command] --help" for more information about a command. ` - COMPLETION_USE = "completion" - COMPLETION_SHORT = "πŸ”§ Generate the autocompletion script for the specified shell." - COMPLETION_LONG = `πŸ”§ Generate the autocompletion script for the specified shell. - -See each sub-command's help for details on how to use the generated script. -You must use sub command below... - - - 🐚 bash - - 🐟 fish - - πŸͺŸ powershell - - πŸ§™ zsh` COMPLETION_MESSAGE_NO_SUB_COMMAND = `Use sub command below... - 🐚 bash - 🐟 fish - πŸͺŸ powershell - - πŸ§™ zsh` + - πŸ§™ zsh +` ) diff --git a/cmd/constant/completion_bash.go b/cmd/constant/completion_bash.go new file mode 100644 index 00000000..0c2ec739 --- /dev/null +++ b/cmd/constant/completion_bash.go @@ -0,0 +1,32 @@ +package constant + +const ( + COMPLETION_BASH_USE = "bash" + COMPLETION_BASH_HELP_TEMPLATE = `πŸ”§πŸš Generate the autocompletion script for the bash shell. + +This script depends on the "bash-completion" package. +If it is not installed already, you can install it via your OS's package manager. + +To load completions in your current shell session: + + source <(jrp completion bash) + +To load completions for every new session, execute once: + + - 🐧 Linux: + + jrp completion bash > /etc/bash_completion.d/jrp + + - 🍎 macOS: + + jrp completion bash > $(brew --prefix)/etc/bash_completion.d/jrp + +You will need to start a new shell for this setup to take effect. + +Usage: + jrp completion bash [flags] + +Flags: + -h, --help 🀝 help for bash +` +) diff --git a/cmd/constant/completion_fish.go b/cmd/constant/completion_fish.go new file mode 100644 index 00000000..dc54dace --- /dev/null +++ b/cmd/constant/completion_fish.go @@ -0,0 +1,23 @@ +package constant + +const ( + COMPLETION_FISH_USE = "fish" + COMPLETION_FISH_HELP_TEMPLATE = `πŸ”§πŸŸ Generate the autocompletion script for the fish shell. + +To load completions in your current shell session: + + jrp completion fish | source + +To load completions for every new session, execute once: + + jrp completion fish > ~/.config/fish/completions/jrp.fish + +You will need to start a new shell for this setup to take effect. + +Usage: + jrp completion fish [flags] + +Flags: + -h, --help 🀝 help for fish +` +) diff --git a/cmd/constant/completion_powershell.go b/cmd/constant/completion_powershell.go new file mode 100644 index 00000000..de8a0e21 --- /dev/null +++ b/cmd/constant/completion_powershell.go @@ -0,0 +1,19 @@ +package constant + +const ( + COMPLETION_POWERSHELL_USE = "powershell" + COMPLETION_POWERSHELL_HELP_TEMPLATE = `πŸ”§πŸͺŸ Generate the autocompletion script for the powershell shell. + +To load completions in your current shell session: + + jrp completion powershell | Out-String | Invoke-Expression + +To load completions for every new session, add the output of the above command to your powershell profile. + +Usage: + jrp completion powershell [flags] + +Flags: + -h, --help 🀝 help for powershell +` +) diff --git a/cmd/constant/completion_zsh.go b/cmd/constant/completion_zsh.go new file mode 100644 index 00000000..7ec83abd --- /dev/null +++ b/cmd/constant/completion_zsh.go @@ -0,0 +1,35 @@ +package constant + +const ( + COMPLETION_ZSH_USE = "zsh" + COMPLETION_ZSH_HELP_TEMPLATE = `πŸ”§πŸ§™ Generate the autocompletion script for the zsh shell. + +If shell completion is not already enabled in your environment you will need to enable it. + +You can execute the following once: + + echo "autoload -U compinit; compinit" >> ~/.zshrc + +To load completions in your current shell session: + + source <(jrp completion zsh) + +To load completions for every new session, execute once: + + - 🐧 Linux: + + jrp completion zsh > "${fpath[1]}/_jrp" + + - 🍎 macOS: + + jrp completion zsh > $(brew --prefix)/share/zsh/site-functions/_jrp + +You will need to start a new shell for this setup to take effect. + +Usage: + jrp completion zsh [flags] + +Flags: + -h, --help 🀝 help for zsh +` +) diff --git a/cmd/constant/download.go b/cmd/constant/download.go new file mode 100644 index 00000000..193fc625 --- /dev/null +++ b/cmd/constant/download.go @@ -0,0 +1,29 @@ +package constant + +const ( + DOWNLOAD_USE = "download" + DOWNLOAD_HELP_TEMPLATE = `πŸ“¦ Download WordNet Japan sqlite database file from the official web site. + +You have to download WordNet Japan sqlite database file to use jrp at first. +jrp will download archive file from the official web site and decompress it to the database file. + +You can set the directory of the database file to the environment variable "JRP_WNJPN_DB_FILE_DIR". +The default directory is "~/.local/share/jrp" ("$XDG_DATA_HOME/jrp"). + +Usage: + jrp download [flags] + jrp dl [flags] + jrp d [flags] + +Flags: + -h, --help 🀝 help for download +` + DOWNLOAD_MESSAGE_DOWNLOADING = " πŸ“¦ Downloading WordNet Japan sqlite database file from the official web site..." + DOWNLOAD_MESSAGE_SUCCEEDED = "βœ… Downloaded successfully! Now, you are ready to use jrp!" + DOWNLOAD_MESSAGE_FAILED = "❌ Failed to download... Please try again later..." + DOWNLOAD_MESSAGE_ALREADY_DOWNLOADED = "βœ… You are already ready to use jrp!" +) + +func GetDownloadAliases() []string { + return []string{"dl", "d"} +} diff --git a/constant/download_test.go b/cmd/constant/download_test.go similarity index 83% rename from constant/download_test.go rename to cmd/constant/download_test.go index b23a7cf9..5e2f979b 100644 --- a/constant/download_test.go +++ b/cmd/constant/download_test.go @@ -19,7 +19,7 @@ func TestGetDownloadAliases(t *testing.T) { got := GetDownloadAliases() for i, v := range got { if v != tt.want[i] { - t.Errorf("GetDownloadAliases()[%v] = %v, want %v", i, got, tt.want) + t.Errorf("GetDownloadAliases()[%v] =\n%v, want\n%v", i, got, tt.want) } } }) diff --git a/cmd/constant/favorite.go b/cmd/constant/favorite.go new file mode 100644 index 00000000..be5a1f91 --- /dev/null +++ b/cmd/constant/favorite.go @@ -0,0 +1,62 @@ +package constant + +const ( + FAVORITE_USE = "favorite" + FAVORITE_HELP_TEMPLATE = `⭐ Manage the favorited phrase(s) of the history of "generate" command. + +You can favorite (add) generated phrase(s) with its ID(s). +Also, You can show, remove, search and clear the phrase(s) you favorited. + +You can specify how many phrases to show by flag "-n" or "--number" or a number argument. +jrp will get the most recent favorited phrase(s). +If you don't specify the number of phrases, jrp will show the most recent 10 phrases by default. +If both are provided, the larger number takes precedence. + +Also, you can show all phrases in the favorite by flag "-a" or "--all". +If you use the flag, the number flag or argument will be ignored. + +Usage: + jrp favorite [flag] + jrp fav [flag] + jrp f [flag] + jrp favorite [command] + jrp fav [command] + jrp f [command] + +Available Subommands: + show β­πŸ“– Show the favorited phrase(s). You can abbreviate "show" sub command. ("jrp favorite" and "jrp favorite show" are the same.) + add β­πŸ“Œ Favorite (add) phrase(s) in the history of the "generate" command. + search β­πŸ” Search the favorited phrase(s). + remove ⭐🧹 Remove the favorited phrase(s). + clear ⭐✨ Clear the favorited phrase(s). + +Flags: + -n, --number πŸ“ number how many phrases to show (default 10, e.g: 50) + -a, --all πŸ“ show all favorited phrase(s) + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for favorite + +Arguments: + number πŸ“ number how many phrases to show (default 10, e.g: 50) + +Use "jrp favorite [command] --help" for more information about a command. +` + FAVORITE_FLAG_NUMBER = "number" + FAVORITE_FLAG_NUMBER_SHORTHAND = "n" + FAVORITE_FLAG_NUMBER_DEFAULT = 10 + FAVORITE_FLAG_NUMBER_DESCRIPTION = "number how many phrases to show" + FAVORITE_FLAG_ALL = "all" + FAVORITE_FLAG_ALL_SHORTHAND = "a" + FAVORITE_FLAG_ALL_DEFAULT = false + FAVORITE_FLAG_ALL_DESCRIPTION = "show all phrases in the favorite" + FAVORITE_FLAG_PLAIN = "plain" + FAVORITE_FLAG_PLAIN_SHORTHAND = "P" + FAVORITE_FLAG_PLAIN_DEFAULT = false + FAVORITE_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + FAVORITE_MESSAGE_NO_FAVORITE_FOUND = "⚑ No favorited phrase(s) found..." +) + +func GetFavoriteAliases() []string { + return []string{"fav", "f"} +} diff --git a/cmd/constant/favorite_add.go b/cmd/constant/favorite_add.go new file mode 100644 index 00000000..4bf26a46 --- /dev/null +++ b/cmd/constant/favorite_add.go @@ -0,0 +1,34 @@ +package constant + +const ( + FAVORITE_ADD_USE = "add" + FAVORITE_ADD_HELP_TEMPLATE = `β­πŸ“Œ Favorite (add) phrase(s) in the history of the "generate" command. + +You can specify the phrase(s) to favorite with ID argument(s). +You have to get ID from the "history" command. +Multiple ID's can be specified separated by spaces. + +This command can make the phrase(s) easier to find. +And you will not be able to remove the phrase(s) with executing "history remove" and "history clear". + +Usage: + jrp favorite add [flag] + jrp favorite ad [flag] + jrp favorite a [flag] + +Flags: + -h, --help 🀝 help for add + +Arguments: + ID πŸ†” add the favorite by the ID (e.g: 1 2 3) +` + FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED = "⚑ No ID argument(s) specified..." + FAVORITE_ADD_MESSAGE_ADDED_SUCCESSFULLY = "βœ… Favorite successfully!" + FAVORITE_ADD_MESSAGE_ADDED_FAILURE = "❌ Failed favorite..." + FAVORITE_ADD_MESSAGE_ADDED_NONE = "⚑ No phrase(s) to favorite..." + FAVORITE_ADD_MESSAGE_ADDED_NOT_ALL = "⚑ Some phrase(s) are not favorited because the id does not exist or have already favorited..." +) + +func GetFavoriteAddAliases() []string { + return []string{"ad", "a"} +} diff --git a/cmd/constant/favorite_add_test.go b/cmd/constant/favorite_add_test.go new file mode 100644 index 00000000..86b825ae --- /dev/null +++ b/cmd/constant/favorite_add_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetFavoriteAddAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"ad", "a"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetFavoriteAddAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetFavoriteAddAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/favorite_clear.go b/cmd/constant/favorite_clear.go new file mode 100644 index 00000000..fa2f82ab --- /dev/null +++ b/cmd/constant/favorite_clear.go @@ -0,0 +1,34 @@ +package constant + +const ( + FAVORITE_CLEAR_USE = "clear" + FAVORITE_CLEAR_HELP_TEMPLATE = `⭐✨ Clear the favorited phrase(s). + +You can clear all favorited phrase(s). +This is the same as the "favorite remove -a" command. +This does not clear the history of the "generate" command, just clear the favorited status. + +Usage: + jrp favorite clear [flag] + jrp favorite cl [flag] + jrp favorite c [flag] + +Flags: + -no-confirm 🚫 do not confirm before clearing the history + -h, --help 🀝 help for clear +` + FAVORITE_CLEAR_FLAG_NO_CONFIRM = "no-confirm" + FAVORITE_CLEAR_FLAG_NO_CONFIRM_SHORTHAND = "" + FAVORITE_CLEAR_FLAG_NO_CONFIRM_DEFAULT = false + FAVORITE_CLEAR_FLAG_NO_CONFIRM_DESCRIPTION = "do not confirm before clearing the favorited phrase(s)" + + FAVORITE_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY = "βœ… Cleared favorited phrase(s) successfully!" + FAVORITE_CLEAR_MESSAGE_CLEARED_NONE = "⚑ No favorited phrase(s) to clear..." + FAVORITE_CLEAR_MESSAGE_CLEARED_FAIRULE = "❌ Failed to clear favorited phraase(s)..." + FAVORITE_CLEAR_MESSAGE_CLEAR_CANCELED = "🚫 Cancelled clearing the favorited phrases(s)." + FAVORITE_CLEAR_PROMPT_LABEL = "Proceed with clearing the favorited phrases(s)? [y/N]" +) + +func GetFavoriteClearAliases() []string { + return []string{"cl", "c"} +} diff --git a/cmd/constant/favorite_clear_test.go b/cmd/constant/favorite_clear_test.go new file mode 100644 index 00000000..6e5faa26 --- /dev/null +++ b/cmd/constant/favorite_clear_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetFavoriteClearAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"cl", "c"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetFavoriteClearAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetFavoriteClearAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/favorite_remove.go b/cmd/constant/favorite_remove.go new file mode 100644 index 00000000..013baf23 --- /dev/null +++ b/cmd/constant/favorite_remove.go @@ -0,0 +1,48 @@ +package constant + +const ( + FAVORITE_REMOVE_USE = "remove" + FAVORITE_REMOVE_HELP_TEMPLATE = `⭐🧹 Remove the favorited phrase(s). + +You can specify the favorited phrase(s) to remove with ID argument(s). +You have to get ID from the "history" command. +Multiple ID's can be specified separated by spaces. +This does not remove the history of the "generate" command, just remove the favorited status. + +Also, you can remove all favorited phrase(s) by flag "-a" or "--all". +This is the same as the "favorite clear" command. + +Usage: + jrp favorite remove [flag] + jrp favorite rm [flag] + jrp favorite r [flag] + +Flags: + -a, --all ✨ remove all favorited phrase(s) + -no-confirm 🚫 do not confirm before removing all the favorited phrase(s) + -h, --help 🀝 help for remove + +Arguments: + ID πŸ†” remove the favorite by the ID (e.g: 1 2 3) +` + FAVORITE_REMOVE_FLAG_ALL = "all" + FAVORITE_REMOVE_FLAG_ALL_SHORTHAND = "a" + FAVORITE_REMOVE_FLAG_ALL_DEFAULT = false + FAVORITE_REMOVE_FLAG_ALL_DESCRIPTION = "remove all favorited phrase(s)" + FAVORITE_REMOVE_FLAG_NO_CONFIRM = "no-confirm" + FAVORITE_REMOVE_FLAG_NO_CONFIRM_SHORTHAND = "" + FAVORITE_REMOVE_FLAG_NO_CONFIRM_DEFAULT = false + FAVORITE_REMOVE_FLAG_NO_CONFIRM_DESCRIPTION = "do not confirm before removing all the favorited phrase(s)" + + FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED = "⚑ No ID argument(s) specified..." + FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY = "βœ… Removed favorited phrase(s) successfully!" + FAVORITE_REMOVE_MESSAGE_REMOVED_FAILURE = "❌ Failed to remove favorited phrase(s)..." + FAVORITE_REMOVE_MESSAGE_REMOVED_NONE = "⚑ No favorited phrase(s) to remove..." + FAVORITE_REMOVE_MESSAGE_REMOVED_NOT_ALL = "⚑ Some favorited phrase(s) was not removed because the id does not exist or have not favorited..." + FAVORITE_REMOVE_MESSAGE_REMOVE_ALL_CANCELED = "🚫 Cancelled removing all the favorited phrase(s)." + FAVORITE_REMOVE_PROMPT_REMOVE_ALL_LABEL = "Proceed with removing all the favorited phrase(s)? [y/N]" +) + +func GetFavoriteRemoveAliases() []string { + return []string{"rm", "r"} +} diff --git a/cmd/constant/favorite_remove_test.go b/cmd/constant/favorite_remove_test.go new file mode 100644 index 00000000..8e5fcc26 --- /dev/null +++ b/cmd/constant/favorite_remove_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetFavoriteRemoveAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"rm", "r"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetFavoriteRemoveAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetFavoriteRemoveAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/favorite_search.go b/cmd/constant/favorite_search.go new file mode 100644 index 00000000..e819b357 --- /dev/null +++ b/cmd/constant/favorite_search.go @@ -0,0 +1,57 @@ +package constant + +const ( + FAVORITE_SEARCH_USE = "search" + FAVORITE_SEARCH_HELP_TEMPLATE = `β­πŸ” Search the favorited phrase(s). + +You can search favorited phrase(s) with keyword argument(s). +Multiple keywords are separated by a space. + +If you want to search favorited phrase(s) by AND condition, you can use flag "-A" or "--and". +OR condition is by default. + +You can specify how many results to show with flag "-n" or "--number". +If you don't specify the number of phrases, jrp will show the most recent 10 results by default. + +Also, you can show all results by flag "-a" or "--all". +If you use the flag, the number flag will be ignored. + +Usage: + jrp favorite search [flag] + jrp favorite se [flag] + jrp favorite S [flag] + +Flags: + -A, --and 🧠 search phrase(s) by AND condition + -n, --number πŸ“ number how many results to show (default 10, e.g: 50) + -a, --all πŸ“ show all results + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for search + +Arguments: + keywords πŸ’¬ search phrase(s) by keywords (multiple keywords are separated by space) +` + FAVORITE_SEARCH_FLAG_AND = "and" + FAVORITE_SEARCH_FLAG_AND_SHORTHAND = "A" + FAVORITE_SEARCH_FLAG_AND_DEFAULT = false + FAVORITE_SEARCH_FLAG_AND_DESCRIPTION = "search phrase(s) by AND condition" + FAVORITE_SEARCH_FLAG_NUMBER = "number" + FAVORITE_SEARCH_FLAG_NUMBER_SHORTHAND = "n" + FAVORITE_SEARCH_FLAG_NUMBER_DEFAULT = 10 + FAVORITE_SEARCH_FLAG_NUMBER_DESCRIPTION = "number how many results to show" + FAVORITE_SEARCH_FLAG_ALL = "all" + FAVORITE_SEARCH_FLAG_ALL_SHORTHAND = "a" + FAVORITE_SEARCH_FLAG_ALL_DEFAULT = false + FAVORITE_SEARCH_FLAG_ALL_DESCRIPTION = "show all phrases in the favorite" + FAVORITE_SEARCH_FLAG_PLAIN = "plain" + FAVORITE_SEARCH_FLAG_PLAIN_SHORTHAND = "P" + FAVORITE_SEARCH_FLAG_PLAIN_DEFAULT = false + FAVORITE_SEARCH_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + FAVORITE_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED = "⚑ No keyword(s) provided..." + FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND = "⚑ No results found..." +) + +func GetFavoriteSearchAliases() []string { + return []string{"se", "S"} +} diff --git a/cmd/constant/favorite_search_test.go b/cmd/constant/favorite_search_test.go new file mode 100644 index 00000000..80220275 --- /dev/null +++ b/cmd/constant/favorite_search_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetFavoriteSearchAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"se", "S"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetFavoriteSearchAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetFavoriteSearchAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/favorite_show.go b/cmd/constant/favorite_show.go new file mode 100644 index 00000000..00f9ce31 --- /dev/null +++ b/cmd/constant/favorite_show.go @@ -0,0 +1,47 @@ +package constant + +const ( + FAVORITE_SHOW_USE = "show" + FAVORITE_SHOW_HELP_TEMPLATE = `β­πŸ“– Show the favorited phrase(s). + +You can specify how many phrases to show by flag "-n" or "--number" or a number argument. +jrp will get the most recent favorited phrase(s). +If you don't specify the number of phrases, jrp will show the most recent 10 phrases by default. +If both are provided, the larger number takes precedence. + +Also, you can show all phrases in the favorite by flag "-a" or "--all". +If you use the flag, the number flag or argument will be ignored. + +Usage: + jrp favorite show [flag] + jrp favorite sh [flag] + jrp favorite s [flag] + +Flags: + -n, --number πŸ“ number how many phrases to show (default 10, e.g: 50) + -a, --all πŸ“ show all favorited phrase(s) + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for show + +Arguments: + number πŸ“ number how many phrases to show (default 10, e.g: 50) +` + FAVORITE_SHOW_FLAG_NUMBER = "number" + FAVORITE_SHOW_FLAG_NUMBER_SHORTHAND = "n" + FAVORITE_SHOW_FLAG_NUMBER_DEFAULT = 10 + FAVORITE_SHOW_FLAG_NUMBER_DESCRIPTION = "number how many phrases to show" + FAVORITE_SHOW_FLAG_ALL = "all" + FAVORITE_SHOW_FLAG_ALL_SHORTHAND = "a" + FAVORITE_SHOW_FLAG_ALL_DEFAULT = false + FAVORITE_SHOW_FLAG_ALL_DESCRIPTION = "show all phrases in the favorite" + FAVORITE_SHOW_FLAG_PLAIN = "plain" + FAVORITE_SHOW_FLAG_PLAIN_SHORTHAND = "P" + FAVORITE_SHOW_FLAG_PLAIN_DEFAULT = false + FAVORITE_SHOW_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND = "⚑ No favorited phrase(s) found..." +) + +func GetFavoriteShowAliases() []string { + return []string{"sh", "s"} +} diff --git a/cmd/constant/favorite_show_test.go b/cmd/constant/favorite_show_test.go new file mode 100644 index 00000000..476ddec2 --- /dev/null +++ b/cmd/constant/favorite_show_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetFavoriteShowAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"sh", "s"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetFavoriteShowAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetFavoriteShowAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/favorite_test.go b/cmd/constant/favorite_test.go new file mode 100644 index 00000000..51205968 --- /dev/null +++ b/cmd/constant/favorite_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetFavoriteAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"fav", "f"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetFavoriteAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetFavoriteAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/generate.go b/cmd/constant/generate.go new file mode 100644 index 00000000..f011d93b --- /dev/null +++ b/cmd/constant/generate.go @@ -0,0 +1,60 @@ +package constant + +const ( + GENERATE_USE = "generate" + GENARETE_HELP_TEMPLATE = `✨ Generate Japanese random phrase(s). + +You can specify how many phrases to generate by flag "-n" or "--number" or a number argument. +If both are provided, the larger number takes precedence. + +And you can specify the prefix or suffix of the phrase(s) to generate +by the flag "-p" or "--prefix" and "-s" or "--suffix". + +Usage: + jrp generate [flags] + jrp gen [flags] + jrp g [flags] + +Flags: + -n, --number πŸ”’ number of phrases to generate (default 1, e.g: 10) + -p --prefix πŸ’¬ prefix of phrase(s) to generate + -s --suffix πŸ’¬ suffix of phrase(s) to generate + -d --dry-run πŸ§ͺ generate phrase(s) without saving to the history + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for generate + +Arguments: + number πŸ”’ number of phrases to generate (default 1, e.g: 10) +` + GENERATE_FLAG_NUMBER = "number" + GENERATE_FLAG_NUMBER_SHORTHAND = "n" + GENERATE_FLAG_NUMBER_DEFAULT = 1 + GENERATE_FLAG_NUMBER_DESCRIPTION = "number of phrases to generate" + GENERATE_FLAG_PREFIX = "prifix" + GENERATE_FLAG_PREFIX_SHORTHAND = "p" + GENERATE_FLAG_PREFIX_DEFAULT = "" + GENERATE_FLAG_PREFIX_DESCRIPTION = "prefix of phrase(s) to generate" + GENERATE_FLAG_SUFFIX = "suffix" + GENERATE_FLAG_SUFFIX_SHORTHAND = "s" + GENERATE_FLAG_SUFFIX_DEFAULT = "" + GENERATE_FLAG_SUFFIX_DESCRIPTION = "suffix of phrase(s) to generate" + GENERATE_FLAG_DRY_RUN = "dry-run" + GENERATE_FLAG_DRY_RUN_SHORTHAND = "d" + GENERATE_FLAG_DRY_RUN_DEFAULT = false + GENERATE_FLAG_DRY_RUN_DESCRIPTION = "generate phrase(s) without saving to the history" + GENERATE_FLAG_PLAIN = "plain" + GENERATE_FLAG_PLAIN_SHORTHAND = "P" + GENERATE_FLAG_PLAIN_DEFAULT = false + GENERATE_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + GENERATE_MESSAGE_GENERATE_FAILURE = "❌ Failed to generate the phrase(s)..." + GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED = "⚑ You have to execute \"download\" to use jrp..." + GENERATE_MESSAGE_NOTIFY_USE_ONLY_ONE = "⚑ You can use only one of prefix or suffix..." + GENERATE_MESSAGE_SAVED_FAILURE = "❌ Failed to save the history..." + GENERATE_MESSAGE_SAVED_NONE = "⚑ No phrase(s) to save to the history..." + GENERATE_MESSAGE_SAVED_NOT_ALL = "⚑ Some phrase(s) are not saved to the history..." +) + +func GetGenerateAliases() []string { + return []string{"gen", "g"} +} diff --git a/constant/generate_test.go b/cmd/constant/generate_test.go similarity index 83% rename from constant/generate_test.go rename to cmd/constant/generate_test.go index 6a70afd3..41d2ade1 100644 --- a/constant/generate_test.go +++ b/cmd/constant/generate_test.go @@ -19,7 +19,7 @@ func TestGetGenerateAliases(t *testing.T) { got := GetGenerateAliases() for i, v := range got { if v != tt.want[i] { - t.Errorf("GetDownloadAliases()[%v] = %v, want %v", i, got, tt.want) + t.Errorf("GetDownloadAliases()[%v] =\n%v, want\n%v", i, got, tt.want) } } }) diff --git a/cmd/constant/godoc.go b/cmd/constant/godoc.go new file mode 100644 index 00000000..f6d65294 --- /dev/null +++ b/cmd/constant/godoc.go @@ -0,0 +1,2 @@ +// Package constant provides constants for jrp. +package constant diff --git a/cmd/constant/history.go b/cmd/constant/history.go new file mode 100644 index 00000000..3a8558df --- /dev/null +++ b/cmd/constant/history.go @@ -0,0 +1,60 @@ +package constant + +const ( + HISTORY_USE = "history" + HISTORY_HELP_TEMPLATE = `πŸ“œ Manage the history of the "generate" command. + +You can show, remove, search and clear the history of the "generate" command. + +You can specify how many phrases to show by flag "-n" or "--number" or a number argument. +jrp will get the most recent phrase(s) from the history. +If you don't specify the number of phrases, jrp will show the most recent 10 phrases by default. +If both are provided, the larger number takes precedence. + +Also, you can show all phrases in the history by flag "-a" or "--all". +If you use the flag, the number flag or argument will be ignored. + +Usage: + jrp history [flag] + jrp hist [flag] + jrp h [flag] + jrp history [command] + jrp hist [command] + jrp h [command] + +Available Subommands: + show πŸ“œπŸ“– Show the history of the "generate" command. You can abbreviate "show" sub command. ("jrp history" and "jrp history show" are the same.) + search πŸ“œπŸ” Search the history of the "generate" command. + remove πŸ“œπŸ§Ή Remove the history of the "generate" command. + clear πŸ“œβœ¨ Clear the history of the "generate" command. + +Flags: + -n, --number πŸ“ number how many phrases to show (default 10, e.g: 50) + -a, --all πŸ“ show all history + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for history + +Arguments: + number πŸ“ number how many phrases to show (default 10, e.g: 50) + +Use "jrp history [command] --help" for more information about a command. +` + HISTORY_FLAG_NUMBER = "number" + HISTORY_FLAG_NUMBER_SHORTHAND = "n" + HISTORY_FLAG_NUMBER_DEFAULT = 10 + HISTORY_FLAG_NUMBER_DESCRIPTION = "number how many phrases to show" + HISTORY_FLAG_ALL = "all" + HISTORY_FLAG_ALL_SHORTHAND = "a" + HISTORY_FLAG_ALL_DEFAULT = false + HISTORY_FLAG_ALL_DESCRIPTION = "show all phrases in the history" + HISTORY_FLAG_PLAIN = "plain" + HISTORY_FLAG_PLAIN_SHORTHAND = "P" + HISTORY_FLAG_PLAIN_DEFAULT = false + HISTORY_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + HISTORY_MESSAGE_NO_HISTORY_FOUND = "⚑ No history found..." +) + +func GetHistoryAliases() []string { + return []string{"hist", "h"} +} diff --git a/cmd/constant/history_clear.go b/cmd/constant/history_clear.go new file mode 100644 index 00000000..5d7b6a91 --- /dev/null +++ b/cmd/constant/history_clear.go @@ -0,0 +1,39 @@ +package constant + +const ( + HISTORY_CLEAR_USE = "clear" + HISTORY_CLEAR_HELP_TEMPLATE = `πŸ“œβœ¨ Clear the history of the "generate" command. + +You can clear all history. +This is the same as the "history remove -a" command. +Also, you can clear the history even if it is favorited by using the "-f" or ""--force" flag. + +Usage: + jrp history clear [flag] + jrp history cl [flag] + jrp history c [flag] + +Flags: + -f, --force πŸ’ͺ clear all the history even if it is favorited + -no-confirm 🚫 do not confirm before clearing the history + -h, --help 🀝 help for clear +` + HISTORY_CLEAR_FLAG_FORCE = "force" + HISTORY_CLEAR_FLAG_FORCE_SHORTHAND = "f" + HISTORY_CLEAR_FLAG_FORCE_DEFAULT = false + HISTORY_CLEAR_FLAG_FORCE_DESCRIPTION = "clear all the history even if it is favorited" + HISTORY_CLEAR_FLAG_NO_CONFIRM = "no-confirm" + HISTORY_CLEAR_FLAG_NO_CONFIRM_SHORTHAND = "" + HISTORY_CLEAR_FLAG_NO_CONFIRM_DEFAULT = false + HISTORY_CLEAR_FLAG_NO_CONFIRM_DESCRIPTION = "do not confirm before clearing the history" + + HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY = "βœ… Cleared history successfully!" + HISTORY_CLEAR_MESSAGE_CLEARED_NONE = "⚑ No history to clear..." + HISTORY_CLEAR_MESSAGE_CLEARED_FAIRULE = "❌ Failed to clear history..." + HISTORY_CLEAR_MESSAGE_CLEAR_CANCELED = "🚫 Cancelled clearing the history." + HISTORY_CLEAR_PROMPT_LABEL = "Proceed with clearing the history? [y/N]" +) + +func GetHistoryClearAliases() []string { + return []string{"cl", "c"} +} diff --git a/cmd/constant/history_clear_test.go b/cmd/constant/history_clear_test.go new file mode 100644 index 00000000..73ae541a --- /dev/null +++ b/cmd/constant/history_clear_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetHistoryClearAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"cl", "c"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetHistoryClearAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetHistoryClearAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/history_remove.go b/cmd/constant/history_remove.go new file mode 100644 index 00000000..08bdf214 --- /dev/null +++ b/cmd/constant/history_remove.go @@ -0,0 +1,54 @@ +package constant + +const ( + HISTORY_REMOVE_USE = "remove" + HISTORY_REMOVE_HELP_TEMPLATE = `πŸ“œπŸ§Ή Remove the history of the "generate" command. + +You can specify the history to remove with ID argument(s). +You have to get ID from the "history" command. +Multiple ID's can be specified separated by spaces. + +You can remove all history by flag "-a" or "--all". +This is the same as the "history clear" command. + +Also, you can remove the history even if it is favorited by using the "-f" or ""--force" flag. + +Usage: + jrp history remove [flag] + jrp history rm [flag] + jrp history r [flag] + +Flags: + -a, --all ✨ remove all history + -f, --force πŸ’ͺ remove the history even if it is favorited + -no-confirm 🚫 do not confirm before removing all the history + -h, --help 🀝 help for remove + +Arguments: + ID πŸ†” remove the history by the ID (e.g: 1 2 3) +` + HISTORY_REMOVE_FLAG_ALL = "all" + HISTORY_REMOVE_FLAG_ALL_SHORTHAND = "a" + HISTORY_REMOVE_FLAG_ALL_DEFAULT = false + HISTORY_REMOVE_FLAG_ALL_DESCRIPTION = "remove all history" + HISTORY_REMOVE_FLAG_FORCE = "force" + HISTORY_REMOVE_FLAG_FORCE_SHORTHAND = "f" + HISTORY_REMOVE_FLAG_FORCE_DEFAULT = false + HISTORY_REMOVE_FLAG_FORCE_DESCRIPTION = "force remove the history even if it is favorited" + HISTORY_REMOVE_FLAG_NO_CONFIRM = "no-confirm" + HISTORY_REMOVE_FLAG_NO_CONFIRM_SHORTHAND = "" + HISTORY_REMOVE_FLAG_NO_CONFIRM_DEFAULT = false + HISTORY_REMOVE_FLAG_NO_CONFIRM_DESCRIPTION = "do not confirm before removing all the history" + + HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED = "⚑ No ID argument(s) specified..." + HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY = "βœ… Removed the history successfully!" + HISTORY_REMOVE_MESSAGE_REMOVED_FAILURE = "❌ Failed to remove the history..." + HISTORY_REMOVE_MESSAGE_REMOVED_NONE = "⚑ No history to remove..." + HISTORY_REMOVE_MESSAGE_REMOVED_NOT_ALL = "⚑ Some phtase(s) was not removed because the id does not exist or have already favorited..." + HISTORY_REMOVE_MESSAGE_REMOVE_ALL_CANCELED = "🚫 Cancelled removing all the history." + HISTORY_REMOVE_PROMPT_REMOVE_ALL_LABEL = "Proceed with removing all the history? [y/N]" +) + +func GetHistoryRemoveAliases() []string { + return []string{"rm", "r"} +} diff --git a/cmd/constant/history_remove_test.go b/cmd/constant/history_remove_test.go new file mode 100644 index 00000000..c383c5b2 --- /dev/null +++ b/cmd/constant/history_remove_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetHistoryRemoveAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"rm", "r"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetHistoryRemoveAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetHistoryRemoveAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/history_search.go b/cmd/constant/history_search.go new file mode 100644 index 00000000..a1898825 --- /dev/null +++ b/cmd/constant/history_search.go @@ -0,0 +1,57 @@ +package constant + +const ( + HISTORY_SEARCH_USE = "search" + HISTORY_SEARCH_HELP_TEMPLATE = `πŸ“œπŸ” Search the history of the "generate" command. + +You can search phrase(s) with keyword argument(s). +Multiple keywords are separated by a space. + +If you want to search phrase(s) by AND condition, you can use flag "-A" or "--and". +OR condition is by default. + +You can specify how many results to show with flag "-n" or "--number". +If you don't specify the number of phrases, jrp will show the most recent 10 results by default. + +Also, you can show all results in the history by flag "-a" or "--all". +If you use the flag, the number flag will be ignored. + +Usage: + jrp history search [flag] + jrp history se [flag] + jrp history S [flag] + +Flags: + -A, --and 🧠 search phrase(s) by AND condition + -n, --number πŸ“ number how many results to show (default 10, e.g: 50) + -a, --all πŸ“ show all results + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for search + +Arguments: + keywords πŸ’¬ search phrase(s) by keywords (multiple keywords are separated by space) +` + HISTORY_SEARCH_FLAG_AND = "and" + HISTORY_SEARCH_FLAG_AND_SHORTHAND = "A" + HISTORY_SEARCH_FLAG_AND_DEFAULT = false + HISTORY_SEARCH_FLAG_AND_DESCRIPTION = "search phrase(s) by AND condition" + HISTORY_SEARCH_FLAG_NUMBER = "number" + HISTORY_SEARCH_FLAG_NUMBER_SHORTHAND = "n" + HISTORY_SEARCH_FLAG_NUMBER_DEFAULT = 10 + HISTORY_SEARCH_FLAG_NUMBER_DESCRIPTION = "number how many results to show" + HISTORY_SEARCH_FLAG_ALL = "all" + HISTORY_SEARCH_FLAG_ALL_SHORTHAND = "a" + HISTORY_SEARCH_FLAG_ALL_DEFAULT = false + HISTORY_SEARCH_FLAG_ALL_DESCRIPTION = "show all phrases in the history" + HISTORY_SEARCH_FLAG_PLAIN = "plain" + HISTORY_SEARCH_FLAG_PLAIN_SHORTHAND = "P" + HISTORY_SEARCH_FLAG_PLAIN_DEFAULT = false + HISTORY_SEARCH_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + HISTORY_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED = "⚑ No keyword(s) provided..." + HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND = "⚑ No results found..." +) + +func GetHistorySearchAliases() []string { + return []string{"se", "S"} +} diff --git a/cmd/constant/history_search_test.go b/cmd/constant/history_search_test.go new file mode 100644 index 00000000..3c521819 --- /dev/null +++ b/cmd/constant/history_search_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetHistorySearchAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"se", "S"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetHistorySearchAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetHistorySearchAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/history_show.go b/cmd/constant/history_show.go new file mode 100644 index 00000000..e24df153 --- /dev/null +++ b/cmd/constant/history_show.go @@ -0,0 +1,47 @@ +package constant + +const ( + HISTORY_SHOW_USE = "show" + HISTORY_SHOW_HELP_TEMPLATE = `πŸ“œπŸ“– Show the history of the "generate" command. + +You can specify how many phrases to show by flag "-n" or "--number" or a number argument. +jrp will get the most recent phrase(s) from the history. +If you don't specify the number of phrases, jrp will show the most recent 10 phrases by default. +If both are provided, the larger number takes precedence. + +Also, you can show all phrases in the history by flag "-a" or "--all". +If you use the flag, the number flag or argument will be ignored. + +Usage: + jrp history show [flag] + jrp history sh [flag] + jrp history s [flag] + +Flags: + -n, --number πŸ“ number how many phrases to show (default 10, e.g: 50) + -a, --all πŸ“ show all history + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for show + +Arguments: + number πŸ“ number how many phrases to show (default 10, e.g: 50) +` + HISTORY_SHOW_FLAG_NUMBER = "number" + HISTORY_SHOW_FLAG_NUMBER_SHORTHAND = "n" + HISTORY_SHOW_FLAG_NUMBER_DEFAULT = 10 + HISTORY_SHOW_FLAG_NUMBER_DESCRIPTION = "number how many phrases to show" + HISTORY_SHOW_FLAG_ALL = "all" + HISTORY_SHOW_FLAG_ALL_SHORTHAND = "a" + HISTORY_SHOW_FLAG_ALL_DEFAULT = false + HISTORY_SHOW_FLAG_ALL_DESCRIPTION = "show all phrases in the history" + HISTORY_SHOW_FLAG_PLAIN = "plain" + HISTORY_SHOW_FLAG_PLAIN_SHORTHAND = "P" + HISTORY_SHOW_FLAG_PLAIN_DEFAULT = false + HISTORY_SHOW_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + HISTORY_SHOW_MESSAGE_NO_HISTORY_FOUND = "⚑ No history found..." +) + +func GetHistoryShowAliases() []string { + return []string{"sh", "s"} +} diff --git a/cmd/constant/history_show_test.go b/cmd/constant/history_show_test.go new file mode 100644 index 00000000..3497ccc5 --- /dev/null +++ b/cmd/constant/history_show_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetHistoryShowAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"sh", "s"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetHistoryShowAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetHistoryShowAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/history_test.go b/cmd/constant/history_test.go new file mode 100644 index 00000000..e098f5c3 --- /dev/null +++ b/cmd/constant/history_test.go @@ -0,0 +1,27 @@ +package constant + +import ( + "testing" +) + +func TestGetHistoryAliases(t *testing.T) { + tests := []struct { + name string + want []string + }{ + { + name: "positive testing", + want: []string{"hist", "h"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := GetHistoryAliases() + for i, v := range got { + if v != tt.want[i] { + t.Errorf("GetHistoryAliases()[%v] =\n%v, want\n%v", i, got, tt.want) + } + } + }) + } +} diff --git a/cmd/constant/root.go b/cmd/constant/root.go new file mode 100644 index 00000000..3fd6dbf0 --- /dev/null +++ b/cmd/constant/root.go @@ -0,0 +1,69 @@ +package constant + +const ( + ROOT_USE = "jrp" + ROOT_HELP_TEMPLATE = `🎲 jrp is the CLI tool to generate Japanese random phrase(s). + +You can generate Japanese random phrase(s). + +You can specify how many phrases to generate by flag "-n" or "--number" or a number argument. +If both are provided, the larger number takes precedence. + +And you can specify the prefix or suffix of the phrase(s) to generate +by the flag "-p" or "--prefix" and "-s" or "--suffix". + +Usage: + jrp [flags] + jrp [command] + +Available Subcommands: + download, dl, d πŸ“¦ Download WordNet Japan sqlite database file from the official web site. + generate, gen, g ✨ Generate Japanese random phrase(s). You can abbreviate "generate" sub command. ("jrp" and "jrp generate" are the same.) + history, hist, h πŸ“œ Manage the history of the "generate" command. + favorite, fav, f ⭐ Manage the favorited phrase(s) of the history of "generate" command. + help 🀝 Help of jrp. + completion πŸ”§ Generate the autocompletion script for the specified shell. + version πŸ”– Show the version of jrp. + +Flags: + -n, --number πŸ”’ number of phrases to generate (default 1, e.g: 10) + -p --prefix πŸ’¬ prefix of phrase(s) to generate + -s --suffix πŸ’¬ suffix of phrase(s) to generate + -d --dry-run πŸ§ͺ generate phrase(s) without saving to the history + -P, --plain πŸ“ plain text output instead of table output + -h, --help 🀝 help for jrp + -v, --version πŸ”– version for jrp + +Arguments: + number πŸ”’ number of phrases to generate (e.g: 10) + +Use "jrp [command] --help" for more information about a command. +` + ROOT_FLAG_NUMBER = "number" + ROOT_FLAG_NUMBER_SHORTHAND = "n" + ROOT_FLAG_NUMBER_DEFAULT = 1 + ROOT_FLAG_NUMBER_DESCRIPTION = "number of phrases to generate" + ROOT_FLAG_PREFIX = "prifix" + ROOT_FLAG_PREFIX_SHORTHAND = "p" + ROOT_FLAG_PREFIX_DEFAULT = "" + ROOT_FLAG_PREFIX_DESCRIPTION = "prefix of phrase(s) to generate" + ROOT_FLAG_SUFFIX = "suffix" + ROOT_FLAG_SUFFIX_SHORTHAND = "s" + ROOT_FLAG_SUFFIX_DEFAULT = "" + ROOT_FLAG_SUFFIX_DESCRIPTION = "suffix of phrase(s) to generate" + ROOT_FLAG_DRY_RUN = "dry-run" + ROOT_FLAG_DRY_RUN_SHORTHAND = "d" + ROOT_FLAG_DRY_RUN_DEFAULT = false + ROOT_FLAG_DRY_RUN_DESCRIPTION = "generate phrase(s) without saving to the history" + ROOT_FLAG_PLAIN = "plain" + ROOT_FLAG_PLAIN_SHORTHAND = "P" + ROOT_FLAG_PLAIN_DEFAULT = false + ROOT_FLAG_PLAIN_DESCRIPTION = "plain text output instead of table output" + + ROOT_MESSAGE_GENERATE_FAILURE = "❌ Failed to generate the phrase(s)..." + ROOT_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED = "⚑ You have to execute \"download\" to use jrp..." + ROOT_MESSAGE_NOTIFY_USE_ONLY_ONE = "⚑ You can use only one of prefix or suffix..." + ROOT_MESSAGE_SAVED_FAILURE = "❌ Failed to save the history..." + ROOT_MESSAGE_SAVED_NONE = "⚑ No phrase(s) to save to the history..." + ROOT_MESSAGE_SAVED_NOT_ALL = "⚑ Some phrase(s) are not saved to the history..." +) diff --git a/constant/version.go b/cmd/constant/version.go similarity index 57% rename from constant/version.go rename to cmd/constant/version.go index 178fda34..19ef5d30 100644 --- a/constant/version.go +++ b/cmd/constant/version.go @@ -1,6 +1,7 @@ package constant const ( + VERSION_USE = "version" VERSION_HELP_TEMPLATE = `πŸ”– Show the version of jrp. Simply show the version of jrp. @@ -11,10 +12,5 @@ Usage: Flags: -h, --help 🀝 help for version ` - VERSION_USE = "version" - VERSION_SHORT = "πŸ”– Show the version of jrp." - VERSION_LONG = `πŸ”– Show the version of jrp. - -Simply show the version of jrp.` VERSION_MESSAGE_TEMPLATE = "jrp version %s" ) diff --git a/cmd/download.go b/cmd/download.go index 20819360..d53c1b64 100644 --- a/cmd/download.go +++ b/cmd/download.go @@ -1,39 +1,61 @@ package cmd import ( - "io" - "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" - "github.com/yanosea/jrp/internal/fs" - "github.com/yanosea/jrp/internal/gzip" - "github.com/yanosea/jrp/internal/httpclient" - "github.com/yanosea/jrp/internal/iomanager" - "github.com/yanosea/jrp/internal/spinnerservice" - "github.com/yanosea/jrp/internal/usermanager" - "github.com/yanosea/jrp/logic" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/downloader" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/spinner" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" ) -type DownloadOption struct { - Out io.Writer - ErrOut io.Writer - Downloader logic.Downloader +// downloadOption is the struct for download command. +type downloadOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Downloader downloader.Downloadable + SpinnerProxy spinnerproxy.Spinner + Utility utility.UtilityInterface } -func NewDownloadCommand(g *GlobalOption) *cobra.Command { - o := &DownloadOption{ - Out: g.Out, - ErrOut: g.ErrOut, +// NewDownloadCommand creates a new download command. +func NewDownloadCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &downloadOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Utility: g.Utility, } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.Downloader = downloader.New( + filepathproxy.New(), + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osproxy.New(), + g.Utility, + ) + o.SpinnerProxy = spinnerproxy.New() - cmd := &cobra.Command{ - Use: constant.DOWNLOAD_USE, - Aliases: constant.GetDownloadAliases(), - Short: constant.DOWNLOAD_SHORT, - Long: constant.DOWNLOAD_LONG, - RunE: o.DownloadRunE, - } + cobraproxy := cobraproxy.New() + cmd := cobraproxy.NewCommand() + + cmd.FieldCommand.Use = constant.DOWNLOAD_USE + cmd.FieldCommand.Aliases = constant.GetDownloadAliases() + cmd.FieldCommand.RunE = o.downloadRunE cmd.SetOut(o.Out) cmd.SetErr(o.ErrOut) @@ -42,21 +64,50 @@ func NewDownloadCommand(g *GlobalOption) *cobra.Command { return cmd } -func (o *DownloadOption) DownloadRunE(_ *cobra.Command, _ []string) error { - o.Downloader = logic.NewDBFileDownloader( - usermanager.OSUserProvider{}, - fs.OsFileManager{}, - httpclient.DefaultHTTPClient{}, - iomanager.DefaultIOHelper{}, - gzip.DefaultGzipHandler{}, - spinnerservice.NewRealSpinnerService(), - ) - return o.Download() +// downloadRunE is the function that is called when the download command is executed. +func (o *downloadOption) downloadRunE(_ *cobra.Command, _ []string) error { + // get wnjpn db file dir path + wnJpnDBFileDirPath, err := o.DBFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(wnJpnDBFileDirPath); err != nil { + return err + } + + return o.download(wnJpnDBFileDirPath) } -func (o *DownloadOption) Download() error { - if err := o.Downloader.Download(); err != nil { +// download downloads wnjpn db file. +func (o *downloadOption) download(wnJpnDBFileDirPath string) error { + // start spinner + spinner := o.SpinnerProxy.NewSpinner() + spinner.Reverse() + if err := spinner.SetColor("yellow"); err != nil { return err } - return nil + colorProxy := colorproxy.New() + spinner.SetSuffix(colorProxy.YellowString(constant.DOWNLOAD_MESSAGE_DOWNLOADING)) + spinner.Start() + + // download with downloader + res, err := o.Downloader.DownloadWNJpnDBFile(wnJpnDBFileDirPath) + spinner.Stop() + o.writeDownloadResult(res) + + return err +} + +// writeDownloadResult writes the download result. +func (o *downloadOption) writeDownloadResult(result downloader.DownloadStatus) { + colorProxy := colorproxy.New() + if result == downloader.DownloadedFailed { + o.Utility.PrintlnWithWriter(o.ErrOut, colorProxy.RedString(constant.DOWNLOAD_MESSAGE_FAILED)) + } else if result == downloader.DownloadedAlready { + o.Utility.PrintlnWithWriter(o.Out, colorProxy.GreenString(constant.DOWNLOAD_MESSAGE_ALREADY_DOWNLOADED)) + } else { + o.Utility.PrintlnWithWriter(o.Out, colorProxy.GreenString(constant.DOWNLOAD_MESSAGE_SUCCEEDED)) + } } diff --git a/cmd/download_test.go b/cmd/download_test.go index 05cdbbaf..48137dbd 100644 --- a/cmd/download_test.go +++ b/cmd/download_test.go @@ -1,113 +1,443 @@ -package cmd_test +package cmd import ( "errors" - "os" "testing" - gomock "go.uber.org/mock/gomock" + "github.com/spf13/cobra" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/downloader" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/spinner" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" - "github.com/yanosea/jrp/cmd" - "github.com/yanosea/jrp/internal/fs" - "github.com/yanosea/jrp/internal/gzip" - "github.com/yanosea/jrp/internal/httpclient" - "github.com/yanosea/jrp/internal/iomanager" - "github.com/yanosea/jrp/internal/spinnerservice" - "github.com/yanosea/jrp/internal/usermanager" - "github.com/yanosea/jrp/logic" - - mock_downloader "github.com/yanosea/jrp/mock/downloader" + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/mock/app/proxy/spinner" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" ) func TestNewDownloadCommand(t *testing.T) { type args struct { - globalOption *cmd.GlobalOption + g *GlobalOption } tests := []struct { - name string - args args + name string + args args + wantError bool }{ { name: "positive testing", - args: args{globalOption: &cmd.GlobalOption{Out: os.Stdout, ErrOut: os.Stderr}}, + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := cmd.NewDownloadCommand(tt.args.globalOption) - if got == nil { - t.Errorf("NewDownloadCommand() : returned nil") + got := NewDownloadCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewDownloadCommand().Execute() : error =\n%v", err) } }) } } -func TestDownloadRunE(t *testing.T) { +func Test_downloadOption_downloadRunE(t *testing.T) { + osProxy := osproxy.New() + globalOption := NewGlobalOption(fmtproxy.New(), osProxy, strconvproxy.New()) + filepathProxy := filepathproxy.New() + dbFilePathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFilePathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFilePathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Downloader downloader.Downloadable + SpinnerProxy spinnerproxy.Spinner + Utility utility.UtilityInterface + } type args struct { - o cmd.DownloadOption + in0 *cobra.Command + in1 []string } tests := []struct { - name string - args args + name string + fields fields + args args + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() }{ { name: "positive testing", - args: args{o: cmd.DownloadOption{Out: os.Stdout, ErrOut: os.Stderr, Downloader: logic.NewDBFileDownloader(usermanager.OSUserProvider{}, fs.OsFileManager{}, httpclient.DefaultHTTPClient{}, iomanager.DefaultIOHelper{}, gzip.DefaultGzipHandler{}, spinnerservice.NewRealSpinnerService())}}, + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + DBFileDirPathProvider: dbFilePathProvider, + Downloader: downloader.New( + filepathproxy.New(), + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osproxy.New(), + globalOption.Utility, + ), + SpinnerProxy: spinnerproxy.New(), + Utility: globalOption.Utility, + }, + args: args{ + in0: nil, + in1: nil, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFilePathProvider.GetWNJpnDBFileDirPath() failed)", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + DBFileDirPathProvider: nil, + Downloader: downloader.New( + filepathproxy.New(), + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osproxy.New(), + globalOption.Utility, + ), + SpinnerProxy: spinnerproxy.New(), + Utility: globalOption.Utility, + }, + args: args{ + in0: nil, + in1: nil, + }, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetWNJpnDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetWNJpnDBFileDirPath() failed")) + tt.DBFileDirPathProvider = mockDBFileDirPathProvider + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExist() failed)", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + DBFileDirPathProvider: dbFilePathProvider, + Downloader: downloader.New( + filepathproxy.New(), + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osproxy.New(), + globalOption.Utility, + ), + SpinnerProxy: spinnerproxy.New(), + Utility: nil, + }, + args: args{ + in0: nil, + in1: nil, + }, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(gomock.Any()).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + tt.Utility = mockUtility + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - err := tt.args.o.DownloadRunE(nil, nil) - if err != nil { - t.Errorf("DownloadRunE() : error = %v", err) + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &downloadOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + Downloader: tt.fields.Downloader, + SpinnerProxy: tt.fields.SpinnerProxy, + Utility: tt.fields.Utility, + } + if err := o.downloadRunE(tt.args.in0, tt.args.in1); (err != nil) != tt.wantErr { + t.Errorf("downloadOption.downloadRunE() : error =\n%v, wantErr \n%v", err, tt.wantErr) + } + if tt.cleanup != nil { + tt.cleanup() } }) } } -func TestDownload(t *testing.T) { +func Test_downloadOption_download(t *testing.T) { + osProxy := osproxy.New() + globalOption := NewGlobalOption(fmtproxy.New(), osProxy, strconvproxy.New()) + filepathProxy := filepathproxy.New() + dbFilePathProvider := dbfiledirpathprovider.New( + filepathProxy, + osProxy, + userproxy.New(), + ) + wnJpnDBFileDirPath, err := dbFilePathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFilePathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Downloader downloader.Downloadable + SpinnerProxy spinnerproxy.Spinner + Utility utility.UtilityInterface + } type args struct { - o cmd.DownloadOption + wnJpnDBFileDirPath string } tests := []struct { name string + fields fields args args wantErr bool - setup func(mockCtrl *gomock.Controller, tt *args) + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() }{ { - name: "positive testing", - args: args{o: cmd.DownloadOption{Out: os.Stdout, ErrOut: os.Stderr, Downloader: logic.NewDBFileDownloader(usermanager.OSUserProvider{}, fs.OsFileManager{}, httpclient.DefaultHTTPClient{}, iomanager.DefaultIOHelper{}, gzip.DefaultGzipHandler{}, spinnerservice.NewRealSpinnerService())}}, + name: "positive testing", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + DBFileDirPathProvider: dbFilePathProvider, + Downloader: downloader.New( + filepathproxy.New(), + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osproxy.New(), + globalOption.Utility, + ), + SpinnerProxy: spinnerproxy.New(), + Utility: globalOption.Utility, + }, + args: args{ + wnJpnDBFileDirPath: wnJpnDBFileDirPath, + }, wantErr: false, - setup: nil, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, }, { - name: "negative testing (Download() fails)", - args: args{o: cmd.DownloadOption{Out: os.Stdout, ErrOut: os.Stderr, Downloader: nil}}, + name: "negative testing (SpinnerProxy.SetColor() failed)", + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + DBFileDirPathProvider: dbFilePathProvider, + Downloader: downloader.New( + filepathproxy.New(), + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osproxy.New(), + globalOption.Utility, + ), + SpinnerProxy: nil, + Utility: globalOption.Utility, + }, + args: args{ + wnJpnDBFileDirPath: wnJpnDBFileDirPath, + }, wantErr: true, - setup: func(mockCtrl *gomock.Controller, tt *args) { - md := mock_downloader.NewMockDownloader(mockCtrl) - md.EXPECT().Download().Return(errors.New("failed to download db file")) - tt.o.Downloader = md + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockSpinnerInstance := mockspinnerproxy.NewMockSpinnerInstanceInterface(mockCtrl) + mockSpinnerInstance.EXPECT().Reverse() + mockSpinnerInstance.EXPECT().SetColor(gomock.Any()).Return(errors.New("SpinnerProxy.SetColor() failed")) + mockSpinnerProxy := mockspinnerproxy.NewMockSpinner(mockCtrl) + mockSpinnerProxy.EXPECT().NewSpinner().Return(mockSpinnerInstance) + tt.SpinnerProxy = mockSpinnerProxy + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } }, }, } - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - if tt.setup != nil { - tt.setup(ctrl, &tt.args) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &downloadOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + Downloader: tt.fields.Downloader, + SpinnerProxy: tt.fields.SpinnerProxy, + Utility: tt.fields.Utility, + } + if err := o.download(tt.args.wnJpnDBFileDirPath); (err != nil) != tt.wantErr { + t.Errorf("downloadOption.download() : error =\n%v, wantErr =\n%v", err, tt.wantErr) } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_downloadOption_writeDownloadResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + globalOption := NewGlobalOption(fmtproxy.New(), osProxy, strconvproxy.New()) + globalOption.Out = capturer.OutBuffer + globalOption.ErrOut = capturer.ErrBuffer + o := &downloadOption{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Utility: globalOption.Utility, + } + colorProxy := colorproxy.New() - err := tt.args.o.Download() + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is DownloadedFailed)", + fields: fields{ + t: t, + fnc: func() { + o.writeDownloadResult(downloader.DownloadedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.DOWNLOAD_MESSAGE_FAILED) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is DownloadedAlready)", + fields: fields{ + t: t, + fnc: func() { + o.writeDownloadResult(downloader.DownloadedAlready) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.DOWNLOAD_MESSAGE_ALREADY_DOWNLOADED) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is DownloadedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + o.writeDownloadResult(downloader.DownloadedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.DOWNLOAD_MESSAGE_SUCCEEDED) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) if (err != nil) != tt.wantErr { - t.Errorf("Download() : error = %v, wantErr = %v", err, tt.wantErr) + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("DownloadOption.writeDownloadResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("DownloadOption.writeDownloadResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } }) } diff --git a/cmd/favorite.go b/cmd/favorite.go new file mode 100644 index 00000000..bfa4f698 --- /dev/null +++ b/cmd/favorite.go @@ -0,0 +1,180 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// favoriteOption is the struct for favorite command. +type favoriteOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + All bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + Utility utility.UtilityInterface +} + +// NewFavoriteCommand creates a new favorite command. +func NewFavoriteCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &favoriteOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.FAVORITE_USE + cmd.FieldCommand.Aliases = constant.GetFavoriteAliases() + cmd.FieldCommand.Args = cobra.MaximumNArgs(1) + cmd.FieldCommand.RunE = o.favoriteRunE + + cmd.PersistentFlags().IntVarP( + &o.Number, + constant.FAVORITE_FLAG_NUMBER, + constant.FAVORITE_FLAG_NUMBER_SHORTHAND, + constant.FAVORITE_FLAG_NUMBER_DEFAULT, + constant.FAVORITE_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.All, + constant.FAVORITE_FLAG_ALL, + constant.FAVORITE_FLAG_ALL_SHORTHAND, + constant.FAVORITE_FLAG_ALL_DEFAULT, + constant.FAVORITE_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.Plain, + constant.FAVORITE_FLAG_PLAIN, + constant.FAVORITE_FLAG_PLAIN_SHORTHAND, + constant.FAVORITE_FLAG_PLAIN_DEFAULT, + constant.FAVORITE_FLAG_PLAIN_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.FAVORITE_HELP_TEMPLATE) + + cmd.AddCommand( + NewFavoriteShowCommand(g), + NewFavoriteAddCommand(g), + NewFavoriteRemoveCommand(g, promptuiproxy.New()), + NewFavoriteSearchCommand(g), + NewFavoriteClearCommand(g, promptuiproxy.New()), + ) + + return cmd +} + +// favoriteRunE is the function that is called when the favorite command is executed. +func (o *favoriteOption) favoriteRunE(_ *cobra.Command, _ []string) error { + strconvProxy := strconvproxy.New() + if len(o.Args) <= 1 { + // if no argument is given, set the default value to args + o.Args = []string{constant.FAVORITE_USE, strconvProxy.Itoa(constant.FAVORITE_FLAG_NUMBER_DEFAULT)} + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.favorite(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME)) +} + +// favorite shows the favorite. +func (o *favoriteOption) favorite(jrpDBFilePath string) error { + var favorites []model.Jrp + var err error + if o.All { + // if all flag is set, get all favorite + favorites, err = o.JrpRepository.GetAllFavorite(jrpDBFilePath) + } else { + if o.Number != constant.FAVORITE_FLAG_NUMBER_DEFAULT && o.Number >= 1 { + // if number flag is set, get favorites with the given number + favorites, err = o.JrpRepository.GetFavoriteWithNumber(jrpDBFilePath, o.Number) + } else { + strconvProxy := strconvproxy.New() + // get favorite with the given number + favorites, err = o.JrpRepository.GetFavoriteWithNumber( + jrpDBFilePath, + // get the larger number between the given number flag and the largest number that can be converted from the args + o.Utility.GetLargerNumber( + o.Number, + o.Utility.GetMaxConvertibleString( + o.Args, + strconvProxy.Itoa(constant.FAVORITE_FLAG_NUMBER_DEFAULT), + ), + ), + ) + } + } + o.writeFavoriteResult(favorites) + + return err +} + +// writeFavoriteResult writes the favorite result. +func (o *favoriteOption) writeFavoriteResult(favorites []model.Jrp) { + if len(favorites) != 0 { + if o.Plain { + for _, favorite := range favorites { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, favorite.Phrase) + } + } else { + // if plain flag is not set, write the favorite as a table + o.JrpWriter.WriteAsTable(o.Out, favorites) + } + } else { + // if no favorite is found, write the message + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.FAVORITE_MESSAGE_NO_FAVORITE_FOUND)) + } +} diff --git a/cmd/favorite_add.go b/cmd/favorite_add.go new file mode 100644 index 00000000..e256eaa8 --- /dev/null +++ b/cmd/favorite_add.go @@ -0,0 +1,131 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// favoriteAddOption is the struct for favorite add command. +type favoriteAddOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + Utility utility.UtilityInterface +} + +// NewFavoriteAddCommand creates a new favorite add command. +func NewFavoriteAddCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &favoriteAddOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.FAVORITE_ADD_USE + cmd.FieldCommand.Aliases = constant.GetFavoriteAddAliases() + cmd.FieldCommand.RunE = o.favoriteAddRunE + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.FAVORITE_ADD_HELP_TEMPLATE) + + return cmd +} + +// favoriteAddRunE is the function that is called when the favorite add command is executed. +func (o *favoriteAddOption) favoriteAddRunE(_ *cobra.Command, _ []string) error { + if len(o.Args) <= 2 { + // if no arguments is given, set default value to args + o.Args = []string{constant.FAVORITE_USE, constant.FAVORITE_ADD_USE, ""} + } + + // set ID + strconvProxy := strconvproxy.New() + var IDs []int + for _, arg := range o.Args[2:] { + if id, err := strconvProxy.Atoi(arg); err != nil { + continue + } else { + IDs = append(IDs, id) + } + } + if len(IDs) == 0 { + // if no ID is specified, print write and return + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED)) + return nil + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.favoriteAdd(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME), IDs) +} + +// favoriteAdd adds the specified ID to favorite. +func (o *favoriteAddOption) favoriteAdd(jrpDBFilePath string, IDs []int) error { + // if ID is specified, add to favorite + res, err := o.JrpRepository.AddFavoriteByIDs(jrpDBFilePath, IDs) + o.writeFavoriteAddResult(res) + + return err +} + +// writeFavoriteAddResult writes the result of favorite add. +func (o *favoriteAddOption) writeFavoriteAddResult(result repository.AddStatus) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == repository.AddedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.FAVORITE_ADD_MESSAGE_ADDED_FAILURE) + } else if result == repository.AddedNone { + message = colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + } else if result == repository.AddedNotAll { + message = colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NOT_ALL) + } else { + message = colorProxy.GreenString(constant.FAVORITE_ADD_MESSAGE_ADDED_SUCCESSFULLY) + } + o.Utility.PrintlnWithWriter(out, message) +} diff --git a/cmd/favorite_add_test.go b/cmd/favorite_add_test.go new file mode 100644 index 00000000..879d87ab --- /dev/null +++ b/cmd/favorite_add_test.go @@ -0,0 +1,2853 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewFavoriteAddCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewFavoriteAddCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewFavoriteAddCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func Test_favoriteAddOption_favoriteAddRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantJrps []model.Jrp + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_ADD_MESSAGE_ADDED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_ADD_MESSAGE_ADDED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + fmt.Printf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: mockUtility, + } + if err := favoriteAddOption.favoriteAddRunE(nil, nil); err != nil { + fmt.Printf("favoriteAddOption.favoriteAddRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteAddOption.favoriteAddRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteAddOption.favoriteAddRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + favoritedJrps, err := jrpRepository.GetAllFavorite(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(favoritedJrps, tt.wantJrps) { + t.Errorf("favoriteAddOption.favoriteAddRunE() : favoritedJrps =\n%v, want =\n%v", favoritedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteAddOption_favoriteAdd(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantJrps []model.Jrp + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{2}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_ADD_MESSAGE_ADDED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{3}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_ADD_MESSAGE_ADDED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{2, 3}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{2}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{3}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "1", "2"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "add", "2", "3"}, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteAddOption.favoriteAdd(jrpDBFilePath, []int{2, 3}); err != nil { + t.Errorf("favoriteAddOption.favoriteAdd() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.AddAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteAddOption.favoriteAdd() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteAddOption.favoriteAdd() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + favoritedJrps, err := jrpRepository.GetAllFavorite(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(favoritedJrps, tt.wantJrps) { + t.Errorf("favoriteAddOption.favoriteAdd() : favoritedJrps =\n%v, want =\n%v", favoritedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteAddOption_writeFavoriteAddResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is AddedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + favoriteAddOption.writeFavoriteAddResult(repository.AddedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_ADD_MESSAGE_ADDED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is AddedFailed)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + favoriteAddOption.writeFavoriteAddResult(repository.AddedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.FAVORITE_ADD_MESSAGE_ADDED_FAILURE) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is AddedNone)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + favoriteAddOption.writeFavoriteAddResult(repository.AddedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NONE) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is AddedNotAll)", + fields: fields{ + t: t, + fnc: func() { + favoriteAddOption := &favoriteAddOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + favoriteAddOption.writeFavoriteAddResult(repository.AddedNotAll) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_ADD_MESSAGE_ADDED_NOT_ALL) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("favoriteAddOption.writeFavoriteAddResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("favoriteAddOption.writeFavoriteAddResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/favorite_clear.go b/cmd/favorite_clear.go new file mode 100644 index 00000000..4c4d4a0c --- /dev/null +++ b/cmd/favorite_clear.go @@ -0,0 +1,134 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// favoriteClearOption is the struct for favorite clear command. +type favoriteClearOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + NoConfirm bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + PromptuiProxy promptuiproxy.Promptui + Utility utility.UtilityInterface +} + +// NewFavoriteClearCommand creates a new favorite clear command. +func NewFavoriteClearCommand(g *GlobalOption, promptuiProxy promptuiproxy.Promptui) *cobraproxy.CommandInstance { + o := &favoriteClearOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.PromptuiProxy = promptuiProxy + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.FAVORITE_CLEAR_USE + cmd.FieldCommand.Aliases = constant.GetFavoriteClearAliases() + cmd.FieldCommand.RunE = o.favoriteClearRunE + + cmd.PersistentFlags().BoolVarP( + &o.NoConfirm, + constant.FAVORITE_CLEAR_FLAG_NO_CONFIRM, + constant.FAVORITE_CLEAR_FLAG_NO_CONFIRM_SHORTHAND, + constant.FAVORITE_CLEAR_FLAG_NO_CONFIRM_DEFAULT, + constant.FAVORITE_CLEAR_FLAG_NO_CONFIRM_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.FAVORITE_CLEAR_HELP_TEMPLATE) + + return cmd +} + +// favoriteClearRunE is the function that is called when the favorite clear command is executed. +func (o *favoriteClearOption) favoriteClearRunE(_ *cobra.Command, _ []string) error { + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + // write prompt if no-confirm option is not set + if !o.NoConfirm { + colorProxy := colorproxy.New() + prompt := o.PromptuiProxy.NewPrompt() + prompt.SetLabel(colorProxy.YellowString(constant.FAVORITE_CLEAR_PROMPT_LABEL)) + result, err := prompt.Run() + if err != nil { + return err + } + if result != "y" && result != "Y" { + // write canceled message and do nothing + o.Utility.PrintlnWithWriter(o.Out, constant.FAVORITE_CLEAR_MESSAGE_CLEAR_CANCELED) + return nil + } + } + + filepathProxy := filepathproxy.New() + return o.favoriteClear(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME)) +} + +// favoriteClear clears all favorite. +func (o *favoriteClearOption) favoriteClear(jrpDBFilePath string) error { + // remove all favorite + res, err := o.JrpRepository.RemoveFavoriteAll(jrpDBFilePath) + o.writeFavoriteClearResult(res) + + return err +} + +// writeFavoriteClearResult writes the result of favorite clear. +func (o *favoriteClearOption) writeFavoriteClearResult(result repository.RemoveStatus) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == repository.RemovedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_FAIRULE) + } else if result == repository.RemovedNone { + message = colorProxy.YellowString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_NONE) + } else { + message = colorProxy.GreenString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + } + o.Utility.PrintlnWithWriter(out, message) +} diff --git a/cmd/favorite_clear_test.go b/cmd/favorite_clear_test.go new file mode 100644 index 00000000..d337ed89 --- /dev/null +++ b/cmd/favorite_clear_test.go @@ -0,0 +1,1219 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/database/jrp/repository" + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/mock/app/proxy/promptui" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewFavoriteClearCommand(t *testing.T) { + type args struct { + g *GlobalOption + promptuiProxy promptuiproxy.Promptui + } + tests := []struct { + name string + args args + wantError bool + setup func(*gomock.Controller, *args) + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + promptuiProxy: nil, + }, + wantError: false, + setup: func(mockCtrl *gomock.Controller, args *args) { + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()) + mockPromptInstance.EXPECT().Run().Return("y", nil) + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance) + args.promptuiProxy = mockPromptuiProxy + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.args) + } + got := NewFavoriteClearCommand(tt.args.g, tt.args.promptuiProxy) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewFavoriteClearCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func Test_favoriteClearOption_favoriteClearRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + noMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + noMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + noMockPromptInstance.EXPECT().Run().Return("n", nil) + noMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + noMockPromptuiProxy.EXPECT().NewPrompt().Return(noMockPromptInstance) + errMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + errMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + errMockPromptInstance.EXPECT().Run().Return("", errors.New("PromptInstance.Run() failed")) + errMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + errMockPromptuiProxy.EXPECT().NewPrompt().Return(errMockPromptInstance) + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantJrps []model.Jrp + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (answering not yes on the prompt)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: noMockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: constant.FAVORITE_CLEAR_MESSAGE_CLEAR_CANCELED + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (set no-confirm option)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: nil, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are not favorited)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favorited)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited and other is not)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + t.Errorf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + fmt.Printf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: mockUtility, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + fmt.Printf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (PromptInstance.run() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: errMockPromptuiProxy, + Utility: util, + } + if err := favoriteClearOption.favoriteClearRunE(nil, nil); err != nil { + fmt.Printf("favoriteClearOption.favoriteClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteClearOption.favoriteClearRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteClearOption.favoriteClearRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + favoritedJrps, err := jrpRepository.GetAllFavorite(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(favoritedJrps, tt.wantJrps) { + t.Errorf("favoriteClearOption.favoriteClearRunE() : favoritedJrps =\n%v, want =\n%v", favoritedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteClearOption_favoriteClear(t *testing.T) { + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + NoConfirm bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + PromptuiProxy promptuiproxy.Promptui + Utility utility.UtilityInterface + } + type args struct { + jrpDBFilePath string + } + tests := []struct { + name string + fields fields + args args + wantJrps []model.Jrp + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are not favorited)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favorited)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited and other is not)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.RemoveFavoriteAll() failed)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: nil, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockJrpRepository := mockrepository.NewMockJrpRepositoryInterface(mockCtrl) + mockJrpRepository.EXPECT().RemoveFavoriteAll(gomock.Any()).Return(repository.RemovedFailed, errors.New("JrpRepository.RemoveFavoriteAll() : error")) + tt.JrpRepository = mockJrpRepository + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &favoriteClearOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + NoConfirm: false, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + JrpRepository: tt.fields.JrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: tt.fields.Utility, + } + if err := o.favoriteClear(tt.args.jrpDBFilePath); (err != nil) != tt.wantErr { + t.Errorf("favoriteClearOption.favoriteClear() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + favoritedJrps, err := jrpRepository.GetAllFavorite(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(favoritedJrps, tt.wantJrps) { + t.Errorf("favoriteClearOption.favoriteClear() : favoritedJrps =\n%v, want =\n%v", favoritedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteClearOption_writeFavoriteClearResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is RemovedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + favoriteClearOption.writeFavoriteClearResult(repository.RemovedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is RemovedFailed)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + favoriteClearOption.writeFavoriteClearResult(repository.RemovedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_FAIRULE) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is RemovedNone)", + fields: fields{ + t: t, + fnc: func() { + favoriteClearOption := &favoriteClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + favoriteClearOption.writeFavoriteClearResult(repository.RemovedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("favoriteClearOption.writeFavoriteClearResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("favoriteClearOption.writeFavoriteClearResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/favorite_remove.go b/cmd/favorite_remove.go new file mode 100644 index 00000000..b0ef813b --- /dev/null +++ b/cmd/favorite_remove.go @@ -0,0 +1,176 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// favoriteRemoveOption is the struct for favorite remove command. +type favoriteRemoveOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + All bool + NoConfirm bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + PromptuiProxy promptuiproxy.Promptui + Utility utility.UtilityInterface +} + +// NewFavoriteRemoveCommand creates a new favorite remove command. +func NewFavoriteRemoveCommand(g *GlobalOption, promptuiProxy promptuiproxy.Promptui) *cobraproxy.CommandInstance { + o := &favoriteRemoveOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.PromptuiProxy = promptuiProxy + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.FAVORITE_REMOVE_USE + cmd.FieldCommand.Aliases = constant.GetFavoriteRemoveAliases() + cmd.FieldCommand.RunE = o.favoriteRemoveRunE + + cmd.PersistentFlags().BoolVarP( + &o.All, + constant.FAVORITE_REMOVE_FLAG_ALL, + constant.FAVORITE_REMOVE_FLAG_ALL_SHORTHAND, + constant.FAVORITE_REMOVE_FLAG_ALL_DEFAULT, + constant.FAVORITE_REMOVE_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.NoConfirm, + constant.FAVORITE_REMOVE_FLAG_NO_CONFIRM, + constant.FAVORITE_REMOVE_FLAG_NO_CONFIRM_SHORTHAND, + constant.FAVORITE_REMOVE_FLAG_NO_CONFIRM_DEFAULT, + constant.FAVORITE_REMOVE_FLAG_NO_CONFIRM_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.FAVORITE_REMOVE_HELP_TEMPLATE) + + return cmd +} + +// favoriteRemoveRunE is the function that is called when the favorite remove command is executed. +func (o *favoriteRemoveOption) favoriteRemoveRunE(_ *cobra.Command, _ []string) error { + if len(o.Args) <= 2 { + // if no arguments is given, set default value to args + o.Args = []string{constant.FAVORITE_USE, constant.FAVORITE_REMOVE_USE, ""} + } + + // set ID + strconvProxy := strconvproxy.New() + var IDs []int + if !o.All { + for _, arg := range o.Args[2:] { + if id, err := strconvProxy.Atoi(arg); err != nil { + continue + } else { + IDs = append(IDs, id) + } + } + } + if len(IDs) == 0 && !o.All { + // if no ID is specified, print write and return + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED)) + return nil + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + // write prompt if all flag is set and no-confirm option is not set + if o.All && !o.NoConfirm { + colorProxy := colorproxy.New() + prompt := o.PromptuiProxy.NewPrompt() + prompt.SetLabel(colorProxy.YellowString(constant.FAVORITE_REMOVE_PROMPT_REMOVE_ALL_LABEL)) + result, err := prompt.Run() + if err != nil { + return err + } + if result != "y" && result != "Y" { + // write canceled message and do nothing + o.Utility.PrintlnWithWriter(o.Out, constant.FAVORITE_REMOVE_MESSAGE_REMOVE_ALL_CANCELED) + return nil + } + } + + filepathProxy := filepathproxy.New() + return o.favoriteRemove(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME), IDs) +} + +// favoriteRemove removes favorite by IDs. +func (o *favoriteRemoveOption) favoriteRemove(jrpDBFilePath string, IDs []int) error { + var res repository.RemoveStatus + var err error + if o.All { + // if all flag is set, remove all favorite + res, err = o.JrpRepository.RemoveFavoriteAll(jrpDBFilePath) + } else { + // if IDs are specified, remove favorite by IDs + res, err = o.JrpRepository.RemoveFavoriteByIDs(jrpDBFilePath, IDs) + } + o.writeFavoriteRemoveResult(res) + + return err +} + +// writeFavoriteRemoveResult writes the result of favorite remove. +func (o *favoriteRemoveOption) writeFavoriteRemoveResult(result repository.RemoveStatus) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == repository.RemovedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_FAILURE) + } else if result == repository.RemovedNone { + message = colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + } else if result == repository.RemovedNotAll { + message = colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NOT_ALL) + } else { + message = colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + } + o.Utility.PrintlnWithWriter(out, message) +} diff --git a/cmd/favorite_remove_test.go b/cmd/favorite_remove_test.go new file mode 100644 index 00000000..c047217f --- /dev/null +++ b/cmd/favorite_remove_test.go @@ -0,0 +1,5438 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/mock/app/proxy/promptui" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewFavoriteRemoveCommand(t *testing.T) { + type args struct { + g *GlobalOption + promptuiProxy promptuiproxy.Promptui + } + tests := []struct { + name string + args args + wantError bool + setup func(*gomock.Controller, *args) + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + promptuiProxy: nil, + }, + wantError: false, + setup: func(mockCtrl *gomock.Controller, args *args) { + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()) + mockPromptInstance.EXPECT().Run().Return("y", nil) + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance) + args.promptuiProxy = mockPromptuiProxy + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewFavoriteRemoveCommand(tt.args.g, tt.args.promptuiProxy) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewFavoriteRemoveCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func Test_favoriteRemoveOption_favoriteRemoveRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + noMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + noMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + noMockPromptInstance.EXPECT().Run().Return("n", nil) + noMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + noMockPromptuiProxy.EXPECT().NewPrompt().Return(noMockPromptInstance) + errMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + errMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + errMockPromptInstance.EXPECT().Run().Return("", errors.New("PromptInstance.Run() failed")) + errMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + errMockPromptuiProxy.EXPECT().NewPrompt().Return(errMockPromptInstance) + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantJrps []model.Jrp + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (answering not yes on the prompt)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: true, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: noMockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: constant.FAVORITE_REMOVE_MESSAGE_REMOVE_ALL_CANCELED + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (set no-confirm option)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: nil, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, ids in args matches a jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, ids in args matches jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, ids in args matches a jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, ids in args matches jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + fmt.Printf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: mockUtility, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + fmt.Printf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (PromptInstance.run() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: true, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: errMockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemoveRunE(nil, nil); err != nil { + fmt.Printf("favoriteRemoveOption.favoriteRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + favoritedJrps, err := jrpRepository.GetAllFavorite(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(favoritedJrps, tt.wantJrps) { + t.Errorf("favoriteRemoveOption.favoriteRemoveRunE() : favoritedJrps =\n%v, want =\n%v", favoritedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteRemoveOption_favoriteRemove(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantJrps []model.Jrp + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{3, 4}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, ids in args matches a jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2, 3}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{3, 4}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, ids in args matches jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2, 3}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{3, 4}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, ids in args matches a jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, not favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2, 3}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "3", "4"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{3, 4}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, ids in args matches jrps)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "1", "2"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, all, favorited, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "remove", "2", "3"}, + All: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := favoriteRemoveOption.favoriteRemove(jrpDBFilePath, []int{2, 3}); err != nil { + t.Errorf("favoriteRemoveOption.favoriteRemove() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteRemoveOption.favoriteRemove() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteRemoveOption.favoriteRemove() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + favoritedJrps, err := jrpRepository.GetAllFavorite(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllFavorite() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(favoritedJrps, tt.wantJrps) { + t.Errorf("favoriteRemoveOption.favoriteRemove() : favoritedJrps =\n%v, want =\n%v", favoritedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteRemoveOption_writeFavoriteRemoveResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is RemovedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + favoriteRemoveOption.writeFavoriteRemoveResult(repository.RemovedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is RemovedFailed)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + favoriteRemoveOption.writeFavoriteRemoveResult(repository.RemovedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_FAILURE) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is RemovedNone)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + favoriteRemoveOption.writeFavoriteRemoveResult(repository.RemovedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is RemovedNotAll)", + fields: fields{ + t: t, + fnc: func() { + favoriteRemoveOption := &favoriteRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + favoriteRemoveOption.writeFavoriteRemoveResult(repository.RemovedNotAll) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("favoriteRemoveOption.writeFavoriteRemoveResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("favoriteRemoveOption.writeFavoriteRemoveResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/favorite_search.go b/cmd/favorite_search.go new file mode 100644 index 00000000..bce7e1fd --- /dev/null +++ b/cmd/favorite_search.go @@ -0,0 +1,180 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// favoriteSearchOption is the struct for favorite search command. +type favoriteSearchOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + And bool + Number int + All bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + Utility utility.UtilityInterface +} + +// NewFavoriteSearchCommand creates a new favorite search command. +func NewFavoriteSearchCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &favoriteSearchOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.FAVORITE_SEARCH_USE + cmd.FieldCommand.Aliases = constant.GetFavoriteSearchAliases() + cmd.FieldCommand.RunE = o.favoriteSearchRunE + + cmd.PersistentFlags().BoolVarP( + &o.And, + constant.FAVORITE_SEARCH_FLAG_AND, + constant.FAVORITE_SEARCH_FLAG_AND_SHORTHAND, + constant.FAVORITE_SEARCH_FLAG_AND_DEFAULT, + constant.FAVORITE_SEARCH_FLAG_AND_DESCRIPTION, + ) + cmd.PersistentFlags().IntVarP(&o.Number, + constant.FAVORITE_SEARCH_FLAG_NUMBER, + constant.FAVORITE_SEARCH_FLAG_NUMBER_SHORTHAND, + constant.FAVORITE_SEARCH_FLAG_NUMBER_DEFAULT, + constant.FAVORITE_SEARCH_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.All, + constant.FAVORITE_SEARCH_FLAG_ALL, + constant.FAVORITE_SEARCH_FLAG_ALL_SHORTHAND, + constant.FAVORITE_SEARCH_FLAG_ALL_DEFAULT, + constant.FAVORITE_SEARCH_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.Plain, + constant.FAVORITE_SEARCH_FLAG_PLAIN, + constant.FAVORITE_SEARCH_FLAG_PLAIN_SHORTHAND, + constant.FAVORITE_SEARCH_FLAG_PLAIN_DEFAULT, + constant.FAVORITE_SEARCH_FLAG_PLAIN_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.FAVORITE_SEARCH_HELP_TEMPLATE) + + return cmd +} + +// favoriteSearchRunE is the function that is called when the favorite search command is executed. +func (o *favoriteSearchOption) favoriteSearchRunE(_ *cobra.Command, _ []string) error { + if len(o.Args) <= 2 { + // if no arguments is given, set default value to args + o.Args = []string{constant.FAVORITE_USE, constant.FAVORITE_SEARCH_USE, ""} + } + + // set keywords + var keywords []string + for _, arg := range o.Args[2:] { + if arg != "" { + keywords = append(keywords, arg) + } + } + if len(keywords) == 0 { + // if no keywords are provided, write message and return + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED)) + return nil + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.favoriteSearch(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME), keywords) +} + +// favoriteSearch searches favorite. +func (o *favoriteSearchOption) favoriteSearch(jrpDBFilePath string, keywords []string) error { + var favorites []model.Jrp + var err error + if o.All { + // if all flag is set, search all favorite + favorites, err = o.JrpRepository.SearchAllFavorite(jrpDBFilePath, keywords, o.And) + } else { + // search favorite with the given number + favorites, err = o.JrpRepository.SearchFavoriteWithNumber( + jrpDBFilePath, + o.Number, + keywords, + o.And, + ) + } + o.writeFavoriteSearchResult(favorites) + + return err +} + +// writeFavoriteSearchResult writes the favorite search result. +func (o *favoriteSearchOption) writeFavoriteSearchResult(favorites []model.Jrp) { + if len(favorites) != 0 { + if o.Plain { + for _, favorite := range favorites { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, favorite.Phrase) + } + } else { + // if plain flag is not set, write the favorite as a table + o.JrpWriter.WriteAsTable(o.Out, favorites) + } + } else { + // if no favorite is found, write the message + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND)) + } +} diff --git a/cmd/favorite_search_test.go b/cmd/favorite_search_test.go new file mode 100644 index 00000000..d9520e1a --- /dev/null +++ b/cmd/favorite_search_test.go @@ -0,0 +1,2849 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewFavoriteSearchCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewFavoriteSearchCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewFavoriteSearchCommand().Execute() : error =\n%v, wantError =\n%v", err, tt.wantError) + } + }) + } +} + +func Test_favoriteSearchOption_favoriteSearchRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords do not match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test2", "test3"}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 10, + All: true, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 9)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 9, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 10)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 11)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 11, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + fmt.Printf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: mockUtility, + } + if err := favoriteSearchOption.favoriteSearchRunE(nil, nil); err != nil { + fmt.Printf("favoriteSearchOption.favoriteSearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteSearchOption.favoriteSearchRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteSearchOption_favoriteSearch(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, nil); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords do not match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test2", "test3"}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"test2", "test3"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"test1", "test2"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"test1", "test2"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 10, + All: true, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 9)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 9, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 10)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 11)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"favorite", "search", "te", "st"}, + And: true, + Number: 11, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteSearchOption.favoriteSearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("favoriteSearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteSearchOption.favoriteSearch() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteSearchOption.favoriteSearch() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteSearchOption_writeFavoriteSearchResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\tprefix\tsuffix\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 1\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\tprefix1\tsuffix1\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\tprefix2\tsuffix2\tβ—‹\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 2\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteSearchOption := &favoriteSearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteSearchOption.writeFavoriteSearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("favoriteSearchOption.writeFavoriteSearchResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("favoriteSearchOption.writeFavoriteSearchResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/favorite_show.go b/cmd/favorite_show.go new file mode 100644 index 00000000..705d2583 --- /dev/null +++ b/cmd/favorite_show.go @@ -0,0 +1,172 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// favoriteShowOption is the struct for favorite show command. +type favoriteShowOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + All bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + Utility utility.UtilityInterface +} + +// NewFavoriteShowCommand creates a new favorite show command. +func NewFavoriteShowCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &favoriteShowOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.FAVORITE_SHOW_USE + cmd.FieldCommand.Aliases = constant.GetFavoriteShowAliases() + cmd.FieldCommand.Args = cobra.MaximumNArgs(1) + cmd.FieldCommand.RunE = o.favoriteShowRunE + + cmd.PersistentFlags().IntVarP( + &o.Number, + constant.FAVORITE_SHOW_FLAG_NUMBER, + constant.FAVORITE_SHOW_FLAG_NUMBER_SHORTHAND, + constant.FAVORITE_SHOW_FLAG_NUMBER_DEFAULT, + constant.FAVORITE_SHOW_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.All, + constant.FAVORITE_SHOW_FLAG_ALL, + constant.FAVORITE_SHOW_FLAG_ALL_SHORTHAND, + constant.FAVORITE_SHOW_FLAG_ALL_DEFAULT, + constant.FAVORITE_SHOW_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.Plain, + constant.FAVORITE_SHOW_FLAG_PLAIN, + constant.FAVORITE_SHOW_FLAG_PLAIN_SHORTHAND, + constant.FAVORITE_SHOW_FLAG_PLAIN_DEFAULT, + constant.FAVORITE_SHOW_FLAG_PLAIN_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.FAVORITE_SHOW_HELP_TEMPLATE) + + cmd.SetArgs(o.Args) + return cmd +} + +// favoriteShowRunE is the function that is called when the favorite show command is executed. +func (o *favoriteShowOption) favoriteShowRunE(_ *cobra.Command, _ []string) error { + strconvProxy := strconvproxy.New() + if len(o.Args) <= 2 { + // ifno arguments are given, set default value to args + o.Args = []string{constant.FAVORITE_USE, constant.FAVORITE_SHOW_USE, strconvProxy.Itoa(constant.FAVORITE_SHOW_FLAG_NUMBER_DEFAULT)} + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.favoriteShow(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME)) +} + +// favoriteShow is the function that gets the favorite. +func (o *favoriteShowOption) favoriteShow(jrpDBFilePath string) error { + var favorites []model.Jrp + var err error + if o.All { + // if all flag is set, get all favorite + favorites, err = o.JrpRepository.GetAllFavorite(jrpDBFilePath) + } else { + strconvProxy := strconvproxy.New() + // get the larger number between the given number flag and the largest number that can be converted from the args + num := o.Utility.GetLargerNumber( + o.Number, + o.Utility.GetMaxConvertibleString( + o.Args, + strconvProxy.Itoa(constant.FAVORITE_SHOW_FLAG_NUMBER_DEFAULT), + ), + ) + if o.Number != num && o.Number > 0 { + // if the number flag is littler than the default number, set the number flag value to num + num = o.Number + } + // get favorite with the given number + favorites, err = o.JrpRepository.GetFavoriteWithNumber( + jrpDBFilePath, + num, + ) + } + o.writeFavoriteShowResult(favorites) + + return err +} + +// writeFavoriteShowResult writes the favorite show result. +func (o *favoriteShowOption) writeFavoriteShowResult(favorites []model.Jrp) { + if len(favorites) != 0 { + if o.Plain { + for _, favorite := range favorites { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, favorite.Phrase) + } + } else { + // if plain flag is not set, write the favorite as a table + o.JrpWriter.WriteAsTable(o.Out, favorites) + } + } else { + // if no favorite is found, write the message + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND)) + } +} diff --git a/cmd/favorite_show_test.go b/cmd/favorite_show_test.go new file mode 100644 index 00000000..6b7afdd7 --- /dev/null +++ b/cmd/favorite_show_test.go @@ -0,0 +1,1762 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewFavoriteShowCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewFavoriteShowCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewFavoriteShowCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func Test_favoriteShowOption_favoriteShowRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + t.Errorf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + t.Errorf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favoriteShowd)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + t.Errorf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favoriteShowd and the other is not)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + t.Errorf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + t.Errorf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + t.Errorf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + fmt.Printf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: mockUtility, + } + if err := favoriteShowOption.favoriteShowRunE(nil, nil); err != nil { + fmt.Printf("favoriteShowOption.favoriteShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteShowOption.favoriteShowRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteShowOption.favoriteShowRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteShowShowShowOption_favoriteShowShowShow(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: true, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 9)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 9, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 10)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 10, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 11)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 11, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteShowOption.favoriteShow(jrpDBFilePath); err != nil { + t.Errorf("favoriteShowOption.favoriteShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteShowOption.favoriteShow() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteShowOption.favoriteShow() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteShowShowOption_writeFavoriteShowResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_SHOW_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteShowOption := &favoriteShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteShowOption.writeFavoriteShowResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteShowOption.writeFavoriteShowResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteShowOption.writeFavoriteShowResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/favorite_test.go b/cmd/favorite_test.go new file mode 100644 index 00000000..45cef18f --- /dev/null +++ b/cmd/favorite_test.go @@ -0,0 +1,1762 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewFavoriteCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewFavoriteCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewFavoriteCommand().Execute() error =\n%v, wantError =\n%v", err, tt.wantError) + } + }) + } +} + +func Test_favoriteOption_favoriteRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + t.Errorf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + t.Errorf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favorited)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + t.Errorf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited and the other is not)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + t.Errorf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is nil)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + t.Errorf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is empty)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + t.Errorf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + fmt.Printf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: mockUtility, + } + if err := favoriteOption.favoriteRunE(nil, nil); err != nil { + fmt.Printf("favoriteOption.favoriteRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteOption.favoriteRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteOption.favoriteRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteOption_favorite(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: true, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 9)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 9, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 10)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 10, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 11)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 11, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := favoriteOption.favorite(jrpDBFilePath); err != nil { + t.Errorf("favoriteOption.favorite() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2\ntest3\ntest4\ntest5\ntest6\ntest7\ntest8\ntest9\ntest10\ntest11\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteOption.favorite() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteOption.favorite() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_favoriteOption_writeFavoriteResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.FAVORITE_MESSAGE_NO_FAVORITE_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, plain)", + fields: fields{ + t: t, + fnc: func() { + favoriteOption := &favoriteOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + favoriteOption.writeFavoriteResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("favoriteOption.writeFavoriteResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("favoriteOption.writeFavoriteResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/generate.go b/cmd/generate.go index c0d55740..72f5365d 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -1,82 +1,286 @@ package cmd import ( - "io" - "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" - "github.com/yanosea/jrp/internal/database" - "github.com/yanosea/jrp/internal/fs" - "github.com/yanosea/jrp/internal/usermanager" - "github.com/yanosea/jrp/logic" - "github.com/yanosea/jrp/util" + "github.com/yanosea/jrp/app/database/jrp/model" + jrprepository "github.com/yanosea/jrp/app/database/jrp/repository" + wnjpnrepository "github.com/yanosea/jrp/app/database/wnjpn/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/generator" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/rand" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" ) -type GenerateOption struct { - Out io.Writer - ErrOut io.Writer - Args []string - Number int - Prefix string - Suffix string - Generator logic.Generator +// generateOption is the struct for generate command. +type generateOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + Prefix string + Suffix string + DryRun bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Generator generator.Generatable + JrpRepository jrprepository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + WNJpnRepository wnjpnrepository.WNJpnRepositoryInterface + Utility utility.UtilityInterface } -func NewGenerateCommand(g *GlobalOption) *cobra.Command { - o := &GenerateOption{ - Out: g.Out, - ErrOut: g.ErrOut, - Args: g.Args, +// NewGenerateCommand creates a new generate command. +func NewGenerateCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &generateOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = jrprepository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + o.WNJpnRepository = wnjpnrepository.New( + sqlproxy.New(), + ) + o.Generator = generator.New( + osproxy.New(), + randproxy.New(), + sqlproxy.New(), + timeproxy.New(), + o.WNJpnRepository, + ) - cmd := &cobra.Command{ - Use: constant.GENERATE_USE, - Aliases: constant.GetGenerateAliases(), - Short: constant.GENERATE_SHORT, - Long: constant.GENERATE_LONG, - Args: cobra.MaximumNArgs(1), - RunE: o.GenerateRunE, - } + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() - cmd.PersistentFlags().IntVarP(&o.Number, constant.GENERATE_FLAG_NUMBER, constant.GENERATE_FLAG_NUMBER_SHORTHAND, 1, constant.GENERATE_FLAG_NUMBER_DESCRIPTION) - cmd.PersistentFlags().StringVarP(&o.Prefix, constant.GENERATE_FLAG_PREFIX, constant.GENERATE_FLAG_PREFIX_SHORTHAND, "", constant.GENERATE_FLAG_PREFIX_DESCRIPTION) - cmd.PersistentFlags().StringVarP(&o.Suffix, constant.GENERATE_FLAG_SUFFIX, constant.GENERATE_FLAG_SUFFIX_SHORTHAND, "", constant.GENERATE_FLAG_SUFFIX_DESCRIPTION) + cmd.FieldCommand.Use = constant.GENERATE_USE + cmd.FieldCommand.Aliases = constant.GetGenerateAliases() + cmd.FieldCommand.Args = cobra.MaximumNArgs(1) + cmd.FieldCommand.RunE = o.generateRunE + + cmd.PersistentFlags().IntVarP( + &o.Number, + constant.GENERATE_FLAG_NUMBER, + constant.GENERATE_FLAG_NUMBER_SHORTHAND, + constant.GENERATE_FLAG_NUMBER_DEFAULT, + constant.GENERATE_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().StringVarP( + &o.Prefix, + constant.GENERATE_FLAG_PREFIX, + constant.GENERATE_FLAG_PREFIX_SHORTHAND, + constant.GENERATE_FLAG_PREFIX_DEFAULT, + constant.GENERATE_FLAG_PREFIX_DESCRIPTION, + ) + cmd.PersistentFlags().StringVarP( + &o.Suffix, + constant.GENERATE_FLAG_SUFFIX, + constant.GENERATE_FLAG_SUFFIX_SHORTHAND, + constant.GENERATE_FLAG_SUFFIX_DEFAULT, + constant.GENERATE_FLAG_SUFFIX_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.DryRun, + constant.GENERATE_FLAG_DRY_RUN, + constant.GENERATE_FLAG_DRY_RUN_SHORTHAND, + constant.GENERATE_FLAG_DRY_RUN_DEFAULT, + constant.GENERATE_FLAG_DRY_RUN_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.Plain, + constant.GENERATE_FLAG_PLAIN, + constant.GENERATE_FLAG_PLAIN_SHORTHAND, + constant.GENERATE_FLAG_PLAIN_DEFAULT, + constant.GENERATE_FLAG_PLAIN_DESCRIPTION, + ) cmd.SetOut(o.Out) cmd.SetErr(o.ErrOut) cmd.SetHelpTemplate(constant.GENARETE_HELP_TEMPLATE) cmd.SetArgs(o.Args) + return cmd } -func (o *GenerateOption) GenerateRunE(_ *cobra.Command, _ []string) error { +// generateRunE is the function that is called when the generate command is executed. +func (o *generateOption) generateRunE(_ *cobra.Command, _ []string) error { + var word string + var mode generator.GenerateMode + if o.Prefix != "" && o.Suffix != "" { + // if both prefix and suffix are provided, notify to use only one + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_USE_ONLY_ONE)) + return nil + } else if o.Prefix != "" { + word = o.Prefix + mode = generator.WithPrefix + } else if o.Suffix != "" { + word = o.Suffix + mode = generator.WithSuffix + } + if len(o.Args) <= 1 { - o.Args = []string{"generate", "1"} + strconvProxy := strconvproxy.New() + // if no args are given, set the default value to the args + o.Args = []string{constant.GENERATE_USE, strconvProxy.Itoa(constant.GENERATE_FLAG_NUMBER_DEFAULT)} } - o.Generator = logic.NewJapaneseRandomPhraseGenerator(usermanager.OSUserProvider{}, database.SQLiteProvider{}, fs.OsFileManager{}) + // get jrp db file dir path + wnJpnDBFileDirPath, err := o.DBFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + return err + } - return o.Generate() + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.generate( + filepathProxy.Join(wnJpnDBFileDirPath, wnjpnrepository.WNJPN_DB_FILE_NAME), + filepathProxy.Join(jrpDBFileDirPath, jrprepository.JRP_DB_FILE_NAME), + word, + mode, + ) } -func (o *GenerateOption) Generate() error { - jrps, err := o.Generator.Generate( - logic.DefineNumber( +// generate generates jrpss and saves them. +func (o *generateOption) generate( + wnJpnDBFilePath string, + jrpDBFilePath string, + word string, + mode generator.GenerateMode, +) error { + var jrps []model.Jrp + var err error + jrps, err = o.generateGenerate(wnJpnDBFilePath, word, mode) + if err != nil { + return err + } + err = o.generateSave(jrpDBFilePath, jrps) + if err != nil { + return err + } + o.writeGenerateResult(jrps) + + return nil +} + +// generateGenerate generates jrps. +func (o *generateOption) generateGenerate(wnJpnDBFilePath string, word string, mode generator.GenerateMode) ([]model.Jrp, error) { + strconvProxy := strconvproxy.New() + res, jrps, err := o.Generator.GenerateJrp( + wnJpnDBFilePath, + // get the larger number between the given number flag and the largest number that can be converted from the args + o.Utility.GetLargerNumber( o.Number, - logic.GetFirstConvertibleToString(o.Args), + o.Utility.GetMaxConvertibleString( + o.Args, + strconvProxy.Itoa(constant.GENERATE_FLAG_NUMBER_DEFAULT), + ), ), - o.Prefix, - o.Suffix, + word, + mode, ) - if err != nil { - return err + o.writeGenerateGenerateResult(res) + + return jrps, err +} + +// writeGenerateGenerateResult writes the result of generating jrps. +func (o *generateOption) writeGenerateGenerateResult(result generator.GenerateResult) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == generator.GeneratedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.GENERATE_MESSAGE_GENERATE_FAILURE) + } else if result == generator.DBFileNotFound { + message = colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) } - for _, jrp := range jrps { - util.PrintlnWithWriter(o.Out, jrp) + if message != "" { + // if success, do not write any message + o.Utility.PrintlnWithWriter(out, message) } +} - return nil +// generateSave saves jrps. +func (o *generateOption) generateSave(jrpDBFilePath string, jrps []model.Jrp) error { + var res jrprepository.SaveStatus + var err error + if !o.DryRun && len(jrps) != 0 { + // if the dry-run flag is not set and the generated phrases are not empty, save the generated phrases + res, err = o.JrpRepository.SaveHistory(jrpDBFilePath, jrps) + } + o.writeGenerateSaveResult(res) + + return err +} + +// writeGenerateSaveResult writes the result of saving jrps. +func (o *generateOption) writeGenerateSaveResult(result jrprepository.SaveStatus) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == jrprepository.SavedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.GENERATE_MESSAGE_SAVED_FAILURE) + } else if result == jrprepository.SavedNone { + message = colorProxy.YellowString(constant.GENERATE_MESSAGE_SAVED_NONE) + } else if result == jrprepository.SavedNotAll { + message = colorProxy.YellowString(constant.GENERATE_MESSAGE_SAVED_NOT_ALL) + } + + if message != "" { + // if success, do not write any message + o.Utility.PrintlnWithWriter(out, message) + } +} + +// writeGenerateResult writes the result of generate command. +func (o *generateOption) writeGenerateResult(jrps []model.Jrp) { + if len(jrps) != 0 { + if o.Plain { + for _, jrp := range jrps { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, jrp.Phrase) + } + } else { + // if plain flag is not set, write the favorite as a table + o.JrpWriter.WriteGenerateResultAsTable(o.Out, jrps) + } + } } diff --git a/cmd/generate_test.go b/cmd/generate_test.go index d19c9f9e..d8b68d21 100644 --- a/cmd/generate_test.go +++ b/cmd/generate_test.go @@ -1,109 +1,2215 @@ -package cmd_test +package cmd import ( "errors" - "os" + "fmt" "testing" - gomock "go.uber.org/mock/gomock" + "github.com/yanosea/jrp/app/database/jrp/model" + jrprepository "github.com/yanosea/jrp/app/database/jrp/repository" + wnjpnrepository "github.com/yanosea/jrp/app/database/wnjpn/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/downloader" + "github.com/yanosea/jrp/app/library/generator" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/rand" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" - "github.com/yanosea/jrp/cmd" - "github.com/yanosea/jrp/internal/database" - "github.com/yanosea/jrp/internal/fs" - "github.com/yanosea/jrp/internal/usermanager" - "github.com/yanosea/jrp/logic" - - mock_generator "github.com/yanosea/jrp/mock/generator" + mockjrprepository "github.com/yanosea/jrp/mock/app/database/jrp/repository" + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/generator" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" ) func TestNewGenerateCommand(t *testing.T) { type args struct { - globalOption *cmd.GlobalOption + g *GlobalOption } tests := []struct { - name string - args args + name string + args args + wantError bool }{ { name: "positive testing", - args: args{globalOption: &cmd.GlobalOption{Out: os.Stdout, ErrOut: os.Stderr, Args: []string{"generate"}}}, + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := cmd.NewGenerateCommand(tt.args.globalOption) - if got == nil { - t.Errorf("NewDownloadCommand() : returned nil") + got := NewGenerateCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewGenerateCommand().Execute() error =\n%v, wantError =\n%v", err, tt.wantError) } }) } } -func TestGenerateRunE(t *testing.T) { - type args struct { - o cmd.GenerateOption +func Test_generateOption_generateRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProviderFailsWNJpnDBFileDirPath := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProviderFailsWNJpnDBFileDirPath.EXPECT().GetWNJpnDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetWNJpnDBFileDirPath() failed")) + mockDBFileDirPathProviderFailsGetJrpDBFileDirPath := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProviderFailsGetJrpDBFileDirPath.EXPECT().GetWNJpnDBFileDirPath().Return("", nil) + mockDBFileDirPathProviderFailsGetJrpDBFileDirPath.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer } tests := []struct { - name string - args args + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() }{ { - name: "positive testing", - args: args{o: cmd.GenerateOption{Out: os.Stdout, ErrOut: os.Stderr, Args: []string{"generate"}, Number: 1, Generator: logic.NewJapaneseRandomPhraseGenerator(usermanager.OSUserProvider{}, database.SQLiteProvider{}, fs.OsFileManager{})}}, + name: "positive testing (wn jpn database file does not exist)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + t.Errorf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (wn jpn database file exists)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + t.Errorf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (both prefix and suffix are specified)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "prefix", + Suffix: "suffix", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + t.Errorf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_USE_ONLY_ONE) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (only prefix is specified)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "prefix", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + t.Errorf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (only suffix is specified)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "suffix", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + t.Errorf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is nil)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + t.Errorf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is empty)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + t.Errorf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetWNJpnDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProviderFailsWNJpnDBFileDirPath, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + fmt.Printf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProviderFailsGetJrpDBFileDirPath, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generateRunE(nil, nil); err != nil { + fmt.Printf("generateOption.generateRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("generateOption.generateRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("generateOption.generateRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} - err := tt.args.o.GenerateRunE(nil, nil) - if err != nil { - t.Errorf("GenerateRunE() : error = %v", err) +func Test_generateOption_generate(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, jrprepository.JRP_DB_FILE_NAME) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockGenerator := mockgenerator.NewMockGeneratable(mockCtrl) + mockGenerator.EXPECT().GenerateJrp(wnJpnDBFilePath, 1, "", generator.WithNoPrefixOrSuffix).Return(generator.GeneratedFailed, nil, errors.New("Generator.GenerateJrp() failed")) + mockJrpRepository := mockjrprepository.NewMockJrpRepositoryInterface(mockCtrl) + mockJrpRepository.EXPECT().SaveHistory(gomock.Any(), gomock.Any()).Return(jrprepository.SavedFailed, errors.New("JrpRepository.SaveHistory() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (wn jpn database file does not exist)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generate(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + t.Errorf("generateOption.generate() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (wn jpn database file exists)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generate(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + t.Errorf("generateOption.generate() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (generateOption.generateGenerate() failed)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: mockGenerator, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generate(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + fmt.Printf("generateOption.generate() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.GENERATE_MESSAGE_GENERATE_FAILURE) + "\n", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (generateOption.generateSave() failed)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: mockJrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := generateOption.generate(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + fmt.Printf("generateOption.generate() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.GENERATE_MESSAGE_SAVED_FAILURE) + "\n", + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("generateOption.generate() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("generateOption.generate() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() } }) } } -func TestGenerate(t *testing.T) { +func Test_generateOption_generateGenerate(t *testing.T) { + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + Prefix string + Suffix string + DryRun bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Generator generator.Generatable + JrpRepository jrprepository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + WNJpnRepository wnjpnrepository.WNJpnRepositoryInterface + Utility utility.UtilityInterface + } type args struct { - o cmd.GenerateOption + wnJpnDBFilePath string + word string + mode generator.GenerateMode } tests := []struct { - name string - args args - wantErr bool - setup func(mockCtrl *gomock.Controller, tt *args) + name string + fields fields + args args + wantCount int + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() }{ { - name: "positive testing", - args: args{o: cmd.GenerateOption{Out: os.Stdout, ErrOut: os.Stderr, Args: []string{"generate", "1"}, Number: 1, Prefix: "", Suffix: "", Generator: logic.NewJapaneseRandomPhraseGenerator(usermanager.OSUserProvider{}, database.SQLiteProvider{}, fs.OsFileManager{})}}, - wantErr: false, - setup: nil, - }, { - name: "negative testing (Generate() fails)", - args: args{o: cmd.GenerateOption{Out: os.Stdout, ErrOut: os.Stderr, Args: []string{"generate", "1"}, Number: 1, Prefix: "", Suffix: "", Generator: nil}}, - wantErr: true, - setup: func(mockCtrl *gomock.Controller, tt *args) { - mg := mock_generator.NewMockGenerator(mockCtrl) - mg.EXPECT().Generate(tt.o.Number, tt.o.Prefix, tt.o.Suffix).Return(nil, errors.New("failed to generate japanese random phrase")) - tt.o.Generator = mg + name: "positive testing (wn jpn database file does not exist)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + word: "dummy", + mode: generator.WithNoPrefixOrSuffix, + }, + wantCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } }, }, + { + name: "positive testing (wn jpn database file exists)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "prefix", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + word: "", + mode: generator.WithNoPrefixOrSuffix, + }, + wantCount: 1, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Generator.GenerateJrp() failed)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "suffix", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: nil, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + word: "", + mode: generator.WithNoPrefixOrSuffix, + }, + wantCount: 0, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + mockGenerator := mockgenerator.NewMockGeneratable(mockCtrl) + mockGenerator.EXPECT().GenerateJrp(wnJpnDBFilePath, 1, "", generator.WithNoPrefixOrSuffix).Return(generator.GeneratedFailed, nil, errors.New("Generator.GenerateJrp() failed")) + tt.Generator = mockGenerator + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &generateOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Number: tt.fields.Number, + Prefix: tt.fields.Prefix, + Suffix: tt.fields.Suffix, + DryRun: tt.fields.DryRun, + Plain: tt.fields.Plain, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + Generator: tt.fields.Generator, + JrpRepository: tt.fields.JrpRepository, + JrpWriter: tt.fields.JrpWriter, + WNJpnRepository: tt.fields.WNJpnRepository, + Utility: tt.fields.Utility, + } + got, err := o.generateGenerate(tt.args.wnJpnDBFilePath, tt.args.word, tt.args.mode) + if (err != nil) != tt.wantErr { + t.Errorf("generateOption.generateGenerate() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if len(got) != tt.wantCount { + t.Errorf("generateOption.generateGenerate() : len(got) =\n%v, wantCount =\n%v", len(got), tt.wantCount) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) } +} + +func Test_generateOption_writeGenerateGenerateResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is GeneratedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateGenerateResult(generator.GeneratedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is DBFileNotFound)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateGenerateResult(generator.DBFileNotFound) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is GeneratedFailed)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateGenerateResult(generator.GeneratedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.GENERATE_MESSAGE_GENERATE_FAILURE) + "\n", + wantErr: false, + }, + } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("generateOption.writeGenerateGenerateResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("generateOption.writeGenerateGenerateResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} + +func Test_generateOption_generateSave(t *testing.T) { + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, jrprepository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + Prefix string + Suffix string + DryRun bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Generator generator.Generatable + JrpRepository jrprepository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + WNJpnRepository wnjpnrepository.WNJpnRepositoryInterface + Utility utility.UtilityInterface + } + type args struct { + jrpDBFilePath string + jrps []model.Jrp + } + tests := []struct { + name string + fields fields + args args + wantJrps []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() + }{ + { + name: "positive testing (not dry run, jrps are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: nil, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not dry run, jrps are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not dry run, jrps is one)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not dry run, jrps are two)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: nil, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps is one)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps are two)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { if tt.setup != nil { - tt.setup(ctrl, &tt.args) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) } + o := &generateOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Number: tt.fields.Number, + Prefix: tt.fields.Prefix, + Suffix: tt.fields.Suffix, + DryRun: tt.fields.DryRun, + Plain: tt.fields.Plain, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + Generator: tt.fields.Generator, + JrpRepository: tt.fields.JrpRepository, + JrpWriter: tt.fields.JrpWriter, + WNJpnRepository: tt.fields.WNJpnRepository, + Utility: tt.fields.Utility, + } + if err := o.generateSave(tt.args.jrpDBFilePath, tt.args.jrps); (err != nil) != tt.wantErr { + t.Errorf("generateOption.generateSave() : error = %v, wantErr %v", err, tt.wantErr) + } + savedJrps, err := jrpRepository.GetAllHistory(tt.args.jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(savedJrps, tt.wantJrps) { + t.Errorf("generateOption.generateSave() : savedJrps =\n%v, want =\n%v", savedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} - err := tt.args.o.Generate() +func Test_generateOption_writeGenerateSaveResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is SavedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateSaveResult(jrprepository.SavedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is SaveFailed)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateSaveResult(jrprepository.SavedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.GENERATE_MESSAGE_SAVED_FAILURE) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is SavedNone)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateSaveResult(jrprepository.SavedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_SAVED_NONE) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is SavedNotAll)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateSaveResult(jrprepository.SavedNotAll) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.GENERATE_MESSAGE_SAVED_NOT_ALL) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) if (err != nil) != tt.wantErr { - t.Errorf("Generate() : error = %v, wantErr = %v", err, tt.wantErr) + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("generateOption.writeGenerateSaveResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("generateOption.writeGenerateSaveResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} + +func Test_generateOption_writeGenerateResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult(nil) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult([]model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\tPREFIX\tSUFFIX\tCREATED AT\ntest\tprefix\tsuffix\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 1\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult([]model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\tPREFIX\tSUFFIX\tCREATED AT\ntest1\tprefix1\tsuffix1\t9999-12-31 00:00:00\ntest2\tprefix2\tsuffix2\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 2\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult(nil) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult([]model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, plain)", + fields: fields{ + t: t, + fnc: func() { + generateOption := &generateOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + generateOption.writeGenerateResult([]model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("generateOption.writeGenerateResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("generateOption.writeGenerateResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } }) } diff --git a/cmd/godoc.go b/cmd/godoc.go new file mode 100644 index 00000000..54157c2b --- /dev/null +++ b/cmd/godoc.go @@ -0,0 +1,2 @@ +// Package cmd is a package for providing command. +package cmd diff --git a/cmd/history.go b/cmd/history.go new file mode 100644 index 00000000..996b0926 --- /dev/null +++ b/cmd/history.go @@ -0,0 +1,177 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// historyOption is the struct for history command. +type historyOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + All bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + Utility utility.UtilityInterface +} + +// NewHistoryCommand creates a new history command. +func NewHistoryCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &historyOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + cmd.FieldCommand.Use = constant.HISTORY_USE + cmd.FieldCommand.Aliases = constant.GetHistoryAliases() + cmd.FieldCommand.Args = cobra.MaximumNArgs(1) + cmd.FieldCommand.RunE = o.historyRunE + + cmd.PersistentFlags().IntVarP(&o.Number, + constant.HISTORY_FLAG_NUMBER, + constant.HISTORY_FLAG_NUMBER_SHORTHAND, + constant.HISTORY_FLAG_NUMBER_DEFAULT, + constant.HISTORY_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.All, + constant.HISTORY_FLAG_ALL, + constant.HISTORY_FLAG_ALL_SHORTHAND, + constant.HISTORY_FLAG_ALL_DEFAULT, + constant.HISTORY_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.Plain, + constant.HISTORY_FLAG_PLAIN, + constant.HISTORY_FLAG_PLAIN_SHORTHAND, + constant.HISTORY_FLAG_PLAIN_DEFAULT, + constant.HISTORY_FLAG_PLAIN_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.HISTORY_HELP_TEMPLATE) + + cmd.AddCommand( + NewHistoryShowCommand(g), + NewHistoryRemoveCommand(g, promptuiproxy.New()), + NewHistorySearchCommand(g), + NewHistoryClearCommand(g, promptuiproxy.New()), + ) + + return cmd +} + +// historyRunE is the function that is called when the history command is executed. +func (o *historyOption) historyRunE(_ *cobra.Command, _ []string) error { + strconvProxy := strconvproxy.New() + if len(o.Args) <= 1 { + // if no argument is given, set the default value to args + o.Args = []string{constant.HISTORY_USE, strconvProxy.Itoa(constant.HISTORY_FLAG_NUMBER_DEFAULT)} + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.history(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME)) +} + +// history searches the history. +func (o *historyOption) history(jrpDBFilePath string) error { + var histories []model.Jrp + var err error + if o.All { + // if all flag is set, get all history + histories, err = o.JrpRepository.GetAllHistory(jrpDBFilePath) + } else { + if o.Number != constant.HISTORY_FLAG_NUMBER_DEFAULT && o.Number >= 1 { + // if number flag is set, get history with the given number + histories, err = o.JrpRepository.GetHistoryWithNumber(jrpDBFilePath, o.Number) + } else { + strconvProxy := strconvproxy.New() + // get history with the given number + histories, err = o.JrpRepository.GetHistoryWithNumber( + jrpDBFilePath, + // get the larger number between the given number flag and the largest number that can be converted from the args + o.Utility.GetLargerNumber( + o.Number, + o.Utility.GetMaxConvertibleString( + o.Args, + strconvProxy.Itoa(constant.HISTORY_FLAG_NUMBER_DEFAULT), + ), + ), + ) + } + } + o.writeHistoryResult(histories) + + return err +} + +// writeHistoryResult writes the history result. +func (o *historyOption) writeHistoryResult(histories []model.Jrp) { + if len(histories) != 0 { + if o.Plain { + for _, history := range histories { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, history.Phrase) + } + } else { + // if plain flag is not set, write the history as a table + o.JrpWriter.WriteAsTable(o.Out, histories) + } + } else { + // if no history is found, write the message + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND)) + } +} diff --git a/cmd/history_clear.go b/cmd/history_clear.go new file mode 100644 index 00000000..4d42a5d5 --- /dev/null +++ b/cmd/history_clear.go @@ -0,0 +1,142 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// historyClearOption is the struct for history clear command. +type historyClearOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Force bool + NoConfirm bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + PromptuiProxy promptuiproxy.Promptui + Utility utility.UtilityInterface +} + +// NewHistoryClearCommand creates a new history clear command. +func NewHistoryClearCommand(g *GlobalOption, promptuiProxy promptuiproxy.Promptui) *cobraproxy.CommandInstance { + o := &historyClearOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.PromptuiProxy = promptuiProxy + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.HISTORY_CLEAR_USE + cmd.FieldCommand.Aliases = constant.GetHistoryClearAliases() + cmd.FieldCommand.RunE = o.historyClearRunE + + cmd.PersistentFlags().BoolVarP( + &o.Force, + constant.HISTORY_CLEAR_FLAG_FORCE, + constant.HISTORY_CLEAR_FLAG_FORCE_SHORTHAND, + constant.HISTORY_CLEAR_FLAG_FORCE_DEFAULT, + constant.HISTORY_CLEAR_FLAG_FORCE_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.NoConfirm, + constant.HISTORY_CLEAR_FLAG_NO_CONFIRM, + constant.HISTORY_CLEAR_FLAG_NO_CONFIRM_SHORTHAND, + constant.HISTORY_CLEAR_FLAG_NO_CONFIRM_DEFAULT, + constant.HISTORY_CLEAR_FLAG_NO_CONFIRM_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.HISTORY_CLEAR_HELP_TEMPLATE) + + return cmd +} + +// historyClearRunE is the function that is called when the history clear command is executed. +func (o *historyClearOption) historyClearRunE(_ *cobra.Command, _ []string) error { + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + // write prompt if no-confirm option is not set + if !o.NoConfirm { + colorProxy := colorproxy.New() + prompt := o.PromptuiProxy.NewPrompt() + prompt.SetLabel(colorProxy.YellowString(constant.HISTORY_CLEAR_PROMPT_LABEL)) + result, err := prompt.Run() + if err != nil { + return err + } + if result != "y" && result != "Y" { + // write canceled message and do nothing + o.Utility.PrintlnWithWriter(o.Out, constant.HISTORY_CLEAR_MESSAGE_CLEAR_CANCELED) + return nil + } + } + + filepathProxy := filepathproxy.New() + return o.historyClear(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME)) +} + +// historyClear clears all history. +func (o *historyClearOption) historyClear(jrpDBFilePath string) error { + // remove all history + res, err := o.JrpRepository.RemoveHistoryAll(jrpDBFilePath, o.Force) + o.writeHistoryClearResult(res) + + return err +} + +// writeHistoryClearResult writes the result of history clear. +func (o *historyClearOption) writeHistoryClearResult(result repository.RemoveStatus) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == repository.RemovedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_FAIRULE) + } else if result == repository.RemovedNone && !o.Force { + message = colorProxy.YellowString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_NONE) + } else { + message = colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + } + o.Utility.PrintlnWithWriter(out, message) +} diff --git a/cmd/history_clear_test.go b/cmd/history_clear_test.go new file mode 100644 index 00000000..2cc68cdb --- /dev/null +++ b/cmd/history_clear_test.go @@ -0,0 +1,1865 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/database/jrp/repository" + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/mock/app/proxy/promptui" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewHistoryClearCommand(t *testing.T) { + type args struct { + g *GlobalOption + promptuiProxy promptuiproxy.Promptui + } + tests := []struct { + name string + args args + wantError bool + setup func(*gomock.Controller, *args) + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + promptuiProxy: nil, + }, + wantError: false, + setup: func(mockCtrl *gomock.Controller, args *args) { + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()) + mockPromptInstance.EXPECT().Run().Return("y", nil) + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance) + args.promptuiProxy = mockPromptuiProxy + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.args) + } + got := NewHistoryClearCommand(tt.args.g, tt.args.promptuiProxy) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewHistoryClearCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func Test_historyClearOption_historyClearRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + noMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + noMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + noMockPromptInstance.EXPECT().Run().Return("n", nil) + noMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + noMockPromptuiProxy.EXPECT().NewPrompt().Return(noMockPromptInstance) + errMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + errMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + errMockPromptInstance.EXPECT().Run().Return("", errors.New("PromptInstance.Run() failed")) + errMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + errMockPromptuiProxy.EXPECT().NewPrompt().Return(errMockPromptInstance) + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantJrps []model.Jrp + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (answering not yes on the prompt)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: noMockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: constant.HISTORY_CLEAR_MESSAGE_CLEAR_CANCELED + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (set no-confirm option)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: nil, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: true, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: true, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are not favorited, not force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are not favorited, force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: true, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favorited, not force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favorited, force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: true, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited and other is not, not force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited and other is not, force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: true, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + t.Errorf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + fmt.Printf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: mockUtility, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + fmt.Printf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (PromptInstance.run() failed)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: errMockPromptuiProxy, + Utility: util, + } + if err := historyClearOption.historyClearRunE(nil, nil); err != nil { + fmt.Printf("historyClearOption.historyClearRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historyClearOption.historyClearRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historyClearOption.historyClearRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + leftJrps, err := jrpRepository.GetAllHistory(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(leftJrps, tt.wantJrps) { + t.Errorf("historyClearOption.historyClearRunE() : leftJrps =\n%v, want =\n%v", leftJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyClearOption_historyClear(t *testing.T) { + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Force bool + NoConfirm bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + PromptuiProxy promptuiproxy.Promptui + Utility utility.UtilityInterface + } + type args struct { + jrpDBFilePath string + } + tests := []struct { + name string + fields fields + args args + wantJrps []model.Jrp + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are not favorited, not force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are not favorited, force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favorited, not force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both are favorited, force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited and other is not, not force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited and other is not, force)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (JrpRepository.RemoveHistoryAll() failed)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: nil, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + }, + wantJrps: nil, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + mockJrpRepository := mockrepository.NewMockJrpRepositoryInterface(mockCtrl) + mockJrpRepository.EXPECT().RemoveHistoryAll(gomock.Any(), gomock.Any()).Return(repository.RemovedFailed, errors.New("JrpRepository.RemoveHistoryAll() : error")) + tt.JrpRepository = mockJrpRepository + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &historyClearOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Force: tt.fields.Force, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + JrpRepository: tt.fields.JrpRepository, + Utility: tt.fields.Utility, + } + if err := o.historyClear(tt.args.jrpDBFilePath); (err != nil) != tt.wantErr { + t.Errorf("historyClearOption.historyClear() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + leftJrps, err := jrpRepository.GetAllHistory(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(leftJrps, tt.wantJrps) { + t.Errorf("historyClearOption.historyClear() : leftJrps =\n%v, want =\n%v", leftJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyClearOption_writeHistoryClearResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is RemovedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyClearOption.writeHistoryClearResult(repository.RemovedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is RemovedFailed)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyClearOption.writeHistoryClearResult(repository.RemovedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_FAIRULE) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is RemovedNone, not force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyClearOption.writeHistoryClearResult(repository.RemovedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_NONE) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is RemovedNone, force)", + fields: fields{ + t: t, + fnc: func() { + historyClearOption := &historyClearOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Force: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyClearOption.writeHistoryClearResult(repository.RemovedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_CLEAR_MESSAGE_CLEARED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("historyClearOption.writeHistoryClearResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("historyClearOption.writeHistoryClearResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/history_remove.go b/cmd/history_remove.go new file mode 100644 index 00000000..6feb2cf7 --- /dev/null +++ b/cmd/history_remove.go @@ -0,0 +1,184 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// historyRemoveOption is the struct for history remove command. +type historyRemoveOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + All bool + Force bool + NoConfirm bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + PromptuiProxy promptuiproxy.Promptui + Utility utility.UtilityInterface +} + +// NewHistoryRemoveCommand creates a new history remove command. +func NewHistoryRemoveCommand(g *GlobalOption, promptuiProxy promptuiproxy.Promptui) *cobraproxy.CommandInstance { + o := &historyRemoveOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.PromptuiProxy = promptuiProxy + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.HISTORY_REMOVE_USE + cmd.FieldCommand.Aliases = constant.GetHistoryRemoveAliases() + cmd.FieldCommand.RunE = o.historyRemoveRunE + + cmd.PersistentFlags().BoolVarP( + &o.All, + constant.HISTORY_REMOVE_FLAG_ALL, + constant.HISTORY_REMOVE_FLAG_ALL_SHORTHAND, + constant.HISTORY_REMOVE_FLAG_ALL_DEFAULT, + constant.HISTORY_REMOVE_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.Force, + constant.HISTORY_REMOVE_FLAG_FORCE, + constant.HISTORY_REMOVE_FLAG_FORCE_SHORTHAND, + constant.HISTORY_REMOVE_FLAG_FORCE_DEFAULT, + constant.HISTORY_REMOVE_FLAG_FORCE_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.NoConfirm, + constant.HISTORY_REMOVE_FLAG_NO_CONFIRM, + constant.HISTORY_REMOVE_FLAG_NO_CONFIRM_SHORTHAND, + constant.HISTORY_REMOVE_FLAG_NO_CONFIRM_DEFAULT, + constant.HISTORY_REMOVE_FLAG_NO_CONFIRM_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.HISTORY_REMOVE_HELP_TEMPLATE) + + return cmd +} + +// historyRemoveRunE is the function that is called when the history remove command is executed. +func (o *historyRemoveOption) historyRemoveRunE(_ *cobra.Command, _ []string) error { + if len(o.Args) <= 2 { + // if no arguments is given, set default value to args + o.Args = []string{constant.HISTORY_USE, constant.HISTORY_REMOVE_USE, ""} + } + + // set ID + strconvProxy := strconvproxy.New() + var IDs []int + if !o.All { + for _, arg := range o.Args[2:] { + if id, err := strconvProxy.Atoi(arg); err != nil { + continue + } else { + IDs = append(IDs, id) + } + } + } + if len(IDs) == 0 && !o.All { + // if no ID is specified, print write and return + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED)) + return nil + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + // write prompt if all flag is set and no-confirm option is not set + if o.All && !o.NoConfirm { + colorProxy := colorproxy.New() + prompt := o.PromptuiProxy.NewPrompt() + prompt.SetLabel(colorProxy.YellowString(constant.HISTORY_REMOVE_PROMPT_REMOVE_ALL_LABEL)) + result, err := prompt.Run() + if err != nil { + return err + } + if result != "y" && result != "Y" { + // write canceled message and do nothing + o.Utility.PrintlnWithWriter(o.Out, constant.HISTORY_REMOVE_MESSAGE_REMOVE_ALL_CANCELED) + return nil + } + } + + filepathProxy := filepathproxy.New() + return o.historyRemove(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME), IDs) +} + +// historyRemove is the function that removes history by IDs. +func (o *historyRemoveOption) historyRemove(jrpDBFilePath string, IDs []int) error { + var res repository.RemoveStatus + var err error + if o.All { + // if all flag is set, remove all history + res, err = o.JrpRepository.RemoveHistoryAll(jrpDBFilePath, o.Force) + } else { + // if IDs are specified, remove history by IDs + res, err = o.JrpRepository.RemoveHistoryByIDs(jrpDBFilePath, IDs, o.Force) + } + o.writeHistoryRemoveResult(res) + + return err +} + +// writeHistoryRemoveResult writes the result of history remove. +func (o *historyRemoveOption) writeHistoryRemoveResult(result repository.RemoveStatus) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == repository.RemovedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_FAILURE) + } else if result == repository.RemovedNone { + message = colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + } else if result == repository.RemovedNotAll { + message = colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NOT_ALL) + } else { + message = colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + } + o.Utility.PrintlnWithWriter(out, message) +} diff --git a/cmd/history_remove_test.go b/cmd/history_remove_test.go new file mode 100644 index 00000000..0cda93ee --- /dev/null +++ b/cmd/history_remove_test.go @@ -0,0 +1,5841 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/promptui" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/mock/app/proxy/promptui" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewHistoryRemoveCommand(t *testing.T) { + type args struct { + g *GlobalOption + promptuiProxy promptuiproxy.Promptui + } + tests := []struct { + name string + args args + wantError bool + setup func(*gomock.Controller, *args) + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + promptuiProxy: nil, + }, + wantError: false, + setup: func(mockCtrl *gomock.Controller, args *args) { + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()) + mockPromptInstance.EXPECT().Run().Return("y", nil) + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance) + args.promptuiProxy = mockPromptuiProxy + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewHistoryRemoveCommand(tt.args.g, tt.args.promptuiProxy) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewHistoryRemoveCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func Test_historyRemoveOption_historyRemoveRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + noMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + noMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + noMockPromptInstance.EXPECT().Run().Return("n", nil) + noMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + noMockPromptuiProxy.EXPECT().NewPrompt().Return(noMockPromptInstance) + errMockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + errMockPromptInstance.EXPECT().SetLabel(gomock.Any()) + errMockPromptInstance.EXPECT().Run().Return("", errors.New("PromptInstance.Run() failed")) + errMockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + errMockPromptuiProxy.EXPECT().NewPrompt().Return(errMockPromptInstance) + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantJrps []model.Jrp + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (answering not yes on the prompt)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: true, + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: noMockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: constant.HISTORY_REMOVE_MESSAGE_REMOVE_ALL_CANCELED + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (set no-confirm option)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: nil, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "3", "4"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, ids in args matches a jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "2", "3"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "3", "4"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, ids in args matches jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "2", "3"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "2"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, id in args matches a jrp)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_NO_ID_SPECIFIED) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, id in args does not match any jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "3", "4"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, ids in args matches jrps)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, one of two ids in args matches jrps but other does not)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "2", "3"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, not favorited, not force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both not favorited, not force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited, the other is not, not force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both favorited, not force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both not favorited, force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited, the other is not, force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both favorited, force, all)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + t.Errorf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + fmt.Printf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: mockUtility, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + fmt.Printf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (PromptInstance.run() failed)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: true, + Force: false, + NoConfirm: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: errMockPromptuiProxy, + Utility: util, + } + if err := historyRemoveOption.historyRemoveRunE(nil, nil); err != nil { + fmt.Printf("historyRemoveOption.historyRemoveRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantJrps: nil, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historyRemoveOption.historyRemoveRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historyRemoveOption.historyRemoveRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + leftJrps, err := jrpRepository.GetAllHistory(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(leftJrps, tt.wantJrps) { + t.Errorf("historyRemoveOption.historyRemoveRunE() : leftJrps =\n%v, want =\n%v", leftJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyRemoveOption_historyRemove(t *testing.T) { + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + All bool + Force bool + NoConfirm bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + PromptuiProxy promptuiproxy.Promptui + Utility utility.UtilityInterface + } + type args struct { + jrpDBFilePath string + IDs []int + } + tests := []struct { + name string + fields fields + args args + wantJrps []model.Jrp + wantErr bool + setup func(*gomock.Controller, *fields) + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, ids are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: nil, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, ids are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, ids are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: nil, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, ids are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, id in args does not match any jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{2}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, id in args matches a jrp)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1, 2}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, ids are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: nil, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, ids are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, id in args does not match any jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "3", "4"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{3, 4}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, ids in args matches a jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1, 2}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, not favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "2", "3"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{2, 3}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, ids are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: nil, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, ids are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, id in args does not match any jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{2}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, id in args matches a jrp)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1, 2}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, ids are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: nil, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: nil, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, ids are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, id in args does not match any jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "3", "4"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{3, 4}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, ids in args matches jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1, 2}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, not force, one of two ids in args matches jrps but other does not)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "2", "3"}, + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{2, 3}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, ids are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: nil, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: nil, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, ids are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, id in args does not match any jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "2"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{2}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, id in args matches a jrp)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, one of two ids in args matches jrps but other does not)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1, 2}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, ids are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: nil, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: nil, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, ids are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, id in args does not match any jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "3", "4"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{3, 4}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, ids in args matches jrps)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "1", "2"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{1, 2}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, favorited, force, one of two ids in args matches jrps but other does not)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove", "2", "3"}, + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{2, 3}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + + { + name: "positive testing (no jrps in the database file, not favorited, not force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, not favorited, not force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, not force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, favorited, force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both not favorited, not force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited, the other is not, not force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both favorited, not force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both not favorited, force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, one is favorited, the other is not, force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file, both favorited, force, all)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: []string{"history", "remove"}, + All: true, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + IDs: []int{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + if _, err := jrpRepository.AddFavoriteByIDs(jrpDBFilePath, []int{1, 2}); err != nil { + t.Errorf("JrpRepository.AddFavoriteByIDs() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &historyRemoveOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + All: tt.fields.All, + Force: tt.fields.Force, + NoConfirm: tt.fields.NoConfirm, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + JrpRepository: tt.fields.JrpRepository, + PromptuiProxy: tt.fields.PromptuiProxy, + Utility: tt.fields.Utility, + } + if err := o.historyRemove(tt.args.jrpDBFilePath, tt.args.IDs); (err != nil) != tt.wantErr { + t.Errorf("historyRemoveOption.historyRemove() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + leftJrps, err := jrpRepository.GetAllHistory(jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(leftJrps, tt.wantJrps) { + t.Errorf("historyRemoveOption.historyRemove() : leftJrps =\n%v, want =\n%v", leftJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyRemoveOption_writeHistoryRemoveResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvproxy.New(), + ) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockPromptInstance := mockpromptuiproxy.NewMockPromptInstanceInterface(mockCtrl) + mockPromptInstance.EXPECT().SetLabel(gomock.Any()).AnyTimes() + mockPromptInstance.EXPECT().Run().Return("y", nil).AnyTimes() + mockPromptuiProxy := mockpromptuiproxy.NewMockPromptui(mockCtrl) + mockPromptuiProxy.EXPECT().NewPrompt().Return(mockPromptInstance).AnyTimes() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is RemovedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyRemoveOption.writeHistoryRemoveResult(repository.RemovedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.GreenString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_SUCCESSFULLY) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is RemovedFailed)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyRemoveOption.writeHistoryRemoveResult(repository.RemovedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_FAILURE) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is RemovedNone)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: false, + Force: false, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyRemoveOption.writeHistoryRemoveResult(repository.RemovedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NONE) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is RemovedNotAll)", + fields: fields{ + t: t, + fnc: func() { + historyRemoveOption := &historyRemoveOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + All: false, + Force: true, + NoConfirm: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + PromptuiProxy: mockPromptuiProxy, + Utility: util, + } + historyRemoveOption.writeHistoryRemoveResult(repository.RemovedNotAll) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_REMOVE_MESSAGE_REMOVED_NOT_ALL) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("historyRemoveOption.writeHistoryRemoveResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("historyRemoveOption.writeHistoryRemoveResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/history_search.go b/cmd/history_search.go new file mode 100644 index 00000000..ff3df8d6 --- /dev/null +++ b/cmd/history_search.go @@ -0,0 +1,180 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// historySearchOption is the struct for history search command. +type historySearchOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + And bool + Number int + All bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + Utility utility.UtilityInterface +} + +// NewHistorySearchCommand creates a new history search command. +func NewHistorySearchCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &historySearchOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.HISTORY_SEARCH_USE + cmd.FieldCommand.Aliases = constant.GetHistorySearchAliases() + cmd.FieldCommand.RunE = o.historySearchRunE + + cmd.PersistentFlags().BoolVarP( + &o.And, + constant.HISTORY_SEARCH_FLAG_AND, + constant.HISTORY_SEARCH_FLAG_AND_SHORTHAND, + constant.HISTORY_SEARCH_FLAG_AND_DEFAULT, + constant.HISTORY_SEARCH_FLAG_AND_DESCRIPTION, + ) + cmd.PersistentFlags().IntVarP(&o.Number, + constant.HISTORY_SEARCH_FLAG_NUMBER, + constant.HISTORY_SEARCH_FLAG_NUMBER_SHORTHAND, + constant.HISTORY_SEARCH_FLAG_NUMBER_DEFAULT, + constant.HISTORY_SEARCH_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.All, + constant.HISTORY_SEARCH_FLAG_ALL, + constant.HISTORY_SEARCH_FLAG_ALL_SHORTHAND, + constant.HISTORY_SEARCH_FLAG_ALL_DEFAULT, + constant.HISTORY_SEARCH_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.Plain, + constant.HISTORY_SEARCH_FLAG_PLAIN, + constant.HISTORY_SEARCH_FLAG_PLAIN_SHORTHAND, + constant.HISTORY_SEARCH_FLAG_PLAIN_DEFAULT, + constant.HISTORY_SEARCH_FLAG_PLAIN_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.HISTORY_SEARCH_HELP_TEMPLATE) + + return cmd +} + +// historySearchRunE is the function that is called when the history search command is executed. +func (o *historySearchOption) historySearchRunE(_ *cobra.Command, _ []string) error { + if len(o.Args) <= 2 { + // if no arguments is given, set default value to args + o.Args = []string{constant.HISTORY_USE, constant.HISTORY_SEARCH_USE, ""} + } + + // set keywords + var keywords []string + for _, arg := range o.Args[2:] { + if arg != "" { + keywords = append(keywords, arg) + } + } + if len(keywords) == 0 { + // if no keywords are provided, write message and return + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED)) + return nil + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.historySearch(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME), keywords) +} + +// historySearch searches the history with the given keywords. +func (o *historySearchOption) historySearch(jrpDBFilePath string, keywords []string) error { + var histories []model.Jrp + var err error + if o.All { + // if all flag is set, search all history + histories, err = o.JrpRepository.SearchAllHistory(jrpDBFilePath, keywords, o.And) + } else { + // search history with the given number + histories, err = o.JrpRepository.SearchHistoryWithNumber( + jrpDBFilePath, + o.Number, + keywords, + o.And, + ) + } + o.writeHistorySearchResult(histories) + + return err +} + +// writeHistorySearchResult writes the history search result. +func (o *historySearchOption) writeHistorySearchResult(histories []model.Jrp) { + if len(histories) != 0 { + if o.Plain { + for _, hist := range histories { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, hist.Phrase) + } + } else { + // if plain flag is not set, write the history as a table + o.JrpWriter.WriteAsTable(o.Out, histories) + } + } else { + // if no history is found, write the message + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND)) + } +} diff --git a/cmd/history_search_test.go b/cmd/history_search_test.go new file mode 100644 index 00000000..ab86ae4a --- /dev/null +++ b/cmd/history_search_test.go @@ -0,0 +1,2771 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewHistorySearchCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewHistorySearchCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewHistorySearchCommand().Execute() : error =\n%v, wantError =\n%v", err, tt.wantError) + } + }) + } +} + +func Test_historySearchOption_historySearchRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_KEYWORDS_PROVIDED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords do not match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test2", "test3"}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 10, + All: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 9)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 9, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 9\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 10)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 11)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 11, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + t.Errorf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + fmt.Printf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: mockUtility, + } + if err := historySearchOption.historySearchRunE(nil, nil); err != nil { + fmt.Printf("historySearchOption.historySearchRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historySearchOption.historySearchRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historySearchOption.historySearchRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historySearchOption_historySearch(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, nil); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (no jrps in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are nil)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, nil); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, args are empty)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords do not match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test2", "test3"}, + And: false, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"test2", "test3"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"test1", "test2"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, or search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "test1", "test2"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"test1", "test2"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, not plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file, and search, plain, keywords match)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "test1", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 10, + All: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 9)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 9, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 9\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 10)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 10, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, number is 11)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{"history", "search", "te", "st"}, + And: true, + Number: 11, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historySearchOption.historySearch(jrpDBFilePath, []string{"te", "st"}); err != nil { + t.Errorf("historySearchOption.historySearch() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historySearchOption.historySearch() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historySearchOption.historySearch() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historySearchOption_writeHistorySearchResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\tprefix\tsuffix\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 1\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\tprefix1\tsuffix1\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\tprefix2\tsuffix2\tβ—‹\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 2\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SEARCH_MESSAGE_NO_RESULT_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, plain)", + fields: fields{ + t: t, + fnc: func() { + historySearchOption := &historySearchOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + And: false, + Number: 1, + All: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historySearchOption.writeHistorySearchResult([]model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("historySearchOption.writeHistorySearchResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("historySearchOption.writeHistorySearchResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/history_show.go b/cmd/history_show.go new file mode 100644 index 00000000..4c7d0bc8 --- /dev/null +++ b/cmd/history_show.go @@ -0,0 +1,171 @@ +package cmd + +import ( + "github.com/spf13/cobra" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" +) + +// historyShowOption is the struct for history show command. +type historyShowOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + All bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + JrpRepository repository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + Utility utility.UtilityInterface +} + +// NewHistoryShowCommand creates a new history show command. +func NewHistoryShowCommand(g *GlobalOption) *cobraproxy.CommandInstance { + o := &historyShowOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: g.Args, + Utility: g.Utility, + } + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = repository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.HISTORY_SHOW_USE + cmd.FieldCommand.Args = cobra.MaximumNArgs(1) + cmd.FieldCommand.RunE = o.historyShowRunE + + cmd.PersistentFlags().IntVarP( + &o.Number, + constant.HISTORY_SHOW_FLAG_NUMBER, + constant.HISTORY_SHOW_FLAG_NUMBER_SHORTHAND, + constant.HISTORY_SHOW_FLAG_NUMBER_DEFAULT, + constant.HISTORY_SHOW_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.All, + constant.HISTORY_SHOW_FLAG_ALL, + constant.HISTORY_SHOW_FLAG_ALL_SHORTHAND, + constant.HISTORY_SHOW_FLAG_ALL_DEFAULT, + constant.HISTORY_SHOW_FLAG_ALL_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP(&o.Plain, + constant.HISTORY_SHOW_FLAG_PLAIN, + constant.HISTORY_SHOW_FLAG_PLAIN_SHORTHAND, + constant.HISTORY_SHOW_FLAG_PLAIN_DEFAULT, + constant.HISTORY_SHOW_FLAG_PLAIN_DESCRIPTION, + ) + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) + cmd.SetHelpTemplate(constant.HISTORY_SHOW_HELP_TEMPLATE) + + cmd.SetArgs(o.Args) + return cmd +} + +// historyShowRunE is the function to run history show command. +func (o *historyShowOption) historyShowRunE(_ *cobra.Command, _ []string) error { + strconvProxy := strconvproxy.New() + if len(o.Args) <= 2 { + // ifno arguments are given, set default value to args + o.Args = []string{constant.HISTORY_USE, constant.HISTORY_SHOW_USE, strconvProxy.Itoa(constant.HISTORY_SHOW_FLAG_NUMBER_DEFAULT)} + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } + + // create the directory if it does not exist + if err := o.Utility.CreateDirIfNotExist(jrpDBFileDirPath); err != nil { + return err + } + + filepathProxy := filepathproxy.New() + return o.historyShow(filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME)) +} + +// historyShow shows the history. +func (o *historyShowOption) historyShow(jrpDBFilePath string) error { + var histories []model.Jrp + var err error + if o.All { + // if all flag is set, get all history + histories, err = o.JrpRepository.GetAllHistory(jrpDBFilePath) + } else { + strconvProxy := strconvproxy.New() + // get the larger number between the given number flag and the largest number that can be converted from the args + num := o.Utility.GetLargerNumber( + o.Number, + o.Utility.GetMaxConvertibleString( + o.Args, + strconvProxy.Itoa(constant.HISTORY_SHOW_FLAG_NUMBER_DEFAULT), + ), + ) + if o.Number != num && o.Number > 0 { + // if the number flag is littler than the default number, set the number flag value to num + num = o.Number + } + // get history with the given num + histories, err = o.JrpRepository.GetHistoryWithNumber( + jrpDBFilePath, + num, + ) + } + o.writeHistoryShowResult(histories) + + return err +} + +// writeHistory writes the history. +func (o *historyShowOption) writeHistoryShowResult(histories []model.Jrp) { + if len(histories) != 0 { + if o.Plain { + for _, history := range histories { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, history.Phrase) + } + } else { + // if plain flag is not set, write the history as a table + o.JrpWriter.WriteAsTable(o.Out, histories) + } + } else { + // if no history is found, write the message + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.HISTORY_SHOW_MESSAGE_NO_HISTORY_FOUND)) + } +} diff --git a/cmd/history_show_test.go b/cmd/history_show_test.go new file mode 100644 index 00000000..dc36909d --- /dev/null +++ b/cmd/history_show_test.go @@ -0,0 +1,1654 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewHistoryShowCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewHistoryShowCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewHistoryShowCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func Test_historyShowOption_historyShowRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShowRunE(nil, nil); err != nil { + t.Errorf("historyShowOption.historyShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShowRunE(nil, nil); err != nil { + t.Errorf("historyShowOption.historyShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShowRunE(nil, nil); err != nil { + t.Errorf("historyShowOption.historyShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 2\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is nil)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShowRunE(nil, nil); err != nil { + t.Errorf("historyShowOption.historyShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is empty)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShowRunE(nil, nil); err != nil { + t.Errorf("historyShowOption.historyShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShowRunE(nil, nil); err != nil { + fmt.Printf("historyShowOption.historyShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: mockUtility, + } + if err := historyShowOption.historyShowRunE(nil, nil); err != nil { + fmt.Printf("historyShowOption.historyShowRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historyShowOption.historyShowRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historyShowOption.historyShowRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyShowOption_historyShow(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 2\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 9)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 9, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 9\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 10)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 10, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 11)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 11, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyShowOption.historyShow(jrpDBFilePath); err != nil { + t.Errorf("historyShowOption.historyShow() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historyShowOption.historyShow() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historyShowOption.historyShow() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyShowOption_writeHistoryShowResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SHOW_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SHOW_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult([]model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\tprefix\tsuffix\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 1\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult([]model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\tprefix1\tsuffix1\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\tprefix2\tsuffix2\tβ—‹\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 2\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_SHOW_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult([]model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, plain)", + fields: fields{ + t: t, + fnc: func() { + historyShowOption := &historyShowOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyShowOption.writeHistoryShowResult([]model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("historyShowOption.writeHistoryShowResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("historyShowOption.writeHistoryShowResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/history_test.go b/cmd/history_test.go new file mode 100644 index 00000000..e74999ce --- /dev/null +++ b/cmd/history_test.go @@ -0,0 +1,1662 @@ +package cmd + +import ( + "errors" + "fmt" + "testing" + + "github.com/yanosea/jrp/app/database/jrp/model" + "github.com/yanosea/jrp/app/database/jrp/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/utility" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" +) + +func TestNewHistoryCommand(t *testing.T) { + type args struct { + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewHistoryCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewHistoryCommand().Execute() error =\n%v, wantError =\n%v", err, tt.wantError) + } + }) + } +} + +func Test_historyOption_historyRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProvider := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProvider.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + mockUtility := mockutility.NewMockUtilityInterface(mockCtrl) + mockUtility.EXPECT().CreateDirIfNotExist(jrpDBFileDirPath).Return(errors.New("Utility.CreateDirIfNotExist() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.historyRunE(nil, nil); err != nil { + t.Errorf("historyOption.historyRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.historyRunE(nil, nil); err != nil { + t.Errorf("historyOption.historyRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.historyRunE(nil, nil); err != nil { + t.Errorf("historyOption.historyRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 2\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is nil)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.historyRunE(nil, nil); err != nil { + t.Errorf("historyOption.historyRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is empty)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.historyRunE(nil, nil); err != nil { + t.Errorf("historyOption.historyRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.historyRunE(nil, nil); err != nil { + fmt.Printf("historyOption.historyRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Utility.CreateDirIfNotExitst() failed)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: mockUtility, + } + if err := historyOption.historyRunE(nil, nil); err != nil { + fmt.Printf("historyOption.historyRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: testutility.TEST_OUTPUT_ANY, + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historyOption.historyRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historyOption.historyRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyOption_history(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, repository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (no jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (one jrp in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 1\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (two jrps in the database file)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\t\t\t\nTOTAL : 2\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, all)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 0, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 9)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 9, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 9\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 10)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 10, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 10\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (11 jrps in the database file, not all, number is 11)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 11, + All: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + if err := historyOption.history(jrpDBFilePath); err != nil { + t.Errorf("historyOption.history() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n3\ttest3\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n4\ttest4\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n5\ttest5\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n6\ttest6\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n7\ttest7\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n8\ttest8\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n9\ttest9\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n10\ttest10\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n11\ttest11\t\t\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\nTOTAL : 11\t\t\t\t\t\t\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + if _, err := jrpRepository.SaveHistory( + jrpDBFilePath, + []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test3", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test4", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test5", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test6", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test7", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test8", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test9", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test10", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test11", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ); err != nil { + t.Errorf("JrpRepository.SaveHistory() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("historyOption.history() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("historyOption.history() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_historyOption_writeHistoryResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + fmtProxy := fmtproxy.New() + jrpRepository := repository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest\tprefix\tsuffix\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 1\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "ID\tPHRASE\tPREFIX\tSUFFIX\tIS FAVORITED\tCREATED AT\tUPDATED AT\n1\ttest1\tprefix1\tsuffix1\t\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n2\ttest2\tprefix2\tsuffix2\tβ—‹\t9999-12-31 00:00:00\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 2\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult(nil) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.HISTORY_MESSAGE_NO_HISTORY_FOUND) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, plain)", + fields: fields{ + t: t, + fnc: func() { + historyOption := &historyOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + Utility: util, + } + historyOption.writeHistoryResult( + []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + IsFavorited: 0, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + IsFavorited: 1, + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + ) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("historyOption.writeHistoryResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("historyOption.writeHistoryResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + }) + } +} diff --git a/cmd/root.go b/cmd/root.go index 60e2e077..239afb8f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,129 +1,350 @@ package cmd import ( - "io" - - "github.com/fatih/color" "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" - "github.com/yanosea/jrp/internal/buildinfo" - "github.com/yanosea/jrp/internal/cmdwrapper" - "github.com/yanosea/jrp/internal/database" - "github.com/yanosea/jrp/internal/fs" - "github.com/yanosea/jrp/internal/usermanager" - "github.com/yanosea/jrp/logic" - "github.com/yanosea/jrp/util" + "github.com/yanosea/jrp/app/database/jrp/model" + jrprepository "github.com/yanosea/jrp/app/database/jrp/repository" + wnjpnrepository "github.com/yanosea/jrp/app/database/wnjpn/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/generator" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/library/versionprovider" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/debug" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/rand" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" ) -var version = "" +// ver is the version of the jrp. +var ver = "" +// GlobalOption is the struct for global option. type GlobalOption struct { - Out io.Writer - ErrOut io.Writer + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface Args []string - NewRootCommand func(ow, ew io.Writer, args []string) cmdwrapper.ICommand + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface } -type RootOption struct { - Out io.Writer - ErrOut io.Writer - Args []string - Number int - Prefix string - Suffix string - Generator logic.Generator +// rootOption is the struct for root command. +type rootOption struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + Prefix string + Suffix string + DryRun bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Generator generator.Generatable + JrpRepository jrprepository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + WNJpnRepository wnjpnrepository.WNJpnRepositoryInterface + Utility utility.UtilityInterface } -func NewGlobalOption(out io.Writer, errOut io.Writer, args []string) *GlobalOption { +// NewGlobalOption creates a new global option. +func NewGlobalOption(fmtProxy fmtproxy.Fmt, osProxy osproxy.Os, strconvProxy strconvproxy.Strconv) *GlobalOption { return &GlobalOption{ - Out: out, - ErrOut: errOut, - Args: args, - NewRootCommand: func(ow, ew io.Writer, _ []string) cmdwrapper.ICommand { - return NewRootCommand(ow, ew, args) - }, + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Utility: utility.New( + fmtProxy, + osProxy, + strconvProxy, + ), + NewRootCommand: NewRootCommand, } } +// Execute executes the jrp. func (g *GlobalOption) Execute() int { rootCmd := g.NewRootCommand(g.Out, g.ErrOut, g.Args) if err := rootCmd.Execute(); err != nil { - util.PrintlnWithWriter(g.ErrOut, color.RedString(err.Error())) + colorProxy := colorproxy.New() + g.Utility.PrintlnWithWriter(g.ErrOut, colorProxy.RedString(err.Error())) return 1 } return 0 } -func NewRootCommand(ow, ew io.Writer, cmdArgs []string) cmdwrapper.ICommand { +// NewRootCommand creates a new root command. +func NewRootCommand(ow, ew ioproxy.WriterInstanceInterface, cmdArgs []string) cobraproxy.CommandInstanceInterface { + util := utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ) g := &GlobalOption{ - Out: ow, - ErrOut: ew, - Args: cmdArgs, + Out: ow, + ErrOut: ew, + Args: cmdArgs, + Utility: util, } - o := &RootOption{ - Out: g.Out, - ErrOut: g.ErrOut, - Args: cmdArgs, + o := &rootOption{ + Out: g.Out, + ErrOut: g.ErrOut, + Args: cmdArgs, + Utility: util, } - v := logic.NewJrpVersionGetter(buildinfo.RealBuildInfoProvider{}) + o.DBFileDirPathProvider = dbfiledirpathprovider.New( + filepathproxy.New(), + osproxy.New(), + userproxy.New(), + ) + o.JrpRepository = jrprepository.New( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + stringsproxy.New(), + ) + o.JrpWriter = jrpwriter.New( + strconvproxy.New(), + tablewriterproxy.New(), + ) + o.WNJpnRepository = wnjpnrepository.New( + sqlproxy.New(), + ) + o.Generator = generator.New( + osproxy.New(), + randproxy.New(), + sqlproxy.New(), + timeproxy.New(), + o.WNJpnRepository, + ) - cmd := &cobra.Command{ - Use: constant.ROOT_USE, - Short: constant.ROOT_SHORT, - Long: constant.ROOT_LONG, - Version: v.GetVersion(version), - SilenceErrors: true, - SilenceUsage: true, - Args: cobra.MaximumNArgs(1), - RunE: o.RootRunE, - } + v := versionprovider.New(debugproxy.New()) - cmd.PersistentFlags().IntVarP(&o.Number, constant.ROOT_FLAG_NUMBER, constant.ROOT_FLAG_NUMBER_SHORTHAND, 1, constant.ROOT_FLAG_NUMBER_DESCRIPTION) - cmd.PersistentFlags().StringVarP(&o.Prefix, constant.ROOT_FLAG_PREFIX, constant.ROOT_FLAG_PREFIX_SHORTHAND, "", constant.ROOT_FLAG_PREFIX_DESCRIPTION) - cmd.PersistentFlags().StringVarP(&o.Suffix, constant.ROOT_FLAG_SUFFIX, constant.ROOT_FLAG_SUFFIX_SHORTHAND, "", constant.ROOT_FLAG_SUFFIX_DESCRIPTION) + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.ROOT_USE + cmd.FieldCommand.Version = v.GetVersion(ver) + cmd.FieldCommand.SilenceErrors = true + cmd.FieldCommand.SilenceUsage = true + cmd.FieldCommand.Args = cobraProxy.MaximumNArgs(1).FieldPositionalArgs + cmd.FieldCommand.RunE = o.rootRunE + + cmd.PersistentFlags().IntVarP( + &o.Number, + constant.ROOT_FLAG_NUMBER, + constant.ROOT_FLAG_NUMBER_SHORTHAND, + constant.ROOT_FLAG_NUMBER_DEFAULT, + constant.ROOT_FLAG_NUMBER_DESCRIPTION, + ) + cmd.PersistentFlags().StringVarP( + &o.Prefix, + constant.ROOT_FLAG_PREFIX, + constant.ROOT_FLAG_PREFIX_SHORTHAND, + constant.ROOT_FLAG_PREFIX_DEFAULT, + constant.ROOT_FLAG_PREFIX_DESCRIPTION, + ) + cmd.PersistentFlags().StringVarP( + &o.Suffix, + constant.ROOT_FLAG_SUFFIX, + constant.ROOT_FLAG_SUFFIX_SHORTHAND, + constant.ROOT_FLAG_SUFFIX_DEFAULT, + constant.ROOT_FLAG_SUFFIX_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.DryRun, + constant.ROOT_FLAG_DRY_RUN, + constant.ROOT_FLAG_DRY_RUN_SHORTHAND, + constant.ROOT_FLAG_DRY_RUN_DEFAULT, + constant.ROOT_FLAG_DRY_RUN_DESCRIPTION, + ) + cmd.PersistentFlags().BoolVarP( + &o.Plain, + constant.ROOT_FLAG_PLAIN, + constant.ROOT_FLAG_PLAIN_SHORTHAND, + constant.ROOT_FLAG_PLAIN_DEFAULT, + constant.ROOT_FLAG_PLAIN_DESCRIPTION, + ) cmd.SetOut(ow) cmd.SetErr(ew) cmd.SetHelpTemplate(constant.ROOT_HELP_TEMPLATE) cmd.AddCommand( - newCompletionCommand(g), NewDownloadCommand(g), + NewFavoriteCommand(g), NewGenerateCommand(g), - newVersionCommand(g), + NewHistoryCommand(g), + NewVersionCommand(g), + NewCompletionCommand(g), ) cmd.SetArgs(cmdArgs) - return cmdwrapper.NewCommandWrapper(cmd) + + return cmd } -func (o *RootOption) RootRunE(_ *cobra.Command, _ []string) error { +// rootRunE is the function to run root command. +func (o *rootOption) rootRunE(_ *cobra.Command, _ []string) error { + var word string + var mode generator.GenerateMode + if o.Prefix != "" && o.Suffix != "" { + // if both prefix and suffix are provided, notify to use only one + colorProxy := colorproxy.New() + o.Utility.PrintlnWithWriter(o.Out, colorProxy.YellowString(constant.GENERATE_MESSAGE_NOTIFY_USE_ONLY_ONE)) + return nil + } else if o.Prefix != "" { + word = o.Prefix + mode = generator.WithPrefix + } else if o.Suffix != "" { + word = o.Suffix + mode = generator.WithSuffix + } + if len(o.Args) == 0 { - o.Args = []string{"1"} + strconvProxy := strconvproxy.New() + // if no args are given, set the default value to the args + o.Args = []string{strconvProxy.Itoa(constant.ROOT_FLAG_NUMBER_DEFAULT)} } - o.Generator = logic.NewJapaneseRandomPhraseGenerator(usermanager.OSUserProvider{}, database.SQLiteProvider{}, fs.OsFileManager{}) + // get jrp db file dir path + wnJpnDBFileDirPath, err := o.DBFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + return err + } + + // get jrp db file dir path + jrpDBFileDirPath, err := o.DBFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + return err + } - return o.RootGenerate() + // execute root command + filepathProxy := filepathproxy.New() + return o.root( + filepathProxy.Join(wnJpnDBFileDirPath, wnjpnrepository.WNJPN_DB_FILE_NAME), + filepathProxy.Join(jrpDBFileDirPath, jrprepository.JRP_DB_FILE_NAME), + word, + mode, + ) } -func (o *RootOption) RootGenerate() error { - jrps, err := o.Generator.Generate( - logic.DefineNumber( +// root generates jrpss and saves them. +func (o *rootOption) root( + wnJpnDBFilePath string, + jrpDBFilePath string, + word string, + mode generator.GenerateMode, +) error { + var jrps []model.Jrp + var err error + jrps, err = o.rootGenerate(wnJpnDBFilePath, word, mode) + if err != nil { + return err + } + err = o.rootSave(jrpDBFilePath, jrps) + if err != nil { + return err + } + o.writeRootResult(jrps) + + return nil +} + +// rootGenerate generates jrpss. +func (o *rootOption) rootGenerate(wnJpnDBFilePath string, word string, mode generator.GenerateMode) ([]model.Jrp, error) { + strconvProxy := strconvproxy.New() + res, jrps, err := o.Generator.GenerateJrp( + wnJpnDBFilePath, + // get the larger number between the given number flag and the largest number that can be converted from the args + o.Utility.GetLargerNumber( o.Number, - logic.GetFirstConvertibleToString(o.Args), + o.Utility.GetMaxConvertibleString( + o.Args, + strconvProxy.Itoa(constant.ROOT_FLAG_NUMBER_DEFAULT), + ), ), - o.Prefix, - o.Suffix, + word, + mode, ) - if err != nil { - return err + o.writeRootGenerateResult(res) + + return jrps, err +} + +// writeRootGenerateResult writes the result of the generation. +func (o *rootOption) writeRootGenerateResult(result generator.GenerateResult) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == generator.GeneratedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.ROOT_MESSAGE_GENERATE_FAILURE) + } else if result == generator.DBFileNotFound { + message = colorProxy.YellowString(constant.ROOT_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) } - for _, jrp := range jrps { - util.PrintlnWithWriter(o.Out, jrp) + if message != "" { + // if success, do not write any message + o.Utility.PrintlnWithWriter(out, message) } +} - return nil +// rootSave saves jrpss. +func (o *rootOption) rootSave(jrpDBFilePath string, jrps []model.Jrp) error { + var res jrprepository.SaveStatus + var err error + if !o.DryRun && len(jrps) != 0 { + // if the dry-run flag is not set and the generated phrases are not empty, save the generated phrases + res, err = o.JrpRepository.SaveHistory(jrpDBFilePath, jrps) + } + o.writeRootSaveResult(res) + return err +} + +// writeRootSaveResult writes the result of saving jrps. +func (o *rootOption) writeRootSaveResult(result jrprepository.SaveStatus) { + var out = o.Out + var message string + colorProxy := colorproxy.New() + if result == jrprepository.SavedFailed { + out = o.ErrOut + message = colorProxy.RedString(constant.ROOT_MESSAGE_SAVED_FAILURE) + } else if result == jrprepository.SavedNone { + message = colorProxy.YellowString(constant.ROOT_MESSAGE_SAVED_NONE) + } else if result == jrprepository.SavedNotAll { + message = colorProxy.YellowString(constant.ROOT_MESSAGE_SAVED_NOT_ALL) + } + + if message != "" { + // if success, do not write any message + o.Utility.PrintlnWithWriter(out, message) + } +} + +// writeRootResult writes the result of the root command. +func (o *rootOption) writeRootResult(jrps []model.Jrp) { + if len(jrps) != 0 { + if o.Plain { + for _, jrp := range jrps { + // if plain flag is set, write only the phrase + o.Utility.PrintlnWithWriter(o.Out, jrp.Phrase) + } + } else { + // if plain flag is not set, write the favorite as a table + o.JrpWriter.WriteGenerateResultAsTable(o.Out, jrps) + } + } } diff --git a/cmd/root_test.go b/cmd/root_test.go index ca899ada..8ec580fc 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -1,234 +1,2322 @@ -package cmd_test +package cmd import ( "errors" - "io" - "os" - "path/filepath" + "fmt" "testing" - gomock "go.uber.org/mock/gomock" - - "github.com/yanosea/jrp/cmd" - "github.com/yanosea/jrp/internal/cmdwrapper" - "github.com/yanosea/jrp/internal/database" - "github.com/yanosea/jrp/internal/fs" - "github.com/yanosea/jrp/internal/gzip" - "github.com/yanosea/jrp/internal/httpclient" - "github.com/yanosea/jrp/internal/iomanager" - "github.com/yanosea/jrp/internal/spinnerservice" - "github.com/yanosea/jrp/internal/usermanager" - "github.com/yanosea/jrp/logic" - - mock_cmdwrapper "github.com/yanosea/jrp/mock/cmdwrapper" - mock_generator "github.com/yanosea/jrp/mock/generator" + "github.com/yanosea/jrp/app/database/jrp/model" + jrprepository "github.com/yanosea/jrp/app/database/jrp/repository" + wnjpnrepository "github.com/yanosea/jrp/app/database/wnjpn/repository" + "github.com/yanosea/jrp/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/app/library/downloader" + "github.com/yanosea/jrp/app/library/generator" + "github.com/yanosea/jrp/app/library/jrpwriter" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/color" + "github.com/yanosea/jrp/app/proxy/filepath" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/gzip" + "github.com/yanosea/jrp/app/proxy/http" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/rand" + "github.com/yanosea/jrp/app/proxy/sort" + "github.com/yanosea/jrp/app/proxy/sql" + "github.com/yanosea/jrp/app/proxy/strconv" + "github.com/yanosea/jrp/app/proxy/strings" + "github.com/yanosea/jrp/app/proxy/tablewriter" + "github.com/yanosea/jrp/app/proxy/time" + "github.com/yanosea/jrp/app/proxy/user" + "github.com/yanosea/jrp/cmd/constant" + + mockjrprepository "github.com/yanosea/jrp/mock/app/database/jrp/repository" + "github.com/yanosea/jrp/mock/app/library/dbfiledirpathprovider" + "github.com/yanosea/jrp/mock/app/library/generator" + "github.com/yanosea/jrp/mock/app/proxy/cobra" + "github.com/yanosea/jrp/test/testutility" + "go.uber.org/mock/gomock" ) func TestNewGlobalOption(t *testing.T) { type args struct { - out io.Writer - errOut io.Writer - args []string + fmtProxy fmtproxy.Fmt + osProxy osproxy.Os + strconvProxy strconvproxy.Strconv } tests := []struct { - name string - args args + name string + args args + wantExitCode int }{ { name: "positive testing", - args: args{out: os.Stdout, errOut: os.Stderr, args: []string{}}, + args: args{ + fmtProxy: fmtproxy.New(), + osProxy: osproxy.New(), + strconvProxy: strconvproxy.New(), + }, + wantExitCode: 0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - u := cmd.NewGlobalOption(tt.args.out, tt.args.errOut, tt.args.args) - if u == nil { - t.Errorf("NewGlobalOption() : returned nil") + got := NewGlobalOption(tt.args.fmtProxy, tt.args.osProxy, tt.args.strconvProxy) + if gotExitCode := got.Execute(); gotExitCode != tt.wantExitCode { + t.Errorf("NewGlobalOption().Execute() : gotExitCode=\n%v, wantExitCode =\n%v", gotExitCode, tt.wantExitCode) } }) } } -func TestExecute(t *testing.T) { - tu := usermanager.OSUserProvider{} - tcu, _ := tu.Current() - dbFileDirPath := filepath.Join(tcu.HomeDir, ".local", "share", "jrp") - tdl := logic.NewDBFileDownloader(usermanager.OSUserProvider{}, fs.OsFileManager{}, httpclient.DefaultHTTPClient{}, iomanager.DefaultIOHelper{}, gzip.DefaultGzipHandler{}, spinnerservice.NewRealSpinnerService()) +func TestGlobalOption_Execute(t *testing.T) { + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + tests := []struct { + name string + fields fields + want int + setup func(mockCtrl *gomock.Controller, tt *fields) + }{ + { + name: "positive testing", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + NewRootCommand: NewRootCommand, + }, + want: 0, + setup: nil, + }, + { + name: "negative testing (GlobalOption.Execute() failed)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Utility: utility.New( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + NewRootCommand: nil, + }, + want: 1, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + tt.NewRootCommand = func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface { + mockCobraCommandInstance := mockcobraproxy.NewMockCommandInstanceInterface(mockCtrl) + mockCobraCommandInstance.EXPECT().Execute().Return(errors.New("Execute() failed")) + return mockCobraCommandInstance + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, + } + if got := g.Execute(); got != tt.want { + t.Errorf("GlobalOption.Execute() got =\n%v, want =\n%v", got, tt.want) + } + }) + } +} +func TestNewRootCommand(t *testing.T) { type args struct { - globalOption *cmd.GlobalOption + ow ioproxy.WriterInstanceInterface + ew ioproxy.WriterInstanceInterface + cmdArgs []string } tests := []struct { - name string - args args - want int - wantErr bool - setup func(mockCmd *mock_cmdwrapper.MockICommand) + name string + args args + wantError bool }{ { - name: "positive testing (with no args, no db file)", - args: args{globalOption: cmd.NewGlobalOption(os.Stdout, os.Stderr, []string{})}, - want: 0, - wantErr: false, - setup: func(_ *mock_cmdwrapper.MockICommand) { - os.RemoveAll(dbFileDirPath) + name: "positive testing", + args: args{ + ow: osproxy.Stdout, + ew: osproxy.Stderr, + cmdArgs: osproxy.Args[1:], }, - }, { - name: "positive testing (with no args, with db file)", - args: args{globalOption: cmd.NewGlobalOption(os.Stdout, os.Stderr, []string{})}, - want: 0, - wantErr: false, - setup: func(_ *mock_cmdwrapper.MockICommand) { - os.RemoveAll(dbFileDirPath) - if err := tdl.Download(); err != nil { - t.Error(err) + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewRootCommand(tt.args.ow, tt.args.ew, tt.args.cmdArgs) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewRootCommand().Execute() error =\n%v, wantError =\n%v", err, tt.wantError) + } + }) + } +} + +func Test_rootOption_rootRunE(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockDBFileDirPathProviderFailsWNJpnDBFileDirPath := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProviderFailsWNJpnDBFileDirPath.EXPECT().GetWNJpnDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetWNJpnDBFileDirPath() failed")) + mockDBFileDirPathProviderFailsGetJrpDBFileDirPath := mockdbfiledirpathprovider.NewMockDBFileDirPathProvidable(mockCtrl) + mockDBFileDirPathProviderFailsGetJrpDBFileDirPath.EXPECT().GetWNJpnDBFileDirPath().Return("", nil) + mockDBFileDirPathProviderFailsGetJrpDBFileDirPath.EXPECT().GetJrpDBFileDirPath().Return("", errors.New("DBFileDirPathProvider.GetJrpDBFileDirPath() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (wn jpn database file does not exist)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + t.Errorf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) } }, - }, { - name: "positive testing (with args, no db file)", - args: args{globalOption: cmd.NewGlobalOption(os.Stdout, os.Stderr, []string{"2"})}, - want: 0, - wantErr: false, - setup: func(_ *mock_cmdwrapper.MockICommand) { - os.RemoveAll(dbFileDirPath) + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } }, - }, { - name: "positive testing (with args, db file)", - args: args{globalOption: cmd.NewGlobalOption(os.Stdout, os.Stderr, []string{"2"})}, - want: 0, - wantErr: false, - setup: func(_ *mock_cmdwrapper.MockICommand) { - os.RemoveAll(dbFileDirPath) - if err := tdl.Download(); err != nil { - t.Error(err) + }, + { + name: "positive testing (wn jpn database file exists)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + t.Errorf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (both prefix and suffix are specified)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "prefix", + Suffix: "suffix", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + t.Errorf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_NOTIFY_USE_ONLY_ONE) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (only prefix is specified)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "prefix", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + t.Errorf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (only suffix is specified)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "suffix", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + t.Errorf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is nil)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: nil, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + t.Errorf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (Args is empty)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + t.Errorf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) } }, - }, { - name: "negative testing (Execute() fails)", - args: args{globalOption: cmd.NewGlobalOption(os.Stdout, os.Stderr, []string{})}, - want: 1, - wantErr: true, - setup: func(mockCmd *mock_cmdwrapper.MockICommand) { - mockCmd.EXPECT().Execute().Return(errors.New("failed to execute command")) + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetWNJpnDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProviderFailsWNJpnDBFileDirPath, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + fmt.Printf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (DBFileDirPathProvider.GetJrpDBFileDirPath() failed)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: []string{}, + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: mockDBFileDirPathProviderFailsGetJrpDBFileDirPath, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.rootRunE(nil, nil); err != nil { + fmt.Printf("rootOption.rootRunE() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } }, }, } - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - if tt.setup != nil && !tt.wantErr { - tt.setup(nil) + if tt.setup != nil { + tt.setup() + } + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("rootOption.rootRunE() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("rootOption.rootRunE() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} - if tt.setup != nil && tt.wantErr { - mockCmd := mock_cmdwrapper.NewMockICommand(ctrl) - tt.setup(mockCmd) - tt.args.globalOption.NewRootCommand = func(ow, ew io.Writer, cmdArgs []string) cmdwrapper.ICommand { - return mockCmd +func Test_rootOption_root(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, jrprepository.JRP_DB_FILE_NAME) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + mockGenerator := mockgenerator.NewMockGeneratable(mockCtrl) + mockGenerator.EXPECT().GenerateJrp(wnJpnDBFilePath, 1, "", generator.WithNoPrefixOrSuffix).Return(generator.GeneratedFailed, nil, errors.New("Generator.GenerateJrp() failed")) + mockJrpRepository := mockjrprepository.NewMockJrpRepositoryInterface(mockCtrl) + mockJrpRepository.EXPECT().SaveHistory(gomock.Any(), gomock.Any()).Return(jrprepository.SavedFailed, errors.New("JrpRepository.SaveHistory() failed")) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + setup func() + cleanup func() + }{ + { + name: "positive testing (wn jpn database file does not exist)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.root(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + t.Errorf("rootOption.root() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (wn jpn database file exists)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.root(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + t.Errorf("rootOption.root() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: testutility.TEST_OUTPUT_ANY, + wantStdErr: "", + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) } + }, + }, + { + name: "negative testing (rootOption.rootGenerate() failed)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: mockGenerator, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.root(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + fmt.Printf("rootOption.root() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.ROOT_MESSAGE_GENERATE_FAILURE) + "\n", + wantErr: false, + setup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (rootOption.rootSave() failed)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: mockJrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + if err := rootOption.root(wnJpnDBFilePath, jrpDBFilePath, "", generator.WithNoPrefixOrSuffix); err != nil { + fmt.Printf("rootOption.root() : error =\n%v", err) + } + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.ROOT_MESSAGE_SAVED_FAILURE) + "\n", + wantErr: false, + setup: func() { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.setup != nil { + tt.setup() } - - if got := tt.args.globalOption.Execute(); (got != 0) != tt.wantErr { - t.Errorf("Execute() : exit code = %v, want = %v", got, tt.want) + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("rootOption.root() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("rootOption.root() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) + } + if tt.cleanup != nil { + tt.cleanup() } }) } - os.RemoveAll(dbFileDirPath) } -func TestNewRootCommand(t *testing.T) { +func Test_rootOption_rootGenerate(t *testing.T) { + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + wnJpnDBFileDirPath, err := dbFileDirPathProvider.GetWNJpnDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetWNJpnDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + dl := downloader.New( + filepathProxy, + gzipproxy.New(), + httpproxy.New(), + ioproxy.New(), + osProxy, + util, + ) + wnJpnDBFilePath := filepathProxy.Join(wnJpnDBFileDirPath, downloader.WNJPN_DB_FILE_NAME) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + Prefix string + Suffix string + DryRun bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Generator generator.Generatable + JrpRepository jrprepository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + WNJpnRepository wnjpnrepository.WNJpnRepositoryInterface + Utility utility.UtilityInterface + } type args struct { - out io.Writer - errOut io.Writer - args []string + wnJpnDBFilePath string + word string + mode generator.GenerateMode } tests := []struct { - name string - args args + name string + fields fields + args args + wantCount int + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() }{ { - name: "positive testing", - args: args{out: os.Stdout, errOut: os.Stderr, args: []string{}}, + name: "positive testing (wn jpn database file does not exist)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + word: "dummy", + mode: generator.WithNoPrefixOrSuffix, + }, + wantCount: 0, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (wn jpn database file exists)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "prefix", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + word: "", + mode: generator.WithNoPrefixOrSuffix, + }, + wantCount: 1, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "negative testing (Generator.GenerateJrp() failed)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "suffix", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: nil, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + wnJpnDBFilePath: wnJpnDBFilePath, + word: "", + mode: generator.WithNoPrefixOrSuffix, + }, + wantCount: 0, + wantErr: true, + setup: func(mockCtrl *gomock.Controller, tt *fields) { + if _, err := dl.DownloadWNJpnDBFile(wnJpnDBFileDirPath); err != nil { + t.Errorf("Downloader.DownloadWNJpnDBFile() : error =\n%v", err) + } + mockGenerator := mockgenerator.NewMockGeneratable(mockCtrl) + mockGenerator.EXPECT().GenerateJrp(wnJpnDBFilePath, 1, "", generator.WithNoPrefixOrSuffix).Return(generator.GeneratedFailed, nil, errors.New("Generator.GenerateJrp() failed")) + tt.Generator = mockGenerator + }, + cleanup: func() { + if err := osProxy.RemoveAll(wnJpnDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := cmd.NewRootCommand(tt.args.out, tt.args.errOut, tt.args.args) - if got == nil { - t.Errorf("NewRootCommand() : returned nil") + if tt.setup != nil { + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &rootOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Number: tt.fields.Number, + Prefix: tt.fields.Prefix, + Suffix: tt.fields.Suffix, + DryRun: tt.fields.DryRun, + Plain: tt.fields.Plain, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + Generator: tt.fields.Generator, + JrpRepository: tt.fields.JrpRepository, + JrpWriter: tt.fields.JrpWriter, + WNJpnRepository: tt.fields.WNJpnRepository, + Utility: tt.fields.Utility, + } + got, err := o.rootGenerate(tt.args.wnJpnDBFilePath, tt.args.word, tt.args.mode) + if (err != nil) != tt.wantErr { + t.Errorf("rootOption.rootGenerate() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + return + } + if len(got) != tt.wantCount { + t.Errorf("rootOption.rootGenerate() : len(got) =\n%v, wantCount =\n%v", len(got), tt.wantCount) + } + if tt.cleanup != nil { + tt.cleanup() } }) } } -func TestRootRunE(t *testing.T) { - type args struct { - o cmd.RootOption +func Test_rootOption_writeRootGenerateResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer } tests := []struct { - name string - args args + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool }{ { - name: "positive testing", - args: args{o: cmd.RootOption{Out: os.Stdout, ErrOut: os.Stderr, Args: nil, Number: 0, Generator: logic.NewJapaneseRandomPhraseGenerator(usermanager.OSUserProvider{}, database.SQLiteProvider{}, fs.OsFileManager{})}}, + name: "positive testing (result is GeneratedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootGenerateResult(generator.GeneratedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is DBFileNotFound)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootGenerateResult(generator.DBFileNotFound) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is GeneratedFailed)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootGenerateResult(generator.GeneratedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.ROOT_MESSAGE_GENERATE_FAILURE) + "\n", + wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - err := tt.args.o.RootRunE(nil, nil) - if err != nil { - t.Errorf("RootRunE() : error = %v", err) + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("rootOption.writeRootGenerateResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("rootOption.writeRootGenerateResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } }) } } -func TestRootGenerate(t *testing.T) { +func Test_rootOption_rootSave(t *testing.T) { + osProxy := osproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + jrpDBFileDirPath, err := dbFileDirPathProvider.GetJrpDBFileDirPath() + if err != nil { + t.Errorf("DBFileDirPathProvider.GetJrpDBFileDirPath() : error =\n%v", err) + } + filepathProxy := filepathproxy.New() + jrpDBFilePath := filepathProxy.Join(jrpDBFileDirPath, jrprepository.JRP_DB_FILE_NAME) + timeProxy := timeproxy.New() + jrpChecker := testutility.NewJrpChecker( + fmtproxy.New(), + sortproxy.New(), + sqlproxy.New(), + strconvproxy.New(), + stringsproxy.New(), + ) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Number int + Prefix string + Suffix string + DryRun bool + Plain bool + DBFileDirPathProvider dbfiledirpathprovider.DBFileDirPathProvidable + Generator generator.Generatable + JrpRepository jrprepository.JrpRepositoryInterface + JrpWriter jrpwriter.JrpWritable + WNJpnRepository wnjpnrepository.WNJpnRepositoryInterface + Utility utility.UtilityInterface + } type args struct { - o cmd.RootOption + jrpDBFilePath string + jrps []model.Jrp } tests := []struct { - name string - args args - wantErr bool - setup func(mockCtrl *gomock.Controller, tt *args) + name string + fields fields + args args + wantJrps []model.Jrp + wantErr bool + setup func(mockCtrl *gomock.Controller, tt *fields) + cleanup func() }{ { - name: "positive testing", - args: args{o: cmd.RootOption{Out: os.Stdout, ErrOut: os.Stderr, Args: []string{""}, Number: 1, Prefix: "", Suffix: "", Generator: logic.NewJapaneseRandomPhraseGenerator(usermanager.OSUserProvider{}, database.SQLiteProvider{}, fs.OsFileManager{})}}, + name: "positive testing (not dry run, jrps are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: nil, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not dry run, jrps are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not dry run, jrps is one)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, wantErr: false, - setup: nil, - }, { - name: "negative testing (Generate() fails)", - args: args{o: cmd.RootOption{Out: os.Stdout, ErrOut: os.Stderr, Args: []string{""}, Number: 1, Prefix: "", Suffix: "", Generator: nil}}, - wantErr: true, - setup: func(mockCtrl *gomock.Controller, tt *args) { - mg := mock_generator.NewMockGenerator(mockCtrl) - mg.EXPECT().Generate(tt.o.Number, tt.o.Prefix, tt.o.Suffix).Return(nil, errors.New("failed to generate japanese random phrase")) - tt.o.Generator = mg + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (not dry run, jrps are two)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: []model.Jrp{ + { + ID: 1, + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + ID: 2, + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps are nil)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: nil, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps are empty)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{}, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps is one)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + }, + { + name: "positive testing (dry run, jrps are two)", + fields: fields{ + Out: osproxy.Stdout, + ErrOut: osproxy.Stderr, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: true, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + }, + args: args{ + jrpDBFilePath: jrpDBFilePath, + jrps: []model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString(""), + Suffix: sqlProxy.StringToNullString(""), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + UpdatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }, + }, + wantJrps: nil, + wantErr: false, + setup: func(_ *gomock.Controller, _ *fields) { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } + }, + cleanup: func() { + if err := osProxy.RemoveAll(jrpDBFilePath); err != nil { + t.Errorf("OsProxy.RemoveAll() : error =\n%v", err) + } }, }, } - for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - if tt.setup != nil { - tt.setup(ctrl, &tt.args) + mockCtrl := gomock.NewController(t) + defer mockCtrl.Finish() + tt.setup(mockCtrl, &tt.fields) + } + o := &rootOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Number: tt.fields.Number, + Prefix: tt.fields.Prefix, + Suffix: tt.fields.Suffix, + DryRun: tt.fields.DryRun, + Plain: tt.fields.Plain, + DBFileDirPathProvider: tt.fields.DBFileDirPathProvider, + Generator: tt.fields.Generator, + JrpRepository: tt.fields.JrpRepository, + JrpWriter: tt.fields.JrpWriter, + WNJpnRepository: tt.fields.WNJpnRepository, + Utility: tt.fields.Utility, + } + if err := o.rootSave(tt.args.jrpDBFilePath, tt.args.jrps); (err != nil) != tt.wantErr { + t.Errorf("rootOption.rootSave() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + savedJrps, err := jrpRepository.GetAllHistory(tt.args.jrpDBFilePath) + if err != nil { + t.Errorf("JrpRepository.GetAllHistory() : error =\n%v", err) + } + if !jrpChecker.IsSameJrps(savedJrps, tt.wantJrps) { + t.Errorf("rootOption.rootSave() : savedJrps =\n%v, want =\n%v", savedJrps, tt.wantJrps) + } + if tt.cleanup != nil { + tt.cleanup() + } + }) + } +} + +func Test_rootOption_writeRootSaveResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + colorProxy := colorproxy.New() + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (result is SavedSuccessfully)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootSaveResult(jrprepository.SavedSuccessfully) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is SaveFailed)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootSaveResult(jrprepository.SavedFailed) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: colorProxy.RedString(constant.ROOT_MESSAGE_SAVED_FAILURE) + "\n", + wantErr: false, + }, + { + name: "positive testing (result is SavedNone)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootSaveResult(jrprepository.SavedNone) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_SAVED_NONE) + "\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (result is SavedNotAll)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootSaveResult(jrprepository.SavedNotAll) + }, + capturer: capturer, + }, + wantStdOut: colorProxy.YellowString(constant.ROOT_MESSAGE_SAVED_NOT_ALL) + "\n", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if (err != nil) != tt.wantErr { + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if tt.wantStdOut != testutility.TEST_OUTPUT_ANY && stdout != tt.wantStdOut { + t.Errorf("rootOption.writeRootSaveResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if tt.wantStdErr != testutility.TEST_OUTPUT_ANY && stderr != tt.wantStdErr { + t.Errorf("rootOption.writeRootSaveResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } + }) + } +} - err := tt.args.o.RootGenerate() +func Test_rootOption_writeRootResult(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + dbFileDirPathProvider := dbfiledirpathprovider.New( + filepathproxy.New(), + osProxy, + userproxy.New(), + ) + sqlProxy := sqlproxy.New() + wnJpnRepository := wnjpnrepository.New( + sqlProxy, + ) + gen := generator.New( + osProxy, + randproxy.New(), + sqlProxy, + timeproxy.New(), + wnJpnRepository, + ) + fmtProxy := fmtproxy.New() + jrpRepository := jrprepository.New( + fmtProxy, + sortproxy.New(), + sqlProxy, + stringsproxy.New(), + ) + strconvProxy := strconvproxy.New() + jrpWriter := jrpwriter.New( + strconvProxy, + tablewriterproxy.New(), + ) + util := utility.New( + fmtProxy, + osProxy, + strconvProxy, + ) + timeProxy := timeproxy.New() + + type fields struct { + t *testing.T + fnc func() + capturer *testutility.Capturer + } + tests := []struct { + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool + }{ + { + name: "positive testing (jrps are nil, not plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult(nil) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, not plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, not plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult([]model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\tPREFIX\tSUFFIX\tCREATED AT\ntest\tprefix\tsuffix\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 1\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: false, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult([]model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "PHRASE\tPREFIX\tSUFFIX\tCREATED AT\ntest1\tprefix1\tsuffix1\t9999-12-31 00:00:00\ntest2\tprefix2\tsuffix2\t9999-12-31 00:00:00\n\t\t\t\nTOTAL : 2\t\t\t\n", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are nil, plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult(nil) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps are empty, plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult([]model.Jrp{}) + }, + capturer: capturer, + }, + wantStdOut: "", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is one, plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult([]model.Jrp{ + { + Phrase: "test", + Prefix: sqlProxy.StringToNullString("prefix"), + Suffix: sqlProxy.StringToNullString("suffix"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test", + wantStdErr: "", + wantErr: false, + }, + { + name: "positive testing (jrps is two, not plain)", + fields: fields{ + t: t, + fnc: func() { + rootOption := &rootOption{ + Out: capturer.OutBuffer, + ErrOut: capturer.ErrBuffer, + Args: osproxy.Args[1:], + Number: 1, + Prefix: "", + Suffix: "", + DryRun: false, + Plain: true, + DBFileDirPathProvider: dbFileDirPathProvider, + Generator: gen, + JrpRepository: jrpRepository, + JrpWriter: jrpWriter, + WNJpnRepository: wnJpnRepository, + Utility: util, + } + rootOption.writeRootResult([]model.Jrp{ + { + Phrase: "test1", + Prefix: sqlProxy.StringToNullString("prefix1"), + Suffix: sqlProxy.StringToNullString("suffix1"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + { + Phrase: "test2", + Prefix: sqlProxy.StringToNullString("prefix2"), + Suffix: sqlProxy.StringToNullString("suffix2"), + CreatedAt: timeProxy.Date(9999, 12, 31, 0, 0, 0, 0, &timeproxy.UTC), + }, + }) + }, + capturer: capturer, + }, + wantStdOut: "test1\ntest2", + wantStdErr: "", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + stdout = testutility.RemoveTabAndSpaceAndLf(stdout) + stderr = testutility.RemoveTabAndSpaceAndLf(stderr) + tt.wantStdOut = testutility.RemoveTabAndSpaceAndLf(tt.wantStdOut) + tt.wantStdErr = testutility.RemoveTabAndSpaceAndLf(tt.wantStdErr) if (err != nil) != tt.wantErr { - t.Errorf("RootGenerate() : error = %v, wantErr = %v", err, tt.wantErr) + t.Errorf("Capturer.CaptureOutput() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("rootOption.writeRootResult() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) + } + if stderr != tt.wantStdErr { + t.Errorf("rootOption.writeRootResult() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } }) } diff --git a/cmd/version.go b/cmd/version.go index 15828fa6..d233e8c3 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -1,36 +1,41 @@ package cmd import ( - "fmt" - "github.com/spf13/cobra" - "github.com/yanosea/jrp/constant" - "github.com/yanosea/jrp/internal/buildinfo" - "github.com/yanosea/jrp/logic" - "github.com/yanosea/jrp/util" + "github.com/yanosea/jrp/app/library/versionprovider" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/debug" + fmtproxy "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/cmd/constant" ) -func newVersionCommand(globalOption *GlobalOption) *cobra.Command { - cmd := &cobra.Command{ - Use: constant.VERSION_USE, - Short: constant.VERSION_SHORT, - Long: constant.VERSION_LONG, - RunE: func(cmd *cobra.Command, args []string) error { - return globalOption.version() - }, - } - - cmd.SetOut(globalOption.Out) - cmd.SetErr(globalOption.ErrOut) +// NewVersionCommand creates a new version command. +func NewVersionCommand(g *GlobalOption) *cobraproxy.CommandInstance { + cobraProxy := cobraproxy.New() + cmd := cobraProxy.NewCommand() + + cmd.FieldCommand.Use = constant.VERSION_USE + cmd.FieldCommand.RunE = g.versionRunE + + cmd.SetOut(g.Out) + cmd.SetErr(g.ErrOut) cmd.SetHelpTemplate(constant.VERSION_HELP_TEMPLATE) return cmd } +// versionRunE is the function that is called when the version command is executed. +func (g *GlobalOption) versionRunE(_ *cobra.Command, _ []string) error { + return g.version() +} + +// version shows the version of jrp. func (g *GlobalOption) version() error { - v := logic.NewJrpVersionGetter(buildinfo.RealBuildInfoProvider{}) - // show version - util.PrintlnWithWriter(g.Out, fmt.Sprintf(constant.VERSION_MESSAGE_TEMPLATE, v.GetVersion(version))) + v := versionprovider.New(debugproxy.New()) + fmtProxy := fmtproxy.New() + // get version from buildinfo and write it + g.Utility.PrintlnWithWriter(g.Out, fmtProxy.Sprintf(constant.VERSION_MESSAGE_TEMPLATE, v.GetVersion(ver))) + return nil } diff --git a/cmd/version_test.go b/cmd/version_test.go index 32d28c1e..c9bc85cf 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -1,76 +1,155 @@ package cmd import ( - "io" - "os" "testing" "github.com/spf13/cobra" + "github.com/yanosea/jrp/app/library/utility" + "github.com/yanosea/jrp/app/proxy/buffer" + "github.com/yanosea/jrp/app/proxy/cobra" + "github.com/yanosea/jrp/app/proxy/fmt" + "github.com/yanosea/jrp/app/proxy/io" + "github.com/yanosea/jrp/app/proxy/os" + "github.com/yanosea/jrp/app/proxy/strconv" - "github.com/yanosea/jrp/constant" + "github.com/yanosea/jrp/test/testutility" ) func TestNewVersionCommand(t *testing.T) { type args struct { - globalOption *GlobalOption + g *GlobalOption + } + tests := []struct { + name string + args args + wantError bool + }{ + { + name: "positive testing", + args: args{ + g: NewGlobalOption( + fmtproxy.New(), + osproxy.New(), + strconvproxy.New(), + ), + }, + wantError: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewVersionCommand(tt.args.g) + if err := got.Execute(); (err != nil) != tt.wantError { + t.Errorf("NewVersionCommand().Execute() : error =\n%v", err) + } + }) + } +} + +func TestGlobalOption_versionRunE(t *testing.T) { + globalOption := NewGlobalOption(fmtproxy.New(), osproxy.New(), strconvproxy.New()) + + type fields struct { + Out ioproxy.WriterInstanceInterface + ErrOut ioproxy.WriterInstanceInterface + Args []string + Utility utility.UtilityInterface + NewRootCommand func(ow, ew ioproxy.WriterInstanceInterface, args []string) cobraproxy.CommandInstanceInterface + } + type args struct { + in0 *cobra.Command + in1 []string } tests := []struct { name string + fields fields args args - want *cobra.Command wantErr bool }{ { name: "positive testing", - args: args{globalOption: &GlobalOption{Out: os.Stdout, ErrOut: os.Stderr}}, - want: &cobra.Command{ - Use: constant.VERSION_USE, - Short: constant.VERSION_SHORT, - Long: constant.VERSION_LONG, + fields: fields{ + Out: globalOption.Out, + ErrOut: globalOption.ErrOut, + Args: globalOption.Args, + Utility: globalOption.Utility, + NewRootCommand: globalOption.NewRootCommand, + }, + args: args{ + in0: nil, + in1: nil, }, - wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := newVersionCommand(tt.args.globalOption) - if got.Use != tt.want.Use || got.Short != tt.want.Short || got.Long != tt.want.Long { - t.Errorf("newVersionCommand() : got = %v, want = %v", got, tt.want) + g := &GlobalOption{ + Out: tt.fields.Out, + ErrOut: tt.fields.ErrOut, + Args: tt.fields.Args, + Utility: tt.fields.Utility, + NewRootCommand: tt.fields.NewRootCommand, } - if err := got.Execute(); (err != nil) != tt.wantErr { - t.Errorf("newVersionCommand().Execute() : error = %v, wantErr = %v", err, tt.wantErr) + if err := g.versionRunE(tt.args.in0, tt.args.in1); (err != nil) != tt.wantErr { + t.Errorf("GlobalOption.versionRunE() : error =\n%v, wantErr =\n%v", err, tt.wantErr) } }) } } func TestGlobalOption_version(t *testing.T) { + osProxy := osproxy.New() + capturer := testutility.NewCapturer( + bufferproxy.New(), + bufferproxy.New(), + osProxy, + ) + globalOption := NewGlobalOption(fmtproxy.New(), osProxy, strconvproxy.New()) + globalOption.Out = capturer.OutBuffer + globalOption.ErrOut = capturer.ErrBuffer + type fields struct { - Out io.Writer - ErrOut io.Writer + t *testing.T + fnc func() + capturer *testutility.Capturer } tests := []struct { - name string - fields fields - wantErr bool + name string + fields fields + wantStdOut string + wantStdErr string + wantErr bool }{ { name: "positive testing", fields: fields{ - Out: os.Stdout, - ErrOut: os.Stderr, + t: t, + fnc: func() { + if err := globalOption.version(); err != nil { + t.Errorf("GlobalOption.version() : error =\n%v", err) + } + }, + capturer: capturer, }, - wantErr: false, + wantStdOut: "jrp version devel\n", + wantStdErr: "", + wantErr: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - g := &GlobalOption{ - Out: tt.fields.Out, - ErrOut: tt.fields.ErrOut, + stdout, stderr, err := tt.fields.capturer.CaptureOutput( + tt.fields.t, + tt.fields.fnc, + ) + if err != nil { + t.Errorf("GlobalOption.completion() : error =\n%v, wantErr =\n%v", err, tt.wantErr) + } + if stdout != tt.wantStdOut { + t.Errorf("GlobalOption.completion() : stdout =\n%v, wantStdOut =\n%v", stdout, tt.wantStdOut) } - if err := g.version(); (err != nil) != tt.wantErr { - t.Errorf("GlobalOption.version() : error = %v, wantErr = %v", err, tt.wantErr) + if stderr != tt.wantStdErr { + t.Errorf("GlobalOption.completion() : stderr =\n%v, wantStdErr =\n%v", stderr, tt.wantStdErr) } }) } diff --git a/constant/completion_bash.go b/constant/completion_bash.go deleted file mode 100644 index d2376e11..00000000 --- a/constant/completion_bash.go +++ /dev/null @@ -1,53 +0,0 @@ -package constant - -const ( - COMPLETION_BASH_HELP_TEMPLATE = `πŸ”§πŸš Generate the autocompletion script for the bash shell. - -This script depends on the "bash-completion" package. -If it is not installed already, you can install it via your OS's package manager. - -To load completions in your current shell session: - - source <(jrp completion bash) - -To load completions for every new session, execute once: - - - 🐧 Linux: - - jrp completion bash > /etc/bash_completion.d/jrp - - - 🍎 macOS: - - jrp completion bash > $(brew --prefix)/etc/bash_completion.d/jrp - -You will need to start a new shell for this setup to take effect. - -Usage: - jrp completion bash [flags] - -Flags: - -h, --help 🀝 help for bash -` - COMPLETION_BASH_USE = "bash" - COMPLETION_BASH_SHORT = "πŸ”§πŸš Generate the autocompletion script for the bash shell." - COMPLETION_BASH_LONG = `πŸ”§πŸš Generate the autocompletion script for the bash shell. - -This script depends on the "bash-completion" package. -If it is not installed already, you can install it via your OS's package manager. - -To load completions in your current shell session: - - source <(jrp completion bash) - -To load completions for every new session, execute once: - - - 🐧 Linux: - - jrp completion bash > /etc/bash_completion.d/jrp - - - 🍎 macOS: - - jrp completion bash > $(brew --prefix)/etc/bash_completion.d/jrp - -You will need to start a new shell for this setup to take effect.` -) diff --git a/constant/completion_fish.go b/constant/completion_fish.go deleted file mode 100644 index 0d72d3e3..00000000 --- a/constant/completion_fish.go +++ /dev/null @@ -1,35 +0,0 @@ -package constant - -const ( - COMPLETION_FISH_HELP_TEMPLATE = `πŸ”§πŸŸ Generate the autocompletion script for the fish shell. - -To load completions in your current shell session: - - jrp completion fish | source - -To load completions for every new session, execute once: - - jrp completion fish > ~/.config/fish/completions/jrp.fish - -You will need to start a new shell for this setup to take effect. - -Usage: - jrp completion fish [flags] - -Flags: - -h, --help 🀝 help for fish -` - COMPLETION_FISH_USE = "fish" - COMPLETION_FISH_SHORT = "πŸ”§πŸŸ Generate the autocompletion script for the fish shell." - COMPLETION_FISH_LONG = `πŸ”§πŸŸ Generate the autocompletion script for the fish shell. - -To load completions in your current shell session: - - jrp completion fish | source - -To load completions for every new session, execute once: - - jrp completion fish > ~/.config/fish/completions/jrp.fish - -You will need to start a new shell for this setup to take effect.` -) diff --git a/constant/completion_powershell.go b/constant/completion_powershell.go deleted file mode 100644 index 0edebd3d..00000000 --- a/constant/completion_powershell.go +++ /dev/null @@ -1,27 +0,0 @@ -package constant - -const ( - COMPLETION_POWERSHELL_HELP_TEMPLATE = `πŸ”§πŸͺŸ Generate the autocompletion script for the powershell shell. - -To load completions in your current shell session: - - jrp completion powershell | Out-String | Invoke-Expression - -To load completions for every new session, add the output of the above command to your powershell profile. - -Usage: - jrp completion powershell [flags] - -Flags: - -h, --help 🀝 help for powershell -` - COMPLETION_POWERSHELL_USE = "powershell" - COMPLETION_POWERSHELL_SHORT = "πŸ”§πŸͺŸ Generate the autocompletion script for the powershell shell." - COMPLETION_POWERSHELL_LONG = `πŸ”§πŸͺŸ Generate the autocompletion script for the powershell shell. - -To load completions in your current shell session: - - jrp completion powershell | Out-String | Invoke-Expression - -To load completions for every new session, add the output of the above command to your powershell profile.` -) diff --git a/constant/completion_zsh.go b/constant/completion_zsh.go deleted file mode 100644 index 437f1dac..00000000 --- a/constant/completion_zsh.go +++ /dev/null @@ -1,59 +0,0 @@ -package constant - -const ( - COMPLETION_ZSH_HELP_TEMPLATE = `πŸ”§πŸ§™ Generate the autocompletion script for the zsh shell. - -If shell completion is not already enabled in your environment you will need to enable it. - -You can execute the following once: - - echo "autoload -U compinit; compinit" >> ~/.zshrc - -To load completions in your current shell session: - - source <(jrp completion zsh) - -To load completions for every new session, execute once: - - - 🐧 Linux: - - jrp completion zsh > "${fpath[1]}/_jrp" - - - 🍎 macOS: - - jrp completion zsh > $(brew --prefix)/share/zsh/site-functions/_jrp - -You will need to start a new shell for this setup to take effect. - -Usage: - jrp completion zsh [flags] - -Flags: - -h, --help 🀝 help for zsh -` - COMPLETION_ZSH_USE = "zsh" - COMPLETION_ZSH_SHORT = "πŸ”§πŸ§™ Generate the autocompletion script for the zsh shell." - COMPLETION_ZSH_LONG = `πŸ”§πŸ§™ Generate the autocompletion script for the zsh shell. - -If shell completion is not already enabled in your environment you will need to enable it. - -You can execute the following once: - - echo "autoload -U compinit; compinit" >> ~/.zshrc - -To load completions in your current shell session: - - source <(jrp completion zsh) - -To load completions for every new session, execute once: - - - 🐧 Linux: - - jrp completion zsh > "${fpath[1]}/_jrp" - - - 🍎 macOS: - - jrp completion zsh > $(brew --prefix)/share/zsh/site-functions/_jrp - -You will need to start a new shell for this setup to take effect.` -) diff --git a/constant/download.go b/constant/download.go deleted file mode 100644 index 03e42f6e..00000000 --- a/constant/download.go +++ /dev/null @@ -1,37 +0,0 @@ -package constant - -const ( - DOWNLOAD_HELP_TEMPLATE = `πŸ“¦ Download Japanese Wordnet sqlite3 database file from the official site. - -You have to download Japanese Wordnet sqlite3 database file to use jrp at first. -jrp will download archive file from the official site and decompress it to the database file. - -You can set the directory of the database file to the environment variable "JRP_WORDNETJP_DIR". -The default directory is "~/.local/share/jrp" ("$XDG_DATA_HOME/jrp"). - -Usage: - jrp download [flags] - jrp dl [flags] - jrp d [flags] - -Flags: - -h, --help 🀝 help for download -` - DOWNLOAD_USE = "download" - DOWNLOAD_SHORT = "πŸ“¦ Download Japanese Wordnet sqlite3 database file from the official site." - DOWNLOAD_LONG = `πŸ“¦ Download Japanese Wordnet sqlite3 database file from the official site. - -You have to download Japanese Wordnet sqlite3 database file to use jrp at first. -jrp will download archive file from the official site and decompress it to the database file. - -You can set the directory of the database file to the environment variable "JRP_WORDNETJP_DIR". -The default directory is "$XDG_DATA_HOME/jrp". -` - DOWNLOAD_MESSAGE_DOWNLOADING = " πŸ“¦ Downloading Japanese Wordnet sqlite3 database file from the official site..." - DOWNLOAD_MESSAGE_SUCCEEDED = "βœ… Downloaded successfully! Now, you are ready to use jrp!" - DOWNLOAD_MESSAGE_ALREADY_DOWNLOADED = "βœ… You are already ready to use jrp!" -) - -func GetDownloadAliases() []string { - return []string{"dl", "d"} -} diff --git a/constant/generate.go b/constant/generate.go deleted file mode 100644 index 3ab1de21..00000000 --- a/constant/generate.go +++ /dev/null @@ -1,56 +0,0 @@ -package constant - -const ( - GENARETE_HELP_TEMPLATE = `✨ Generate Japanese random phrase(s). - -You can specify how many phrases to generate by flag "-n" or "--number" or a number argument. -If both are provided, the larger number takes precedence. - -And you can specify the prefix or suffix of the phrase(s) to generate -by the flag "-p" or "--prefix" and "-s" or "--suffix". - -Usage: - jrp generate [flags] - jrp gen [flags] - jrp g [flags] - -Flags: - -n, --number πŸ”’ number of phrases to generate (default 1, e.g: 10). - -p --prefix πŸ’¬ prefix of phrase(s) to generate. - -s --suffix πŸ’¬ suffix of phrase(s) to generate. - -h, --help 🀝 help for generate - -Arguments: - number πŸ”’ number of phrases to generate (default 1, e.g: 10). -` - GENERATE_USE = "generate" - GENERATE_SHORT = "✨ Generate Japanese random phrase(s)." - GENERATE_LONG = `✨ Generate Japanese random phrase(s). - -You can specify how many phrases to generate by flag "-n" or "--number" or a number argument. -If both are provided, the larger number takes precedence. - -And you can specify the prefix or suffix of the phrase(s) to generate -by the flag "-p" or "--prefix" and "-s" or "--suffix". -` - GENERATE_FLAG_NUMBER = "number" - GENERATE_FLAG_NUMBER_SHORTHAND = "n" - GENERATE_FLAG_NUMBER_DESCRIPTION = "number of phrases to generate" - GENERATE_FLAG_PREFIX = "prifix" - GENERATE_FLAG_PREFIX_SHORTHAND = "p" - GENERATE_FLAG_PREFIX_DESCRIPTION = "prefix of phrase(s) to generate" - GENERATE_FLAG_SUFFIX = "suffix" - GENERATE_FLAG_SUFFIX_SHORTHAND = "s" - GENERATE_FLAG_SUFFIX_DESCRIPTION = "suffix of phrase(s) to generate" - - GENERATE_MESSAGE_NOTIFY_DOWNLOAD_REQUIRED = "⚑ You have to execute 'download' to use jrp..." - GENERATE_MESSAGE_NOTIFY_USE_ONLY_ONE = "⚑ You can use only one of prefix or suffix..." - - GENERATE_SQL_GET_ALL_JAPANESE_AVN_WORDS = "SELECT word.Lemma, word.Pos FROM word WHERE word.Lang = 'jpn' AND word.Pos in ('a', 'v', 'n');" - GENERATE_SQL_GET_ALL_JAPANESE_AV_WORDS = "SELECT word.Lemma, word.Pos FROM word WHERE word.Lang = 'jpn' AND word.Pos in ('a', 'v');" - GENERATE_SQL_GET_ALL_JAPANESE_N_WORDS = "SELECT word.Lemma, word.Pos FROM word WHERE word.Lang = 'jpn' AND word.Pos = 'n';" -) - -func GetGenerateAliases() []string { - return []string{"gen", "g"} -} diff --git a/constant/jrp.go b/constant/jrp.go deleted file mode 100644 index be3112f5..00000000 --- a/constant/jrp.go +++ /dev/null @@ -1,12 +0,0 @@ -package constant - -const ( - // jrp environment variable saving the directory of wnjpn.db - JRP_ENV_WORDNETJP_DIR = "JRP_WORDNETJP_DIR" - // WordNet Japanese database archive file URL - WNJPN_DB_ARCHIVE_FILE_URL = "https://github.com/bond-lab/wnja/releases/download/v1.1/wnjpn.db.gz" - // WordNet Japanese database archive file name - WNJPN_DB_ARCHIVE_FILE_NAME = "wnjpn.db.gz" - // WordNet Japanese database file name - WNJPN_DB_FILE_NAME = "wnjpn.db" -) diff --git a/constant/root.go b/constant/root.go deleted file mode 100644 index d3143fc6..00000000 --- a/constant/root.go +++ /dev/null @@ -1,58 +0,0 @@ -package constant - -const ( - ROOT_HELP_TEMPLATE = `🎲 jrp is the CLI tool to generate Japanese random phrase(s). - -You can generate Japanese random phrase(s). - -You can specify how many phrases to generate by flag "-n" or "--number" or a number argument. -If both are provided, the larger number takes precedence. - -And you can specify the prefix or suffix of the phrase(s) to generate -by the flag "-p" or "--prefix" and "-s" or "--suffix". - -Usage: - jrp [flags] - jrp [command] - -Available Subcommands: - download, dl, d πŸ“¦ Download Japanese Wordnet sqlite3 database file from the official site. - generate, gen, g ✨ Generate Japanese random phrase(s). You can abbreviate "generate" sub command. ("jrp" and "jrp generate" are the same.) - help 🀝 Help of jrp. - completion πŸ”§ Generate the autocompletion script for the specified shell. - version πŸ”– Show the version of jrp. - -Flags: - -n, --number πŸ”’ number of phrases to generate (default 1, e.g: 10). - -p --prefix πŸ’¬ prefix of phrase(s) to generate. - -s --suffix πŸ’¬ suffix of phrase(s) to generate. - -h, --help 🀝 help for jrp - -v, --version πŸ”– version for jrp - -Arguments: - number πŸ”’ number of phrases to generate (e.g: 10). - -Use "jrp [command] --help" for more information about a command. -` - ROOT_USE = "jrp" - ROOT_SHORT = "🎲 jrp is the CLI tool to generate Japanese random phrase(s)." - ROOT_LONG = `🎲 jrp is the CLI tool to generate Japanese random phrase(s). - -You can generate Japanese random phrase(s). - -You can specify how many phrases to generate by flag "-n" or "--number" or a number argument. -If both are provided, the larger number takes precedence. - -And you can specify the prefix or suffix of the phrase(s) to generate -by the flag "-p" or "--prefix" and "-s" or "--suffix". -` - ROOT_FLAG_NUMBER = "number" - ROOT_FLAG_NUMBER_SHORTHAND = "n" - ROOT_FLAG_NUMBER_DESCRIPTION = "number of phrases to generate" - ROOT_FLAG_PREFIX = "prifix" - ROOT_FLAG_PREFIX_SHORTHAND = "p" - ROOT_FLAG_PREFIX_DESCRIPTION = "prefix of phrase(s) to generate" - ROOT_FLAG_SUFFIX = "suffix" - ROOT_FLAG_SUFFIX_SHORTHAND = "s" - ROOT_FLAG_SUFFIX_DESCRIPTION = "suffix of phrase(s) to generate" -) diff --git a/docs/coverage.html b/docs/coverage.html index aa9a9fcb..863436af 100644 --- a/docs/coverage.html +++ b/docs/coverage.html @@ -3,7 +3,7 @@ - cmd: Go Coverage Report + repository: Go Coverage Report