1
0
mirror of https://github.com/pcvolkmer/osc-variant.git synced 2025-04-19 19:56:50 +00:00

test: change default values for form fields with profile

This commit is contained in:
Paul-Christian Volkmer 2024-03-18 14:48:15 +01:00
parent 43298e402f
commit 6a5b7ce6de
4 changed files with 148 additions and 15 deletions

View File

@ -28,16 +28,16 @@ use std::collections::HashSet;
use console::style; use console::style;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::checks::{Checkable, CheckNotice};
use crate::checks::CheckNotice::ErrorWithCode; use crate::checks::CheckNotice::ErrorWithCode;
use crate::checks::{CheckNotice, Checkable};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::requirements::{Requirement, Requires};
use crate::model::{ use crate::model::{
apply_profile_to_form_entry, apply_profile_to_form_field, Ansichten, Comparable, Entries, Ansichten, apply_profile_to_form_entry, apply_profile_to_form_field, Comparable, Entries,
Filter, FolderContent, FormEntry, FormEntryContainer, Kennzahlen, Listable, MenuCategory, Filter, FolderContent, FormEntry, FormEntryContainer, Kennzahlen, Listable, MenuCategory,
PlausibilityRules, PunkteKategorien, RefEntries, Script, Sortable, PlausibilityRules, PunkteKategorien, RefEntries, Script, Sortable,
}; };
use crate::model::{Haeufigkeiten, Ordner}; use crate::model::{Haeufigkeiten, Ordner};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::requirements::{Requirement, Requires};
use crate::profile::Profile; use crate::profile::Profile;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -190,7 +190,7 @@ impl FormEntryContainer for DataForm {
apply_profile_to_form_entry(entry, form_reference) apply_profile_to_form_entry(entry, form_reference)
}); });
// Hide form field using filter set to "false" if requested // Hide form field using filter set to "false" if requested and change default value
profile_form profile_form
.form_fields .form_fields
.iter() .iter()
@ -647,6 +647,10 @@ impl FormEntry for Entry {
}); });
} }
fn update_default_value(&mut self, value: String) {
self.default_value = value
}
fn hide(&mut self) { fn hide(&mut self) {
self.filter = Some(Filter { self.filter = Some(Filter {
condition: "false".into(), condition: "false".into(),
@ -683,3 +687,62 @@ pub struct DataFormEntries {
#[serde(rename = "EntryName")] #[serde(rename = "EntryName")]
entry_name: Option<Vec<String>>, entry_name: Option<Vec<String>>,
} }
#[cfg(test)]
mod tests {
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile;
#[test]
fn should_change_dataform_entry_default_value() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Hauptformular'
form_fields:
- name: Auswahl
default_value: 'B'
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl");
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, "");
onkostar_editor.apply_profile(&profile);
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl");
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, "B")
}
#[test]
fn should_not_change_dataform_entry_default_value() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Hauptformular'
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl");
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, "");
onkostar_editor.apply_profile(&profile);
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].name, "Auswahl");
assert_eq!(onkostar_editor.editor.data_form[0].entries.entry[2].default_value, "")
}
}

View File

@ -335,6 +335,12 @@ where
if entry.get_name() == form_field.name && form_field.hide { if entry.get_name() == form_field.name && form_field.hide {
entry.hide() entry.hide()
} }
if entry.get_name() == form_field.name {
if let Some(new_default_value) = &form_field.default_value {
entry.update_default_value(new_default_value.to_string())
}
}
} }
pub trait FormEntryContainer { pub trait FormEntryContainer {
@ -379,6 +385,7 @@ pub trait FormEntry {
fn update_anzeige(&mut self, value: String); fn update_anzeige(&mut self, value: String);
fn update_anzeige_auswahl(&mut self, value: String); fn update_anzeige_auswahl(&mut self, value: String);
fn update_scripts_code(&mut self, value: String); fn update_scripts_code(&mut self, value: String);
fn update_default_value(&mut self, value: String);
fn hide(&mut self); fn hide(&mut self);
} }

View File

@ -45,7 +45,7 @@ pub struct OnkostarEditor {
#[serde(rename = "InfoXML")] #[serde(rename = "InfoXML")]
info_xml: InfoXML, info_xml: InfoXML,
#[serde(rename = "Editor")] #[serde(rename = "Editor")]
editor: Editor, pub editor: Editor,
} }
impl OnkostarEditor { impl OnkostarEditor {
@ -506,13 +506,13 @@ pub struct InfoXML {
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
struct Editor { pub struct Editor {
#[serde(rename = "PropertyCatalogue", default)] #[serde(rename = "PropertyCatalogue", default)]
property_catalogue: Vec<PropertyCatalogue>, property_catalogue: Vec<PropertyCatalogue>,
#[serde(rename = "DataCatalogue", default)] #[serde(rename = "DataCatalogue", default)]
data_catalogue: Vec<DataCatalogue>, data_catalogue: Vec<DataCatalogue>,
#[serde(rename = "Unterformular", default)] #[serde(rename = "Unterformular", default)]
unterformular: Vec<Unterformular>, pub unterformular: Vec<Unterformular>,
#[serde(rename = "DataForm", default)] #[serde(rename = "DataForm", default)]
data_form: Vec<DataForm>, pub data_form: Vec<DataForm>,
} }

View File

@ -25,19 +25,19 @@
use std::cmp::Ordering; use std::cmp::Ordering;
use std::collections::HashSet; use std::collections::HashSet;
use crate::checks::CheckNotice::ErrorWithCode;
use crate::checks::{CheckNotice, Checkable};
use console::style; use console::style;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::model::onkostar_editor::OnkostarEditor; use crate::checks::{Checkable, CheckNotice};
use crate::model::requirements::{Requirement, Requires}; use crate::checks::CheckNotice::ErrorWithCode;
use crate::model::{ use crate::model::{
apply_profile_to_form_entry, apply_profile_to_form_field, Ansichten, Comparable, Entries, Ansichten, apply_profile_to_form_entry, apply_profile_to_form_field, Comparable, Entries,
Filter, FolderContent, FormEntry, FormEntryContainer, Kennzahlen, Listable, MenuCategory, Filter, FolderContent, FormEntry, FormEntryContainer, Kennzahlen, Listable, MenuCategory,
PlausibilityRules, PunkteKategorien, RefEntries, Script, Sortable, PlausibilityRules, PunkteKategorien, RefEntries, Script, Sortable,
}; };
use crate::model::{Haeufigkeiten, Ordner}; use crate::model::{Haeufigkeiten, Ordner};
use crate::model::onkostar_editor::OnkostarEditor;
use crate::model::requirements::{Requirement, Requires};
use crate::profile::Profile; use crate::profile::Profile;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -193,7 +193,7 @@ impl FormEntryContainer for Unterformular {
apply_profile_to_form_entry(entry, form_reference) apply_profile_to_form_entry(entry, form_reference)
}); });
// Hide form field using filter set to "false" if requested // Hide form field using filter set to "false" if requested and change default value
profile_form profile_form
.form_fields .form_fields
.iter() .iter()
@ -645,6 +645,10 @@ impl FormEntry for Entry {
}); });
} }
fn update_default_value(&mut self, value: String) {
self.default_value = value
}
fn hide(&mut self) { fn hide(&mut self) {
self.filter = Some(Filter { self.filter = Some(Filter {
condition: "false".into(), condition: "false".into(),
@ -682,3 +686,62 @@ pub struct DataFormEntries {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
entry_name: Option<Vec<String>>, entry_name: Option<Vec<String>>,
} }
#[cfg(test)]
mod tests {
use std::str::FromStr;
use crate::model::onkostar_editor::OnkostarEditor;
use crate::profile::Profile;
#[test]
fn should_change_dataform_entry_default_value() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Unterformular'
form_fields:
- name: Termin
default_value: '2024-03-18'
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin");
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, "");
onkostar_editor.apply_profile(&profile);
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin");
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, "2024-03-18")
}
#[test]
fn should_not_change_dataform_entry_default_value() {
let onkostar_editor = OnkostarEditor::from_str(include_str!("../../tests/test.osc"));
assert!(onkostar_editor.is_ok());
let mut onkostar_editor = onkostar_editor.unwrap();
let profile = "forms:
- name: 'Unterformular'
";
let profile = Profile::from_str(profile);
assert!(profile.is_ok());
let profile = profile.unwrap();
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin");
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, "");
onkostar_editor.apply_profile(&profile);
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].name, "Termin");
assert_eq!(onkostar_editor.editor.unterformular[0].entries.entry[1].default_value, "")
}
}