Created
July 20, 2015 20:11
-
-
Save myronmarston/fb7a29fa71ca62ec6546 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.SnapshotRepositoryTest do | |
use ExUnit.Case, async: true | |
doctest MyApp.SnapshotRepository | |
alias MyApp.SnapshotRepository.SnapshotOptions | |
test "queries S3 with a prefix and returns the matched keys" do | |
options = %SnapshotOptions{ campaign_id: "prod.0.1", backend: "SomeBackend", cycle_type: "week" } | |
prefix = SnapshotOptions.to_key_prefix(options) | |
key_1 = "#{prefix}0/2013-03-19T15:04:50Z/mtoken.json.gz" | |
key_2 = "#{prefix}1/2013-03-19T15:04:50Z/mtoken.json.gz" | |
defmodule FakeS3 do | |
def list_objects!(_bucket_name, prefix: prefix) do | |
# would be nice to not have to define these keys a second time, | |
# but we can't access `key_1` and `key_2` from here :(. | |
key_1 = "#{prefix}0/2013-03-19T15:04:50Z/mtoken.json.gz" | |
key_2 = "#{prefix}1/2013-03-19T15:04:50Z/mtoken.json.gz" | |
%HTTPoison.Response{body: %{contents: [ %{ key: key_1 }, %{ key: key_2 } ] }, status_code: 200} | |
end | |
end | |
keys = MyApp.SnapshotRepository.find_snapshot_keys(options, FakeS3) | |
assert keys == [key_1, key_2] | |
end | |
test "dedups keys for the same cycle type, selecting the one with the later timestamp" do | |
options = %SnapshotOptions{ campaign_id: "prod.0.1", backend: "SomeBackend", cycle_type: "week" } | |
prefix = SnapshotOptions.to_key_prefix(options) | |
key_1 = "#{prefix}0/2013-03-19T15:04:50Z/mtoken.json.gz" | |
key_2 = "#{prefix}0/2013-03-21T15:04:50Z/mtoken.json.gz" # the latest one | |
key_3 = "#{prefix}0/2013-03-20T15:04:50Z/mtoken.json.gz" | |
# To avoid Elixir module redef warnings, we need to use a different module name here... | |
defmodule FakeS3_2 do | |
def list_objects!(_bucket_name, prefix: prefix) do | |
key_1 = "#{prefix}0/2013-03-19T15:04:50Z/mtoken.json.gz" | |
key_2 = "#{prefix}0/2013-03-21T15:04:50Z/mtoken.json.gz" # the latest one | |
key_3 = "#{prefix}0/2013-03-20T15:04:50Z/mtoken.json.gz" | |
%HTTPoison.Response{body: %{contents: [ %{ key: key_1 }, %{ key: key_2 }, %{ key: key_3 } ] }, status_code: 200} | |
end | |
end | |
keys = MyApp.SnapshotRepository.find_snapshot_keys(options, FakeS3_2) | |
assert keys == [key_2] | |
end | |
test "fails if S3 responds with a non-200 status" do | |
options = %SnapshotOptions{ campaign_id: "prod.0.1", backend: "SomeBackend", cycle_type: "week" } | |
prefix = SnapshotOptions.to_key_prefix(options) | |
defmodule FakeS3_3 do | |
def list_objects!(_bucket_name, prefix: prefix) do | |
key_1 = "#{prefix}0/2013-03-19T15:04:50Z/mtoken.json.gz" | |
%HTTPoison.Response{body: %{contents: [ %{ key: key_1 } ] }, status_code: 404} | |
end | |
end | |
assert_raise MyApp.SnapshotRepository.UnsuccessfulS3ResponseError, ~r/expected a 200 response.*got a 404/i, fn -> | |
MyApp.SnapshotRepository.find_snapshot_keys(options, FakeS3_3) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment