| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
mod client; |
| 6 |
mod data; |
| 7 |
|
| 8 |
pub use client::{MockAttachment, MockIcon, MockRecord, MockRemoteSettingsClient}; |
| 9 |
pub use data::*; |
| 10 |
|
| 11 |
use crate::{suggestion::YelpSubjectType, Suggestion}; |
| 12 |
|
| 13 |
use parking_lot::Once; |
| 14 |
use serde_json::Value as JsonValue; |
| 15 |
|
| 16 |
pub use serde_json::json; |
| 17 |
pub use sql_support::ConnExt; |
| 18 |
|
| 19 |
use std::{borrow::Borrow, hash::Hash}; |
| 20 |
|
| 21 |
/// Trait with utility functions for JSON handling in the tests |
| 22 |
pub trait JsonExt { |
| 23 |
fn merge(self, other: JsonValue) -> JsonValue; |
| 24 |
|
| 25 |
fn remove<Q>(self, key: &Q) -> JsonValue |
| 26 |
where |
| 27 |
String: Borrow<Q>, |
| 28 |
Q: ?Sized + Ord + Eq + Hash; |
| 29 |
} |
| 30 |
|
| 31 |
impl JsonExt for JsonValue { |
| 32 |
fn merge(mut self, mut other: JsonValue) -> JsonValue { |
| 33 |
let JsonValue::Object(self_map) = &mut self else { |
| 34 |
panic!("merge called on non-object `self`: {self:?}"); |
| 35 |
}; |
| 36 |
let JsonValue::Object(other_map) = &mut other else { |
| 37 |
panic!("merge called on non-object `other`: {other:?}"); |
| 38 |
}; |
| 39 |
self_map.append(other_map); |
| 40 |
self |
| 41 |
} |
| 42 |
|
| 43 |
fn remove<Q>(mut self, key: &Q) -> JsonValue |
| 44 |
where |
| 45 |
String: Borrow<Q>, |
| 46 |
Q: ?Sized + Ord + Eq + Hash, |
| 47 |
{ |
| 48 |
let JsonValue::Object(obj) = &mut self else { |
| 49 |
panic!("remove called on non-object: {self:?}"); |
| 50 |
}; |
| 51 |
obj.remove(key); |
| 52 |
self |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// Extra methods for testing |
| 57 |
impl Suggestion { |
| 58 |
pub fn with_score(mut self, score: f64) -> Self { |
| 59 |
let current_score = match &mut self { |
| 60 |
Self::Amp { score, .. } => score, |
| 61 |
Self::Pocket { score, .. } => score, |
| 62 |
Self::Amo { score, .. } => score, |
| 63 |
Self::Yelp { score, .. } => score, |
| 64 |
Self::Mdn { score, .. } => score, |
| 65 |
Self::Weather { score, .. } => score, |
| 66 |
Self::Wikipedia { .. } => panic!("with_score not valid for wikipedia suggestions"), |
| 67 |
Self::Fakespot { score, .. } => score, |
| 68 |
Self::Dynamic { score, .. } => score, |
| 69 |
}; |
| 70 |
*current_score = score; |
| 71 |
self |
| 72 |
} |
| 73 |
|
| 74 |
pub fn has_location_sign(self, has_location_sign: bool) -> Self { |
| 75 |
match self { |
| 76 |
Self::Yelp { |
| 77 |
title, |
| 78 |
url, |
| 79 |
icon, |
| 80 |
icon_mimetype, |
| 81 |
score, |
| 82 |
subject_exact_match, |
| 83 |
subject_type, |
| 84 |
location_param, |
| 85 |
.. |
| 86 |
} => Self::Yelp { |
| 87 |
title, |
| 88 |
url, |
| 89 |
icon, |
| 90 |
icon_mimetype, |
| 91 |
score, |
| 92 |
subject_exact_match, |
| 93 |
subject_type, |
| 94 |
location_param, |
| 95 |
has_location_sign, |
| 96 |
}, |
| 97 |
_ => panic!("has_location_sign only valid for yelp suggestions"), |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
pub fn subject_exact_match(self, subject_exact_match: bool) -> Self { |
| 102 |
match self { |
| 103 |
Self::Yelp { |
| 104 |
title, |
| 105 |
url, |
| 106 |
icon, |
| 107 |
icon_mimetype, |
| 108 |
score, |
| 109 |
subject_type, |
| 110 |
has_location_sign, |
| 111 |
location_param, |
| 112 |
.. |
| 113 |
} => Self::Yelp { |
| 114 |
title, |
| 115 |
url, |
| 116 |
icon, |
| 117 |
icon_mimetype, |
| 118 |
score, |
| 119 |
subject_exact_match, |
| 120 |
subject_type, |
| 121 |
location_param, |
| 122 |
has_location_sign, |
| 123 |
}, |
| 124 |
_ => panic!("subject_exact_match only valid for yelp suggestions"), |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
pub fn subject_type(self, subject_type: YelpSubjectType) -> Self { |
| 129 |
match self { |
| 130 |
Self::Yelp { |
| 131 |
title, |
| 132 |
url, |
| 133 |
icon, |
| 134 |
icon_mimetype, |
| 135 |
score, |
| 136 |
subject_exact_match, |
| 137 |
has_location_sign, |
| 138 |
location_param, |
| 139 |
.. |
| 140 |
} => Self::Yelp { |
| 141 |
title, |
| 142 |
url, |
| 143 |
icon, |
| 144 |
icon_mimetype, |
| 145 |
score, |
| 146 |
subject_exact_match, |
| 147 |
subject_type, |
| 148 |
location_param, |
| 149 |
has_location_sign, |
| 150 |
}, |
| 151 |
_ => panic!("subject_type only valid for yelp suggestions"), |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
pub fn before_each() { |
| 157 |
static ONCE: Once = Once::new(); |
| 158 |
ONCE.call_once(|| { |
| 159 |
error_support::init_for_tests_with_level(error_support::Level::Trace); |
| 160 |
}); |
| 161 |
} |