fix the tests

This commit is contained in:
Tamo 2022-10-03 18:50:06 +02:00 committed by Clément Renault
parent e3bc87bf22
commit b5ebab5c66
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
9 changed files with 327 additions and 107 deletions

View file

@ -1,14 +1,16 @@
use std::io::Read;
use std::path::Path;
use std::{fs::File, io::BufReader};
use flate2::{bufread::GzDecoder, Compression};
use index::{Settings, Unchecked};
use index::{Checked, Settings, Unchecked};
use index_scheduler::TaskView;
use meilisearch_auth::Key;
use serde::{Deserialize, Serialize};
use tempfile::TempDir;
use time::OffsetDateTime;
use uuid::Uuid;
use crate::{Result, Version};
@ -21,12 +23,12 @@ use crate::{Result, Version};
mod v6;
pub fn open(
dump_path: &Path,
dump: impl Read,
) -> Result<
Box<
dyn DumpReader<
Document = serde_json::Map<String, serde_json::Value>,
Settings = Settings<Unchecked>,
Settings = Settings<Checked>,
Task = TaskView,
UpdateFile = File,
Key = Key,
@ -34,15 +36,13 @@ pub fn open(
>,
> {
let path = TempDir::new()?;
let dump = File::open(dump_path)?;
let mut dump = BufReader::new(dump);
let gz = GzDecoder::new(&mut dump);
let mut archive = tar::Archive::new(gz);
archive.unpack(path.path())?;
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct MetadataVersion {
pub dump_version: Version,
}
@ -61,7 +61,7 @@ pub fn open(
as Box<
dyn DumpReader<
Document = serde_json::Map<String, serde_json::Value>,
Settings = Settings<Unchecked>,
Settings = Settings<Checked>,
Task = TaskView,
UpdateFile = File,
Key = Key,
@ -85,9 +85,12 @@ pub trait DumpReader {
/// Return the version of the dump.
fn version(&self) -> Version;
/// Return at which date the index was created.
/// Return at which date the dump was created if there was one.
fn date(&self) -> Option<OffsetDateTime>;
/// Return the instance-uid if there was one.
fn instance_uid(&self) -> Result<Option<Uuid>>;
/// Return an iterator over each indexes.
fn indexes(
&self,

View file

@ -4,9 +4,10 @@ use std::{
path::Path,
};
use index::Unchecked;
use index::{Checked, Unchecked};
use tempfile::TempDir;
use time::OffsetDateTime;
use uuid::Uuid;
use crate::{Error, Result, Version};
@ -61,7 +62,7 @@ impl V6Reader {
impl DumpReader for V6Reader {
type Document = serde_json::Map<String, serde_json::Value>;
type Settings = index::Settings<Unchecked>;
type Settings = index::Settings<Checked>;
type Task = index_scheduler::TaskView;
type UpdateFile = File;
@ -76,6 +77,11 @@ impl DumpReader for V6Reader {
Some(self.metadata.dump_date)
}
fn instance_uid(&self) -> Result<Option<Uuid>> {
let uuid = fs::read_to_string(self.dump.path().join("instance-uid"))?;
Ok(Some(Uuid::parse_str(&uuid)?))
}
fn indexes(
&self,
) -> Result<
@ -125,7 +131,11 @@ impl DumpReader for V6Reader {
&mut self,
) -> Box<dyn Iterator<Item = Result<(Self::Task, Option<Self::UpdateFile>)>> + '_> {
Box::new((&mut self.tasks).lines().map(|line| -> Result<_> {
let task: index_scheduler::TaskView = serde_json::from_str(&line?)?;
let mut task: index_scheduler::TaskView = serde_json::from_str(&line?)?;
// TODO: this can be removed once we can `Deserialize` the duration from the `TaskView`.
if let Some((started_at, finished_at)) = task.started_at.zip(task.finished_at) {
task.duration = Some(finished_at - started_at);
}
let update_file_path = self
.dump
.path()
@ -152,7 +162,7 @@ impl DumpReader for V6Reader {
impl IndexReader for V6IndexReader {
type Document = serde_json::Map<String, serde_json::Value>;
type Settings = index::Settings<Unchecked>;
type Settings = index::Settings<Checked>;
fn name(&self) -> &str {
&self.name
@ -165,6 +175,7 @@ impl IndexReader for V6IndexReader {
}
fn settings(&mut self) -> Result<Self::Settings> {
Ok(serde_json::from_reader(&mut self.settings)?)
let settings: index::Settings<Unchecked> = serde_json::from_reader(&mut self.settings)?;
Ok(settings.check())
}
}