From 3d019cc14fef7d5d5426a9cb23ddf04a59055e17 Mon Sep 17 00:00:00 2001 From: mihanentalpo Date: Thu, 21 Aug 2025 12:04:23 +0700 Subject: [PATCH] Automatic backups: Added test for DatabaseUtils.saveDatabaseCopy with or without datetime in it's name --- .../isoron/uhabits/utils/DatabaseUtilsTest.kt | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 uhabits-android/src/test/java/org/isoron/uhabits/utils/DatabaseUtilsTest.kt diff --git a/uhabits-android/src/test/java/org/isoron/uhabits/utils/DatabaseUtilsTest.kt b/uhabits-android/src/test/java/org/isoron/uhabits/utils/DatabaseUtilsTest.kt new file mode 100644 index 000000000..a1202380e --- /dev/null +++ b/uhabits-android/src/test/java/org/isoron/uhabits/utils/DatabaseUtilsTest.kt @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2016-2021 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.utils + +import android.content.Context +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.isoron.uhabits.core.utils.DateUtils.Companion.setFixedLocalTime +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever +import java.io.File +import java.nio.file.Files +import kotlin.test.assertTrue + +class DatabaseUtilsTest { + private lateinit var baseDir: File + private lateinit var filesDir: File + private lateinit var backupDir: File + private lateinit var context: Context + + @Before + fun setUp() { + baseDir = Files.createTempDirectory("dbutils").toFile() + filesDir = File(baseDir, "files").apply { mkdirs() } + File(baseDir, "databases").apply { + mkdirs() + File(this, "uhabits.db").writeText("data") + } + backupDir = File(baseDir, "backup").apply { mkdirs() } + + context = mock() + whenever(context.filesDir).thenReturn(filesDir) + setFixedLocalTime(1422172800000L) + } + + @After + fun tearDown() { + setFixedLocalTime(null) + } + + @Test + fun testSaveDatabaseCopyWithDate() { + val path = DatabaseUtils.saveDatabaseCopy(context, backupDir, true) + val expected = File(backupDir, "Loop Habits Backup 2015-01-25 080000.db").absolutePath + assertThat(path, equalTo(expected)) + assertTrue(File(path).exists()) + } + + @Test + fun testSaveDatabaseCopyWithoutDate() { + val path = DatabaseUtils.saveDatabaseCopy(context, backupDir, false) + val expected = File(backupDir, "Loop Habits Backup.db").absolutePath + assertThat(path, equalTo(expected)) + assertTrue(File(path).exists()) + } +}