Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 21] Migrate to pytest #30

Merged
merged 17 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
version: 1
jobs:
build:
working_directory: ~/cbc-casper
docker:
# NOTE: to be updated in issue #18
- image: circleci/python:2.7
steps:
- checkout
- restore_cache:
key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
- run:
command: |
virtualenv venv
. venv/bin/activate
pip install -r requirements.txt
- save_cache:
key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
paths:
- "venv"
- run:
command: |
. venv/bin/activate
pytest
- store_artifacts:
path: test-reports/
destination: tr1
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ venv*
*.prof
*.orig
results.txt

.cache/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ kernprof -l casper.py (rand | rrob | full | nofinal)
```

## Run Tests
To run all unit tests:
To run all tests:
```
python -m unittest discover
pytest
```
To run a specific test, use (or the equivalent for whatever test you wish to run)
```
python -m unittest test.test_safety_oracle
pytest tests/test_safety_oracle.py
```
22 changes: 12 additions & 10 deletions casper.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@
import sys

import random as r # to ensure the tie-breaking property
import settings as s
import casper.settings as s

from justification import Justification
from view import View
from network import Network
from validator import Validator
from safety_oracles.clique_oracle import CliqueOracle
import utils
import plot_tool
import presets
from casper.justification import Justification
from casper.view import View
from casper.network import Network
from casper.validator import Validator
from casper.safety_oracles.clique_oracle import CliqueOracle
import casper.utils as utils
import casper.plot_tool as plot_tool
import casper.presets as presets


def main():
Expand Down Expand Up @@ -130,4 +130,6 @@ def main():
#for i in xrange(s.NUM_VALIDATORS):
# plot_tool.plot_view(network.validators[i].view)

main()

if __name__ == "__main__":
main()
File renamed without changes.
6 changes: 2 additions & 4 deletions block.py → casper/block.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import copy
import settings as s

from justification import Justification
import casper.settings as s
from casper.justification import Justification


class Block:
Expand Down
8 changes: 3 additions & 5 deletions forkchoice.py → casper/forkchoice.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import settings as s
import random as r
import copy
import utils
import casper.settings as s


def get_max_weight_indexes(scores):
Expand Down Expand Up @@ -34,7 +31,8 @@ def get_fork_choice(last_finalized_block, children, latest_messages):
curr_scores[child] = scores.get(child, 0)
max_score = max(curr_scores[child], max_score)

# we don't choose weight 0 children. Also possible to make non-deterministic decision here.
# we don't choose weight 0 children.
# Also possible to make non-deterministic decision here.
if max_score == 0:
break

Expand Down
4 changes: 0 additions & 4 deletions justification.py → casper/justification.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import random as r
import forkchoice


class Justification:
def __init__(self, last_finalized_block=None, latest_messages=dict()):
self.last_finalized_block = last_finalized_block
Expand Down
10 changes: 4 additions & 6 deletions network.py → casper/network.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import random as r # to ensure the tie-breaking property
import settings as s
import casper.settings as s

from view import View
from validator import Validator
from block import Block
import plot_tool
from casper.validator import Validator
from casper.view import View
import casper.plot_tool as plot_tool


class Network:
Expand Down
6 changes: 4 additions & 2 deletions plot_tool.py → casper/plot_tool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import networkx as nx
from block import Block
import settings as s
from math import pi

import matplotlib as mpl
Expand All @@ -14,6 +12,10 @@
from PIL import Image
import os

from casper.block import Block
import casper.settings as s


base = 10000000
IMAGE_LIMIT = 75
FRAMES = "graphs/"
Expand Down
3 changes: 2 additions & 1 deletion presets.py → casper/presets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import settings as s
import random as r
import itertools

import casper.settings as s


def message_maker(mode):

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import settings as s
from model_validator import ModelValidator
import casper.settings as s
from casper.safety_oracles.adversary_models.model_validator import (
ModelValidator
)


class Adversary:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import settings as s
import casper.settings as s


class ModelBet:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import settings as s
import casper.settings as s


def get_estimate_from_latest_messages(latest_bets, default=None):

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import settings as s
import model_utils
from model_bet import ModelBet
from model_view import Model_View
import casper.settings as s
import casper.safety_oracles.adversary_models.model_utils as model_utils
from casper.safety_oracles.adversary_models.model_bet import (
ModelBet
)


class ModelValidator:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import settings as s
from adversary_models.model_bet import ModelBet
from adversary_models.adversary import Adversary
import utils
import casper.settings as s
from casper.adversary_models.model_bet import ModelBet
from casper.adversary_models.adversary import Adversary
import capser.utils as utils


class AdversaryOracle:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import settings as s
from block import Block
import utils
import casper.settings as s
import casper.utils as utils

import copy
import itertools
import networkx as nx

Expand Down
File renamed without changes.
11 changes: 7 additions & 4 deletions test/testing_language.py → casper/testing_language.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import re
import random as r

import settings as s
from network import Network
from safety_oracles.clique_oracle import CliqueOracle
import utils
import casper.settings as s
from casper.network import Network
from casper.safety_oracles.clique_oracle import CliqueOracle
import casper.utils as utils


class TestLangCBC:
# signal to py.test that TestLangCBC should not be discovered
__test__ = False

TOKEN_PATTERN = '([A-Za-z]*)([0-9]*)([-]*)([A-Za-z0-9]*)'

def __init__(self, test_string, val_weights, display=False):
Expand Down
2 changes: 1 addition & 1 deletion utils.py → casper/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import settings as s
import casper.settings as s

def are_conflicting_estimates(estimate, possibly_conflicting_estimate):
return not estimate.is_in_blockchain(possibly_conflicting_estimate)
Expand Down
2 changes: 0 additions & 2 deletions validator.py → casper/validator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import settings as s
from block import Block
from view import View
from justification import Justification
Expand All @@ -16,7 +15,6 @@ class Validator:
# The validator's state is a function of its view and name alone (along with global variables)
# However, for performance's sake the validator also stores
def __init__(self, name):
assert name in s.VALIDATOR_NAMES, "expected a validator name"
self.name = name
self.view = View(set())

Expand Down
6 changes: 2 additions & 4 deletions view.py → casper/view.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from block import Block
from justification import Justification
import forkchoice
import copy
from casper.justification import Justification
import casper.forkchoice as forkchoice


class View:
Expand Down
29 changes: 29 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

from casper.testing_language import TestLangCBC


def pytest_addoption(parser):
parser.addoption("--report", action="store_true", default=False,
help="plot TestLangCBC tests")


def run_test_lang_with_reports(test_string, weights):
TestLangCBC(test_string, weights, True).parse()


def run_test_lang_without_reports(test_string, weights):
TestLangCBC(test_string, weights, False).parse()


@pytest.fixture
def report(request):
return request.config.getoption("--report")


@pytest.fixture
def test_lang_runner(report):
if report:
return run_test_lang_with_reports
else:
return run_test_lang_without_reports
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ pickleshare==0.7.4
Pillow==4.3.0
prompt-toolkit==1.0.15
ptyprocess==0.5.2
py==1.4.34
Pygments==2.2.0
pyparsing==2.2.0
pytest==3.2.3
python-dateutil==2.6.1
pytz==2017.2
scandir==1.6
Expand Down
81 changes: 0 additions & 81 deletions test/test_block.py

This file was deleted.

Loading