I'm confused. I've just started to write tests with jest, and i cant figure out what is going on. Here is my output:
Expected value to equal:
ObjectContaining {"field": "f1", "module": "m1", "rights": {"read": true}}
Received:
{"_id": "5ae85cd0bc5ad3569bf66df8", "field": "f1", "module": "m1", "rights": {"read": true}}
Difference:
- Expected
+ Received
## -1,6 +1,7 ##
- ObjectContaining {
+ Object {
+ "_id": "5ae85cd0bc5ad3569bf66df8",
"field": "f1",
"module": "m1",
"rights": Object {
"read": true,
},
183 | ]),
184 | );
> 185 | expect(user.acl[0]).toEqual(
186 | expect.objectContaining({ module: 'm1', field: 'f1', rights: {read: true}}),
187 | );
188 | });
I think its failing because user is a mongoose object.
You should remove _id from your object before comparing.
Alternatively, you should use toMatchSnapshot() (but still need to remove any keys that will change on each run.
Looks like you can convert mongoose object to plain object by calling user.toObject() function. And everything works as expected.
Related
Here what i did.
Inside $AddFields
{
ClientStatus:{
$switch: {
branches: [
{ case: {
$eq:
[
"$CaseClientStatus",
/In Progress/i
]},
then:'In Progress'
},
{ case: {
$eq:
[
"$CaseClientStatus",
{regex:/Cancelled/i}
],
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
}
but with this my ClientStatus is showing only Complete,Other,Case On Hold not the one with specified with regex. alghough field contains those words.
here is the one of the doc
{
"CandidateName": "Bruce Consumer",
"_id": "61b30daeaa237672bb7a17cc",
"CaseClientStatus": "Background Check Case In Progress",
"TAT": "N/A",
"CaseCloseDate": null,
"FormationAutomationStatus": "Automated",
"MethodOfDataSupply": "Automated",
"Status": "Background Case In Progress",
"CreatedDate": "2021-12-10T08:19:58.389Z",
"OrderId": "Ord3954",
"PONumber": "",
"Position": "",
"FacilityCode": "",
"IsCaseClose": false,
"Requester": "Shah Shah",
"ReportErrorList": 0
}
Assuming you are on version 4.2 or higher (and you should be because 4.2 came out almost 2 years ago) then the $regexFind function gives you what you need. Prior to 4.2, regex was only available in a $match operator, not in complex agg expressions. Your attempt above is admirable but the // regex syntax is not doing what you think it should be doing. Notably, {regex:/Cancelled/i} is simply creating a new object with key regex and string value /Cancelled/i (including the slashes) which clearly will not equal anything in $CaseClientStatus. Here is a solution:
ClientStatus:{
$switch: {
branches: [
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /In Progress/i}}]
}, then:'In Progress'},
{ case: {
$ne: [null, {$regexFind: {input: "$CaseClientStatus", regex: /Cancelled/i}}]
},then:'Cancelled'},
{ case: {$eq:['$CaseClientStatus','Complete - All Results Clear']}, then:'Complete'},
{ case: {$eq:['$CaseClientStatus','Case on Hold']}, then:'Case on Hold'}
],
default: 'Other'
}}
It looks like you are trying to take a somewhat free-form status "description" field and create a strong enumerated status from it. I would recommend that your $ClientStatus output be more code-ish e.g. IN_PROGRESS, COMPLETE, CXL etc. Eliminate case and certainly whitespace.
I have the following ModelFactory:
class ProjectDataModelFactory(DjangoModelFactory):
project = factory.SubFactory("projects.factories.ProjectModelFactory")
version = "1"
content = {
"id": {"value": fuzzy.FuzzyText(length=7, chars=string.ascii_letters).fuzz().upper(), "type": "string"},
"win_probability": {"value": fuzzy.FuzzyInteger(0, 100).fuzz(), "type": "number"},
"bid_value": {
"value": fuzzy.FuzzyInteger(0, 10000000).fuzz(),
"type": "number",
},
"status": {"value": random.choice(["Won", "Lost"]), "type": "string"},
"sector": {
"value": random.choice(
["Construction", "Technology", "Finance", "Consulting", "Agriculture", "Engineering"]
),
"type": "string",
},
}
In content["id"], I would like to have value be project.external_id instead of fuzzy.FuzzyText(length=7, chars=string.ascii_letters).fuzz().upper(). external_idis part of the ProjectModel. But when I put project.external_id in the place of the fuzzer, I get this error:
AttributeError: 'SubFactory' object has no attribute 'external_id
This only seems to be a problem with dicts. We have not had issues using it in other contexts.
I do not have a lot of experience using FactoryBoy, so it could easily be something trivial. Any ideas?
You have a first issue: all the calls to random or .fuzz() that you put in the content dict are evaluated when the class is imported: each instance will use the exact same values.
In order to perform lazy evaluation, you MUST wrap them in some factory_boy-provided declaration:
class ProjectDataModelFactory(DjangoModelFactory):
project = factory.SubFactory("projects.factories.ProjectModelFactory")
version = "1"
content = factory.Dict(
id=factory.Dict(
value=fuzzy.FuzzyText(length=7, chars=string.ascii_uppercase),
type="string",
),
win_probability=factory.Dict(
value=fuzzy.FuzzyInteger(0, 100),
type="number",
),
bid_value=factory.Dict(
value=fuzzy.FuzzyInteger(0, 10_000_000),
type="number",
),
status=factory.Dict(
value=fuzzy.FuzzyChoice(["Won", "Lost"]),
type="string",
),
sector=factory.Dict(
value=fuzzy.FuzzyChoice(
["Construction", "Technology", "Finance", "Consulting", "Agriculture", "Engineering"],
),
type="string",
),
)
factory.Dict(**kwargs) is basically an alias for factory.SubFactory(DictFactory, **kwargs); you can then use any declaration as if using a simple SubFactory.
For your initial goal, you'll now be able to use all features easily:
class ProjectDataModelFactory(DjangoModelFactory):
project = factory.SubFactory("projects.factories.ProjectModelFactory")
version = "1"
content = factory.Dict(
id=factory.Dict(
# . is the {"value": x, "type": "string"} dict;
# .. is {"id": {"value": x, ...}} dict;
# ... is the ProjectData(content={"id": {...}}) object.
value=factory.SelfAttribute('...project.external_id'),
type="string",
),
# Other fields here
)
If you are reading this, you will read that you can add children to the fields query parameter. That means that if you have a media with the type "CAROUSEL_ALBUM" you will get the ids of the images as well.
Example CURL:
https://graph.instagram.com/me?access_token=<mytoken>&fields=id,media_type,media_url,children
The result:
...
"id": "some-id",
"media_type": "CAROUSEL_ALBUM",
"media_url": "some-url",
"children": {
"data": [
{
"id": "another-id"
},
{
"id": "other-id"
}
]
}
...
Is it possible to add media_url to the children's data? I don't really want to fetch everything in a loop...
#Mecha I did this for PHP, but maybe you can read this
/**
* Fetches media from the user
*
*/
public function fetchUserMedia(array $options, int $limit = 24): object
{
$query = http_build_query(array_merge([
'limit' => $limit,
'fields' => 'username,caption,id,media_type,media_url,thumbnail_url,children{media_url,thumbnail_url}',
'access_token' => $this->token
], $options));
return json_decode((string) ($this->client->get("https://graph.instagram.com/me/media?{$query}"))->getBody());
}
One output example is like this:
{
"caption": "I\u2018m addicted to chia pudding, how about you? \ud83d\ude0d Layers of zingy lemon vanilla chia pudding, mango thyme infused coconut yoghurt and juicy fresh mango roses. \ud83e\udd24\nMade by \u0040addictedtodates \ud83c\udf4b\u2063\u2063\n\u2063\u2063\u2800\nAND now to our tagged picture of today \ud83d\ude0b\ud83d\ude0b Swipe left to see \u0040smoothie_yumm\u2019s coconut vanilla smoothie bowl topped with turmeric chia pudding \ud83e\udd29\n\u2800\nYou can find the recipe on her page \ud83d\udc9b\n\u2800\n\ud83c\udf31 Tag #avidofood or\u00a0\u0040avidofood\u00a0to be featured in the future!\n\u2800\n\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\u22c6\n#vegan #veganfood #veganfoodie #veganfoodshare #vegatarisch #veganpasta #vegandinner #veganeats #healthyfood #plantbased #plantbasedfood #veganpower #plantbaseddiet #veganiseasy #whatveganseat #forksoverknives #vegetarianrecipes #veganinspo #vegetarian #veganism #veganmeals #veganlife #veganlifestyle #veganlove #veganmealprep #veganrecipes #veganhealth #veganlunch",
"id": "18039820117203997",
"media_type": "CAROUSEL_ALBUM",
"media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/71511404_203552310658678_3865693276191599368_n.jpg?_nc_cat=100&_nc_oc=AQlet8stFGS32TTdBhXT4NNfpzd8eNq7oI0xilix4qyiVvt50avuk6RVotMgM-BUptmCrsVwLCGkPCc-sL7b-eAy&_nc_ht=scontent.xx&oh=f1a700b4d021d2d577d54cd74f4838fa&oe=5E534677",
"permalink": "https://www.instagram.com/p/B3-XRMOIWPW/",
"username": "avidofood",
"children": {
"data": [
{
"media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/71511404_203552310658678_3865693276191599368_n.jpg?_nc_cat=100&_nc_oc=AQlet8stFGS32TTdBhXT4NNfpzd8eNq7oI0xilix4qyiVvt50avuk6RVotMgM-BUptmCrsVwLCGkPCc-sL7b-eAy&_nc_ht=scontent.xx&oh=f1a700b4d021d2d577d54cd74f4838fa&oe=5E534677",
"id": "18041124712207922"
},
{
"media_url": "https://scontent.xx.fbcdn.net/v/t51.2885-15/72483636_1251439178368296_6758852942587086206_n.jpg?_nc_cat=109&_nc_oc=AQmVrktP2g7Z72WifVdu4z17OzwM7ZNFLln1e1ZQxjdUi-j79Ttf-i840mjYkOb-TW3Dwm39Gyoe3EefxwB7UydW&_nc_ht=scontent.xx&oh=a07d3f51aa5eb5eb30697c4ad25d4e35&oe=5E60AEC3",
"id": "17851162897631131"
}
]
}
You just need to nest the fields in the children query params
/media?fields=id,media_url,children{media_url}&access_token=
I'm quite new to Python and programming in general, so apologies if this is quite basic or has been asked and answered before. Here is a sample of the data I'm working with:
{
"homeTeam": {
"formation": [
"4",
"4",
"2"
],
"lineupsSorted": [
{
"player": {
"name": "Scott P. Brown",
"slug": "scott-p-brown",
"shortName": "S. P. Brown",
"id": 19889,
"hasImage": true
},
"position": 1,
"shirtNumber": 1,
"substitute": false,
"positionName": "Goalkeeper",
"positionNameshort": "G",
"captain": false
},
{
"player": {
"name": "Carl Winchester",
"slug": "carl-winchester",
"shortName": "C. Winchester",
"id": 110785,
"hasImage": true
},
"position": 2,
"shirtNumber": 27,
"substitute": false,
"positionName": "Midfielder",
"positionNameshort": "M",
"captain": false
},
I am looking to automate populating defined names as I have done manually here:
hometeamPositions =['Goalkeeper','Midfielder','Defender','Defender','Defender','Midfielder','Midfielder','Midfielder','Midfielder','Forward','Forward','Goalkeeper','Defender','Defender','Midfielder','Midfielder','Forward','Forward']
hometeamPlayers = ['S. P. Brown','C. Winchester','M. Onariase','W.
Boyle','J. Cranston','H. Pell','J. Rowe','K. Storer','B. Waters','D.
Wright','D. Holman','R. Lovett','J. Barthram','T. Plavotic','J.
Munns','L. Davis','K. Wootton','J. Dayton']
As I will be repeating this process many hundreds of times with different data (same structure) I was wondering if anyone could give me some tips on automatically building these ranges?
Thanks,
Peter
I'm not sure I understood what is the problem you are trying to solve but I'll try to help.
Assuming you have a dictionary team_dict and you want to create 2 list: hometeamPositions and hometeamPlayers you can use the following code:
hometeamPlayers = []
hometeamPositions = []
for player_dict in teams_dict['homeTeam']['lineupsSorted']:
hometeamPlayers.append(player_dict['player']['shortName'])
hometeamPositions.append(player_dict['positionName'])
The output on your example will be:
hometeamPlayers = ['S. P. Brown', 'C. Winchester']
hometeamPositions = ['Goalkeeper', 'Midfielder']
I'm trying to create some simple custom highlighting in Sublime Text 3 using AAAPackageDev.
In the line
void plastic my_material,
I'd like to match plastic, i.e. the material type, as a keyword and my_material, i.e. the material name, as a string.
I've got the following which however only matches plastic and not the name that follows, i.e. my_material:
// [PackageDev] target_format: plist, ext: tmLanguage
{ "name": "Radiance",
"scopeName": "source.radiance",
"fileTypes": ["rad", "sky", "mat"],
"uuid": "885b4450-d2bb-4a67-aea0-ceb5a0554630",
"patterns": [
{
"name": "keyword.source.radiance",
"match": "\\bplastic\\b | \\blight\\b"
},
{
"name": "keyword.source.radiance",
"begin": "(\\bplastic\\b | \\blight\\b)",
"patterns": [
{ "include": "$self"},
{
"name": "string.source.radiance",
"match": "[^\\s]+"
}],
"end": "\\n"
}]
}
I'm trying to follow these docs here. Any ideas?
EDIT
The full text I'm trying to parse is:
void plastic my_material
0
0
5 0.4 0.25 0.05 0.05 0.03
void and plastic maintain position but might change their value. my_material is just an arbitrary name that could comprise pretty much anything but must follow a keyword from the first pattern and will be followed by a new line.