def test_upsert(): s = utils.session() dataset = utils.dataset() url = tc.URL(path="datasets/1:updateRecords") updates = [ tc.record._create_command(record, primary_key_name="primary_key") for record in _records_json ] snoop: Dict = {} responses.add_callback( responses.POST, str(url), partial(utils.capture_payload, snoop=snoop, status=200, response_json=_response_json), ) response = tc.record.upsert(s, dataset, _records_json, primary_key_name="primary_key") assert response == _response_json assert snoop["payload"] == utils.stringify(updates)
def test_upsert_index_as_primary_key(): s = utils.session() dataset = utils.dataset() url = tc.URL(path="datasets/1:updateRecords") updates = [ tc.record._create_command(record, primary_key_name="primary_key") for record in _records_with_keys_json_2 ] snoop: Dict = {} responses.add_callback( responses.POST, str(url), partial( utils.capture_payload, snoop=snoop, status=200, response_json=_response_json ), ) df = pd.DataFrame( _records_json_2, index=[record["primary_key"] for record in _records_with_keys_json_2], ) df.index.name = "primary_key" response = tc.dataframe.upsert(s, dataset, df, primary_key_name="primary_key") assert response == _response_json assert snoop["payload"] == utils.stringify(updates)
def test_create(): s = utils.session() dataset = utils.dataset() attrs = tuple([ tc.SubAttribute( name=str(i), is_nullable=True, type=tc.attribute.type.Array(tc.attribute.type.STRING), ) for i in range(4) ]) attrs_url = tc.URL(path=dataset.url.path + "/attributes") url = replace(attrs_url, path=attrs_url.path + "/attr") attr_json = utils.load_json("attribute.json") responses.add(responses.POST, str(attrs_url), json=attr_json) attr = tc.attribute.create( s, dataset, name="attr", is_nullable=False, type=tc.attribute.type.Record(attributes=attrs), ) assert attr == tc.attribute._from_json(url, attr_json)
def test_upsert_primary_key_not_found(): s = utils.session() dataset = utils.dataset() df = pd.DataFrame(_records_json) with pytest.raises(tc.primary_key.NotFound): tc.dataframe.upsert(s, dataset, df, primary_key_name="wrong_primary_key")
def test_create_attribute_exists(): s = utils.session() dataset = utils.dataset() url = replace(dataset.url, path=dataset.url.path + "/attributes") responses.add(responses.POST, str(url), status=409) with pytest.raises(tc.attribute.AlreadyExists): tc.attribute.create(s, dataset, name="attr", is_nullable=False)
def test_upsert_primary_key_not_found(): s = utils.session() dataset = utils.dataset() with pytest.raises(tc.primary_key.NotFound): tc.record.upsert( s, dataset, _records_json, primary_key_name="wrong_primary_key" )
def test_from_resource_id_attribute_not_found(): s = utils.session() dataset = utils.dataset() url = replace(dataset.url, path=dataset.url.path + "/attributes/attr") responses.add(responses.GET, str(url), status=404) with pytest.raises(tc.attribute.NotFound): tc.attribute.from_resource_id(s, dataset, "attr")
def test_delete_primary_key_not_found(): s = utils.session() dataset = utils.dataset() with pytest.raises(tc.record.PrimaryKeyNotFound): tc.record.delete(s, dataset, _records_json, primary_key_name="wrong_primary_key")
def test_from_resource_id(): s = utils.session() dataset = utils.dataset() url = tc.URL(path=dataset.url.path + "/attributes/attr") attr_json = utils.load_json("attribute.json") responses.add(responses.GET, str(url), json=attr_json) attr = tc.attribute.from_resource_id(s, dataset, "attr") assert attr == tc.attribute._from_json(url, attr_json)
def test_stream(): s = utils.session() dataset = utils.dataset() url = tc.URL(path="datasets/1/records") responses.add( responses.GET, str(url), body="\n".join(json.dumps(x) for x in _records_json) ) records = tc.record.stream(s, dataset) assert list(records) == _records_json
def test_upsert_index_column_name_collision(): s = utils.session() dataset = utils.dataset() df = pd.DataFrame(_records_json_2) df.index.name = "primary_key" # create column in `df` with same name as index and matching "primary_key" df.insert(0, df.index.name, df.index) with pytest.raises(tc.primary_key.Ambiguous): tc.dataframe.upsert(s, dataset, df, primary_key_name="primary_key")
def test_from_dataset_all(): s = utils.session() dataset = utils.dataset() attrs_url = replace(dataset.url, path=dataset.url.path + "/attributes") attrs_json = utils.load_json("attributes.json") responses.add(responses.GET, str(attrs_url), json=attrs_json, status=204) attrs = tc.attribute.from_dataset_all(s, dataset) row_num = attrs[0] assert row_num.name == "RowNum" assert row_num.type == tc.attribute.type.STRING geom = attrs[1] assert geom.name == "geom" assert isinstance(geom.type, tc.attribute.type.Record)
def test_create_reserved_attribute_name(): s = utils.session() dataset = utils.dataset() with pytest.raises(tc.attribute.ReservedName): tc.attribute.create(s, dataset, name="clusterId", is_nullable=False)