Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / test / tasks / config_test.exs
index 2e56e6cfe928fd08bc20c171950b6896793adcf5..d79d34276db85673d0da3b951f20a1faddbd663a 100644 (file)
@@ -25,30 +25,50 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
   end
 
   test "error if file with custom settings doesn't exist" do
-    Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
+    Mix.Tasks.Pleroma.Config.migrate_to_db("config/not_existance_config_file.exs")
 
     assert_receive {:mix_shell, :info,
                     [
-                      "To migrate settings, you must define custom settings in config/test.secret.exs."
+                      "To migrate settings, you must define custom settings in config/not_existance_config_file.exs."
                     ]},
                    15
   end
 
-  test "settings are migrated to db" do
-    initial = Application.get_env(:quack, :level)
-    on_exit(fn -> Application.put_env(:quack, :level, initial) end)
-    assert Repo.all(ConfigDB) == []
+  describe "migrate_to_db/1" do
+    setup do
+      initial = Application.get_env(:quack, :level)
+      on_exit(fn -> Application.put_env(:quack, :level, initial) end)
+    end
+
+    test "settings are migrated to db" do
+      assert Repo.all(ConfigDB) == []
+
+      Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
+
+      config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
+      config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
+      config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
+      refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
+
+      assert ConfigDB.from_binary(config1.value) == [key: "value", key2: [Repo]]
+      assert ConfigDB.from_binary(config2.value) == [key: "value2", key2: ["Activity"]]
+      assert ConfigDB.from_binary(config3.value) == :info
+    end
 
-    Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
+    test "config table is truncated before migration" do
+      ConfigDB.create(%{
+        group: ":pleroma",
+        key: ":first_setting",
+        value: [key: "value", key2: ["Activity"]]
+      })
+
+      assert Repo.aggregate(ConfigDB, :count, :id) == 1
 
-    config1 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
-    config2 = ConfigDB.get_by_params(%{group: ":pleroma", key: ":second_setting"})
-    config3 = ConfigDB.get_by_params(%{group: ":quack", key: ":level"})
-    refute ConfigDB.get_by_params(%{group: ":pleroma", key: "Pleroma.Repo"})
+      Mix.Tasks.Pleroma.Config.migrate_to_db("test/fixtures/config/temp.secret.exs")
 
-    assert ConfigDB.from_binary(config1.value) == [key: "value", key2: [Repo]]
-    assert ConfigDB.from_binary(config2.value) == [key: "value2", key2: ["Activity"]]
-    assert ConfigDB.from_binary(config3.value) == :info
+      config = ConfigDB.get_by_params(%{group: ":pleroma", key: ":first_setting"})
+      assert ConfigDB.from_binary(config.value) == [key: "value", key2: [Repo]]
+    end
   end
 
   describe "with deletion temp file" do