68 lines
2.2 KiB
Elixir
68 lines
2.2 KiB
Elixir
defmodule Bright.CacheTest do
|
|
use Bright.DataCase
|
|
|
|
alias Bright.Cache
|
|
|
|
@sample_url "https://example.com/my_video.mp4"
|
|
|
|
## IDK what I'm doing here. Ideally I want a redis-like k/v store where I can temporarily put VODs and they expire after 12 hours or so.
|
|
## this would potentially speed up vod processing because it would prevent having to download the VOD from S3 during every Oban worker performance.
|
|
## BUT I don't want to implement it myself because of the idiom, "There are only two unsolved problems in CS. Naming things and cache invalidation"
|
|
## Meaning I don't think I can do any better than the experts in the field.
|
|
## Anyway, this is FEATURE CREEP! Solve the problem without caching and LET IT BE SLOW.
|
|
## To implement this cache before the system works is pre-mature optimization!
|
|
|
|
# describe "cache k/v" do
|
|
# test "get/1 with string cache key" do
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
describe "cache" do
|
|
@tag :unit
|
|
test "get_cache_dir/0" do
|
|
assert Regex.match?(~r/.cache\/futureporn/, Cache.get_cache_dir())
|
|
end
|
|
|
|
@tag :unit
|
|
test "generate_basename/1" do
|
|
# Test with a URL
|
|
url = @sample_url
|
|
filename = Cache.generate_basename(url)
|
|
|
|
assert Regex.match?(~r/^[a-zA-Z0-9]+\/my_video\.mp4$/, filename)
|
|
|
|
# Test with a file path
|
|
path = "/home/cj/Downloads/taco.mp4"
|
|
filename = Cache.generate_basename(path)
|
|
|
|
assert Regex.match?(~r/^[a-zA-Z0-9]+\/taco\.mp4$/, filename)
|
|
end
|
|
|
|
@tag :unit
|
|
test "generate_basename/2" do
|
|
filename = Cache.generate_basename(@sample_url, "png")
|
|
assert Regex.match?(~r/^[a-zA-Z0-9]+\/my_video\.png/, filename)
|
|
end
|
|
|
|
@tag :unit
|
|
test "generate_filename/1" do
|
|
filename = Cache.generate_filename(@sample_url)
|
|
assert Regex.match?(~r/.cache\/futureporn\/.+\/my_video\.mp4/, filename)
|
|
|
|
filename = Cache.generate_filename("/home/cj/Downloads/test.mp4")
|
|
assert Regex.match?(~r/.cache\/futureporn\/.+\/test\.mp4/, filename)
|
|
|
|
assert File.exists?(Path.dirname(filename))
|
|
assert not File.exists?(filename)
|
|
end
|
|
|
|
@tag :unit
|
|
test "generate_filename/2" do
|
|
filename = Cache.generate_filename(@sample_url, "png")
|
|
assert Regex.match?(~r/.cache\/futureporn\/.+\/my_video\.png/, filename)
|
|
end
|
|
end
|
|
end
|