Skip to content

Commit c101b8d

Browse files
committed
[EnvironmentCookbookVersionsEndpoint] Test sort_versions and filter_by_constraint
Add unit tests for the EnvironmentCookbookVersionsEndpoint helpers: - sort_versions: descending semver sort, single element, empty, multi-digit - filter_by_constraint: nil passthrough, exact/>=/>~ constraints, no match, immutability Signed-off-by: David Crosby <dcrosby@fb.com>
1 parent a30599c commit c101b8d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
require "chef_zero/endpoints/environment_cookbook_versions_endpoint"
2+
3+
describe ChefZero::Endpoints::EnvironmentCookbookVersionsEndpoint do
4+
let(:server) { double("server") }
5+
let(:endpoint) { described_class.new(server) }
6+
7+
describe "#sort_versions" do
8+
it "returns versions sorted descending by semver" do
9+
versions = ["1.0.0", "2.0.0", "1.5.0", "0.9.0"]
10+
expect(endpoint.sort_versions(versions)).to eq(["2.0.0", "1.5.0", "1.0.0", "0.9.0"])
11+
end
12+
13+
it "handles single-element lists" do
14+
expect(endpoint.sort_versions(["1.0.0"])).to eq(["1.0.0"])
15+
end
16+
17+
it "handles an empty list" do
18+
expect(endpoint.sort_versions([])).to eq([])
19+
end
20+
21+
it "sorts multi-digit version components numerically" do
22+
versions = ["1.2.3", "1.10.0", "1.9.0"]
23+
expect(endpoint.sort_versions(versions)).to eq(["1.10.0", "1.9.0", "1.2.3"])
24+
end
25+
end
26+
27+
describe "#filter_by_constraint" do
28+
let(:versions) { { "apache" => ["1.0.0", "1.5.0", "2.0.0", "2.1.0"] } }
29+
30+
it "returns versions unchanged when constraint is nil" do
31+
result = endpoint.filter_by_constraint(versions, "apache", nil)
32+
expect(result["apache"]).to eq(["1.0.0", "1.5.0", "2.0.0", "2.1.0"])
33+
end
34+
35+
it "filters with an exact version constraint" do
36+
result = endpoint.filter_by_constraint(versions, "apache", "= 1.5.0")
37+
expect(result["apache"]).to eq(["1.5.0"])
38+
end
39+
40+
it "filters with a >= constraint" do
41+
result = endpoint.filter_by_constraint(versions, "apache", ">= 2.0.0")
42+
expect(result["apache"]).to eq(["2.0.0", "2.1.0"])
43+
end
44+
45+
it "filters with a ~> constraint" do
46+
result = endpoint.filter_by_constraint(versions, "apache", "~> 1.0")
47+
expect(result["apache"]).to eq(["1.0.0", "1.5.0"])
48+
end
49+
50+
it "returns empty array when no versions match" do
51+
result = endpoint.filter_by_constraint(versions, "apache", "= 9.9.9")
52+
expect(result["apache"]).to eq([])
53+
end
54+
55+
it "does not mutate the original versions hash" do
56+
original = versions.dup
57+
endpoint.filter_by_constraint(versions, "apache", "= 1.0.0")
58+
expect(versions).to eq(original)
59+
end
60+
end
61+
end

0 commit comments

Comments
 (0)