How to refer to a row index in a Fancordion fixture table? - fantom

Using tables in Fancordion v1.0.4, how can I use the row index in a column command to validate its value.
For example if my Fixture is:
class MyFixture : FixtureTest {
[Str:Obj?][] getCountries() {
return [["code":"AU", "name":"Australia"], ["code":"NZ", "name":"New Zealand"], ["code":"UK", "name":"United Kingdom"]]
}
}
And the spec is:
table:
col[0]+verifyEq:getCountries[#ROW]["code"]
col[1]+verifyEq:getCountries[#ROW]["name"]
Country Code Country Name
------------ ------------
AU Australia
NZ New Zealand
UK United Kingdom
What should I use instead of the #ROW placeholder in the specification example above?
Is there a better way to write this specification and fixture? e.g. is it better to create a method in the fixture to retrieve each individual map in the list instead of the full list?

As you noted there is no #ROW macro (but it may be a nice addition!) so currently you'll need to change either the assertions, or the data structure.
Change the assertions:
Here we change the assertions slightly and introduce our own row field that gets incremented at the end of every row iteration:
** Hi!
**
** table:
** col[0]+verifyEq:getCountries[#FIXTURE.row]["code"]
** col[1]+verifyEq:getCountries[#FIXTURE.row]["name"]
** row+exe:row++
**
** Country Code Country Name
** ------------ ------------
** AU Australia
** NZ New Zealand
** UK United Kingdom
**
class CountryTest : FixtureTest {
Int row := 0
[Str:Obj?][] getCountries() {
return [["code":"AU", "name":"Australia"], ["code":"NZ", "name":"New Zealand"], ["code":"UK", "name":"United Kingdom"]]
}
}
Change the data structure:
A better way would probably be to change the data being tested into a list of lists. For that has the added advantage of failing when the test data is too much or too little.
** Hi!
**
** table:
** verifyRows:getCountries()
**
** Country Code Country Name
** ------------ ------------
** AU Australia
** NZ New Zealand
** UK United Kingdom
**
class CountryTest : FixtureTest {
Str[][] getCountries() {
countries := [["code":"AU", "name":"Australia"], ["code":"NZ", "name":"New Zealand"], ["code":"UKs", "name":"United Kingdom"]]
return countries.map { it.vals }
}
}

Related

Fake email generation using factory.LazyAttribute

I am trying to generate fake email ID's for the purpose of research. I employ LazyAttribute towards this end. I want the email ID to correspond to the first and the last names of the person (generated using Faker). The function I wrote is below.
I am unable to get the expected output. If the name of the person is John Snow, I see the following as the output:
John Snow <factory.declarations.LazyAttribute object at ......
Can I expect some help to fix my code? Thanks!
def faker_categorical(num=1, seed=None):
np.random.seed(seed)
fake.seed_instance(seed)
output = []
for x in range(num):
gender = np.random.choice(["M", "F"], p=[0.5, 0.5])
output.append(
{
"First name": fake.first_name(),
"Last name": fake.last_name(),
"E-mail": factory.LazyAttribute(lambda obj: "%s#example.com" % obj.first_name),
})
return output

Laravel 5.8 hasOneThrough relationship problems

Can someone PLEASE correct this! I have been banging my head on it on and off for two days! I have never managed to successfully make a hasOneThrough relationship work, but I decided to try again. Right now the error I am getting is "Call to undefined method App\Models\OppModels\opp_main::hasOneThrough()". I believe my issue is with the local keys vs foriegn keys, but if I truly knew the issues, then i could fix it. I do realize I could hard code the org_id into my opp_main table, but I really want to make this work due to future considerations. My table design is:
---------
opp_main
---------
id
---------
---------
opp_org
---------
id
opp_id
org_id
---------
---------
org_main
---------
id
---------
My relationship looks this (the comments were taken from the documentation to try to organize it in my mind):
public function org()
{
return $this->hasOneThrough(
'App\Models\OrgModels\org_main', // Final model
'App\Models\OppModels\opp_org', // Intermediate model
'opp_id', // Foreign key on intermediate model
'id', // Foreign key on the final model
'id', // local key
'org_id' // local key of the intermediate model
);
}
And this is how I am calling it (I have tried both $item->org; and $item->org(); ):
public function getAllOpportunities()
{
$opps = opp_main::orderBy('status', 'asc')->get();
$opps->map(function ($item) {
$item['org'] = $item->org;
return $item;
});
return response()->json([ 'success' => true, 'data' => $opps ]);
}
I appreciate any help!
Using the documentation this is how it is supposed to be arranged. Note difference from your solution
Org_main has an FK to opp_org which is needed in hasOneThrough() to
link the required model to the intermediate model
org_main
id
opp_org_id
opp_org
id
opp_id
org_id
opp_main
id
return $this->hasOneThrough(
'org_main', // final model
'opp_org', // intermediate or linking model
'opp_id', //fk on opp_org linking to opp_main
'opp_org_id', // fk on org_main linking ot opp_org table,
'id', //local key on opp_main
'id', //local key on opp_org / intermediate model
);

how to insert checkbox value to database in python

In the front end they are selecting multiple checkboxes i.e;(district,taluk)
,so how can i get the values to the backend and how to save in the database.
note: I want to write this code based on definition not generics
this is my models
class Example(models.Model):
district = models.CharField(max_length=20)
taluk = models.CharField(max_length=20)
if we give input in json format like
{
"Bangalore" :{
"dodballapura",
" Hosakote"
}
}
then it should save in database and
in the above example bangalore is the district and remaining are talukas

I am having issues consolidating data from dictionaries

This was written in swift but its more of a logic question hopefully someone can help with, I've been working on an app that takes expenses and then splits the totals between users based on which users were specified in the "owees" property of the expense. I was able to divvy out the totals by creating a dictionary for all the users with the key being their name and the value being more dictionaries for each of the other users with the key being the other users' name, and the values being the amount owed (postive number if the money is owed to the parent dictionary, negative number for the opposite), my issue is now consolidating all the money so that all the users owed amounts are divvied out amongst each other (e.g.: if User1 owes User2 $10 but User2 owes User3 $5 then User1 now only owes User2 $5 but now also owes User3 $5 and User2's debt is cleared)
I know its seems confusing with the dictionary-inception but here is an example of the dictionary, and a link to my .playground where it shows the idea of what I am trying to and how I set it up, the last for loop is the problem, maybe theres a better way?
User1[
User2: -20
User3: 10
User4: -5
]
User2[
User1: 20
User3: -15
User4: 0
]
gist link:
https://gist.github.com/wilks7/0c5e3ab4f5d95c945579
playground file:
https://www.dropbox.com/s/6h2iuic84un2uh3/expenseSplit.playground.zip?dl=0
What you are trying to model is essentially a Directed Acyclic Graph. However if the queries that you need to make on the relationships are relatively simple (i.e. list the outstanding amounts for each person), then you may be able to get away with some object-oriented modelling rather than a complete DAG.. Constructing classes and embedding logic in those classes will make your code much easier to read compared to trying to accomplish the same thing with arrays and/or dictionaries.
A skeleton example is:
public class Debt {
var owedBy: Person
var amount: Float
init(amount: Float, from: Person) {
self.amount = amount
self.owedBy = from
}
}
public class Person {
var name: String
var loans: [Debt] = []
init(name: String) {
self.name = name
}
public func addLoan(debt: Debt) {
loans.append(debt)
}
public func showLoans() {
print("\(name):")
for debt in loans {
print("Owed \(debt.amount) by \(debt.owedBy.name)")
}
}
}
This would allow you to create loans and show what exists with code like:
let Bill = Person(name: "Bill")
let Tony = Person(name: "Tony")
Bill.addLoan(Debt(amount: 20.0, from: Tony))
Bill.showLoans()
Calling showLoans() for Bill as above would produce:
Bill:
Owed 20.0 by Tony

Cassandra/NoSQL newbie: the right way to model?

as the title says I am fairly (read: completely) new to NoSQL DBS such as Cassandra. As many others, I learned RMDBS before. So I did a little reading on 'WTF is a super column' and other obvious google hits, but I am still not sure how to model this:
Say I want to save Users, as in username/password/name/etc... what if that user has like, a mobile phone and a landline telephone? is this the 'right' way to do it? (using the same abbreviated JSON style as seen on other sites)
Users: { // <-- this is the Users SuperColumnFamily, keyed by username
myuser: { // <-- this is a User SuperColumn
username = "myuser", // <-- this is the username Column
email = "myuser#googlemail.com",
...
},
...
}
Phone: { // <-- this is where the users phone numbers are stored
myuser: {
mobile = "0129386835235",
landline = "123876912384",
},
...
}
opinions/corrections please
First things first, don't use super columns. See:
http://www.quora.com/Cassandra-database/Why-is-it-bad-to-use-supercolumns-in-Cassandra
Now to answer your question. The example you described is easily modeled with just a regular column family:
Users: { <- This is the name of the column family
username1: { <- this is a row key within the column family, it is one of your usernames
email: user#email.com <- these are all of the columns within this row, they correspond to attributes for this user
mobile: ...
landline: ...
}
username2: { <- another row for a different user
email: diff#email.com
}
}
You can see the flexible schema above in that each row has a different set of columns for describing that user.
For more info on the cassandra data model I would recommend reading over http://www.datastax.com/docs/1.0/ddl/index