def test_parse_storage_single_sequence(self):
        transactions = [
            dict(date='01/02/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23'),
            dict(date='01/12/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23'),
            dict(date='01/22/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23'),
            dict(date='02/01/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23')
        ]
        storage = parse_storage(transactions)
        sequence1 = storage.get_sequence(
            Transaction(date='01/02/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))
        sequence2 = storage.get_sequence(
            Transaction(date='01/12/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))
        sequence3 = storage.get_sequence(
            Transaction(date='01/22/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))
        sequence4 = storage.get_sequence(
            Transaction(date='02/01/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))

        assert sequence1 == sequence2 == sequence3 == sequence4
Exemple #2
0
 def test_compare_description(self, compare):
     json_transaction = dict(date='12/24/2019',
                             description='TEST INVOICE 1234',
                             amount='435.23')
     other_description = 'TEST 1014'
     transaction = Transaction(**json_transaction)
     transaction.compare_description(other_description)
     compare.assert_called_with(transaction.description, other_description)
Exemple #3
0
 def test_id(self):
     json_transaction = dict(date='12/24/2019',
                             description='TEST INVOICE 1234',
                             amount='435.23')
     transaction = Transaction(**json_transaction)
     transaction2 = Transaction(**json_transaction)
     transaction3 = Transaction(**json_transaction)
     transaction4 = Transaction(date='12/24/2019',
                                description='TEST INVOICE 1234',
                                amount='432.23')
     assert transaction.id == transaction2.id == transaction3.id != transaction4.id
    def test_parse_short_sequence(self):
        transaction1 = Transaction(date='01/02/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction2 = Transaction(date='01/12/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction3 = Transaction(date='01/22/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactions = [transaction1, transaction2, transaction3]

        sequences = parse_sequences(transactions)

        assert len(sequences) == 0
Exemple #5
0
def parse_storage(json_transactions):
    """
    Parses a list of dict transactions into a Sequence Storage.

    :param json_transactions: List of transactions in a dict format.
    :return: SequenceStorage
    """
    transactions = [
        Transaction(**transaction) for transaction in json_transactions
    ]

    storage = SequenceStorage()

    # We iterate through the list of transactions, parsing sequences from those that have similar descriptions
    # and adding those to a Sequence Storage.
    while len(transactions) > 0:
        transaction = transactions[0]

        descs = list(
            filter(
                lambda x: x.compare_description(transaction.description) >
                SIMILARITY_RATIO, transactions))
        sequences = parse_sequences(descs)
        storage.add_sequences(sequences)
        for tran in descs:
            transactions.remove(tran)

    return storage
Exemple #6
0
 def create_sequence(self):
     transaction1 = Transaction(date='01/02/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction2 = Transaction(date='01/12/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction3 = Transaction(date='01/22/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transactions = [transaction1, transaction2, transaction3]
     interval = 10
     margin = 3
     ownership = True
     sequence = TransactionSequence(interval)
     sequence.add_transactions(transactions, margin, ownership)
     return sequence
Exemple #7
0
 def test_contains(self):
     transaction1 = Transaction(date='01/02/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction2 = Transaction(date='01/12/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction3 = Transaction(date='01/22/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transactions = [transaction1, transaction2]
     interval = 10
     margin = 3
     ownership = True
     sequence = TransactionSequence(interval)
     sequence.add_transactions(transactions, margin, ownership)
     assert transaction1 in sequence
     assert transaction3 not in sequence
Exemple #8
0
 def test_get_last_transaction(self):
     transaction1 = Transaction(date='01/02/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction2 = Transaction(date='01/12/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction3 = Transaction(date='01/22/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transactions = [transaction1, transaction2, transaction3]
     interval = 10
     margin = 3
     ownership = True
     sequence = TransactionSequence(interval)
     sequence.add_transactions(transactions, margin, ownership)
     first = sequence.get_last_transaction()
     assert first == transaction3
Exemple #9
0
 def test_len(self):
     transaction1 = Transaction(date='01/02/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction2 = Transaction(date='01/12/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transaction3 = Transaction(date='01/22/2020',
                                description='TEST INVOICE 1234',
                                amount='435.23')
     transactions = [transaction1, transaction2, transaction3]
     interval = 10
     margin = 3
     ownership = True
     expected_length = 3
     sequence = TransactionSequence(interval)
     sequence.add_transactions(transactions, margin, ownership)
     length = len(sequence)
     assert length == expected_length
Exemple #10
0
 def test_creation(self):
     json_transaction = dict(date='12/24/2019',
                             description='TEST INVOICE 1234',
                             amount='435.23')
     transaction = Transaction(**json_transaction)
     assert isinstance(transaction, Transaction)
     assert transaction.date.strftime(
         '%m/%d/%Y') == json_transaction['date']
     assert transaction.description == json_transaction['description']
     assert transaction.amount == json_transaction['amount']
     assert transaction.sequence is None
    def test_parse_single_sequence_with_singletons(self):
        transaction1 = Transaction(date='01/02/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactionX1 = Transaction(date='01/10/2020',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transaction2 = Transaction(date='01/12/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactionX2 = Transaction(date='01/18/2020',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transaction3 = Transaction(date='01/22/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction4 = Transaction(date='02/01/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactions = [transaction1, transaction2, transaction3, transaction4]

        sequences = parse_sequences(transactions)

        assert len(sequences) == 1
        assert isinstance(sequences[0], TransactionSequence)
        assert transaction1 in sequences[0]
        assert transaction2 in sequences[0]
        assert transaction3 in sequences[0]
        assert transaction4 in sequences[0]
        assert transactionX1 not in sequences[0]
        assert transactionX2 not in sequences[0]
Exemple #12
0
    def test_add_sequence(self):
        transaction1 = Transaction(date='01/02/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction2 = Transaction(date='01/12/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction3 = Transaction(date='01/22/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactions = [transaction1, transaction2, transaction3]
        interval = 10
        margin = 3
        ownership = True
        sequence = TransactionSequence(interval)
        sequence.add_transactions(transactions, margin, ownership)

        storage = SequenceStorage()
        storage.add_sequence(sequence)
        assert transaction1.id in storage.sequences
        assert transaction2.id in storage.sequences
        assert transaction3.id in storage.sequences
Exemple #13
0
    def test_add_second_transaction_outside_margin(self):
        transaction = self.create_transaction()
        second_transaction = Transaction(date='01/02/2021',
                                         description='TEST INVOICE 1234',
                                         amount='435.23')
        interval = 10
        margin = 3
        sequence = TransactionSequence(interval)
        sequence.add_transaction(transaction, margin)
        sequence.add_transaction(second_transaction, margin)

        assert transaction.sequence is None
        assert second_transaction.sequence is None
        assert transaction.id in sequence.transactions
        assert second_transaction.id not in sequence.transactions
Exemple #14
0
    def test_get_sequence(self):
        transaction1 = Transaction(date='01/02/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction2 = Transaction(date='01/12/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction3 = Transaction(date='01/22/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactions = [transaction1, transaction2]
        interval = 10
        margin = 3
        ownership = True
        sequence = TransactionSequence(interval)
        sequence.add_transactions(transactions, margin, ownership)

        storage = SequenceStorage()
        storage.add_sequence(sequence)
        sequence1 = storage.get_sequence(transaction1)
        sequence2 = storage.get_sequence(transaction2)
        sequence3 = storage.get_sequence(transaction3)
        assert sequence1 == sequence2 == sequence
        assert sequence3 is None
Exemple #15
0
    def test_iter(self):
        called = Mock()
        transaction1 = Transaction(date='01/02/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction2 = Transaction(date='01/12/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction3 = Transaction(date='01/22/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactions = [transaction1, transaction2, transaction3]
        interval = 10
        margin = 3
        ownership = True
        sequence = TransactionSequence(interval)
        sequence.add_transactions(transactions, margin, ownership)
        for transaction in sequence:
            called(transaction)

        called.assert_has_calls(
            [call(transaction1),
             call(transaction2),
             call(transaction3)])
def get_sequence():
    jdata = request.get_json()
    try:
        jtransaction = jdata['transaction']
    except KeyError:
        raise BadRequest("The Transaction was not provided")

    try:
        storage = current_app.storage
    except AttributeError:
        raise BadRequest("The Storage was not loaded")

    transaction = Transaction(**jtransaction)
    sequence = storage.get_sequence(transaction)

    response = make_response(str(sequence))
    response.mimetype = 'application/json'
    return response, 200
Exemple #17
0
 def create_transaction(self):
     json_transaction = dict(date='12/24/2019',
                             description='TEST INVOICE 1234',
                             amount='435.23')
     return Transaction(**json_transaction)
    def test_parse_multiple_sequences(self):
        transaction1 = Transaction(date='01/01/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction2 = Transaction(date='01/15/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction3 = Transaction(date='02/01/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transaction4 = Transaction(date='02/15/2020',
                                   description='TEST INVOICE 1234',
                                   amount='435.23')
        transactionX1 = Transaction(date='01/01/2021',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactionX2 = Transaction(date='02/10/2021',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactionX3 = Transaction(date='03/20/2021',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactionX4 = Transaction(date='04/30/2021',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactionY1 = Transaction(date='03/10/2022',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactionY2 = Transaction(date='03/20/2022',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactionY3 = Transaction(date='03/30/2022',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactionY4 = Transaction(date='04/09/2022',
                                    description='TEST INVOICE 1234',
                                    amount='435.23')
        transactions = [
            transaction1, transaction2, transaction3, transaction4,
            transactionX1, transactionX2, transactionX3, transactionX4,
            transactionY1, transactionY2, transactionY3, transactionY4
        ]

        sequences = parse_sequences(transactions)
        print(sequences)
        assert len(sequences) == 3
        assert isinstance(sequences[0], TransactionSequence)
        assert isinstance(sequences[1], TransactionSequence)
        assert isinstance(sequences[2], TransactionSequence)
        assert transaction1 in sequences[0]
        assert transaction2 in sequences[0]
        assert transaction3 in sequences[0]
        assert transaction4 in sequences[0]
        assert transactionX1 in sequences[1]
        assert transactionX2 in sequences[1]
        assert transactionX3 in sequences[1]
        assert transactionX4 in sequences[1]
        assert transactionY1 in sequences[2]
        assert transactionY2 in sequences[2]
        assert transactionY3 in sequences[2]
        assert transactionY4 in sequences[2]
Exemple #19
0
    def setUp(self):

        self.username = "******"
        self.email = "*****@*****.**"
        self.password = "******"
        self.testuser = User.objects.create_user(self.username,
                                                 self.email,
                                                 is_superuser=False,
                                                 is_staff=False)
        self.testuser.set_password(self.password)
        self.testuser.save()

        self.client.force_authenticate(user=User.objects.first())

        self.topic = Topic.objects.create(
            title='Test topic 1',
            owner=self.testuser,
        )
        self.topic_url = reverse('topic-detail', kwargs={'pk': self.topic.pk})

        self.comment = Comment(
            topic=self.topic,
            # 1. time spent inside "{...}" brackets
            # 2. estimates of future time needed inside "{?...}"
            # 3. declared work result - the content of comment
            text="""
            - {1.5},{?0.5} for coming up with basic class structure,
            - {?2.5} for implementation,
            - {?13.5} for testing.

            Here is the result so far:
            https://github.com/wefindx/infty2.0/commit/9f096dc54f94c31eed9558eb32ef0858f51b1aec
            """,
            owner=self.testuser,
        )
        self.comment.save()
        self.comment_url = reverse('comment-detail',
                                   kwargs={'pk': self.comment.pk})

        self.snapshot = self.comment.create_snapshot()

        self.usd = Currency(label='usd')
        self.usd.save()

        self.hprice = HourPriceSnapshot(
            name='FRED',
            base=self.usd,
            data=json.loads("""
{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","observation_start":"1600-01-01","observation_end":"9999-12-31","units":"lin","output_type":1,"file_type":"json","order_by":"observation_date","sort_order":"desc","count":136,"offset":0,"limit":1,"observations":[{"realtime_start":"2017-07-28","realtime_end":"2017-07-28","date":"2017-06-01","value":"26.25"}]}"""
                            ),
            endpoint=
            'https://api.stlouisfed.org/fred/series/observations?series_id=CES0500000003&api_key=0a90ca7b5204b2ed6e998d9f6877187e&limit=1&sort_order=desc&file_type=json',
        )
        self.hprice.save()

        self.cprice = CurrencyPriceSnapshot(
            name='FIXER',
            base=self.usd,
            data=json.loads("""
{"base":"EUR","date":"2017-07-28","rates":{"AUD":1.4732,"BGN":1.9558,"BRL":3.7015,"CAD":1.4712,"CHF":1.1357,"CNY":7.9087,"CZK":26.048,"DKK":7.4364,"GBP":0.89568,"HKD":9.1613,"HRK":7.412,"HUF":304.93,"IDR":15639.0,"ILS":4.1765,"INR":75.256,"JPY":130.37,"KRW":1317.6,"MXN":20.809,"MYR":5.0229,"NOK":9.3195,"NZD":1.5694,"PHP":59.207,"PLN":4.2493,"RON":4.558,"RUB":69.832,"SEK":9.5355,"SGD":1.5947,"THB":39.146,"TRY":4.1462,"USD":1.1729,"ZAR":15.281}}"""
                            ),
            endpoint='https://api.fixer.io/latest?base=eur',
        )
        self.cprice.save()

        self.transaction = Transaction(comment=self.comment,
                                       snapshot=self.snapshot,
                                       hour_price=self.hprice,
                                       currency_price=self.cprice,
                                       payment_amount=Decimal(10),
                                       payment_currency=self.usd,
                                       payment_recipient=self.testuser,
                                       payment_sender=self.testuser,
                                       hour_unit_cost=(1))
        self.transaction.save()
    def test_parse_storage_multiple_sequences(self):
        transactions = [
            dict(date='01/02/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23'),
            dict(date='01/12/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23'),
            dict(date='01/22/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23'),
            dict(date='02/01/2020',
                 description='TEST INVOICE 1234',
                 amount='435.23'),
            dict(date='01/02/2020',
                 description='ANOTHER TEST 1432',
                 amount='435.23'),
            dict(date='01/12/2020',
                 description='ANOTHER TEST 1234',
                 amount='435.23'),
            dict(date='01/22/2020',
                 description='ANOTHER TEST 2234',
                 amount='435.23'),
            dict(date='02/01/2020',
                 description='ANOTHER TEST 3334',
                 amount='435.23'),
            dict(date='01/02/2020',
                 description='THIRD*ONE*6565',
                 amount='435.23'),
            dict(date='01/12/2020',
                 description='THIRD*ONE*2907',
                 amount='435.23'),
            dict(date='01/22/2020',
                 description='THIRD*ONE*5555',
                 amount='435.23'),
            dict(date='02/01/2020',
                 description='THIRD*ONE*2356',
                 amount='435.23')
        ]
        storage = parse_storage(transactions)
        sequence1 = storage.get_sequence(
            Transaction(date='01/02/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))
        sequence2 = storage.get_sequence(
            Transaction(date='01/12/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))
        sequence3 = storage.get_sequence(
            Transaction(date='01/22/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))
        sequence4 = storage.get_sequence(
            Transaction(date='02/01/2020',
                        description='TEST INVOICE 1234',
                        amount='435.23'))
        sequence5 = storage.get_sequence(
            Transaction(date='01/02/2020',
                        description='ANOTHER TEST 1432',
                        amount='435.23'))
        sequence6 = storage.get_sequence(
            Transaction(date='01/12/2020',
                        description='ANOTHER TEST 1234',
                        amount='435.23'))
        sequence7 = storage.get_sequence(
            Transaction(date='01/22/2020',
                        description='ANOTHER TEST 2234',
                        amount='435.23'))
        sequence8 = storage.get_sequence(
            Transaction(date='02/01/2020',
                        description='ANOTHER TEST 3334',
                        amount='435.23'))
        sequence9 = storage.get_sequence(
            Transaction(date='01/02/2020',
                        description='THIRD*ONE*6565',
                        amount='435.23'))
        sequence10 = storage.get_sequence(
            Transaction(date='01/12/2020',
                        description='THIRD*ONE*2907',
                        amount='435.23'))
        sequence11 = storage.get_sequence(
            Transaction(date='01/22/2020',
                        description='THIRD*ONE*5555',
                        amount='435.23'))
        sequence12 = storage.get_sequence(
            Transaction(date='02/01/2020',
                        description='THIRD*ONE*2356',
                        amount='435.23'))

        assert sequence1 == sequence2 == sequence3 == sequence4
        assert sequence5 == sequence6 == sequence7 == sequence8
        assert sequence9 == sequence10 == sequence11 == sequence12
        assert sequence1 != sequence5 != sequence12