From: href Date: Tue, 15 Jan 2019 15:18:18 +0000 (+0100) Subject: Test FlakeID old id compat & Ecto type X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=9d63b27dcd61dff61b77b6df0ef8a4cf8d7c87e3;p=akkoma Test FlakeID old id compat & Ecto type --- diff --git a/lib/pleroma/flake_id.ex b/lib/pleroma/flake_id.ex index 3c72807ca..f23c6d4b0 100644 --- a/lib/pleroma/flake_id.ex +++ b/lib/pleroma/flake_id.ex @@ -1,3 +1,7 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + defmodule Pleroma.FlakeId do @moduledoc """ Flake is a decentralized, k-ordered id generation service. @@ -38,6 +42,8 @@ defmodule Pleroma.FlakeId do def from_string(unquote(Kernel.to_string(i))), do: <<0::integer-size(128)>> end + def from_string(flake = <<_::integer-size(128)>>), do: flake + def from_string(string) when is_binary(string) and byte_size(string) < 18 do case Integer.parse(string) do {id, _} -> <<0::integer-size(64), id::integer-size(64)>> diff --git a/test/flake_id_test.exs b/test/flake_id_test.exs new file mode 100644 index 000000000..e480fbdf3 --- /dev/null +++ b/test/flake_id_test.exs @@ -0,0 +1,41 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.FlakeIdTest do + use Pleroma.DataCase + import Kernel, except: [to_string: 1] + import Pleroma.FlakeId + + describe "fake flakes (compatibility with older serial integers)" do + test "from_string/1" do + fake_flake = <<0::integer-size(64), 42::integer-size(64)>> + assert from_string("42") == fake_flake + end + + test "zero or -1 is a null flake" do + fake_flake = <<0::integer-size(128)>> + assert from_string("0") == fake_flake + assert from_string("-1") == fake_flake + end + + test "to_string/1" do + fake_flake = <<0::integer-size(64), 42::integer-size(64)>> + assert to_string(fake_flake) == "42" + end + end + + test "ecto type behaviour" do + flake = <<0, 0, 1, 104, 80, 229, 2, 235, 140, 22, 69, 201, 53, 210, 0, 0>> + flake_s = "9eoozpwTul5mjSEDRI" + + assert cast(flake) == {:ok, flake_s} + assert cast(flake_s) == {:ok, flake_s} + + assert load(flake) == {:ok, flake_s} + assert load(flake_s) == {:ok, flake_s} + + assert dump(flake_s) == {:ok, flake} + assert dump(flake) == {:ok, flake} + end +end