Skip to content

Commit 046a9d8

Browse files
committed
typosquat/test_util: Convert Faker struct to faker module
Since the `Faker` struct has no fields anymore, we may as well convert these to regular functions that don't need an instance of the struct.
1 parent 8293148 commit 046a9d8

File tree

3 files changed

+19
-33
lines changed

3 files changed

+19
-33
lines changed

src/typosquat/database.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl From<crate::models::Owner> for Owner {
163163

164164
#[cfg(test)]
165165
mod tests {
166-
use crate::{test_util::test_db_connection, typosquat::test_util::Faker};
166+
use crate::{test_util::test_db_connection, typosquat::test_util::faker};
167167
use thiserror::Error;
168168

169169
use super::*;
@@ -172,21 +172,19 @@ mod tests {
172172
fn top_crates() -> Result<(), Error> {
173173
let (_test_db, mut conn) = test_db_connection();
174174

175-
let mut faker = Faker::new();
176-
177175
// Set up two users.
178-
let user_a = faker.user(&mut conn, "a")?;
179-
let user_b = faker.user(&mut conn, "b")?;
176+
let user_a = faker::user(&mut conn, "a")?;
177+
let user_b = faker::user(&mut conn, "b")?;
180178

181179
// Set up three crates with various ownership schemes.
182-
let _top_a = faker.crate_and_version(&mut conn, "a", "Hello", &user_a, 2)?;
183-
let top_b = faker.crate_and_version(&mut conn, "b", "Yes, this is dog", &user_b, 1)?;
184-
let not_top_c = faker.crate_and_version(&mut conn, "c", "Unpopular", &user_a, 0)?;
180+
let _top_a = faker::crate_and_version(&mut conn, "a", "Hello", &user_a, 2)?;
181+
let top_b = faker::crate_and_version(&mut conn, "b", "Yes, this is dog", &user_b, 1)?;
182+
let not_top_c = faker::crate_and_version(&mut conn, "c", "Unpopular", &user_a, 0)?;
185183

186184
// Let's set up a team that owns both b and c, but not a.
187-
let not_the_a_team = faker.team(&mut conn, "org", "team")?;
188-
faker.add_crate_to_team(&mut conn, &user_b, &top_b.0, &not_the_a_team)?;
189-
faker.add_crate_to_team(&mut conn, &user_b, &not_top_c.0, &not_the_a_team)?;
185+
let not_the_a_team = faker::team(&mut conn, "org", "team")?;
186+
faker::add_crate_to_team(&mut conn, &user_b, &top_b.0, &not_the_a_team)?;
187+
faker::add_crate_to_team(&mut conn, &user_b, &not_top_c.0, &not_the_a_team)?;
190188

191189
let top_crates = TopCrates::new(&mut conn, 2)?;
192190

src/typosquat/test_util.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@ use crate::{
88
schema::{crate_downloads, crate_owners, users},
99
};
1010

11-
pub struct Faker {}
12-
13-
impl Faker {
14-
pub fn new() -> Self {
15-
Self {}
16-
}
11+
pub mod faker {
12+
use super::*;
1713

1814
pub fn add_crate_to_team(
19-
&mut self,
2015
conn: &mut PgConnection,
2116
user: &User,
2217
krate: &Crate,
@@ -38,7 +33,6 @@ impl Faker {
3833
}
3934

4035
pub fn crate_and_version(
41-
&mut self,
4236
conn: &mut PgConnection,
4337
name: &str,
4438
description: &str,
@@ -68,12 +62,7 @@ impl Faker {
6862
Ok((krate, version))
6963
}
7064

71-
pub fn team(
72-
&mut self,
73-
conn: &mut PgConnection,
74-
org: &str,
75-
team: &str,
76-
) -> anyhow::Result<Owner> {
65+
pub fn team(conn: &mut PgConnection, org: &str, team: &str) -> anyhow::Result<Owner> {
7766
Ok(Owner::Team(
7867
NewTeam::new(
7968
&format!("github:{org}:{team}"),
@@ -86,7 +75,7 @@ impl Faker {
8675
))
8776
}
8877

89-
pub fn user(&mut self, conn: &mut PgConnection, login: &str) -> QueryResult<User> {
78+
pub fn user(conn: &mut PgConnection, login: &str) -> QueryResult<User> {
9079
let user = NewUser::new(next_gh_id(), login, None, None, "token");
9180

9281
diesel::insert_into(users::table)

src/worker/jobs/typosquat.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ Specific squat checks that triggered:
122122

123123
#[cfg(test)]
124124
mod tests {
125-
use crate::{test_util::test_db_connection, typosquat::test_util::Faker};
125+
use crate::{test_util::test_db_connection, typosquat::test_util::faker};
126126
use lettre::Address;
127127

128128
use super::*;
@@ -131,25 +131,24 @@ mod tests {
131131
fn integration() -> anyhow::Result<()> {
132132
let emails = Emails::new_in_memory();
133133
let (_test_db, mut conn) = test_db_connection();
134-
let mut faker = Faker::new();
135134

136135
// Set up a user and a popular crate to match against.
137-
let user = faker.user(&mut conn, "a")?;
138-
faker.crate_and_version(&mut conn, "my-crate", "It's awesome", &user, 100)?;
136+
let user = faker::user(&mut conn, "a")?;
137+
faker::crate_and_version(&mut conn, "my-crate", "It's awesome", &user, 100)?;
139138

140139
// Prime the cache so it only includes the crate we just created.
141140
let cache = Cache::new(vec!["[email protected]".to_string()], &mut conn)?;
142141

143142
// Now we'll create new crates: one problematic, one not so.
144-
let other_user = faker.user(&mut conn, "b")?;
145-
let (angel, _version) = faker.crate_and_version(
143+
let other_user = faker::user(&mut conn, "b")?;
144+
let (angel, _version) = faker::crate_and_version(
146145
&mut conn,
147146
"innocent-crate",
148147
"I'm just a simple, innocent crate",
149148
&other_user,
150149
0,
151150
)?;
152-
let (demon, _version) = faker.crate_and_version(
151+
let (demon, _version) = faker::crate_and_version(
153152
&mut conn,
154153
"mycrate",
155154
"I'm even more innocent, obviously",

0 commit comments

Comments
 (0)