Getting the primary key (id) in fixture (Python, SQLAlchemy)
Asked Answered
S

1

7

I'm using fixture to test a Pylons app, but I stumbled upon a problem.

Let's say I have such data set:

class CompanyData(DataSet):

    class test_company:
        company_full_name = u'Firma Tęst'
        company_short_name = u'TęstCo'

class UserData(DataSet):

    class test_user:
        user_login = 'testuser'
        user_password = 'test'
        company = CompanyData.test_company

Now, the problem is, that when I use this data in a functional test (like described at http://farmdev.com/projects/fixture/using-fixture-with-pylons.html), I can't obtain the id (primary key) of the company.

In my application the user after logging in should be redirected to the company profile page and that's why I need the company's id. The test looks more or less like this:

self.app.post(url(controller='main', action='login'), params={
    'login': UserData.test_user.user_login,
    'password': UserData.test_user.user_password
})

response = self.app.get(url(
    controller='events', action='index',
    company_id=UserData.test_user.company.company_id, # This doesn't work
    view='active'))
assert ... in response

The first request logs in the user and the second one checks if after logging in she can access the company profile page.

This way I get:

AttributeError: class test_company has no attribute 'company_id'

I also tried:

UserData.test_user.company.ref('company_id')

But it results in:

<Ref.RefValue for CompanyData.test_company.company_id (not yet loaded)>

which seems weird to me... Why isn't it loaded?

Is there any way of finding out what is the primary key?

Slosh answered 15/1, 2011 at 16:14 Comment(0)
D
0
UserData.test_user.company.ref('id')
or
UserData.test_user.ref('company_id')
Durkin answered 21/1, 2011 at 14:55 Comment(5)
company_id is the primary key column in the company table, not a foreign key in the user table (which is user_company_id). Don't blame me for the naming convention, I didn't design this database, I just work with it ;)Slosh
By default in yours DataSet`s primary key would be 'id' farmdev.com/projects/fixture/api/…Durkin
I tried that, it doesn't change anything. I keep getting "<Ref.RefValue for CompanyData.test_company.id (not yet loaded)>". I even tried adding class Meta: primary_key = ['company_id'] to the dataset and it changes nothing in both variants.Slosh
For accessing id your must appirate with fixture created by yours DataSets (not direct DataSets). See this working example pastebin.com/zUVUDnkrDurkin
Thank you! Using the data object like this: self.data = dbfixture.data(UserData); self.data.UserData.test_user.company.company_id works like a charm. I don't know why the official docs don't highlight this (most examples use DataSets directly).Slosh

© 2022 - 2024 — McMap. All rights reserved.