Skip to content

Commit

Permalink
feat(#124): no inversed, <uuid, github>
Browse files Browse the repository at this point in the history
  • Loading branch information
h1alexbel committed Aug 29, 2024
1 parent ee7acc1 commit f0d2715
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 71 deletions.
63 changes: 49 additions & 14 deletions server/src/objects/fakehub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,40 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::objects::github::GitHub;
use crate::objects::inversed::inversed;
use crate::objects::user::User;
use std::collections::HashMap;
use uuid::Uuid;

/// Fakehub.
#[derive(Clone)]
pub struct Fakehub {
hubs: HashMap<Uuid, String>,
inverse: HashMap<String, Uuid>,
hubs: HashMap<Uuid, GitHub>,
urled: HashMap<String, GitHub>,
}

impl Default for Fakehub {
fn default() -> Fakehub {
let mut hubs: HashMap<Uuid, String> = HashMap::new();
hubs.insert(Uuid::new_v4(), String::from("https://github.com"));
let mut hubs: HashMap<Uuid, GitHub> = HashMap::new();
let id = Uuid::new_v4();
hubs.insert(
id,
GitHub {
id,
url: String::from("https://github.com"),
users: vec![User::new(String::from("jeff"))],
},
);
Fakehub {
hubs: hubs.clone(),
inverse: inversed(hubs),
urled: hubs.into_values().map(|v| (v.clone().url, v)).collect(),
}
}
}

impl Fakehub {
/// Add new GitHub
pub fn add(&mut self, github: GitHub) {
self.hubs.insert(github.id, github.url.clone());
self.inverse.insert(github.url, github.id);
self.hubs.insert(github.id, github.clone());
self.urled.insert(github.clone().url, github);
}

/// All GitHub instances.
Expand All @@ -55,7 +62,8 @@ impl Fakehub {
.iter()
.map(|(k, v)| GitHub {
id: *k,
url: v.clone(),
url: v.url.clone(),
users: v.users.clone(),
})
.collect()
}
Expand All @@ -73,10 +81,10 @@ mod tests {
fn creates_default_fakehub() -> Result<()> {
let fakehub = Fakehub::default();
let default = fakehub
.inverse
.urled
.get("https://github.com")
.expect("Failed to get GitHub");
assert_that!(default.is_nil(), is(equal_to(false)));
assert_that!(default.id.is_nil(), is(equal_to(false)));
Ok(())
}

Expand All @@ -88,9 +96,10 @@ mod tests {
fakehub.add(GitHub {
id: expected,
url: url.clone(),
users: vec![],
});
let id = fakehub.inverse.get(&url).expect("Failed to get GitHub");
assert_that!(*id, is(equal_to(expected)));
let github = fakehub.urled.get(&url).expect("Failed to get GitHub");
assert_that!(github.id, is(equal_to(expected)));
Ok(())
}

Expand All @@ -100,9 +109,35 @@ mod tests {
fakehub.add(GitHub {
id: Uuid::new_v4(),
url: String::from("https://test.github.com"),
users: vec![],
});
let instances = fakehub.githubs();
assert_that!(instances.len(), is(equal_to(2)));
Ok(())
}

#[test]
fn returns_default_instance() -> Result<()> {
let fakehub = Fakehub::default();
let instances = fakehub.githubs();
let github = instances.first().expect("Failed to get GitHub");
let users = github.clone().users;
let user = users.first().expect("Failed to get user");
assert_that!(&github.url, is(equal_to("https://github.com")));
assert_that!(&user.username, is(equal_to("jeff")));
Ok(())
}

#[test]
fn returns_github_with_users() -> Result<()> {
let fakehub = Fakehub::default();
let github = fakehub
.urled
.get("https://github.com")
.expect("Failed to get GitHub");
let users = &github.users;
let user = users.first().expect("Failed to get user");
assert_that!(&user.username, is(equal_to("jeff")));
Ok(())
}
}
41 changes: 41 additions & 0 deletions server/src/objects/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,51 @@
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
use crate::objects::user::User;
use uuid::Uuid;

/// GitHub.
#[derive(Clone)]
pub struct GitHub {
pub(crate) id: Uuid,
pub(crate) url: String,
pub(crate) users: Vec<User>,
}

impl GitHub {
/// Add user to GitHub.
/// `user` User
pub fn add_user(&mut self, user: User) {
self.users.push(user);
}

/// Users in GitHub.
pub fn users(self) -> Vec<User> {
self.users
}
}

#[cfg(test)]
mod tests {

use crate::objects::github::GitHub;
use crate::objects::user::User;
use anyhow::Result;
use hamcrest::{equal_to, is, HamcrestMatcher};
use uuid::Uuid;

#[test]
fn adds_user() -> Result<()> {
let mut github = GitHub {
id: Uuid::new_v4(),
url: String::from("https://test.github.com"),
users: vec![],
};
let expected = String::from("jeff");
github.add_user(User::new(expected.clone()));
let users = github.users();
let user = users.first().expect("Failed to get user");
assert_that!(&user.username, is(equal_to(&expected)));
Ok(())
}
}
54 changes: 0 additions & 54 deletions server/src/objects/inversed.rs

This file was deleted.

2 changes: 0 additions & 2 deletions server/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,5 @@
pub mod fakehub;
/// GitHub.
pub mod github;
/// Inverse.
pub mod inversed;
/// GitHub user.
pub mod user;
2 changes: 1 addition & 1 deletion server/src/objects/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};
use serde_xml_rs::to_string;

/// GitHub user.
#[derive(Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct User {
/// Username.
pub(crate) username: String,
Expand Down

0 comments on commit f0d2715

Please sign in to comment.