Trying to run a bunch of dynamically-generated tests within a single describe(). I've copied in the example from the Mocha docs, and it works fine:
describe('add()', function() {
var tests = [
{args: [1, 2], expected: 3},
{args: [1, 2, 3], expected: 6},
{args: [1, 2, 3, 4], expected: 10}
];
tests.forEach(function(test) {
it('correctly adds ' + test.args.length + ' args', function() {
var res = add.apply(null, test.args);
assert.equal(res, test.expected);
});
});
});
BUT, when I try iterating over an array (or cursor) from a database call the entire describe() module is ignored - no errors, just doesn't run at all and doesn't appear in the reporter.
i.e. if I replace the tests array with
const tests = Tests.find().fetch();
the whole thing is just skipped.
What am I doing wrong? Is there a way around this?
Related
I'm designing a HasTraits subclass with dependent properties:
#!/usr/bin/env python
# Example for SO question on dynamically changing Dict contents.
from traits.api import HasTraits, Dict, Property, Trait, Int, cached_property
from traitsui.api import View, Item
class Foo(HasTraits):
"Has dependent properties, which I'd like to remain up-to-date in the GUI."
_dicts = [
{"zero": 0, "one": 1},
{"zero": 1, "one": 2},
{"zero": 2, "one": 3},
]
zap = Int(0)
bar = Property(Trait, depends_on=["zap"])
baz = Trait(list(_dicts[0])[0], _dicts[0])
#cached_property
def _get_bar(self):
return Trait(list(self._dicts)[self.zap], self._dicts)
traits_view = View(
Item("zap"),
Item("bar"),
Item("baz"),
width=500,
)
if __name__ == '__main__':
Foo().configure_traits()
When I run this code I see:
And if I change the value of Zap:
Note the following:
After changing Zap, the address of Bar has changed.
This means that changes to Bar are being dynamically updated in the GUI, while it's still opened; that's great! However...
The way Bar is displayed in the GUI is not very useful.
I'd love to have Bar displayed as Baz is displayed: selectable by the user.
What I'd like is to have the best of both worlds:
the dynamic GUI updating I see with Bar, and
the display format of Baz.
Does anyone know how I can get this?
I've tried several ways of updating a Baz-like item dynamically, to no avail.
(See this previous SO question.)
I assume you wish both bar and baz to be dict type (in traits Dict). Actually, there are default display widgets for pre-defined trait types, which are more useful than showing address. I believe traitsui doesn't know how to properly display your custom Trait object unless you explicitly assign an editor for it. Note that for baz, although a dropdown menu is generated, it is only displaying the keys, which is not very useful either.
With that said, the following codes might meet your expectations.
class Foo(HasTraits):
"Has dependent properties, which I'd like to remain up-to-date in the GUI."
_dicts = [
{"zero": 0, "one": 1},
{"zero": 1, "one": 2},
{"zero": 2, "one": 3},
]
zap = Int(0)
bar = Property(Dict, depends_on=["zap"])
baz = Trait(list(_dicts[0])[0], _dicts[0])
#cached_property
def _get_bar(self):
return self._dicts[self.zap]
traits_view = View(
Item("zap"),
Item("bar", style="custom"),
Item("baz"),
width=500,
)
The following code gets me the behavior I want:
#!/usr/bin/env python
# Example for SO question on dynamically changing Dict contents.
from traits.api import HasTraits, Dict, Property, Trait, Int, cached_property, Enum, List
from traitsui.api import View, Item
class Foo(HasTraits):
"Has dependent properties, which I'd like to remain up-to-date in the GUI."
_dict = {
"zero": 0,
"one": 1,
"two": 2,
}
_zaps = [
["zero", "one"],
["one", "two"],
["zero", "two"],
]
zaps = List(_zaps[0])
zap = Enum([0,1,2]) # Selection of `zap` should limit the items of `_dict` available for selection.
bar = Enum(_zaps[0][0], values="zaps")
bar_ = Int(_dict[_zaps[0][0]])
def _zap_changed(self, new_value):
self.zaps = self._zaps[new_value]
self.bar_ = self._dict[self.bar]
def _bar_changed(self, new_value):
self.bar_ = self._dict[self.bar]
traits_view = View(
Item("zap"),
Item("bar"),
Item("bar_", style="readonly"),
width=500,
)
if __name__ == '__main__':
Foo().configure_traits()
Immediately after program start-up:
And after changing to Zap to '1':
I want to plot a function interactively (i.e. the function has a parameter that can change) using Vega or Vega-Lite. As far as I can tell the data parameter can only be from a file or object.
Obviously I can recreate the entire graph/spec every time the function parameter changes, but I'd rather just update the data, so the entire graph doesn't need to be re-rendered. Is there a way to do that?
My function is too complex for Vega's built-in expression system so please don't suggest that.
You can do this using the Vega view API. Here is an example of a script that inserts dynamically-generated data into a Vega-Lite chart:
var spec = {
$schema: 'https://vega.github.io/schema/vega-lite/v3.json',
data: {name: 'table'},
width: 400,
mark: 'line',
encoding: {
x: {field: 'x', type: 'quantitative', scale: {domain: [0, 100]}},
y: {field: 'y', type: 'quantitative', scale: {domain: [-1, 1]}}
}
};
function makeData(N) {
data = [];
for (x = 0; x < N; x++) {
data.push({x: x, y: Math.sin(x / 10)})
}
return data
}
vegaEmbed('#chart', spec).then(function(res) {
var changeSet = vega.changeset().insert(makeData(100));
res.view.change('table', changeSet).run();
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega/5.7.0/vega.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-lite/3.4.0/vega-lite.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vega-embed/5.1.3/vega-embed.js"></script>
<div id="chart"></div>
I have an array tour_plan in my state which contains an object(s).
tour_plan = [
{
day:1,
location_id:3,
location_name:'Colombo'
}
]
I want to make this object
tour_plan = [
{
day:1,
location_id:3,
location_name:'Colombo',
tour_id:3,
tour_name:'City tour'
}
]
After gothourgh the docs in vuex in figureout that this won't work in reactive manner in vuex mutations.
state.tour_plan[state.tour_plan.length - 1].tour_id = payload.tour_id;
state.tour_plan[state.tour_plan.length - 1].tour_name = payload.tour_name;
So I tried these 2 ways.
state.tour_plan[state.tour_plan.length - 1] = { ...state.tour_plan[state.tour_plan.length - 1],
tour_id: payload.tour
};
Vue.$set(state.tour_plan[state.tour_plan.length - 1], 'tour_id', payload.tour);
But non of them also doesn't work.
Where is the problem in my code?
You are correct about the fact you can't mutate an object/array item by index access without "breaking" the reactivity system.
About what You've tried, according to the documentation of Vue.set, it expects as first argument the entire object/array rather than the length.
So the correct manner is:
Vue.$set(state.tour_plan, 0, payload.tour);
where 0 is the index of the element you want to update(replace)
This worked for me.
Vue.set(state.tour_plan[state.tour_plan.length - 1], 'tour_id', payload.tour)
Vue.set(state.tour_plan[state.tour_plan.length - 1], 'tour_payment', payload.tour_payment)
Vue.set(state.tour_plan[state.tour_plan.length - 1], 'tour_name', payload.tour_name)
I have a function that runs asynchronously, console logging the numbers 1 through 5 in order after a random setTimeout. I want to write a test for this function using Jest. How do I write one that tests that the console.log is 1, 2, 3, 4, 5 ?
Yes you can using jest.fn.
Here is an example:
File hello.js
console.log("Hello World");
File hello.test.js
let outputData = "";
storeLog = inputs => (outputData += inputs);
test("console log Hello World", () => {
console["log"] = jest.fn(storeLog);
require("./hello.js");
expect(outputData).toBe("Hello World");
});
I am using the Python client for the Google Sheets API to build a spreadsheet. I am able to create a new sheet and update values in it, but I have been unable to merge cells for the header row that I want.
top_header_format = [
{'mergeCells': {
'mergeType': 'MERGE_COLUMNS',
'range': {
'endColumnIndex': 3,
'endRowIndex': 1,
'sheetId': '112501875',
'startColumnIndex': 0,
'startRowIndex': 0
}
}},
{'mergeCells': {
'mergeType': 'MERGE_COLUMNS',
'range': {
'endColumnIndex': 5,
'endRowIndex': 1,
'sheetId': '112501875',
'startColumnIndex': 3,
'startRowIndex': 0
}
}}
]
service.spreadsheets().batchUpdate(
spreadsheetId=spreadsheet_id,
body={'requests': top_header_format}
).execute()
This is the code I am running. There are no errors. The response's replies are empty and the spreadsheet doesn't have merged cells. The documentation is here.
Start indexes are inclusive and end indexes are exclusive, so by asking to merge row [0,1) you're saying "i want to merge row 0 into row 0", which is a no-op. You probably want [0,2), e.g:
top_header_format = [
{'mergeCells': {
'mergeType': 'MERGE_COLUMNS',
'range': {
'endColumnIndex': 4,
'endRowIndex': 2,
'sheetId': '112501875',
'startColumnIndex': 0,
'startRowIndex': 0
}
}},
{'mergeCells': {
'mergeType': 'MERGE_COLUMNS',
'range': {
'endColumnIndex': 6,
'endRowIndex': 2,
'sheetId': '112501875',
'startColumnIndex': 3,
'startRowIndex': 0
}
}}
]
If someone looking for doing merging with PHP, you can use this code below:
For merging cells first you will have to download the PHP library from composer as in their documentation ( https://developers.google.com/sheets/api/quickstart/php )
once installed follow their guide to set up your client to get authenticated.
//$client will be your Google_Client authentication, for more info check the documentation link above.
Use below code for doing merging of rows and columns
$service = new Google_Service_Sheets($client);
$spreadsheetId = '1jfUz2VMUUEt2s4BP2Ye'; //this is test spreadsheet it, use your spreadsheet id here
$rangeinst = new Google_Service_Sheets_GridRange();
$rangeinst->setSheetId(0); //your sheet id
$rangeinst->setStartRowIndex(1); // row index from where to start
$rangeinst->setEndRowIndex(11); //ending row upto which you want merging
$rangeinst->setStartColumnIndex(0); //start of column index, first column has 0 index like row
$rangeinst->setEndColumnIndex(4); //end of column upto which you want merging
$merge = new Google_Service_Sheets_MergeCellsRequest();
$merge->setMergeType('MERGE_COLUMNS'); //merge type request
$merge->setRange($rangeinst);
$requestBody = new Google_Service_Sheets_Request();
$requestBody->setMergeCells($merge);
$requestBatchBody = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$requestBatchBody->setRequests($requestBody);
$response = $service->spreadsheets->batchUpdate($spreadsheetId, $requestBatchBody);
echo "<pre>";
print_r($response);