Code Monkey home page Code Monkey logo

Comments (4)

hjwp avatar hjwp commented on June 19, 2024

Is there supposed to be an @DataClass annotation on FakeSession? Otherwise there is a class instance and object instance "committed" variable. If not, deleting the "committed = False" line will be OK.

that was me. intention is to have the default .committed be False for all instances, but any instance that calls commit() will get its attribute set to True.

this may be a bit of Python weirdness?

>>> class Classy:
...     attribute = False
... 
>>> instance = Classy()
>>> instance.attribute
False
>>> instance.attribute = True
>>> instance.attribute
True
>>> Classy.attribute
False

from book.

xtaje avatar xtaje commented on June 19, 2024

Re: the fake session, I see what you were going for (kind of a "Copy-on-Write"). It is a bit unusual, and looks maybe like it was a mistake The scenarios I am thinking are things like this:

class Thing(object):
    stack = [1, 2, 3]

    def pop(self):
        return self.stack.pop()

    def push(self, val):
        return self.stack.append(val)

def test_first():
    assert Thing().pop() == 3

def test_second():
    assert Thing().pop() == 3

def test_third():
    thing = Thing()
    thing.push(100)
    assert thing.stack == [1, 2, 3, 100]

If you run that with a test randomizer or a parallel-runner using threads, the tests looks will look like they fail randomly due to the shared state.

from book.

hjwp avatar hjwp commented on June 19, 2024

I suppose people are going to cargo-cult copy these sorts of code listings so maybe we should be careful not to leave footguns like that lying around? will add a note to change it when i get a moment... thanks Ed.

from book.

hjwp avatar hjwp commented on June 19, 2024

re: the first comment about making the random-reference-generators more readable, i found that quite appealing. here's what i've been playing around with so far:

def random_suffix():
    return uuid.uuid4().hex[:6]

def random_sku(name=''):
    return f'sku-{name}-{random_suffix()}'

def random_batchref(name=''):
    return f'batch-{name}-{random_suffix()}'

def random_orderid(name=''):
    return f'order-{name}-{random_suffix()}


@pytest.mark.usefixtures('postgres_db')
@pytest.mark.usefixtures('restart_api')
def test_happy_path_returns_201_and_allocated_batch():
    sku, othersku = random_sku('desirable-chair'), random_sku('ugly-chair')
    oldbatch, newbatch = random_batchref('old'), random_batchref('new')
    otherbatch = random_batchref('other')
    post_to_add_batch(oldbatch, sku, 100, '2011-01-02')
    post_to_add_batch(newbatch, sku, 100, '2011-01-01')
    post_to_add_batch(otherbatch, othersku, 100, None)

    data = {'orderid': random_orderid(), 'sku': sku, 'qty': 3}
    url = config.get_api_url()
    r = requests.post(f'{url}/allocate', json=data)
    assert r.status_code == 201

    assert r.json()['batchref'] == newbatch

worth it? overengineered? what do y'all think?

from book.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.