<commit_before>//
//  PostTest.swift
//  Example
//
//  Created by <NAME> on 21/07/16.
//  Copyright © 2016 Tapglue. All rights reserved.
//

import XCTest
import Tapglue
import RxSwift
import Nimble
import RxBlocking

class PostTest: XCTestCase {

    let username = "PostTestUser1"
    let password = "<PASSWORD>"
    let tapglue = RxTapglue(configuration: Configuration())
    var user = User()

    let attachment = Attachment(contents: ["en":"contents"], name: "userPost", type: .Text)
    var post: Post!
    
    override func setUp() {
        super.setUp()
        user.username = username
        user.password = password
        
        do {
            user = try tapglue.createUser(user).toBlocking().first()!
            user = try tapglue.loginUser(username, password: password).toBlocking().first()!

            post = Post(visibility: .Connections, attachments: [attachment])
        } catch {
            fail("failed to create and login user for integration tests")
        }
    }
    
    override func tearDown() {
        super.tearDown()
        do {
            try tapglue.loginUser(username, password: password).toBlocking().first()
            try tapglue.deleteCurrentUser().toBlocking().first()
        } catch {
            fail("failed to login and delete user for integration tests")
        }
    }
    
    func testCreatePost() {
        var networkPost: Post?
        _ = tapglue.createPost(post).subscribeNext { post in
            networkPost = post
        }
        
        expect(networkPost?.id).toEventuallyNot(beNil())
    }
    
    func testDeletePost() throws {
        let networkPost = try tapglue.createPost(post).toBlocking().first()!

        var wasDeleted = false
        _ = tapglue.deletePost(networkPost.id!).subscribeCompleted {
            wasDeleted = true
        }

        expect(wasDeleted).toEventually(beTrue())
    }

    func testRetrievePost() throws {
        let createdPost = try tapglue.createPost(post).toBlocking().first()!
        let retrievedPost = try tapglue.retrievePost(createdPost.id!).toBlocking().first()!
        expect(createdPost.id!).to(equal(retrievedPost.id!))
    }

    func testUpdatePost() throws {
        let createdPost = try tapglue.createPost(post).toBlocking().first()!
        createdPost.attachments!.append(Attachment(contents: ["es":"contenidos"], name: "spanish", type: .Text))
        let updatedPost = try tapglue.updatePost(createdPost).toBlocking().first()
        
        expect(updatedPost?.attachments?.count).to(equal(2))
    }
}<commit_msg>Add acceptance tests for posts by users retrieval<commit_after>//
//  PostTest.swift
//  Example
//
//  Created by <NAME> on 21/07/16.
//  Copyright © 2016 Tapglue. All rights reserved.
//

import XCTest
import Tapglue
import RxSwift
import Nimble
import RxBlocking

class PostTest: XCTestCase {

    let username = "PostTestUser1"
    let password = "<PASSWORD>"
    let tapglue = RxTapglue(configuration: Configuration())
    var user = User()

    let attachment = Attachment(contents: ["en":"contents"], name: "userPost", type: .Text)
    var post: Post!
    
    override func setUp() {
        super.setUp()
        user.username = username
        user.password = password
        
        do {
            user = try tapglue.createUser(user).toBlocking().first()!
            user = try tapglue.loginUser(username, password: password).toBlocking().first()!

            post = Post(visibility: .Connections, attachments: [attachment])
        } catch {
            fail("failed to create and login user for integration tests")
        }
    }
    
    override func tearDown() {
        super.tearDown()
        do {
            try tapglue.loginUser(username, password: password).toBlocking().first()
            try tapglue.deleteCurrentUser().toBlocking().first()
        } catch {
            fail("failed to login and delete user for integration tests")
        }
    }
    
    func testCreatePost() {
        var networkPost: Post?
        _ = tapglue.createPost(post).subscribeNext { post in
            networkPost = post
        }
        
        expect(networkPost?.id).toEventuallyNot(beNil())
    }
    
    func testDeletePost() throws {
        let networkPost = try tapglue.createPost(post).toBlocking().first()!

        var wasDeleted = false
        _ = tapglue.deletePost(networkPost.id!).subscribeCompleted {
            wasDeleted = true
        }

        expect(wasDeleted).toEventually(beTrue())
    }

    func testRetrievePost() throws {
        let createdPost = try tapglue.createPost(post).toBlocking().first()!
        let retrievedPost = try tapglue.retrievePost(createdPost.id!).toBlocking().first()!
        expect(createdPost.id!).to(equal(retrievedPost.id!))
    }

    func testUpdatePost() throws {
        let createdPost = try tapglue.createPost(post).toBlocking().first()!
        createdPost.attachments!.append(Attachment(contents: ["es":"contenidos"], name: "spanish", type: .Text))
        let updatedPost = try tapglue.updatePost(createdPost).toBlocking().first()
        
        expect(updatedPost?.attachments?.count).to(equal(2))
    }
    
    func testRetrievePostsByUser() throws {
        let createdPost = try tapglue.createPost(post).toBlocking().first()!
        let retrievedPosts = try tapglue.retrievePostsByUser(user.id!).toBlocking().first()!
        
        expect(retrievedPosts.first?.id).to(equal(createdPost.id))
    }
    
    func testRetrievePostsByUserMapsUsersToPosts() throws {
        let createdPost = try tapglue.createPost(post).toBlocking().first()!
        let retrievedPosts = try tapglue.retrievePostsByUser(user.id!).toBlocking().first()!
        
        expect(retrievedPosts.first?.user?.id).to(equal(user.id))
    }
}
{'fraction_non_alphanumeric': 0.11986835267625151, 'fraction_numerical': 0.0041572839078468735, 'mean_word_length': 2.5663990117356392, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4104737', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1646, 'n_words': 351}