Add tests for connect/disconnect
This commit is contained in:
		
							parent
							
								
									83d05f086a
								
							
						
					
					
						commit
						52408d8189
					
				
					 2 changed files with 36 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -57,13 +57,11 @@ impl ActorCache {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    pub(crate) async fn add_connection(&self, actor: Actor) -> Result<(), MyError> {
 | 
			
		||||
        log::debug!("Adding connection: {}", actor.id);
 | 
			
		||||
        self.db.add_connection(actor.id.clone()).await?;
 | 
			
		||||
        self.db.save_actor(actor).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub(crate) async fn remove_connection(&self, actor: &Actor) -> Result<(), MyError> {
 | 
			
		||||
        log::debug!("Removing connection: {}", actor.id);
 | 
			
		||||
        self.db.remove_connection(actor.id.clone()).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										36
									
								
								src/db.rs
									
										
									
									
									
								
							
							
						
						
									
										36
									
								
								src/db.rs
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -485,6 +485,7 @@ impl Db {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    pub(crate) async fn remove_connection(&self, actor_id: Url) -> Result<(), MyError> {
 | 
			
		||||
        log::debug!("Removing Connection: {}", actor_id);
 | 
			
		||||
        self.unblock(move |inner| {
 | 
			
		||||
            inner
 | 
			
		||||
                .connected_actor_ids
 | 
			
		||||
| 
						 | 
				
			
			@ -496,6 +497,7 @@ impl Db {
 | 
			
		|||
    }
 | 
			
		||||
 | 
			
		||||
    pub(crate) async fn add_connection(&self, actor_id: Url) -> Result<(), MyError> {
 | 
			
		||||
        log::debug!("Adding Connection: {}", actor_id);
 | 
			
		||||
        self.unblock(move |inner| {
 | 
			
		||||
            inner
 | 
			
		||||
                .connected_actor_ids
 | 
			
		||||
| 
						 | 
				
			
			@ -648,6 +650,40 @@ mod tests {
 | 
			
		|||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn disconnect_and_verify() {
 | 
			
		||||
        run(|db| async move {
 | 
			
		||||
            let example_actor: Url = "http://example.com/actor".parse().unwrap();
 | 
			
		||||
            let example_sub_actor: Url = "http://example.com/users/fake".parse().unwrap();
 | 
			
		||||
            db.add_connection(example_actor.clone()).await.unwrap();
 | 
			
		||||
            assert!(db.is_connected(example_sub_actor.clone()).await.unwrap());
 | 
			
		||||
 | 
			
		||||
            db.remove_connection(example_actor).await.unwrap();
 | 
			
		||||
            assert!(!db.is_connected(example_sub_actor).await.unwrap());
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn connected_actor_in_connected_list() {
 | 
			
		||||
        run(|db| async move {
 | 
			
		||||
            let example_actor: Url = "http://example.com/actor".parse().unwrap();
 | 
			
		||||
            db.add_connection(example_actor.clone()).await.unwrap();
 | 
			
		||||
 | 
			
		||||
            assert!(db.connected_ids().await.unwrap().contains(&example_actor));
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[test]
 | 
			
		||||
    fn disconnected_actor_not_in_connected_list() {
 | 
			
		||||
        run(|db| async move {
 | 
			
		||||
            let example_actor: Url = "http://example.com/actor".parse().unwrap();
 | 
			
		||||
            db.add_connection(example_actor.clone()).await.unwrap();
 | 
			
		||||
            db.remove_connection(example_actor.clone()).await.unwrap();
 | 
			
		||||
 | 
			
		||||
            assert!(!db.connected_ids().await.unwrap().contains(&example_actor));
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn run<F, Fut>(f: F)
 | 
			
		||||
    where
 | 
			
		||||
        F: Fn(Db) -> Fut,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue