Skip to content

Commit

Permalink
fix: on import do not try to fetch metadata if binary not configured #3
Browse files Browse the repository at this point in the history
  • Loading branch information
bayang committed May 13, 2022
1 parent efc8d9f commit 92f2b90
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ dependencies {

implementation("org.apache.commons:commons-csv:1.9.0")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(module = "mockito-core")
}
testImplementation("io.mockk:mockk:1.12.4")
testImplementation("com.ninja-squad:springmockk:3.1.1")
testImplementation("io.projectreactor:reactor-test")

val springdocVersion = "1.6.6"
Expand Down
26 changes: 23 additions & 3 deletions src/jelu-ui/src/components/Imports.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<script setup lang="ts">
import { ref } from "vue";
import { computed, ref } from "vue";
import { useStore } from 'vuex'
import { key } from '../store'
import { ImportSource } from "../model/ImportConfiguration";
import dataService from "../services/DataService";
import { useTitle } from '@vueuse/core'
Expand All @@ -12,6 +14,7 @@ const { t } = useI18n({
useTitle('Jelu | Imports')
const store = useStore(key)
const file = ref(new File([], "dummy"));
const importSource = ref(ImportSource.GOODREADS);
const fetchMetadata = ref(true)
Expand Down Expand Up @@ -54,6 +57,14 @@ const exportFile =async () => {
}
}
let fetchCoversDisabled = computed(() => {
if (store != null && !store.getters.getMetadataFetchEnabled) {
return true
} else {
return !fetchMetadata.value
}
})
</script>

<template>
Expand All @@ -62,6 +73,12 @@ const exportFile =async () => {
{{ t('csv_import.import_csv') }}
</h1>
<div class="w-11/12 sm:w-8/12">
<div
v-if="store != null && !store.getters.getMetadataFetchEnabled"
class="mb-3 text-warning font-bold"
>
!! {{ t('labels.auto_import_disabled') }}
</div>
<div class="form-control">
<o-field
horizontal
Expand All @@ -83,7 +100,10 @@ const exportFile =async () => {
:label="t('csv_import.auto_fetch_online')"
class="capitalize"
>
<o-checkbox v-model="fetchMetadata">
<o-checkbox
v-model="fetchMetadata"
:disabled="store != null && !store.getters.getMetadataFetchEnabled"
>
{{ fetchMetadata }}
</o-checkbox>
</o-field>
Expand All @@ -96,7 +116,7 @@ const exportFile =async () => {
>
<o-checkbox
v-model="fetchCovers"
:disabled="!fetchMetadata"
:disabled="fetchCoversDisabled"
>
{{ fetchCovers }}
</o-checkbox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ class CsvImportService(
try {
userMessageService.save(
CreateUserMessageDto(
"Import started at $nowString",
null,
MessageCategory.INFO
), userEntity)
"Import started at $nowString",
null,
MessageCategory.INFO
),
userEntity
)
} catch (e: Exception) {
logger.error(e) { "failed to save message for ${file.absolutePath} import" }
}
Expand All @@ -116,7 +118,9 @@ class CsvImportService(
"Import for ${file.absolutePath} ended after : $deltaInSec seconds, with $success imports and $failures failures",
null,
MessageCategory.SUCCESS
), userEntity)
),
userEntity
)
} catch (e: Exception) {
logger.error(e) { "failed to save message for ${file.absolutePath} import" }
}
Expand Down Expand Up @@ -149,7 +153,7 @@ class CsvImportService(
try {
importService.updateStatus(importEntity.id.value, ProcessingStatus.PROCESSING)
var metadata = MetadataDto()
if (importEntity.shouldFetchMetadata) {
if (importEntity.shouldFetchMetadata && !properties.metadata.calibre.path.isNullOrBlank()) {
val isbn: String = getIsbn(importEntity)
if (isbn.isNotBlank()) {
metadata = fetchMetadataService.fetchMetadata(isbn, null, null, onlyUseCorePlugins = true, fetchCover = importConfig.shouldFetchCovers)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package io.github.bayang.jelu.service.imports

import com.ninjasquad.springmockk.MockkBean
import io.github.bayang.jelu.config.JeluProperties
import io.github.bayang.jelu.dao.ImportSource
import io.github.bayang.jelu.dao.ProcessingStatus
import io.github.bayang.jelu.dao.User
import io.github.bayang.jelu.dto.CreateUserDto
import io.github.bayang.jelu.dto.ImportConfigurationDto
import io.github.bayang.jelu.dto.JeluUser
import io.github.bayang.jelu.dto.UserBookWithoutEventsAndUserDto
import io.github.bayang.jelu.importConfigurationDto
import io.github.bayang.jelu.service.BookService
import io.github.bayang.jelu.service.ImportService
import io.github.bayang.jelu.service.ReadingEventService
import io.github.bayang.jelu.service.UserService
import io.github.bayang.jelu.service.metadata.FetchMetadataService
import io.mockk.verify
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Assertions
Expand Down Expand Up @@ -67,6 +72,9 @@ class CsvImportServiceTest(
}
}

@MockkBean
private lateinit var fetchMetadataService: FetchMetadataService

@Test
fun testParse() {
val userId = user().id.value
Expand All @@ -85,6 +93,29 @@ class CsvImportServiceTest(
}
}

@Test
fun testParseAndImport() {
val userId = user().id.value
val csv = File(this::class.java.getResource("/csv-import/goodreads1.csv").file)
// shouldFetchMetadata true but binary path is null so we shouldn't try to call fetchMetadata
csvImportService.parse(csv, userId, ImportConfigurationDto(shouldFetchMetadata = true, shouldFetchCovers = false, ImportSource.GOODREADS))
val nb = importService.countByprocessingStatusAndUser(ProcessingStatus.SAVED, userId)
Assertions.assertEquals(10, nb)
val dtos = importService.getByprocessingStatusAndUser(ProcessingStatus.SAVED, userId)
dtos.forEach {
if (it.title.equals("La somme de nos folies")) {
Assertions.assertEquals("9782843048302", it.isbn13)
Assertions.assertEquals("Éditions Zulma", it.publisher)
val shelves = it.tags?.split(",")
Assertions.assertEquals(3, shelves?.size)
}
}
val (success, failures) = csvImportService.importFromDb(userId, importConfigurationDto())
Assertions.assertEquals(10, success)
Assertions.assertEquals(0, failures)
verify(exactly = 0) { fetchMetadataService.fetchMetadata(any(), any(), any(), any(), any()) }
}

@Test
fun testNoDuplicates() {
val userId = user().id.value
Expand All @@ -106,6 +137,7 @@ class CsvImportServiceTest(
Assertions.assertEquals(1, userbook.readingEvents?.size)
}
}
verify(exactly = 0) { fetchMetadataService.fetchMetadata(any(), any(), any(), any(), any()) }
}

fun user(): User {
Expand Down

0 comments on commit 92f2b90

Please sign in to comment.