|
| 1 | +// This file is part of libraries-repository-engine. |
| 2 | +// |
| 3 | +// Copyright 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published |
| 7 | +// by the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | +// You can be released from the requirements of the above licenses by purchasing |
| 19 | +// a commercial license. Buying such a license is mandatory if you want to |
| 20 | +// modify or otherwise use the software for commercial activities involving the |
| 21 | +// Arduino software without disclosing the source code of your own applications. |
| 22 | +// To purchase a commercial license, send an email to [email protected]. |
| 23 | + |
| 24 | +package db |
| 25 | + |
| 26 | +import ( |
| 27 | + "testing" |
| 28 | + |
| 29 | + "github.com/stretchr/testify/assert" |
| 30 | + "github.com/stretchr/testify/require" |
| 31 | +) |
| 32 | + |
| 33 | +func testerDB() *DB { |
| 34 | + tDB := DB{ |
| 35 | + Libraries: []*Library{ |
| 36 | + { |
| 37 | + Name: "FooLib", |
| 38 | + Repository: "https://github.com/Bar/FooLib.git", |
| 39 | + SupportLevel: "", |
| 40 | + }, |
| 41 | + { |
| 42 | + Name: "BazLib", |
| 43 | + Repository: "https://github.com/Bar/BazLib.git", |
| 44 | + SupportLevel: "", |
| 45 | + }, |
| 46 | + { |
| 47 | + Name: "QuxLib", |
| 48 | + Repository: "https://github.com/Zeb/QuxLib.git", |
| 49 | + SupportLevel: "", |
| 50 | + }, |
| 51 | + }, |
| 52 | + Releases: []*Release{ |
| 53 | + { |
| 54 | + LibraryName: "FooLib", |
| 55 | + Version: Version{"1.0.0"}, |
| 56 | + Author: "Barthor", |
| 57 | + Maintainer: "Bartainer", |
| 58 | + License: "MIT", |
| 59 | + Sentence: "asdf", |
| 60 | + Paragraph: "zxcv", |
| 61 | + Website: "https://example.com", |
| 62 | + Category: "Other", |
| 63 | + Architectures: []string{"avr"}, |
| 64 | + Types: []string{"Contributed"}, |
| 65 | + URL: "http://www.example.com/libraries/github.com/Bar/FooLib-1.0.0.zip", |
| 66 | + ArchiveFileName: "FooLib-1.0.0.zip", |
| 67 | + Size: 123, |
| 68 | + Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420", |
| 69 | + Includes: []string{"FooLib.h"}, |
| 70 | + Dependencies: []*Dependency{ |
| 71 | + { |
| 72 | + Name: "BazLib", |
| 73 | + Version: "2.0.0", |
| 74 | + }, |
| 75 | + }, |
| 76 | + Log: "Some log messages", |
| 77 | + }, |
| 78 | + { |
| 79 | + LibraryName: "BazLib", |
| 80 | + Version: Version{"2.0.0"}, |
| 81 | + Author: "Barthor", |
| 82 | + Maintainer: "Bartainer", |
| 83 | + License: "MIT", |
| 84 | + Sentence: "asdf", |
| 85 | + Paragraph: "zxcv", |
| 86 | + Website: "https://example.com", |
| 87 | + Category: "Other", |
| 88 | + Architectures: []string{"avr"}, |
| 89 | + Types: []string{"Contributed"}, |
| 90 | + URL: "http://www.example.com/libraries/github.com/Bar/BazLib-2.0.0.zip", |
| 91 | + ArchiveFileName: "BazLib-2.0.0.zip", |
| 92 | + Size: 123, |
| 93 | + Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420", |
| 94 | + Includes: []string{"BazLib.h"}, |
| 95 | + Dependencies: []*Dependency{}, |
| 96 | + Log: "Some log messages", |
| 97 | + }, |
| 98 | + { |
| 99 | + LibraryName: "BazLib", |
| 100 | + Version: Version{"2.1.0"}, |
| 101 | + Author: "Barthor", |
| 102 | + Maintainer: "Bartainer", |
| 103 | + License: "MIT", |
| 104 | + Sentence: "asdf", |
| 105 | + Paragraph: "zxcv", |
| 106 | + Website: "https://example.com", |
| 107 | + Category: "Other", |
| 108 | + Architectures: []string{"avr"}, |
| 109 | + Types: []string{"Contributed"}, |
| 110 | + URL: "http://www.example.com/libraries/github.com/Bar/BazLib-2.1.0.zip", |
| 111 | + ArchiveFileName: "BazLib-2.1.0.zip", |
| 112 | + Size: 123, |
| 113 | + Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420", |
| 114 | + Includes: []string{"BazLib.h"}, |
| 115 | + Dependencies: []*Dependency{}, |
| 116 | + Log: "Some log messages", |
| 117 | + }, |
| 118 | + { |
| 119 | + LibraryName: "FooLib", |
| 120 | + Version: Version{"1.1.0"}, |
| 121 | + Author: "Barthor", |
| 122 | + Maintainer: "Bartainer", |
| 123 | + License: "MIT", |
| 124 | + Sentence: "asdf", |
| 125 | + Paragraph: "zxcv", |
| 126 | + Website: "https://example.com", |
| 127 | + Category: "Other", |
| 128 | + Architectures: []string{"avr"}, |
| 129 | + Types: []string{"Contributed"}, |
| 130 | + URL: "http://www.example.com/libraries/github.com/Bar/FooLib-1.1.0.zip", |
| 131 | + ArchiveFileName: "FooLib-1.1.0.zip", |
| 132 | + Size: 123, |
| 133 | + Checksum: "SHA-256:887f897cfb1818a53652aef39c2a4b8de3c69c805520b2953a562a787b422420", |
| 134 | + Includes: []string{"FooLib.h"}, |
| 135 | + Dependencies: []*Dependency{ |
| 136 | + { |
| 137 | + Name: "BazLib", |
| 138 | + Version: "", |
| 139 | + }, |
| 140 | + }, |
| 141 | + Log: "Some log messages", |
| 142 | + }, |
| 143 | + }, |
| 144 | + libraryFile: "some-file.json", |
| 145 | + } |
| 146 | + |
| 147 | + return &tDB |
| 148 | +} |
| 149 | + |
| 150 | +func TestRemoveLibrary(t *testing.T) { |
| 151 | + testDB := testerDB() |
| 152 | + assert.True(t, testDB.HasLibrary("FooLib")) |
| 153 | + assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0")) |
| 154 | + err := testDB.RemoveLibrary("FooLib") |
| 155 | + require.NoError(t, err) |
| 156 | + assert.False(t, testDB.HasLibrary("FooLib")) |
| 157 | + assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0")) |
| 158 | + assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0")) |
| 159 | + |
| 160 | + assert.True(t, testDB.HasLibrary("QuxLib")) |
| 161 | + err = testDB.RemoveLibrary("QuxLib") |
| 162 | + require.NoError(t, err) |
| 163 | + assert.False(t, testDB.HasLibrary("QuxLib")) |
| 164 | + |
| 165 | + err = testDB.RemoveLibrary("nonexistent") |
| 166 | + assert.Error(t, err) |
| 167 | +} |
| 168 | + |
| 169 | +func TestRemoveReleaseByNameVersion(t *testing.T) { |
| 170 | + testDB := testerDB() |
| 171 | + assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0")) |
| 172 | + assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0")) |
| 173 | + err := testDB.RemoveReleaseByNameVersion("FooLib", "1.0.0") |
| 174 | + require.NoError(t, err) |
| 175 | + assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0")) |
| 176 | + assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0")) |
| 177 | + |
| 178 | + err = testDB.RemoveReleaseByNameVersion("nonexistent", "1.0.0") |
| 179 | + assert.Error(t, err) |
| 180 | + err = testDB.RemoveReleaseByNameVersion("FooLib", "99.99.99") |
| 181 | + assert.Error(t, err) |
| 182 | +} |
| 183 | + |
| 184 | +func TestRemoveReleases(t *testing.T) { |
| 185 | + testDB := testerDB() |
| 186 | + assert.True(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0")) |
| 187 | + err := testDB.RemoveReleases("FooLib") |
| 188 | + require.NoError(t, err) |
| 189 | + assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.0.0")) |
| 190 | + assert.False(t, testDB.HasReleaseByNameVersion("FooLib", "1.1.0")) |
| 191 | + |
| 192 | + err = testDB.RemoveReleases("nonexistent") |
| 193 | + assert.Error(t, err) |
| 194 | +} |
0 commit comments